Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/usage cyborg #1348

Merged
merged 6 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.akto.trafficFilter.HostFilter;
import com.akto.trafficFilter.ParamFilter;
import com.akto.util.Constants;
import com.akto.dto.usage.MetricTypes;
import com.akto.log.LoggerMaker;
import com.akto.log.LoggerMaker.LogDb;
import com.akto.util.enums.GlobalEnums.TestErrorSource;
Expand Down Expand Up @@ -224,6 +225,26 @@ public void setIssuesIds(BasicDBList issuesIds) {
KafkaUtils kafkaUtils = new KafkaUtils();
String endpointLogicalGroupId;

String metricType;

public String getMetricType() {
return metricType;
}

public void setMetricType(String metricType) {
this.metricType = metricType;
}

int deltaUsage;

public int getDeltaUsage() {
return deltaUsage;
}

public void setDeltaUsage(int deltaUsage) {
this.deltaUsage = deltaUsage;
}

DataControlSettings dataControlSettings;
public String fetchDataControlSettings() {
try {
Expand Down Expand Up @@ -939,6 +960,25 @@ public String fetchStiBasedOnHostHeaders() {
return Action.SUCCESS.toUpperCase();
}

public String fetchDeactivatedCollections() {
try {
apiCollectionIds = DbLayer.fetchDeactivatedCollections();
} catch (Exception e) {
return Action.ERROR.toUpperCase();
}
return Action.SUCCESS.toUpperCase();
}

public String updateUsage() {
try {
MetricTypes metric = MetricTypes.valueOf(metricType);
DbLayer.updateUsage(metric, deltaUsage);
} catch (Exception e) {
return Action.ERROR.toUpperCase();
}
return Action.SUCCESS.toUpperCase();
}

public String fetchApiCollectionIds() {
try {
apiCollectionIds = DbLayer.fetchApiCollectionIds();
Expand Down
22 changes: 22 additions & 0 deletions apps/database-abstractor/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,28 @@
</result>
</action>

<action name="api/fetchDeactivatedCollections" class="com.akto.action.DbAction" method="fetchDeactivatedCollections">
<interceptor-ref name="json"/>
<interceptor-ref name="defaultStack" />
<result name="SUCCESS" type="json"/>
<result name="ERROR" type="json">
<param name="statusCode">422</param>
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">^actionErrors.*</param>
</result>
</action>

<action name="api/updateUsage" class="com.akto.action.DbAction" method="updateUsage">
<interceptor-ref name="json"/>
<interceptor-ref name="defaultStack" />
<result name="SUCCESS" type="json"/>
<result name="ERROR" type="json">
<param name="statusCode">422</param>
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">^actionErrors.*</param>
</result>
</action>

<action name="api/fetchEstimatedDocCount" class="com.akto.action.DbAction" method="fetchEstimatedDocCount">
<interceptor-ref name="json"/>
<interceptor-ref name="defaultStack" />
Expand Down
14 changes: 14 additions & 0 deletions libs/dao/src/main/java/com/akto/dao/SetupDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.akto.dao;

import com.akto.dto.Setup;
import com.mongodb.BasicDBObject;

public class SetupDao extends CommonContextDao<Setup> {

Expand All @@ -16,4 +17,17 @@ public Class<Setup> getClassT() {
return Setup.class;
}

private final static String SAAS = "saas";
public boolean isMetered() {
Setup setup = SetupDao.instance.findOne(new BasicDBObject());
boolean isSaas = false;
if (setup != null) {
String dashboardMode = setup.getDashboardMode();
if (dashboardMode != null) {
isSaas = dashboardMode.equalsIgnoreCase(SAAS);
}
}
return isSaas;
}

}
18 changes: 16 additions & 2 deletions libs/utils/src/main/java/com/akto/billing/UsageMetricUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.json.JSONException;
import org.json.JSONObject;

import com.akto.RuntimeMode;
import com.akto.dao.SetupDao;
import com.akto.dao.billing.OrganizationsDao;
import com.akto.dao.context.Context;
import com.akto.dao.usage.UsageMetricInfoDao;
Expand Down Expand Up @@ -235,7 +237,13 @@ public static boolean checkTestRunsOverage(int accountId){
public static FeatureAccess getFeatureAccess(int accountId, MetricTypes metricType) {
FeatureAccess featureAccess = FeatureAccess.fullAccess;
try {
if (!DashboardMode.isMetered()) {
/*
* runtime mode check for hybrid mini-runtime deployments.
* Setup Dao check for database-abstractor
*/
if (!(DashboardMode.isMetered() ||
RuntimeMode.isHybridDeployment() ||
SetupDao.instance.isMetered())) {
return featureAccess;
}
Organization organization = OrganizationsDao.instance.findOneByAccountId(accountId);
Expand All @@ -249,7 +257,13 @@ public static FeatureAccess getFeatureAccess(int accountId, MetricTypes metricTy
public static FeatureAccess getFeatureAccess(Organization organization, MetricTypes metricType) {
FeatureAccess featureAccess = FeatureAccess.fullAccess;
try {
if (!DashboardMode.isMetered()) {
/*
* runtime mode check for hybrid mini-runtime deployments.
* Setup Dao check for database-abstractor
*/
if (!(DashboardMode.isMetered() ||
RuntimeMode.isHybridDeployment() ||
SetupDao.instance.isMetered())) {
return featureAccess;
}
if (organization == null) {
Expand Down
48 changes: 48 additions & 0 deletions libs/utils/src/main/java/com/akto/data_actor/ClientActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.akto.dto.traffic.SampleData;
import com.akto.dto.type.SingleTypeInfo;
import com.akto.dto.type.URLMethods;
import com.akto.dto.usage.MetricTypes;
import com.akto.log.LoggerMaker;
import com.akto.log.LoggerMaker.LogDb;
import com.fasterxml.jackson.databind.DeserializationFeature;
Expand Down Expand Up @@ -297,6 +298,53 @@ public void bulkWriteTestingRunIssues(List<Object> writesForTestingRunIssues) {
bulkWrite(writesForTestingRunIssues, "/bulkWriteTestingRunIssues", "writesForTestingRunIssues");
}

public List<Integer> fetchDeactivatedCollections(){
List<Integer> ids = new ArrayList<>();

Map<String, List<String>> headers = buildHeaders();
OriginalHttpRequest request = new OriginalHttpRequest(url + "/fetchDeactivatedCollections", "", "GET", null, headers, "");
try {
OriginalHttpResponse response = ApiExecutor.sendRequest(request, true, null, false, null);
String responsePayload = response.getBody();
if (response.getStatusCode() != 200 || responsePayload == null) {
loggerMaker.errorAndAddToDb("invalid response in fetchDeactivatedCollections", LoggerMaker.LogDb.RUNTIME);
return ids;
}
BasicDBObject payloadObj;
try {
payloadObj = BasicDBObject.parse(responsePayload);
BasicDBList objList = (BasicDBList) payloadObj.get("apiCollectionIds");
for (Object obj: objList) {
int obj2 = (Integer) obj;
ids.add(obj2);
}
} catch(Exception e) {
loggerMaker.errorAndAddToDb("error extracting response in fetchDeactivatedCollections" + e, LoggerMaker.LogDb.RUNTIME);
}
} catch (Exception e) {
loggerMaker.errorAndAddToDb("error in fetchDeactivatedCollections" + e, LoggerMaker.LogDb.RUNTIME);
}
return ids;
};

public void updateUsage(MetricTypes metricType, int deltaUsage){
Map<String, List<String>> headers = buildHeaders();
BasicDBObject obj = new BasicDBObject();
obj.put("metricType", metricType.name());
obj.put("deltaUsage", deltaUsage);
OriginalHttpRequest request = new OriginalHttpRequest(url + "/updateUsage", "", "POST", obj.toString(), headers, "");
try {
OriginalHttpResponse response = ApiExecutor.sendRequest(request, true, null, false, null);
String responsePayload = response.getBody();
if (response.getStatusCode() != 200 || responsePayload == null) {
loggerMaker.errorAndAddToDb("invalid response in updateUsage", LoggerMaker.LogDb.RUNTIME);
}
} catch (Exception e) {
loggerMaker.errorAndAddToDb("error in updateUsage" + e, LoggerMaker.LogDb.RUNTIME);
}
return;
};

public TestSourceConfig findTestSourceConfig(String subType) {
Map<String, List<String>> headers = buildHeaders();
BasicDBObject obj = new BasicDBObject();
Expand Down
7 changes: 4 additions & 3 deletions libs/utils/src/main/java/com/akto/data_actor/DataActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
import com.akto.dto.testing.WorkflowTestResult;
import com.akto.dto.testing.sources.TestSourceConfig;
import com.akto.dto.traffic.SampleData;
import com.akto.dto.traffic.TrafficInfo;
import com.akto.dto.type.SingleTypeInfo;
import com.akto.dto.type.URLMethods;
import com.mongodb.client.model.WriteModel;
import com.akto.dto.usage.MetricTypes;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -186,7 +185,9 @@ public abstract class DataActor {

public abstract List<TestingRunIssues> fetchIssuesByIds(Set<TestingIssuesId> issuesIds);

// public abstract List<Testing
public abstract List<Integer> fetchDeactivatedCollections();

public abstract void updateUsage(MetricTypes metricType, int deltaUsage);

public abstract List<SingleTypeInfo> findStiByParam(int apiCollectionId, String param);

Expand Down
11 changes: 11 additions & 0 deletions libs/utils/src/main/java/com/akto/data_actor/DbActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.akto.dto.type.SingleTypeInfo;
import com.akto.dto.type.URLMethods;
import com.akto.dto.type.URLMethods.Method;
import com.akto.dto.usage.MetricTypes;
import com.mongodb.client.model.WriteModel;

import java.util.ArrayList;
Expand Down Expand Up @@ -405,6 +406,16 @@ public TestingRunResultSummary updateIssueCountInSummary(String summaryId,
return DbLayer.updateIssueCountInSummary(summaryId, totalCountIssues);
}

public List<Integer> fetchDeactivatedCollections() {
return DbLayer.fetchDeactivatedCollections();
}

public void updateUsage(MetricTypes metricType,int deltaUsage){
DbLayer.updateUsage(metricType, deltaUsage);
return;
}


public void updateIssueCountInTestSummary(String summaryId, Map<String, Integer> totalCountIssues) {
DbLayer.updateIssueCountInTestSummary(summaryId, totalCountIssues, false);
}
Expand Down
13 changes: 13 additions & 0 deletions libs/utils/src/main/java/com/akto/data_actor/DbLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@
import com.akto.dto.traffic_metrics.TrafficMetrics;
import com.akto.dto.type.SingleTypeInfo;
import com.akto.dto.type.URLMethods;
import com.akto.dto.usage.MetricTypes;
import com.akto.log.LoggerMaker;
import com.akto.log.LoggerMaker.LogDb;
import com.akto.usage.UsageMetricCalculator;
import com.akto.usage.UsageMetricHandler;
import com.akto.util.Constants;
import com.mongodb.BasicDBObject;
import com.mongodb.bulk.BulkWriteResult;
Expand Down Expand Up @@ -632,6 +635,16 @@ public static TestingRunResultSummary updateIssueCountInSummary(String summaryId
options);
}

public static List<Integer> fetchDeactivatedCollections() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix or add a todo for adding projection in mongo query

return new ArrayList<>(UsageMetricCalculator.getDeactivatedLatest());
}

public static void updateUsage(MetricTypes metricType, int deltaUsage){
int accountId = Context.accountId.get();
UsageMetricHandler.calcAndFetchFeatureAccessUsingDeltaUsage(metricType, accountId, deltaUsage);
return;
}

public static List<TestingRunResult> fetchLatestTestingRunResultBySummaryId(String summaryId, int limit, int skip) {
ObjectId summaryObjectId = new ObjectId(summaryId);
return TestingRunResultDao.instance
Expand Down
Loading