Skip to content

Commit

Permalink
Add totals and percentages
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Jan 31, 2024
1 parent 3de5578 commit c6c1e25
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
34 changes: 21 additions & 13 deletions packages/cursorless-engine/src/CommandHistoryAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
PartialPrimitiveTargetDescriptor,
ScopeType,
} from "@cursorless/common";
import globRaw from "glob";
import { groupBy, map } from "lodash";
import { promisify } from "node:util";
import { groupBy, map, sum } from "lodash";
import { asyncIteratorToList } from "./asyncIteratorToList";
import { canonicalizeAndValidateCommand } from "./core/commandVersionUpgrades/canonicalizeAndValidateCommand";
import { generateCommandHistoryEntries } from "./generateCommandHistoryEntries";
Expand All @@ -15,8 +13,6 @@ import { getPartialTargetDescriptors } from "./util/getPartialTargetDescriptors"
import { getPartialPrimitiveTargets } from "./util/getPrimitiveTargets";
import { getScopeType } from "./util/getScopeType";

export const glob = promisify(globRaw);

/**
* Analyzes the command history for a given time period, and outputs a report
*/
Expand All @@ -36,21 +32,22 @@ class Period {

toString(): string {
return [
`[${this.period}]`,
`Total commands: ${this.count}`,
`# ${this.period}`,
`Total command count: ${this.count}`,
this.serializeMap("Actions", this.actions),
this.serializeMap("Modifiers", this.modifiers),
this.serializeMap("Scope types", this.scopeTypes),
].join("\n\n");
}

private serializeMap(name: string, map: Record<string, number>) {
const total = sum(Object.values(map));
const entries = Object.entries(map);
entries.sort((a, b) => b[1] - a[1]);
const entriesSerialized = entries
.map(([key, value]) => ` ${key}: ${value}`)
.map(([key, value]) => ` ${key}: ${value} (${toPercent(value / total)})`)
.join("\n");
return `${name} (${entries.length}):\n${entriesSerialized}`;
return `${name}:\n${entriesSerialized}`;
}

private append(entry: CommandHistoryEntry) {
Expand Down Expand Up @@ -102,10 +99,21 @@ function getMonth(entry: CommandHistoryEntry): string {
export async function analyzeCommandHistory(dir: string) {
const entries = await asyncIteratorToList(generateCommandHistoryEntries(dir));

const content = map(
Object.entries(groupBy(entries, getMonth)),
([key, entries]) => new Period(key, entries).toString(),
).join("\n\n");
const content = [
new Period("Totals", entries).toString(),

...map(Object.entries(groupBy(entries, getMonth)), ([key, entries]) =>
new Period(key, entries).toString(),
),
].join("\n\n\n");

await ide().openUntitledTextDocument({ content });
}

function toPercent(value: number) {
return Intl.NumberFormat(undefined, {
style: "percent",
minimumFractionDigits: 0,
maximumFractionDigits: 1,
}).format(value);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CommandHistoryEntry } from "@cursorless/common";
import globRaw from "glob";
import { readFile } from "node:fs/promises";
import path from "node:path";
import { glob } from "./CommandHistoryAnalyzer";
import { promisify } from "node:util";

export async function* generateCommandHistoryEntries(dir: string) {
const files = await glob("*.jsonl", { cwd: dir });
Expand All @@ -20,3 +21,5 @@ export async function* generateCommandHistoryEntries(dir: string) {
}
}
}

const glob = promisify(globRaw);

0 comments on commit c6c1e25

Please sign in to comment.