From e23fec2ce42e1345ebf1f66eb6df97c282810117 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Sat, 13 Jul 2024 08:41:20 -0700 Subject: [PATCH] metrics: store summary counts for specific date in cache key (#3811) --- .../metrics/files/summaries/summaries.js | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/ansible/roles/metrics/files/summaries/summaries.js b/ansible/roles/metrics/files/summaries/summaries.js index 43fda433a..f5baebce0 100755 --- a/ansible/roles/metrics/files/summaries/summaries.js +++ b/ansible/roles/metrics/files/summaries/summaries.js @@ -10,7 +10,6 @@ const app = express() app.use(express.json()) - function csvStream (chunk) { try { const line = [] @@ -25,8 +24,11 @@ function csvStream (chunk) { } } -const counts = { bytes: 0, total: 0 } -function increment (type, key) { +// ToDo: Remove global variable when refactoring. +const cache = {} + +function increment (date, type, key) { + const counts = cache[date] if (!key) { key = 'unknown' } @@ -40,7 +42,8 @@ function increment (type, key) { } } -function prepare () { +function prepare (date) { + const counts = cache[date] function sort (type) { const ca = Object.entries(counts[type]) ca.sort((e1, e2) => e2[1] > e1[1] ? 1 : e2[1] < e1[1] ? -1 : 0) @@ -52,14 +55,19 @@ function prepare () { 'country version os arch'.split(' ').forEach(sort) } -function summary (chunk) { - const [date, country, region, path, version, os, arch, bytes] = chunk[0] - increment('country', country) - increment('version', version) - increment('os', os) - increment('arch', arch) - counts.bytes += parseInt(bytes, 10) - counts.total++ +function summary (date, chunk) { + const [, country, region, path, version, os, arch, bytes] = chunk[0] + + if (!cache[date]) { + cache[date] = { bytes: 0, total: 0 } + } + + increment(date, 'country', country) + increment(date, 'version', version) + increment(date, 'os', os) + increment(date, 'arch', arch) + cache[date].bytes += parseInt(bytes, 10) + cache[date].total++ return } @@ -75,7 +83,7 @@ async function collectData (date) { for (const line of contentsArray) { try { const csvparse = csvStream(line) - if (csvparse !== undefined && csvparse[0][0] !== '') { summary(csvparse) } + if (csvparse !== undefined && csvparse[0][0] !== '') { summary(date, csvparse) } } catch (err) { console.log(err) } } } @@ -84,9 +92,9 @@ async function collectData (date) { async function produceSummaries (date) { const storage = new Storage() await collectData(date) - prepare() + prepare(date) - const fileContents = JSON.stringify(counts) + const fileContents = JSON.stringify(cache[date]) const fileName = `nodejs.org-access.log.${date.toString()}.json` try { await storage.bucket('access-logs-summaries-nodejs').file(fileName).save(fileContents)