Created
February 5, 2021 10:13
-
-
Save makotom/b85e891a83c4e779dd06ced215830e3b to your computer and use it in GitHub Desktop.
Generate per-minute histogram from timestamps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
function countTimestamps(timestamps) { | |
const ret = new Map(); | |
timestamps.forEach((timestamp) => { | |
ret.set(timestamp, (ret.get(timestamp) || 0) + 1); | |
}); | |
return ret; | |
} | |
function getHistogramTSVArray(timestamps) { | |
const ret = []; | |
const values = []; | |
const tsCounts = countTimestamps(timestamps); | |
const epochStart = new Date(`${timestamps[0]}Z`).getTime(); | |
const epochEnd = new Date(`${timestamps[timestamps.length - 1]}Z`).getTime(); | |
tsCounts.forEach((value, key) => { | |
values[(new Date(`${key}Z`).getTime() - epochStart) / 1000 / 60] = value; | |
}); | |
for (let iter = 0; epochStart + iter * 60 * 1000 <= epochEnd; iter += 1) { | |
ret.push([new Date(epochStart + iter * 60 * 1000), values[iter] || 0].join('\t')); | |
} | |
return ret; | |
} | |
{ | |
const src = fs.readFileSync('task-started-timestamps.txt', 'utf-8'); | |
const timestamps = src.trim().split('\n'); | |
console.log(getHistogramTSVArray(timestamps).join('\n')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment