diff --git a/source/bulkdata/profile.c b/source/bulkdata/profile.c index 6d7c094e..f2d27eac 100644 --- a/source/bulkdata/profile.c +++ b/source/bulkdata/profile.c @@ -686,6 +686,8 @@ static void* CollectAndReport(void* data) Vector_RemoveItem(profile->cachedReportList, (void*) thirdCachedReport, NULL); free(thirdCachedReport); } + // Before caching the report, add "REPORT_TYPE": "CACHED" + tagReportAsCached(&jsonReport); Vector_PushBack(profile->cachedReportList, jsonReport); T2Info("Report Cached, No. of reportes cached = %lu\n", (unsigned long )Vector_Size(profile->cachedReportList)); diff --git a/source/bulkdata/profilexconf.c b/source/bulkdata/profilexconf.c index d101020e..9840b4dd 100644 --- a/source/bulkdata/profilexconf.c +++ b/source/bulkdata/profilexconf.c @@ -349,6 +349,8 @@ static void* CollectAndReportXconf(void* data) Vector_RemoveItem(profile->cachedReportList, (void*) thirdCachedReport, NULL); free(thirdCachedReport); } + // Before caching the report, add "REPORT_TYPE": "CACHED" + // tagReportAsCached(&jsonReport); Vector_PushBack(profile->cachedReportList, strdup(jsonReport)); profile->reportInProgress = false; /* CID 187010: Dereference before null check */ @@ -419,6 +421,8 @@ static void* CollectAndReportXconf(void* data) Vector_RemoveItem(profile->cachedReportList, (void*) thirdCachedReport, NULL); free(thirdCachedReport); } + // Before caching the report, add "REPORT_TYPE": "CACHED" + tagReportAsCached(&jsonReport); Vector_PushBack(profile->cachedReportList, strdup(jsonReport)); T2Info("Report Cached, No. of reportes cached = %lu\n", (unsigned long)Vector_Size(profile->cachedReportList)); diff --git a/source/reportgen/reportgen.c b/source/reportgen/reportgen.c index ea8a0fa0..f4249b13 100644 --- a/source/reportgen/reportgen.c +++ b/source/reportgen/reportgen.c @@ -876,3 +876,61 @@ char *prepareHttpUrl(T2HTTP *http) return httpUrl; } + +void tagReportAsCached(char **jsonReport) +{ + if (!jsonReport) + { + T2Error("jsonReport is NULL\n"); + return; + } + + cJSON *jsonReportObj = cJSON_Parse(*jsonReport); + if (!jsonReportObj) + { + T2Error("Failed to parse JSON report\n"); + return; + } + + cJSON *reportTypeEntry = cJSON_CreateObject(); + if (!reportTypeEntry) + { + T2Error("Failed to create REPORT_TYPE object\n"); + destroyJSONReport(jsonReportObj); + return; + } + cJSON_AddStringToObject(reportTypeEntry, "REPORT_TYPE", "CACHED"); + cJSON *searchResult = cJSON_GetObjectItemCaseSensitive(jsonReportObj, "searchResult"); + if (searchResult && cJSON_IsArray(searchResult)) + { + T2Info("Inserting REPORT_TYPE: CACHED at index 1 of searchResult array\n"); + cJSON_InsertItemInArray(searchResult, 1, reportTypeEntry); + } + else + { + cJSON *reportArray = cJSON_GetObjectItemCaseSensitive(jsonReportObj, "Report"); + if (reportArray && cJSON_IsArray(reportArray)) + { + T2Info("Inserting REPORT_TYPE: CACHED at beginning of Report array\n"); + cJSON_InsertItemInArray(reportArray, 0, reportTypeEntry); + } + else + { + T2Error("Neither 'searchResult' nor 'Report' arrays found in the JSON\n"); + destroyJSONReport(reportTypeEntry); + destroyJSONReport(jsonReportObj); + return; + } + } + + char *updatedJsonReport = cJSON_PrintUnformatted(jsonReportObj); + if (updatedJsonReport) + { + free(*jsonReport); // Freeing the old report only after the new one is generated + *jsonReport = updatedJsonReport; + } + + destroyJSONReport(jsonReportObj); +} + + diff --git a/source/reportgen/reportgen.h b/source/reportgen/reportgen.h index f0e1d916..2f9f66a5 100644 --- a/source/reportgen/reportgen.h +++ b/source/reportgen/reportgen.h @@ -79,4 +79,6 @@ T2ERROR prepareJSONReport(cJSON* jsonObj, char** reportBuff); char *prepareHttpUrl(T2HTTP *http); +void tagReportAsCached(char **jsonReport); + #endif /* _REPORTGEN_H_ */