Skip to content

Commit

Permalink
moved test results counting to other api
Browse files Browse the repository at this point in the history
  • Loading branch information
TangoBeeAkto committed Dec 29, 2024
1 parent f5c6bf8 commit 24e7e32
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,10 @@ private List<Bson> prepareTestRunResultsFilters(ObjectId testingRunResultSummary
List<Bson> filterList = new ArrayList<>();
filterList.add(Filters.eq(TestingRunResult.TEST_RUN_RESULT_SUMMARY_ID, testingRunResultSummaryId));

Bson filtersForTestingRunResults = com.akto.action.testing.Utils.createFiltersForTestingReport(reportFilterList);
if(!filtersForTestingRunResults.equals(Filters.empty())) filterList.add(filtersForTestingRunResults);
if(reportFilterList != null) {
Bson filtersForTestingRunResults = com.akto.action.testing.Utils.createFiltersForTestingReport(reportFilterList);
if (!filtersForTestingRunResults.equals(Filters.empty())) filterList.add(filtersForTestingRunResults);
}

if(queryMode == null) {
if(fetchOnlyVulnerable) {
Expand Down Expand Up @@ -574,6 +576,35 @@ public static Bson prepareTestingRunResultCustomSorting(String sortKey, int sort
return sortStage;
}

Map<String, Integer> testCountMap;
public String fetchTestRunResultsCount() {
ObjectId testingRunResultSummaryId;
try {
testingRunResultSummaryId = new ObjectId(testingRunResultSummaryHexId);
} catch (Exception e) {
addActionError("Invalid test summary id");
return ERROR.toUpperCase();
}

testCountMap = new HashMap<>();
int timeNow = Context.now();
for(QueryMode qm : QueryMode.values()) {
if(qm.equals(QueryMode.ALL) || qm.equals(QueryMode.SKIPPED_EXEC_NO_ACTION)) {
continue;
}

timeNow = Context.now();
int count = (int) TestingRunResultDao.instance.count(Filters.and(
prepareTestRunResultsFilters(testingRunResultSummaryId, qm)
));
loggerMaker.infoAndAddToDb("[" + (Context.now() - timeNow) + "] Fetched total count of testingRunResults for: " + qm.name(), LogDb.DASHBOARD);
testCountMap.put(qm.toString(), count);
}

testCountMap.put(QueryMode.VULNERABLE.name(), testCountMap.getOrDefault(QueryMode.VULNERABLE.name(), 0));
return SUCCESS.toUpperCase();
}

String testingRunResultSummaryHexId;
List<TestingRunResult> testingRunResults;
private boolean fetchOnlyVulnerable;
Expand All @@ -583,8 +614,7 @@ public enum QueryMode {
private QueryMode queryMode;

private Map<TestError, String> errorEnums = new HashMap<>();

Map<String, Integer> testCountMap;
List<TestingRunIssues> issueslist;

public String fetchTestingRunResults() {
ObjectId testingRunResultSummaryId;
Expand All @@ -604,7 +634,7 @@ public String fetchTestingRunResults() {
Filters.in(TestingRunIssues.TEST_RUN_ISSUES_STATUS, "IGNORED"),
Filters.in(TestingRunIssues.LATEST_TESTING_RUN_SUMMARY_ID, testingRunResultSummaryId)
);
List<TestingRunIssues> issueslist = TestingRunIssuesDao.instance.findAll(ignoredIssuesFilters, Projections.include("_id"));
issueslist = TestingRunIssuesDao.instance.findAll(ignoredIssuesFilters, Projections.include("_id"));
loggerMaker.infoAndAddToDb("[" + (Context.now() - timeNow) + "] Fetched testing run issues of size: " + issueslist.size(), LogDb.DASHBOARD);

List<Bson> testingRunResultFilters = prepareTestRunResultsFilters(testingRunResultSummaryId, queryMode);
Expand Down Expand Up @@ -632,23 +662,6 @@ public String fetchTestingRunResults() {
timeNow = Context.now();
removeTestingRunResultsByIssues(testingRunResults, issueslist, false);
loggerMaker.infoAndAddToDb("[" + (Context.now() - timeNow) + "] Removed ignored issues from testing run results. Current size of testing run results: " + testingRunResults.size(), LogDb.DASHBOARD);

testCountMap = new HashMap<>();
for(QueryMode qm : QueryMode.values()) {
if(qm.equals(QueryMode.ALL) || qm.equals(QueryMode.SKIPPED_EXEC_NO_ACTION)) {
continue;
}

timeNow = Context.now();
int count = (int) TestingRunResultDao.instance.count(Filters.and(
prepareTestRunResultsFilters(testingRunResultSummaryId, qm)
));
loggerMaker.infoAndAddToDb("[" + (Context.now() - timeNow) + "] Fetched total count of testingRunResults for: " + qm.name(), LogDb.DASHBOARD);
testCountMap.put(qm.toString(), count);
}

testCountMap.put(QueryMode.VULNERABLE.name(), Math.abs(testCountMap.getOrDefault(QueryMode.VULNERABLE.name(), 0)- issueslist.size()));
testCountMap.put("IGNORED_ISSUES", issueslist.size());
} catch (Exception e) {
loggerMaker.errorAndAddToDb(e, "error in fetchLatestTestingRunResult: " + e);
}
Expand Down Expand Up @@ -1401,4 +1414,8 @@ public Map<String, Integer> getTestCountMap() {
public void setReportFilterList(Map<String, List<String>> reportFilterList) {
this.reportFilterList = reportFilterList;
}

public List<TestingRunIssues> getIssueslist() {
return issueslist;
}
}
21 changes: 21 additions & 0 deletions apps/dashboard/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3751,6 +3751,27 @@
</result>
</action>

<action name="api/fetchTestRunResultsCount" class="com.akto.action.testing.StartTestAction" method="fetchTestRunResultsCount">
<interceptor-ref name="json"/>
<interceptor-ref name="defaultStack" />
<interceptor-ref name="roleAccessInterceptor">
<param name="featureLabel">TEST_RESULTS</param>
<param name="accessType">READ</param>
</interceptor-ref>

<result name="FORBIDDEN" type="json">
<param name="statusCode">403</param>
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">^actionErrors.*</param>
</result>
<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/fetchVulnerableTestRunResults" class="com.akto.action.testing.StartTestAction" method="fetchVulnerableTestRunResults">
<interceptor-ref name="json"/>
<interceptor-ref name="defaultStack" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ function SingleTestRunPage() {
let testRunResultsRes = []
let testRunCountMap = []
let totalIgnoredIssuesCount = 0
let issuesList = []
const { testingRun, workflowTest, testingRunType } = testingRunResultSummariesObj
if(testingRun === undefined){
return {value: [], total: 0}
Expand All @@ -271,14 +272,19 @@ function SingleTestRunPage() {
testRunResultsRes = ignoredTestRunResults
totalIgnoredIssuesCount = ignoredTestRunResults.length
} else {
await api.fetchTestingRunResults(localSelectedTestRun.testingRunResultSummaryHexId, tableTabMap[selectedTab], sortKey, sortOrder, skip, limit, filters, queryValue).then(({ testingRunResults, testCountMap, errorEnums }) => {
testRunCountMap = testCountMap
await api.fetchTestingRunResults(localSelectedTestRun.testingRunResultSummaryHexId, tableTabMap[selectedTab], sortKey, sortOrder, skip, limit, filters, queryValue).then(({ testingRunResults, issueslist, errorEnums }) => {
issuesList = issueslist || []
testRunResultsRes = transform.prepareTestRunResults(hexId, testingRunResults, subCategoryMap, subCategoryFromSourceConfigMap)
if(selectedTab === 'domain_unreachable' || selectedTab === 'skipped' || selectedTab === 'need_configurations') {
errorEnums['UNKNOWN_ERROR_OCCURRED'] = "OOPS! Unknown error occurred."
setErrorsObject(errorEnums)
setMissingConfigs(transform.getMissingConfigs(testRunResultsRes))
}
})
await api.fetchTestRunResultsCount(localSelectedTestRun.testingRunResultSummaryHexId).then(({testCountMap}) => {
testRunCountMap = testCountMap || []
testRunCountMap['VULNERABLE'] = Math.abs(testRunCountMap['VULNERABLE']-issuesList.length)
testRunCountMap['IGNORED_ISSUES'] = (issuesList.length || 0)
const orderedValues = tableTabsOrder.map(key => testCountMap[tableTabMap[key]] || 0)
setTestRunResultsCount(orderedValues)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export default {
})
return resp
},
async fetchTestRunResultsCount(testingRunResultSummaryHexId) {
const resp = await request({
url: '/api/fetchTestRunResultsCount',
method: 'post',
data: {
testingRunResultSummaryHexId
}
})
return resp
},
async fetchAllSubCategories(fetchOnlyActive, mode, skip, limit) {
const resp = await request({
url: 'api/fetchAllSubCategories',
Expand Down

0 comments on commit 24e7e32

Please sign in to comment.