All files / src/lib/overview series.ts

100% Statements 21/21
93.75% Branches 15/16
100% Functions 5/5
100% Lines 21/21

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                                                        71x 784x 784x                             71x 71x 784x             784x 713x                     71x                 71x 71x 71x 71x 71x 71x 71x       71x     4x                   48x 17x 17x      
/**
 * Shared pieces for nodes that carry a series (issue #271): the unfold set
 * of month nodes chained in time, and the status a series gives its node.
 */
import {
  isOutOfRange,
  trendOf,
  type Range,
  type SeriesPoint,
  type Severity,
} from "./derive";
import type { OverviewLink, OverviewNode } from "./types";
 
export interface SeriesSpec {
  series: SeriesPoint[];
  unit: string;
  measure: string;
  range?: Range | null;
  higherIsWorse?: boolean;
}
 
/** The month nodes a series unfolds into, chained oldest to newest. */
function expandSeries(
  parentId: string,
  parentLabel: string,
  spec: SeriesSpec,
  layer: string,
): { nodes: OverviewNode[]; links: OverviewLink[] } {
  const nodes: OverviewNode[] = spec.series.map((p) => {
    const out = isOutOfRange(p.value, spec.range);
    return {
      id: `pt:${parentId}:${p.date}`,
      label: `${p.date.slice(0, 7)} ยท ${p.value}`,
      layer,
      kind: "Month",
      size: 0.9,
      color: out ? "#f59e0b" : "#93c5fd",
      title: `${parentLabel}, ${p.date.slice(0, 7)}`,
      sub: `${p.value} ${spec.unit}${out ? ", outside the expected band" : ""}`,
      facts: [
        ["measure", spec.measure],
        ["band", spec.range?.text ?? "none"],
      ],
    };
  });
  const links: OverviewLink[] = [];
  nodes.forEach((n, i) => {
    links.push({
      source: parentId,
      target: n.id,
      kind: "month",
      color: "#64748b",
      distance: 45,
    });
    if (i > 0) {
      links.push({
        source: nodes[i - 1].id,
        target: n.id,
        kind: "next",
        color: "#93c5fd",
        distance: 28,
        arrow: true,
        particles: 1,
      });
    }
  });
  return { nodes, links };
}
 
/** Attach a series to a node and return the severity the trend gives it. */
export function attachSeries(
  node: OverviewNode,
  spec: SeriesSpec,
  timeLayer: string,
): Severity {
  node.series = spec.series;
  node.unit = spec.unit;
  node.measure = spec.measure;
  node.range = spec.range ?? null;
  node.higherIsWorse = spec.higherIsWorse !== false;
  node.expand = expandSeries(node.id, node.label, spec, timeLayer);
  const t = trendOf(spec.series, {
    range: spec.range,
    higherIsWorse: spec.higherIsWorse !== false,
  });
  return t?.severity ?? "ok";
}
 
const RANK: Record<string, number> = {
  none: 0,
  ok: 1,
  info: 1,
  warn: 2,
  bad: 3,
};
 
/** Raise a node's status, never lower it. */
export function raiseStatus(node: OverviewNode, sev: Severity): void {
  if (RANK[sev] > RANK[node.status ?? "none"]) {
    node.status = sev;
    node.pulse = sev === "bad";
  }
}