Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
Rename class names and variables to be more meaningful
  • Loading branch information
SavinduDimal committed Sep 6, 2024
1 parent e7732a6 commit 9e0e6cc
Show file tree
Hide file tree
Showing 38 changed files with 420 additions and 376 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

package org.wso2.carbon.apimgt.api.model.policy;

public class AIQuotaLimit extends Limit {
public class AIAPIQuotaLimit extends Limit {
private long requestCount;
private long totalTokenCount;
private long requestTokenCount;
private long responseTokenCount;
private long promptTokenCount;
private long completionTokenCount;

public long getRequestCount() {
return requestCount;
Expand All @@ -40,26 +40,26 @@ public void setTotalTokenCount(long totalTokenCount) {
this.totalTokenCount = totalTokenCount;
}

public long getRequestTokenCount() {
return requestTokenCount;
public long getPromptTokenCount() {
return promptTokenCount;
}

public void setRequestTokenCount(long requestTokenCount) {
this.requestTokenCount = requestTokenCount;
public void setPromptTokenCount(long promptTokenCount) {
this.promptTokenCount = promptTokenCount;
}

public long getResponseTokenCount() {
return responseTokenCount;
public long getCompletionTokenCount() {
return completionTokenCount;
}

public void setResponseTokenCount(long responseTokenCount) {
this.responseTokenCount = responseTokenCount;
public void setCompletionTokenCount(long completionTokenCount) {
this.completionTokenCount = completionTokenCount;
}

@Override
public String toString() {
return "RequestCountLimit [requestCount=" + requestCount + ",totalTokenCount=" + totalTokenCount
+ ", requestTokenCount=" + requestTokenCount + ", responseTokenCount=" + responseTokenCount
return "RequestCountLimit [requestCount=" + requestCount + ", totalTokenCount=" + totalTokenCount
+ ", promptTokenCount=" + promptTokenCount + ", completionTokenCount=" + completionTokenCount
+ ", toString()=" + super.toString() + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public class PolicyConstants {

public static final String EVENT_COUNT_TYPE = "eventCount";

public static final String AI_QUOTA_TYPE = "aiQuota";
public static final String AI_API_QUOTA_TYPE = "aiApiQuota";

public static final String AI_QUOTA_TYPE_ENUM_VALUE = "AIQUOTALIMIT";
public static final String AI_API_QUOTA_TYPE_ENUM_VALUE = "AIAPIQUOTALIMIT";

public static final String DATE_QUERY = "date";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public class ThrottlePolicyConstants {

public static final String COLUMN_TOTAL_TOKEN_COUNT = "TOTAL_TOKEN_COUNT";

public static final String COLUMN_REQUEST_TOKEN_COUNT = "REQUEST_TOKEN_COUNT";
public static final String COLUMN_PROMPT_TOKEN_COUNT = "PROMPT_TOKEN_COUNT";

public static final String COLUMN_RESPONSE_TOKEN_COUNT = "RESPONSE_TOKEN_COUNT";
public static final String COLUMN_COMPLETION_TOKEN_COUNT = "COMPLETION_TOKEN_COUNT";

public static final String COLUMN_APPLICABLE_LEVEL = "APPLICABLE_LEVEL";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
import org.wso2.carbon.apimgt.api.model.botDataAPI.BotDetectionData;
import org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.CustomComplexityDetails;
import org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo;
import org.wso2.carbon.apimgt.api.model.policy.AIQuotaLimit;
import org.wso2.carbon.apimgt.api.model.policy.AIAPIQuotaLimit;
import org.wso2.carbon.apimgt.api.model.policy.APIPolicy;
import org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy;
import org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit;
Expand Down Expand Up @@ -11452,11 +11452,11 @@ public void addSubscriptionPolicy(SubscriptionPolicy policy) throws APIManagemen
policyStatement.setInt(15, policy.getGraphQLMaxDepth());
policyStatement.setInt(16, policy.getGraphQLMaxComplexity());
policyStatement.setString(17, policy.getBillingPlan());
if (PolicyConstants.AI_QUOTA_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
AIQuotaLimit limit = (AIQuotaLimit) policy.getDefaultQuotaPolicy().getLimit();
if (PolicyConstants.AI_API_QUOTA_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
AIAPIQuotaLimit limit = (AIAPIQuotaLimit) policy.getDefaultQuotaPolicy().getLimit();
policyStatement.setLong(18, limit.getTotalTokenCount());
policyStatement.setLong(19, limit.getRequestTokenCount());
policyStatement.setLong(20, limit.getResponseTokenCount());
policyStatement.setLong(19, limit.getPromptTokenCount());
policyStatement.setLong(20, limit.getCompletionTokenCount());
} else {
policyStatement.setLong(18, 0);
policyStatement.setLong(19, 0);
Expand Down Expand Up @@ -13114,13 +13114,13 @@ public void updateSubscriptionPolicy(SubscriptionPolicy policy) throws APIManage
updateStatement.setLong(6, 0);
updateStatement.setLong(7, 0);
updateStatement.setLong(8, 0);
} else if (PolicyConstants.AI_QUOTA_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
AIQuotaLimit limit = (AIQuotaLimit) policy.getDefaultQuotaPolicy().getLimit();
} else if (PolicyConstants.AI_API_QUOTA_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
AIAPIQuotaLimit limit = (AIAPIQuotaLimit) policy.getDefaultQuotaPolicy().getLimit();
updateStatement.setLong(4, limit.getRequestCount());
updateStatement.setString(5, null);
updateStatement.setLong(6, limit.getTotalTokenCount());
updateStatement.setLong(7, limit.getRequestTokenCount());
updateStatement.setLong(8, limit.getResponseTokenCount());
updateStatement.setLong(7, limit.getPromptTokenCount());
updateStatement.setLong(8, limit.getCompletionTokenCount());
}

updateStatement.setLong(9, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
Expand Down Expand Up @@ -13398,8 +13398,8 @@ private void setCommonParametersForPolicy(PreparedStatement policyStatement, Pol
EventCountLimit limit = (EventCountLimit) policy.getDefaultQuotaPolicy().getLimit();
policyStatement.setLong(6, limit.getEventCount());
policyStatement.setString(7, null);
} else if (PolicyConstants.AI_QUOTA_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
AIQuotaLimit limit = (AIQuotaLimit) policy.getDefaultQuotaPolicy().getLimit();
} else if (PolicyConstants.AI_API_QUOTA_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
AIAPIQuotaLimit limit = (AIAPIQuotaLimit) policy.getDefaultQuotaPolicy().getLimit();
policyStatement.setLong(6, limit.getRequestCount());
policyStatement.setString(7, null);
}
Expand Down Expand Up @@ -13463,15 +13463,15 @@ private void setCommonPolicyDetails(Policy policy, ResultSet resultSet) throws S
eventCountLimit.setEventCount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
quotaPolicy.setLimit(eventCountLimit);
} else if (resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE)
.equalsIgnoreCase(PolicyConstants.AI_QUOTA_TYPE)) {
AIQuotaLimit aiQuotaLimit = new AIQuotaLimit();
aiQuotaLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
aiQuotaLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
aiQuotaLimit.setRequestCount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
aiQuotaLimit.setTotalTokenCount(resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_TOTAL_TOKEN_COUNT));
aiQuotaLimit.setRequestTokenCount(resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_REQUEST_TOKEN_COUNT));
aiQuotaLimit.setResponseTokenCount(resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_RESPONSE_TOKEN_COUNT));
quotaPolicy.setLimit(aiQuotaLimit);
.equalsIgnoreCase(PolicyConstants.AI_API_QUOTA_TYPE)) {
AIAPIQuotaLimit AIAPIQuotaLimit = new AIAPIQuotaLimit();
AIAPIQuotaLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
AIAPIQuotaLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
AIAPIQuotaLimit.setRequestCount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
AIAPIQuotaLimit.setTotalTokenCount(resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_TOTAL_TOKEN_COUNT));
AIAPIQuotaLimit.setPromptTokenCount(resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_PROMPT_TOKEN_COUNT));
AIAPIQuotaLimit.setCompletionTokenCount(resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_COMPLETION_TOKEN_COUNT));
quotaPolicy.setLimit(AIAPIQuotaLimit);
}

policy.setUUID(resultSet.getString(ThrottlePolicyConstants.COLUMN_UUID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.wso2.carbon.apimgt.api.dto.ConditionDTO;
import org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO;
import org.wso2.carbon.apimgt.api.model.OperationPolicy;
import org.wso2.carbon.apimgt.api.model.policy.AIQuotaLimit;
import org.wso2.carbon.apimgt.api.model.policy.AIAPIQuotaLimit;
import org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit;
import org.wso2.carbon.apimgt.api.model.policy.EventCountLimit;
import org.wso2.carbon.apimgt.api.model.policy.PolicyConstants;
Expand Down Expand Up @@ -1029,18 +1029,18 @@ private void setCommonProperties(Policy policy, ResultSet resultSet) throws SQLE
eventCountLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
eventCountLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
quotaPolicy.setLimit(eventCountLimit);
} else if (PolicyConstants.AI_QUOTA_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
AIQuotaLimit aiQuotaLimit = new AIQuotaLimit();
aiQuotaLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
aiQuotaLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
aiQuotaLimit.setRequestCount(resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
aiQuotaLimit.setTotalTokenCount(
} else if (PolicyConstants.AI_API_QUOTA_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
AIAPIQuotaLimit AIAPIQuotaLimit = new AIAPIQuotaLimit();
AIAPIQuotaLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
AIAPIQuotaLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
AIAPIQuotaLimit.setRequestCount(resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
AIAPIQuotaLimit.setTotalTokenCount(
resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_TOTAL_TOKEN_COUNT));
aiQuotaLimit.setRequestTokenCount(
resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_REQUEST_TOKEN_COUNT));
aiQuotaLimit.setResponseTokenCount(
resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_RESPONSE_TOKEN_COUNT));
quotaPolicy.setLimit(aiQuotaLimit);
AIAPIQuotaLimit.setPromptTokenCount(
resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_PROMPT_TOKEN_COUNT));
AIAPIQuotaLimit.setCompletionTokenCount(
resultSet.getLong(prefix + ThrottlePolicyConstants.COLUMN_COMPLETION_TOKEN_COUNT));
quotaPolicy.setLimit(AIAPIQuotaLimit);
}
policy.setQuotaPolicy(quotaPolicy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2353,15 +2353,15 @@ public class SQLConstants {
"INSERT INTO AM_POLICY_SUBSCRIPTION (NAME, DISPLAY_NAME, TENANT_ID, DESCRIPTION, QUOTA_TYPE, QUOTA, \n" +
" QUOTA_UNIT, UNIT_TIME, TIME_UNIT, IS_DEPLOYED, UUID, RATE_LIMIT_COUNT, \n" +
" RATE_LIMIT_TIME_UNIT,STOP_ON_QUOTA_REACH, MAX_DEPTH, MAX_COMPLEXITY, \n" +
" BILLING_PLAN, TOTAL_TOKEN_COUNT, REQUEST_TOKEN_COUNT, RESPONSE_TOKEN_COUNT, \n" +
" BILLING_PLAN, TOTAL_TOKEN_COUNT, PROMPT_TOKEN_COUNT, COMPLETION_TOKEN_COUNT, \n" +
" MONETIZATION_PLAN,FIXED_RATE,BILLING_CYCLE,PRICE_PER_REQUEST,CURRENCY, \n" +
" CONNECTIONS_COUNT) \n" + " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

public static final String INSERT_SUBSCRIPTION_POLICY_WITH_CUSTOM_ATTRIB_SQL =
"INSERT INTO AM_POLICY_SUBSCRIPTION (NAME, DISPLAY_NAME, TENANT_ID, DESCRIPTION, QUOTA_TYPE, QUOTA, \n" +
" QUOTA_UNIT, UNIT_TIME, TIME_UNIT, IS_DEPLOYED, UUID, RATE_LIMIT_COUNT, \n" +
" RATE_LIMIT_TIME_UNIT, STOP_ON_QUOTA_REACH, MAX_DEPTH, MAX_COMPLEXITY, \n" +
" BILLING_PLAN, TOTAL_TOKEN_COUNT, REQUEST_TOKEN_COUNT, RESPONSE_TOKEN_COUNT, CUSTOM_ATTRIBUTES, MONETIZATION_PLAN, \n" +
" BILLING_PLAN, TOTAL_TOKEN_COUNT, PROMPT_TOKEN_COUNT, COMPLETION_TOKEN_COUNT, CUSTOM_ATTRIBUTES, MONETIZATION_PLAN, \n" +
" FIXED_RATE, BILLING_CYCLE, PRICE_PER_REQUEST, CURRENCY, CONNECTIONS_COUNT) \n" +
" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

Expand Down Expand Up @@ -2572,8 +2572,8 @@ public class SQLConstants {
"QUOTA = ?, " +
"QUOTA_UNIT = ?, " +
"TOTAL_TOKEN_COUNT = ?, " +
"REQUEST_TOKEN_COUNT = ?, " +
"RESPONSE_TOKEN_COUNT = ?, " +
"PROMPT_TOKEN_COUNT = ?, " +
"COMPLETION_TOKEN_COUNT = ?, " +
"UNIT_TIME = ?, " +
"TIME_UNIT = ?, " +
"RATE_LIMIT_COUNT = ?," +
Expand All @@ -2599,8 +2599,8 @@ public class SQLConstants {
"QUOTA = ?, " +
"QUOTA_UNIT = ?, " +
"TOTAL_TOKEN_COUNT = ?, " +
"REQUEST_TOKEN_COUNT = ?, " +
"RESPONSE_TOKEN_COUNT = ?, " +
"PROMPT_TOKEN_COUNT = ?, " +
"COMPLETION_TOKEN_COUNT = ?, " +
"UNIT_TIME = ?, " +
"TIME_UNIT = ?, " +
"RATE_LIMIT_COUNT = ?," +
Expand All @@ -2627,8 +2627,8 @@ public class SQLConstants {
"QUOTA = ?, " +
"QUOTA_UNIT = ?, " +
"TOTAL_TOKEN_COUNT = ?, " +
"REQUEST_TOKEN_COUNT = ?, " +
"RESPONSE_TOKEN_COUNT = ?, " +
"PROMPT_TOKEN_COUNT = ?, " +
"COMPLETION_TOKEN_COUNT = ?, " +
"UNIT_TIME = ?, " +
"TIME_UNIT = ?, " +
"RATE_LIMIT_COUNT = ?," +
Expand All @@ -2654,8 +2654,8 @@ public class SQLConstants {
"QUOTA = ?, " +
"QUOTA_UNIT = ?, " +
"TOTAL_TOKEN_COUNT = ?, " +
"REQUEST_TOKEN_COUNT = ?, " +
"RESPONSE_TOKEN_COUNT = ?, " +
"PROMPT_TOKEN_COUNT = ?, " +
"COMPLETION_TOKEN_COUNT = ?, " +
"UNIT_TIME = ?, " +
"TIME_UNIT = ?, " +
"RATE_LIMIT_COUNT = ?," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ public class SubscriptionValidationSQLConstants {
" APS.UNIT_TIME AS UNIT_TIME, " +
" APS.TIME_UNIT AS TIME_UNIT, " +
" APS.TOTAL_TOKEN_COUNT AS TOTAL_TOKEN_COUNT, " +
" APS.REQUEST_TOKEN_COUNT AS REQUEST_TOKEN_COUNT, " +
" APS.RESPONSE_TOKEN_COUNT AS RESPONSE_TOKEN_COUNT, " +
" APS.PROMPT_TOKEN_COUNT AS PROMPT_TOKEN_COUNT, " +
" APS.COMPLETION_TOKEN_COUNT AS COMPLETION_TOKEN_COUNT, " +
" FROM " +
" AM_POLICY_SUBSCRIPTION APS";

Expand Down Expand Up @@ -323,8 +323,8 @@ public class SubscriptionValidationSQLConstants {
" APS.UNIT_TIME AS UNIT_TIME, " +
" APS.TIME_UNIT AS TIME_UNIT, " +
" APS.TOTAL_TOKEN_COUNT AS TOTAL_TOKEN_COUNT, " +
" APS.REQUEST_TOKEN_COUNT AS REQUEST_TOKEN_COUNT, " +
" APS.RESPONSE_TOKEN_COUNT AS RESPONSE_TOKEN_COUNT, " +
" APS.PROMPT_TOKEN_COUNT AS PROMPT_TOKEN_COUNT, " +
" APS.COMPLETION_TOKEN_COUNT AS COMPLETION_TOKEN_COUNT, " +
" FROM " +
" AM_POLICY_SUBSCRIPTION APS" +
" WHERE " +
Expand All @@ -347,8 +347,8 @@ public class SubscriptionValidationSQLConstants {
" APS.UNIT_TIME AS UNIT_TIME, " +
" APS.TIME_UNIT AS TIME_UNIT, " +
" APS.TOTAL_TOKEN_COUNT AS TOTAL_TOKEN_COUNT, " +
" APS.REQUEST_TOKEN_COUNT AS REQUEST_TOKEN_COUNT, " +
" APS.RESPONSE_TOKEN_COUNT AS RESPONSE_TOKEN_COUNT, " +
" APS.PROMPT_TOKEN_COUNT AS PROMPT_TOKEN_COUNT, " +
" APS.COMPLETION_TOKEN_COUNT AS COMPLETION_TOKEN_COUNT, " +
" FROM " +
" AM_POLICY_SUBSCRIPTION APS" +
" WHERE " +
Expand Down
Loading

0 comments on commit 9e0e6cc

Please sign in to comment.