Skip to content

Instantly share code, notes, and snippets.

@Psidium
Created July 22, 2022 13:35
Show Gist options
  • Select an option

  • Save Psidium/f2a96d38e2b0f72e9b290f5a36959444 to your computer and use it in GitHub Desktop.

Select an option

Save Psidium/f2a96d38e2b0f72e9b290f5a36959444 to your computer and use it in GitHub Desktop.
Using Deno to gather Statistics about the Zsh history file
import _ from 'https://deno.land/x/lodash@4.17.15-es/lodash.js';
const file = await Deno.readTextFile("/Users/psidium/.zsh_history");
const iterator = file.matchAll(/:\s*(\d*):0;/g);
const dates: Date[] = [];
for (const [, timestamp] of iterator) {
dates.push(new Date(1000*Number(timestamp)));
}
enum Granularity {
Hour = 0,
Day = 1,
Month = 2,
Year = 3,
}
const makeGrouperBy =
(granularity: Granularity) =>
(date: Date): string => {
let output = String(date.getFullYear());
if (granularity <= Granularity.Month) {
output += " " + date.getMonth();
}
if (granularity <= Granularity.Day) {
output += " " + date.getDay();
}
if (granularity <= Granularity.Hour) {
output += " " + date.getHours();
}
return output;
};
function count(grouped: {[key: string]: Date[]}) {
return Object.entries(grouped).map(([key, value]) => [key, value.length]);
}
for (const curr of [0, 1, 2, 3]) {
const grouped = _.groupBy(dates, makeGrouperBy(curr));
console.log(Granularity[curr]);
const counted = count(grouped);
const sum = counted.reduce((acc, [, value]) => acc + Number(value), 0);
const average = sum / counted.length;
console.log({average});
// console.log(counted);
}
@Psidium
Copy link
Copy Markdown
Author

Psidium commented Jul 22, 2022

run with deno run --allow-read ~/hist_count.ts

watch for the history filepath 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment