Skip to content

Commit

Permalink
Merge pull request #1871 from akto-api-security/feature/logs_in_fetch…
Browse files Browse the repository at this point in the history
…ing_test_run_result

Feature/logs in fetching test run result
  • Loading branch information
notshivansh authored Jan 2, 2025
2 parents ad2ccd4 + d20fad7 commit 81ba0a1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,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 @@ -576,6 +578,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 @@ -585,8 +616,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 @@ -606,7 +636,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 @@ -634,23 +664,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 @@ -1404,6 +1417,10 @@ public void setReportFilterList(Map<String, List<String>> reportFilterList) {
this.reportFilterList = reportFilterList;
}

public List<TestingRunIssues> getIssueslist() {
return issueslist;
}

public boolean getCleanUpTestingResources() {
return cleanUpTestingResources;
}
Expand Down
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 @@ -130,6 +130,12 @@ function GithubServerTable(props) {
}
}

useEffect(() => {
if(Number.isInteger(props?.pageTotalCount)) {
setTotal(props?.pageTotalCount)
}
}, [props?.pageTotalCount, data])

const handleSelectedTab = (x) => {
const tableTabs = props.tableTabs ? props.tableTabs : props.tabs
if(tableTabs){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function SingleTestRunPage() {
const [testingRunResultSummariesObj, setTestingRunResultSummariesObj] = useState({})
const [allResultsLength, setAllResultsLength] = useState(undefined)
const [currentSummary, setCurrentSummary] = useState('')
const [pageTotalCount, setPageTotalCount] = useState(0)

const tableTabMap = {
vulnerable: "VULNERABLE",
Expand Down Expand Up @@ -247,6 +248,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 @@ -270,17 +272,24 @@ function SingleTestRunPage() {
})
testRunResultsRes = ignoredTestRunResults
totalIgnoredIssuesCount = ignoredTestRunResults.length
setPageTotalCount(selectedTab === 'ignored_issues' ? totalIgnoredIssuesCount : testRunCountMap[tableTabMap[selectedTab]])
} 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))
}
})
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)
setPageTotalCount(testRunCountMap[tableTabMap[selectedTab]])
})
}
}
Expand Down Expand Up @@ -499,6 +508,7 @@ const promotedBulkActions = (selectedDataHexIds) => {
"selected": 1
}}
callFromOutside={updateTable}
pageTotalCount={pageTotalCount}
/>
)

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 81ba0a1

Please sign in to comment.