traceWorse

tcmts.cli.traced.traceWorse · #7nexix4j · term · runs on the server

uses tcmts.cli.main.versionsOf, which uses tcmts.share.NameTrees, which uses tcmts.store.unpacked, which uses node:zlib

Of the rows traceTotals gave, what runs worse than its version before it (since since), and the change that did it: what changed in what it reaches, and the history entry that pointed its name at this version.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { changesBetween, versionsOf, versionVerdict } from "#codebase/tcmts/cli/main";
import { statsOf } from "#codebase/tcmts/cli/traced";
import type { Version } from "#codebase/tcmts/cli/traced";
import type { DatabaseSync } from "node:sqlite";

export function traceWorse(
  db: DatabaseSync,
  cb: Codebase,
  since: number,
  rows: { hash: string | null; kind: string; name: string }[],
): { name: string; kind: string; said: string; since?: Version; changed: string[] }[] {
  const out: { name: string; kind: string; said: string; since?: Version; changed: string[] }[] =
    [];
  for (const g of rows) {
    const named = g.hash ? cb.store.pathsOf(g.hash)[0] : undefined;
    if (!g.hash || !named) continue;
    const before = db
      .prepare(
        "SELECT hash, max(start) AS last FROM spans WHERE name = ? AND kind = ? AND hash IS NOT NULL AND hash != ? AND start >= ? GROUP BY hash ORDER BY last DESC LIMIT 1",
      )
      .get(g.name, g.kind, g.hash, since) as { hash: string } | undefined;
    if (!before) continue;
    const of = (h: string) =>
      statsOf(db, "hash = ? AND kind = ? AND start >= ?", [h, g.kind, since]);
    const verdict = versionVerdict(of(before.hash), of(g.hash));
    if (!verdict?.worse) continue;
    const v = versionsOf(cb, named).find((x) => x.hash === g.hash);
    out.push({
      name: named,
      kind: g.kind,
      said: verdict.said,
      ...(v ? { since: v } : {}),
      changed: changesBetween(cb, before.hash, g.hash),
    });
  }
  return out;
}

uses

used by