Skip to content

Commit

Permalink
fix: fetching sampleData from a random API only if it's not available…
Browse files Browse the repository at this point in the history
… for test editor

# Conflicts:
#	apps/dashboard/src/main/java/com/akto/action/TrafficAction.java
  • Loading branch information
TangoBeeAkto committed Dec 11, 2024
1 parent 78a6a4a commit 883c030
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
32 changes: 32 additions & 0 deletions apps/dashboard/src/main/java/com/akto/action/TrafficAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,38 @@ public String fetchSampleData() {
return Action.SUCCESS.toUpperCase();
}

public String fetchSampleDataForTestEditor() {
fetchSampleData();
if(sampleDataList == null || sampleDataList.isEmpty()) {
sampleDataList = new ArrayList<>();
ApiCollection randomActiveCollection = ApiCollectionsDao.instance.findOne(Filters.and(
Filters.eq(ApiCollection._DEACTIVATED, false),
Filters.not(
Filters.size(ApiCollection.URLS_STRING, 0)
),
Filters.ne(ApiCollection.ID, 0)
));

if(randomActiveCollection == null) {
return Action.SUCCESS.toUpperCase();
}

int activeCollectionId = randomActiveCollection.getId();
List<String> endpointsList = new ArrayList<>(randomActiveCollection.getUrls());
String[] endpointAndMethod = endpointsList.get(0).split(" ");

sampleDataList = SampleDataDao.instance.findAll(Filters.and(
Filters.eq("_id.url", endpointAndMethod[0]),
Filters.in(SingleTypeInfo._COLLECTION_IDS, activeCollectionId),
Filters.eq("_id.responseCode", -1),
Filters.eq("_id.method", endpointAndMethod[1]),
Filters.gte("_id.bucketStartEpoch", 0),
Filters.lte("_id.bucketEndEpoch", 0)
));
}
return Action.SUCCESS.toUpperCase();
}

public String fetchAllSampleData() {
sampleDataList = SampleDataDao.instance.findAll(Filters.eq(Constants.ID + "." + ApiInfoKey.API_COLLECTION_ID, apiCollectionId), skip, limit == 0 ? 50 : limit, null);
return Action.SUCCESS.toUpperCase();
Expand Down
25 changes: 24 additions & 1 deletion apps/dashboard/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2400,7 +2400,7 @@
<param name="includeProperties">^actionErrors.*</param>
</result>
</action>

<action name="api/fetchSampleData" class="com.akto.action.TrafficAction" method="fetchSampleData">
<interceptor-ref name="json"/>
<interceptor-ref name="defaultStack" />
Expand All @@ -2424,6 +2424,29 @@
<param name="includeProperties">^actionErrors.*</param>
</result>
</action>
<action name="api/fetchSampleDataForTestEditor" class="com.akto.action.TrafficAction" method="fetchSampleDataForTestEditor">
<interceptor-ref name="json"/>
<interceptor-ref name="defaultStack" />
<interceptor-ref name="roleAccessInterceptor">
<param name="featureLabel">SAMPLE_DATA</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>
<interceptor-ref name="collectionInterceptor"/>
<result name="SUCCESS" type="json"/>
<result name="ERROR" type="httpheader">
<param name="status">401</param>
</result>
<result name="UNAUTHORIZED" type="json">
<param name="statusCode">403</param>
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">^actionErrors.*</param>
</result>
</action>

<action name="api/deleteCollection" class="com.akto.action.ApiCollectionsAction" method="deleteCollection">
<interceptor-ref name="json"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import request from "@/util/request"

const testEditorRequests = {
fetchSampleData(collectionId, apiEndpointUrl, apiEndpointMethod) {
fetchSampleDataForTestEditor(collectionId, apiEndpointUrl, apiEndpointMethod) {
return request({
url: '/api/fetchSampleData',
url: '/api/fetchSampleDataForTestEditor',
method: 'post',
data: {
apiCollectionId: collectionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const SampleApi = () => {

const fetchSampleData = async (collectionId, apiEndpointUrl, apiEndpointMethod) => {
setShowEmptyLayout(false)
const sampleDataResponse = await testEditorRequests.fetchSampleData(collectionId, apiEndpointUrl, apiEndpointMethod)
const sampleDataResponse = await testEditorRequests.fetchSampleDataForTestEditor(collectionId, apiEndpointUrl, apiEndpointMethod)
if (sampleDataResponse) {
if (sampleDataResponse.sampleDataList.length > 0 && sampleDataResponse.sampleDataList[0].samples && sampleDataResponse.sampleDataList[0].samples.length > 0) {
const sampleDataJson = JSON.parse(sampleDataResponse.sampleDataList[0].samples[sampleDataResponse.sampleDataList[0].samples.length - 1])
Expand Down

0 comments on commit 883c030

Please sign in to comment.