Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

11.4 KB
Raw Blame
/**
 * The finance page's shaping (pure, unit tested): the spend tree in reporting-line order with
 * depths, the ticket ledger along parent tickets and which of its rows a fold leaves visible,
 * period arithmetic for the this / previous switch, the tone a budget ratio takes, the marks an
 * employee's state column carries, the daily series in the trend chart's shape (with the axis
 * breaks where days were skipped), the KPI row's numbers, the alert list grouped by state, and
 * the two row tooltips that carry the figures the tables no longer spend a column on.
 */
import type {
  OrgBudgetAlert,
  OrgEmployeeState,
  OrgFinanceEmployee,
  OrgFinanceResponse,
  OrgFinanceTicket,
  UsageSeriesPoint,
} from "@adelie/server/api";
import type { Tone } from "../../lib/tone";
import { employeeStateTone } from "./chart-view";

/** Employees in DFS order from the root(s), each with its depth; a row whose manager is missing starts a tree of its own. */
export function spendTreeRows(
  employees: readonly OrgFinanceEmployee[],
): Array<{ employee: OrgFinanceEmployee; depth: number }> {
  const ids = new Set(employees.map((e) => e.agentId));
  const childrenOf = new Map<string | null, OrgFinanceEmployee[]>();
  for (const e of employees) {
    const parent = e.reportsTo !== null && ids.has(e.reportsTo) ? e.reportsTo : null;
    const list = childrenOf.get(parent);
    if (list) list.push(e);
    else childrenOf.set(parent, [e]);
  }
  const out: Array<{ employee: OrgFinanceEmployee; depth: number }> = [];
  const seen = new Set<string>();
  const walk = (parent: string | null, depth: number): void => {
    for (const e of childrenOf.get(parent) ?? []) {
      if (seen.has(e.agentId)) continue;
      seen.add(e.agentId);
      out.push({ employee: e, depth });
      walk(e.agentId, depth + 1);
    }
  };
  walk(null, 0);
  return out;
}

/** Tickets in parent-first order with depths (a child under its parent, an unknown parent starting at the top). */
export function ticketTreeRows(
  tickets: readonly OrgFinanceTicket[],
): Array<{ ticket: OrgFinanceTicket; depth: number }> {
  const ids = new Set(tickets.map((t) => t.ticketId));
  const childrenOf = new Map<string | null, OrgFinanceTicket[]>();
  for (const t of tickets) {
    const parent = t.parent !== undefined && ids.has(t.parent) ? t.parent : null;
    const list = childrenOf.get(parent);
    if (list) list.push(t);
    else childrenOf.set(parent, [t]);
  }
  const out: Array<{ ticket: OrgFinanceTicket; depth: number }> = [];
  const seen = new Set<string>();
  const walk = (parent: string | null, depth: number): void => {
    for (const t of childrenOf.get(parent) ?? []) {
      if (seen.has(t.ticketId)) continue;
      seen.add(t.ticketId);
      out.push({ ticket: t, depth });
      walk(t.ticketId, depth + 1);
    }
  };
  walk(null, 0);
  return out;
}

/** A ledger row on screen: the ticket, its depth, and how many children hang directly under it. */
export interface LedgerRow {
  ticket: OrgFinanceTicket;
  depth: number;
  /** Direct children only — what a folded parent reports, and 0 on a leaf (which wears no chevron). */
  children: number;
}

/**
 * The ticket ledger's rows as the table draws them: a parent's subtree is hidden unless the
 * parent's id is in `expanded`, so a board of a hundred tickets opens as a short list of roots
 * and grows only where the reader asks. A row is dropped when *any* ancestor is folded, not
 * just its own parent, which is what makes expanding a grandchild's parent enough on its own.
 *
 * `rows` is `ticketTreeRows`' output: parent-first depth-first, so a row's subtree is exactly
 * the run of deeper rows that follows it. Expansion is the caller's state and deliberately not
 * persisted — a fold is how the reader looks at the table now, not a setting.
 */
