Skip to content

Commit

Permalink
metrics: replace moment with JavaScript Date API (#3783)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Jun 28, 2024
1 parent 787eab4 commit 6ab5164
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"@google-cloud/storage": "^5.0.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"moment": "^2.29.1",
"split2": "~3.1.1",
"strftime": "~0.10.0"
},
Expand Down
3 changes: 1 addition & 2 deletions ansible/roles/metrics/files/summaries/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"dependencies": {
"@google-cloud/storage": "^5.0.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"moment": "^2.29.1"
"express": "^4.17.1"
},
"devDependencies": {
"eslint": "^7.8.1"
Expand Down
24 changes: 11 additions & 13 deletions ansible/roles/metrics/files/summaries/summaries.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

const { Storage } = require('@google-cloud/storage')
const moment = require('moment')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
Expand All @@ -24,7 +23,7 @@ function csvStream (chunk) {
return
} catch (e) {
console.log(e)
}
}
}

const counts = { bytes: 0, total: 0 }
Expand Down Expand Up @@ -65,19 +64,17 @@ function summary (chunk) {
return
}

async function collectData () {
async function collectData (date) {
const storage = new Storage({
keyFilename: "metrics-processor-service-key.json",
})
let date = moment(new Date())
date = moment(date, 'YYYYMMDD').subtract(1, 'days').format('YYYYMMDD')
const filePrefix = date.toString().concat('/')
console.log(filePrefix)
const [files] = await storage.bucket('processed-logs-nodejs').getFiles({ prefix: `${filePrefix}`})
for (const file of files) {
const data = await storage.bucket('processed-logs-nodejs').file(file.name).download()
const data = await storage.bucket('processed-logs-nodejs').file(file.name).download()
const stringContents = data[0].toString()
const contentsArray = stringContents.split('\n')
const contentsArray = stringContents.split('\n')
for (const line of contentsArray) {
try {
const csvparse = csvStream(line)
Expand All @@ -87,14 +84,12 @@ async function collectData () {
}
}

async function produceSummaries () {
async function produceSummaries (date) {
const storage = new Storage({
keyFilename: "metrics-processor-service-key.json",
})
await collectData()
})
await collectData(date)
prepare()
let date = moment(new Date())
date = moment(date, 'YYYYMMDD').subtract(1, 'days').format('YYYYMMDD')
let outputFile = "nodejs.org-access.log." + date.toString() + ".json"
storage.bucket('access-logs-summaries-nodejs').file(outputFile).save(JSON.stringify(counts), function (err) {
if (err) {
Expand All @@ -106,7 +101,10 @@ async function produceSummaries () {
}

app.post('/', async (req, res) => {
await produceSummaries()
// ToDo: accept optional date parameter https://github.com/nodejs/build/issues/3780
const yesterday = new Date().getTime() - (24 * 60 * 60 * 1000)
const date = new Date(yesterday).toISOString().slice(0, 10).replace(/-/g, '')
await produceSummaries(date)
res.status(200).send()
})

Expand Down

0 comments on commit 6ab5164

Please sign in to comment.