Created
July 22, 2022 13:35
-
-
Save Psidium/f2a96d38e2b0f72e9b290f5a36959444 to your computer and use it in GitHub Desktop.
Using Deno to gather Statistics about the Zsh history file
This file contains hidden or 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
| 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); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
run with
deno run --allow-read ~/hist_count.tswatch for the history filepath 😂