export function visibleLedgerRows(
  rows: ReadonlyArray<{ ticket: OrgFinanceTicket; depth: number }>,
  expanded: ReadonlySet<string>,
): LedgerRow[] {
  const out: LedgerRow[] = [];
  /** The depth of the shallowest folded parent whose subtree is being skipped; null while none is. */
  let folded: number | null = null;
  for (let i = 0; i < rows.length; i++) {
    const row = rows[i]!;
    if (folded !== null && row.depth > folded) continue;
    folded = null;
    let children = 0;
    for (let j = i + 1; j < rows.length && rows[j]!.depth > row.depth; j++) {
      if (rows[j]!.depth === row.depth + 1) children++;
    }
    out.push({ ticket: row.ticket, depth: row.depth, children });
    if (children > 0 && !expanded.has(row.ticket.ticketId)) folded = row.depth;
  }
  return out;
}

/**
 * The tooltip an employee row of the spend tree carries: own spend, cumulative spend and the
 * budget, each named. The table shows cumulative against budget in one cell and leaves own
 * spend to this line, so the reader still reaches it without a column of its own. `money`
 * formats an amount in the reader's currency; `labels.noBudget` stands in for an unbounded one.
 */
export function spendRowTooltip(
  row: { own: number; cumulative: number; budget?: number },
  labels: { own: string; cumulative: string; budget: string; noBudget: string },
  money: (value: number) => string,
): string {
  return [
    `${labels.own} ${money(row.own)}`,
    `${labels.cumulative} ${money(row.cumulative)}`,
    `${labels.budget} ${row.budget === undefined ? labels.noBudget : money(row.budget)}`,
  ].join(" · ");
}

/**
 * The tooltip a ticket row carries: its id, its owner, its own cost and its rolled-up cost —
 * the three the table dropped so it fits beside the spend tree. `owner` is the display name of
 * whoever owns it, absent when nobody does.
 */
export function ticketRowTooltip(
  row: { ticketId: string; cost: number; rolledUp: number },
  owner: string | undefined,
  labels: { owner: string; noOwner: string; cost: string; rolledUp: string },
  money: (value: number) => string,
): string {
  return [
    row.ticketId,
    `${labels.owner} ${owner ?? labels.noOwner}`,
    `${labels.cost} ${money(row.cost)}`,
    `${labels.rolledUp} ${money(row.rolledUp)}`,
  ].join(" · ");
}

/** `yyyy-mm` shifted by whole months (a negative delta goes back). Null for anything not of that shape. */
export function shiftPeriod(period: string, delta: number): string | null {
  const m = /^(\d{4})-(\d{2})$/.exec(period);
  if (!m) return null;
  const year = Number(m[1]);
  const month0 = Number(m[2]) - 1;
  if (month0 < 0 || month0 > 11) return null;
  const total = year * 12 + month0 + delta;
  const y = Math.floor(total / 12);
  const mo = total - y * 12;
  return `${String(y).padStart(4, "0")}-${String(mo + 1).padStart(2, "0")}`;
}

/**
 * The four tones a budget reading takes. Narrower than `Tone`, so a reading with a budget is
 * also a package `ToneName` (a `StatTile`'s or a `ProgressBar`'s tone) once `muted` is ruled out.
 */
export type BudgetTone = Extract<Tone, "muted" | "success" | "attention" | "danger">;

/** The tone a spend-against-budget ratio takes: attention from 80%, danger from 100%, success below; muted without a budget. */
export function budgetTone(ratio: number | undefined): BudgetTone {
  if (ratio === undefined || !Number.isFinite(ratio)) return "muted";
  if (ratio >= 1) return "danger";
  if (ratio >= 0.8) return "attention";
  return "success";
}

/** What a mark in the spend tree's state column says; the caller spells each one's label. */
export type SpendStateKey = "running" | "idle" | "warned" | "paused";

export interface SpendStateMark {
  tone: Tone;
  key: SpendStateKey;
}

/**
 * The state column's marks for one employee: its live state first — running, or on the desk —
 * then what its budget has to say about it. A budget pause is the *reason* a live state reads
 * `paused`, so the two collapse into the single danger mark that names it; a row never says
 * "paused" twice. The live state is joined from the org chart, which the finance page fetches
 * best effort, so an absent one leaves the budget marks standing alone rather than blocking
 * the table on a second request.
 */
export function spendStateMarks(
  row: { warned: boolean; paused: boolean },
  live: OrgEmployeeState | undefined,
): SpendStateMark[] {
  const out: SpendStateMark[] = [];
  if (live !== undefined && live !== "paused")
    out.push({ tone: employeeStateTone(live), key: live });
  if (row.paused || live === "paused") out.push({ tone: "danger", key: "paused" });
  else if (row.warned) out.push({ tone: "attention", key: "warned" });
  return out;
}

/** The finance page's daily costs in the cost trend chart's series shape (tokens unknown here, so zero). */
export function financeSeries(
  daily: ReadonlyArray<{ date: string; cost: number }>,
): UsageSeriesPoint[] {
  return daily.map((d) => ({
    bucket: d.date,
    cacheRead: 0,
    cacheWrite: 0,
    output: 0,
    total: 0,
    cost: d.cost,
    requests: 0,
    completed: 0,
    denominator: 0,
  }));
}

/** `yyyy-mm-dd` as a UTC day number; null for anything else. */
function dayNumber(date: string): number | null {
  const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date);
  if (!m) return null;
  const ms = Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]));
  return Number.isNaN(ms) ? null : Math.round(ms / 86_400_000);
}

/**
 * Indices after which the daily series skips at least one calendar day: the server lists
 * only the days that recorded a cost, so the chart marks the gaps on its axis rather than
 * drawing the remaining days as neighbours.
 */
export function dailyBreaks(daily: ReadonlyArray<{ date: string }>): number[] {
  const out: number[] = [];
  for (let i = 0; i + 1 < daily.length; i++) {
    const a = dayNumber(daily[i]!.date);
    const b = dayNumber(daily[i + 1]!.date);
    if (a !== null && b !== null && b - a > 1) out.push(i);
  }
  return out;
}

/** The KPI row: the organization's total against the root's (CEO's) budget, head counts and alert counts. */
export interface FinanceKpis {
  total: number;
  /** The root employee's budget, which covers the whole organization; absent = unbounded. */
  budget?: number;
  ratio?: number;
  employees: number;
  /** Employees with a budget of their own. */
  budgeted: number;
  /** Warned but not (yet) paused. */
  warned: number;
  paused: number;
}

export function financeKpis(data: Pick<OrgFinanceResponse, "employees" | "total">): FinanceKpis {
  const root = data.employees.find((e) => e.reportsTo === null) ?? data.employees[0];
  const budget = root?.budget;
  const ratio = budget !== undefined && budget > 0 ? data.total / budget : undefined;
  return {
    total: data.total,
    ...(budget !== undefined ? { budget } : {}),
    ...(ratio !== undefined ? { ratio } : {}),
    employees: data.employees.length,
    budgeted: data.employees.filter((e) => e.budget !== undefined).length,
    warned: data.employees.filter((e) => e.warned && !e.paused).length,
    paused: data.employees.filter((e) => e.paused).length,
  };
}

/**
 * Alerts grouped by state, newest first inside each group: a pause outranks the warning
 * that preceded it, so an alert carrying both timestamps lists under `paused` only.
 */
export function groupAlerts(alerts: readonly OrgBudgetAlert[]): {
  paused: OrgBudgetAlert[];
  warned: OrgBudgetAlert[];
} {
  const paused = alerts.filter((a) => a.pausedAt !== undefined);
  const warned = alerts.filter((a) => a.pausedAt === undefined && a.warnedAt !== undefined);
  const newestFirst = (key: "pausedAt" | "warnedAt") => (a: OrgBudgetAlert, b: OrgBudgetAlert) =>
    (b[key] ?? "").localeCompare(a[key] ?? "");
  return {
    paused: paused.sort(newestFirst("pausedAt")),
    warned: warned.sort(newestFirst("warnedAt")),
  };
}