Skip to content

Commit c6c1e25

Browse files
committed
Add totals and percentages
1 parent 3de5578 commit c6c1e25

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

packages/cursorless-engine/src/CommandHistoryAnalyzer.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import {
44
PartialPrimitiveTargetDescriptor,
55
ScopeType,
66
} from "@cursorless/common";
7-
import globRaw from "glob";
8-
import { groupBy, map } from "lodash";
9-
import { promisify } from "node:util";
7+
import { groupBy, map, sum } from "lodash";
108
import { asyncIteratorToList } from "./asyncIteratorToList";
119
import { canonicalizeAndValidateCommand } from "./core/commandVersionUpgrades/canonicalizeAndValidateCommand";
1210
import { generateCommandHistoryEntries } from "./generateCommandHistoryEntries";
@@ -15,8 +13,6 @@ import { getPartialTargetDescriptors } from "./util/getPartialTargetDescriptors"
1513
import { getPartialPrimitiveTargets } from "./util/getPrimitiveTargets";
1614
import { getScopeType } from "./util/getScopeType";
1715

18-
export const glob = promisify(globRaw);
19-
2016
/**
2117
* Analyzes the command history for a given time period, and outputs a report
2218
*/
@@ -36,21 +32,22 @@ class Period {
3632

3733
toString(): string {
3834
return [
39-
`[${this.period}]`,
40-
`Total commands: ${this.count}`,
35+
`# ${this.period}`,
36+
`Total command count: ${this.count}`,
4137
this.serializeMap("Actions", this.actions),
4238
this.serializeMap("Modifiers", this.modifiers),
4339
this.serializeMap("Scope types", this.scopeTypes),
4440
].join("\n\n");
4541
}
4642

4743
private serializeMap(name: string, map: Record<string, number>) {
44+
const total = sum(Object.values(map));
4845
const entries = Object.entries(map);
4946
entries.sort((a, b) => b[1] - a[1]);
5047
const entriesSerialized = entries
51-
.map(([key, value]) => ` ${key}: ${value}`)
48+
.map(([key, value]) => ` ${key}: ${value} (${toPercent(value / total)})`)
5249
.join("\n");
53-
return `${name} (${entries.length}):\n${entriesSerialized}`;
50+
return `${name}:\n${entriesSerialized}`;
5451
}
5552

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

105-
const content = map(
106-
Object.entries(groupBy(entries, getMonth)),
107-
([key, entries]) => new Period(key, entries).toString(),
108-
).join("\n\n");
102+
const content = [
103+
new Period("Totals", entries).toString(),
104+
105+
...map(Object.entries(groupBy(entries, getMonth)), ([key, entries]) =>
106+
new Period(key, entries).toString(),
107+
),
108+
].join("\n\n\n");
109109

110110
await ide().openUntitledTextDocument({ content });
111111
}
112+
113+
function toPercent(value: number) {
114+
return Intl.NumberFormat(undefined, {
115+
style: "percent",
116+
minimumFractionDigits: 0,
117+
maximumFractionDigits: 1,
118+
}).format(value);
119+
}

packages/cursorless-engine/src/generateCommandHistoryEntries.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { CommandHistoryEntry } from "@cursorless/common";
2+
import globRaw from "glob";
23
import { readFile } from "node:fs/promises";
34
import path from "node:path";
4-
import { glob } from "./CommandHistoryAnalyzer";
5+
import { promisify } from "node:util";
56

67
export async function* generateCommandHistoryEntries(dir: string) {
78
const files = await glob("*.jsonl", { cwd: dir });
@@ -20,3 +21,5 @@ export async function* generateCommandHistoryEntries(dir: string) {
2021
}
2122
}
2223
}
24+
25+
const glob = promisify(globRaw);

0 commit comments

Comments
 (0)