From c43babc7d06618f766bc2e13b2133a2623e85073 Mon Sep 17 00:00:00 2001 From: Sid Sahoo Date: Tue, 8 Oct 2024 01:21:15 +0000 Subject: [PATCH 1/4] Use node-cache for report-store. --- services/ad-tech/package-lock.json | 22 +++++++++++++++++++ services/ad-tech/package.json | 1 + .../ad-tech/src/controllers/report-store.ts | 22 ++++++++++--------- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/services/ad-tech/package-lock.json b/services/ad-tech/package-lock.json index 4dba1f91..bde44ddb 100644 --- a/services/ad-tech/package-lock.json +++ b/services/ad-tech/package-lock.json @@ -13,6 +13,7 @@ "cors": "^2.8.5", "ejs": "^3.1.9", "express": "^4.18.2", + "node-cache": "^5.1.2", "structured-field-values": "^2.0.1" }, "devDependencies": { @@ -380,6 +381,15 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, + "node_modules/clone": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz", + "integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==", + "license": "MIT", + "engines": { + "node": ">=0.8" + } + }, "node_modules/color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", @@ -941,6 +951,18 @@ "node": ">= 0.6" } }, + "node_modules/node-cache": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz", + "integrity": "sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==", + "license": "MIT", + "dependencies": { + "clone": "2.x" + }, + "engines": { + "node": ">= 8.0.0" + } + }, "node_modules/nofilter": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/nofilter/-/nofilter-3.1.0.tgz", diff --git a/services/ad-tech/package.json b/services/ad-tech/package.json index cfbe9c7a..e13d825c 100644 --- a/services/ad-tech/package.json +++ b/services/ad-tech/package.json @@ -14,6 +14,7 @@ "cors": "^2.8.5", "ejs": "^3.1.9", "express": "^4.18.2", + "node-cache": "^5.1.2", "structured-field-values": "^2.0.1" }, "devDependencies": { diff --git a/services/ad-tech/src/controllers/report-store.ts b/services/ad-tech/src/controllers/report-store.ts index f21b1c7d..f8615a0f 100644 --- a/services/ad-tech/src/controllers/report-store.ts +++ b/services/ad-tech/src/controllers/report-store.ts @@ -14,6 +14,8 @@ limitations under the License. */ +import NodeCache from 'node-cache'; + /** Basic categories of reports. */ export enum ReportCategory { EVENT_LEVEL_LOG, @@ -34,25 +36,25 @@ export interface Report { data: any; } +/** TTL for in-memory reports: 10 minutes */ +export const REPORT_TTL_SECONDS = 10 * 60; + +/** Simple in-memory implementation of report storage. */ export const ReportStore = (() => { // In-memory storage for reports. - const Reports: Report[] = []; - // Clear in-memory storage every 10 min - setInterval( - () => { - Reports.length = 0; - }, - 1000 * 60 * 10, - ); + const Reports = new NodeCache({stdTTL: REPORT_TTL_SECONDS}); /** Add a new report to the in-memory storage. */ const addReport = (report: Report) => { - Reports.push(report); + Reports.set( + `${report.category}||${report.timestamp}`, + report, + ); }; /** Returns all reports from in-memory storage. */ const getAllReports = (): Report[] => { - return [...Reports]; + return Reports.keys().map(key => Reports.get(key) as Report); }; return { From 361158d20b600b3774a62a2d0766c4e51a54330c Mon Sep 17 00:00:00 2001 From: Sid Sahoo Date: Tue, 8 Oct 2024 15:40:49 +0000 Subject: [PATCH 2/4] Fix lint --- services/ad-tech/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/ad-tech/package.json b/services/ad-tech/package.json index e13d825c..f57180ff 100644 --- a/services/ad-tech/package.json +++ b/services/ad-tech/package.json @@ -7,7 +7,8 @@ "scripts": { "start": "npm run build && node ./build/main.js", "build": "tsc && cp -r src/views build/", - "ncu": "npx npm-check-updates -u" + "ncu": "npx npm-check-updates -u", + "fmt": "prettier --write ." }, "dependencies": { "cbor": "^9.0.2", From 29aa8605c366d75486c5d3018f01a9a92bb96454 Mon Sep 17 00:00:00 2001 From: Sid Sahoo Date: Tue, 8 Oct 2024 15:42:25 +0000 Subject: [PATCH 3/4] Fix lint --- services/ad-tech/src/controllers/report-store.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/services/ad-tech/src/controllers/report-store.ts b/services/ad-tech/src/controllers/report-store.ts index f8615a0f..427e20f1 100644 --- a/services/ad-tech/src/controllers/report-store.ts +++ b/services/ad-tech/src/controllers/report-store.ts @@ -46,15 +46,12 @@ export const ReportStore = (() => { /** Add a new report to the in-memory storage. */ const addReport = (report: Report) => { - Reports.set( - `${report.category}||${report.timestamp}`, - report, - ); + Reports.set(`${report.category}||${report.timestamp}`, report); }; /** Returns all reports from in-memory storage. */ const getAllReports = (): Report[] => { - return Reports.keys().map(key => Reports.get(key) as Report); + return Reports.keys().map((key) => Reports.get(key) as Report); }; return { From 015b50bbbbd33585f9800eac3226c0f81ed32568 Mon Sep 17 00:00:00 2001 From: Sid Sahoo Date: Thu, 10 Oct 2024 18:55:02 +0000 Subject: [PATCH 4/4] Address review comments --- services/ad-tech/package.json | 2 +- services/ad-tech/src/controllers/report-store.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/services/ad-tech/package.json b/services/ad-tech/package.json index f57180ff..0857bcde 100644 --- a/services/ad-tech/package.json +++ b/services/ad-tech/package.json @@ -8,7 +8,7 @@ "start": "npm run build && node ./build/main.js", "build": "tsc && cp -r src/views build/", "ncu": "npx npm-check-updates -u", - "fmt": "prettier --write ." + "fmt": "pre-commit run --all-files" }, "dependencies": { "cbor": "^9.0.2", diff --git a/services/ad-tech/src/controllers/report-store.ts b/services/ad-tech/src/controllers/report-store.ts index 427e20f1..af096803 100644 --- a/services/ad-tech/src/controllers/report-store.ts +++ b/services/ad-tech/src/controllers/report-store.ts @@ -46,6 +46,7 @@ export const ReportStore = (() => { /** Add a new report to the in-memory storage. */ const addReport = (report: Report) => { + // TODO: Consider partitioning by use-case. Reports.set(`${report.category}||${report.timestamp}`, report); };