Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | 34x 34x 34x 3150x 34x 35x 35x 3150x 35x 34x 34x 34x 34x 45x 16x 34x 34x 34x 186x 825x 34x 49x 34x 24x 24x 24x 42x 664x 42x 42x 18x 34x 210x 34x 5x 11x 2x 5x 5x 6x 5x 5x 2x 5x 6x 8x 34x 34x 34x 34x 34x 17x 34x 40x 34x 4x 3x 34x 34x 6x 4x 3x 4x 3x 3x 15x 4x 4x 34x 34x 5x 6x 5x 5x 6x 1x 5x 5x 5x 5x 25x 5x | import type { DayUsage } from "#layers/snapshot/shared/types/DayUsage.ts";
import type { LintUsage } from "#layers/snapshot/shared/types/LintUsage.ts";
import type { UsageArtifact } from "#layers/snapshot/shared/types/UsageArtifact.ts";
import { FOUGHT_DISABLES, NOISY_SHARE } from "./lintUsage.ts";
import { FAILING_RATE, MIN_RUNS, windowStart } from "./usage.ts";
const DAY_MS = 24 * 60 * 60 * 1000;
const EMPTY_DAY: DayUsage = { runs: 0, agent: 0, failures: 0 };
interface ScriptRow extends DayUsage {
name: string;
/** One entry per `windowDays` day, in the same order. */
days: DayUsage[];
/** `null` for a script folder with no recorded run. */
last: string | null;
failing: boolean;
}
interface RuleRow {
rule: string;
hits: number;
fixable: number;
disabled: number;
/** Hits per project, in the order of `LintTable.projects`. */
projects: number[];
}
interface LintTable {
/** Busiest first. */
projects: string[];
rows: RuleRow[];
}
const nextDay = (day: string): string =>
new Date(Date.parse(`${day}T00:00:00Z`) + DAY_MS).toISOString().slice(0, 10);
/** Every day from the window's first through the snapshot's, so a day with no run still gets a cell. */
export const windowDays = (generatedAt: string): string[] => {
const days: string[] = [];
const last = generatedAt.slice(0, 10);
for (let day = windowStart(generatedAt); day <= last; day = nextDay(day)) days.push(day);
return days;
};
/** The fewest days a strip shows, so a log a day old still reads as a timeline. */
const MIN_SHOWN_DAYS = 14;
/** The window's days from the first with a run, so a young log isn't drawn as months of nothing. */
export const shownDays = ({
generatedAt,
scripts,
}: Pick<UsageArtifact, "generatedAt" | "scripts">): string[] => {
const days = windowDays(generatedAt);
const first = Object.values(scripts)
.flatMap((usage) => Object.keys(usage.days ?? {}))
.sort((a, b) => a.localeCompare(b))[0];
const from = first ? Math.max(0, days.indexOf(first)) : 0;
return days.slice(Math.max(0, Math.min(from, days.length - MIN_SHOWN_DAYS)));
};
export const sumDays = (days: DayUsage[]): DayUsage =>
days.reduce(
(total, day) => ({
runs: total.runs + day.runs,
agent: total.agent + day.agent,
failures: total.failures + day.failures,
}),
EMPTY_DAY,
);
export const isFailing = ({ runs, failures }: DayUsage): boolean =>
runs >= MIN_RUNS && failures / runs >= FAILING_RATE;
/** Every known script and every recorded one, busiest in the window first. */
export const scriptRows = ({
generatedAt,
known,
scripts,
}: Omit<UsageArtifact, "lint">): ScriptRow[] => {
const days = shownDays({ generatedAt, scripts });
const names = [...new Set([...known, ...Object.keys(scripts)])];
return names
.map((name) => {
const usage = scripts[name];
const daily = days.map((day) => usage?.days?.[day] ?? EMPTY_DAY);
// A snapshot collected before per-day counts still knows how many runs fell in the window.
const total =
usage && !usage.days ? { ...EMPTY_DAY, runs: usage.recent } : sumDays(daily);
return {
name,
days: daily,
...total,
last: usage?.last ?? null,
failing: isFailing(total),
};
})
.sort(
(a, b) =>
b.runs - a.runs ||
(b.last ?? "").localeCompare(a.last ?? "") ||
a.name.localeCompare(b.name),
);
};
/** Each window day summed across scripts. */
export const dailyTotals = (rows: ScriptRow[]): DayUsage[] =>
(rows[0]?.days ?? []).map((_, index) =>
sumDays(rows.map((row) => row.days[index] ?? EMPTY_DAY)),
);
/** Rules that fired or are disabled inline, against every project any of them fired in. */
export const lintTable = (lint: LintUsage): LintTable => {
const shown = Object.entries(lint.rules)
.filter(([, usage]) => usage.recent > 0 || usage.disabled > 0)
.sort(([, a], [, b]) => b.recent - a.recent || b.disabled - a.disabled);
const totals: Record<string, number> = {};
for (const [, usage] of shown)
for (const [project, count] of Object.entries(usage.projects))
totals[project] = (totals[project] ?? 0) + count;
const projects = Object.keys(totals).sort(
(a, b) => (totals[b] ?? 0) - (totals[a] ?? 0) || a.localeCompare(b),
);
return {
projects,
rows: shown.map(([rule, usage]) => ({
rule,
hits: usage.recent,
fixable: usage.fixable,
disabled: usage.disabled,
projects: projects.map((project) => usage.projects[project] ?? 0),
})),
};
};
export type CallerMix = "agents" | "both" | "people";
export type FixKind = "manual" | "auto";
interface Tick {
/** Percent along the axis. */
at: number;
label: string;
}
interface Axis {
ticks: Tick[];
/** Percent along the axis where the alert's threshold sits. */
threshold: number;
}
interface Point<Kind extends string> {
name: string;
/** Percent from the left. */
x: number;
/** Percent from the bottom. */
y: number;
kind: Kind;
/** The figures a tooltip names, beside the point's `name`. */
detail: Record<string, number>;
labelled: boolean;
}
export interface Quadrants<Kind extends string> {
x: Axis;
y: Axis;
points: Point<Kind>[];
}
/** Keeps zero off the log scale's first decade, so a count of 1 isn't drawn on the axis. */
const ZERO_BAND = 8;
const decadesFor = (max: number): number => Math.max(1, Math.ceil(Math.log10(Math.max(10, max))));
/** Percent along a log axis of `decades` powers of ten; zero gets a lane of its own off the axis. */
const logAt = (value: number, decades: number): number =>
value <= 0 ? ZERO_BAND / 2 : ZERO_BAND + ((100 - ZERO_BAND) * Math.log10(value)) / decades;
const logTicks = (decades: number): Tick[] => [
{ at: ZERO_BAND / 2, label: "0" },
...Array.from({ length: decades + 1 }, (_, power) => ({
at: logAt(10 ** power, decades),
label: String(10 ** power),
})),
];
const linearTicks = (max: number, format: (value: number) => string): Tick[] =>
[0, 0.25, 0.5, 0.75, 1].map((step) => ({ at: step * 100, label: format(step * max) }));
const callerMix = ({ runs, agent }: DayUsage): CallerMix => {
if (agent === runs) return "agents";
return agent === 0 ? "people" : "both";
};
const rate = (part: number, whole: number): number => (whole === 0 ? 0 : part / whole);
/** Scripts run in the window: how often against how often they fail, on the alerts' thresholds. */
export const scriptQuadrants = (rows: ScriptRow[]): Quadrants<CallerMix> => {
const plotted = rows.filter((row) => row.runs > 0);
const decades = decadesFor(Math.max(MIN_RUNS, ...plotted.map((row) => row.runs)));
const topRate = Math.max(
FAILING_RATE * 2,
...plotted.map((row) => rate(row.failures, row.runs)),
);
const yMax = Math.ceil(topRate * 10) / 10;
return {
x: { ticks: logTicks(decades), threshold: logAt(MIN_RUNS, decades) },
y: {
ticks: linearTicks(yMax, (value) => `${Math.round(value * 100)}%`),
threshold: (FAILING_RATE / yMax) * 100,
},
points: plotted.map((row) => {
const failed = rate(row.failures, row.runs);
return {
name: row.name,
x: logAt(row.runs, decades),
y: (failed / yMax) * 100,
kind: callerMix(row),
detail: { runs: row.runs, failed: Math.round(failed * 100), agent: row.agent },
labelled: true,
};
}),
};
};
/** How many labelled rules a quiet chart still names, so it never reads as anonymous dots. */
const NAMED_BUSIEST = 3;
/** Rules that fired in the window or are disabled inline: hits against `eslint-disable` comments. */
export const ruleQuadrants = (lint: LintUsage): Quadrants<FixKind> => {
const rules = Object.entries(lint.rules);
const recentTotal = rules.reduce((sum, [, usage]) => sum + usage.recent, 0);
const noisyAt = NOISY_SHARE * recentTotal;
const shown = rules
.filter(([, usage]) => usage.recent > 0 || usage.disabled > 0)
.sort(([, a], [, b]) => b.recent - a.recent || b.disabled - a.disabled);
const decades = decadesFor(Math.max(noisyAt, ...shown.map(([, usage]) => usage.recent)));
const topDisabled = Math.max(FOUGHT_DISABLES * 2, ...shown.map(([, usage]) => usage.disabled));
const yMax = Math.ceil(topDisabled / 4) * 4;
return {
x: { ticks: logTicks(decades), threshold: logAt(Math.max(1, noisyAt), decades) },
y: {
ticks: linearTicks(yMax, (value) => String(Math.round(value))),
threshold: (FOUGHT_DISABLES / yMax) * 100,
},
points: shown.map(([name, usage], index) => ({
name,
x: logAt(usage.recent, decades),
y: (usage.disabled / yMax) * 100,
kind: usage.recent > 0 && usage.fixable === usage.recent ? "auto" : "manual",
detail: { hits: usage.recent, fixed: usage.fixable, disabled: usage.disabled },
labelled:
index < NAMED_BUSIEST ||
usage.disabled >= FOUGHT_DISABLES ||
(recentTotal > 0 && usage.recent >= noisyAt),
})),
};
};
|