Skip to content

Instantly share code, notes, and snippets.

@makotom
Created February 5, 2021 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makotom/b85e891a83c4e779dd06ced215830e3b to your computer and use it in GitHub Desktop.
Save makotom/b85e891a83c4e779dd06ced215830e3b to your computer and use it in GitHub Desktop.
Generate per-minute histogram from timestamps
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