Files
CatoandClaude Opus 5.5 3cf5c26f49 feat(data): quality criteria, automated checks and /data/quality; fix what they found (#116)
* fix(data): correct what the quality checks and a 36-row source sample found

- listings: 8 pinned home lines stored a composite FIGI that OpenFIGI cannot
  resolve (openfigi.com/id 404, ID_BB_GLOBAL mapping empty); pinnedLine now
  keeps the exchange line's own FIGI (TSMC, UMC, GlobalWafers, ASE, SMIC,
  Rio Tinto, NKT, TSMC Advanced Packaging)
- sanctions: all 49 EU amendment dates were a day early (Brussels-midnight
  stamps read in UTC); 4 OFAC programmes had empty titles (menu link parsed first)
- usgs-producers: wrapped workbook cells were split into bogus facilities
  (64 fragments -> 5); Chile's copper table had lost Escondida, Spence, Sierra
  Gorda and 12 more rows behind a 'Continued' commodity cell
- events/dependencies: 7 paraphrased quotes replaced with the source's
  verbatim text; SK Siltron event names its directory company
- series: 18 Cameco points get the date their MD&A states
- directory: Shin-Etsu Handotai source 404 replaced; Fangyuan careers URL
  (domain does not resolve) set to null as its evidence already said

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat(data): written quality criteria per dataset, automated checks, /data/quality

- config/substrata-quality.ts: 15 datasets x 6 criteria (completeness,
  correctness, provenance, freshness, link health, consistency); each a rule or
  'na' with a reason; freshness reuses config/substrata-freshness.ts
- lib/quality: pure checks (USGS world sums, quote states the value, names
  resolve, cross-dataset agreement with USGS) run in verify with a failure-count
  ratchet (test/quality.test.ts); network checks (links with 3 tries and a
  blocked/broken distinction, quotes on page, SEC ticker file, OpenFIGI mapping,
  USGS values on the chapter PDF) run on a rotation via /api/cron/quality
- migration 014: research_quality_checks + research_quality_runs (trend)
- /data/quality: scores, counts, trend and every failing row with its source
  and a prefilled correction issue; linked from /data, footer, palette
- check-series.ts shares the reader (CDATA and ' ,' fixes found by the sample)
- pnpm run quality [-- --network | --all | --json out.json]; no AI anywhere

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 00:23:09 -07:00

104 lines
3.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The shape every data-quality check returns, and the one scoring rule.
*
* A check answers one written criterion (config/substrata-quality.ts) for one
* dataset: how many rows it looked at, how many held, and each row that did
* not — with the link a reader needs to see for themselves. Pure checks run in
* `verify` and on every page view; network checks run on a timer and are read
* back from the database. Both return this, so the page and the ratchet never
* need to know which kind they are holding.
*/
export const CRITERIA = [
'completeness',
'correctness',
'provenance',
'freshness',
'links',
'consistency',
] as const;
export type CriterionId = (typeof CRITERIA)[number];
export interface Failure {
/** Which row, as a reader would name it ("tin: Minsur", "lithium 2025 Chile"). */
row: string;
/** What is wrong, in one line. */
problem: string;
/** The source to open, when the row has one. */
link?: string;
/** The page on this site that shows the row. */
page?: string;
}
export interface CheckResult {
dataset: string;
criterion: CriterionId;
/** Stable id of the check, `dataset/what`. */
check: string;
/** One line: what was compared with what. */
label: string;
checked: number;
passed: number;
failures: Failure[];
/** 'pure' re-derives from committed files; 'network' and 'database' are read back from the last run. */
kind: 'pure' | 'network' | 'database';
/** For network checks: rows not looked at yet in the current rotation. */
unchecked?: number;
/** For network checks: looks that could not tell (robots refused, unreadable here); not scored. */
cannotTell?: number;
/** When the evidence was gathered, for network and database checks. */
asOf?: string;
}
export interface CriterionScore {
criterion: CriterionId;
checked: number;
passed: number;
/** passed / checked, 0–100, or null when nothing could be checked. */
score: number | null;
}
/** The rule: rows that held over rows looked at, pooled across a criterion's checks. */
export function scoreOf(results: readonly CheckResult[]): number | null {
const checked = results.reduce((n, r) => n + r.checked, 0);
if (checked === 0) return null;
const passed = results.reduce((n, r) => n + r.passed, 0);
return Math.floor((passed / checked) * 1000) / 10;
}
export function criterionScores(results: readonly CheckResult[]): CriterionScore[] {
return CRITERIA.map((criterion) => {
const mine = results.filter((r) => r.criterion === criterion);
return {
criterion,
checked: mine.reduce((n, r) => n + r.checked, 0),
passed: mine.reduce((n, r) => n + r.passed, 0),
score: scoreOf(mine),
};
});
}
/** A dataset's one number: the mean of the criteria that could be scored. */
export function overallScore(scores: readonly CriterionScore[]): number | null {
const known = scores.map((s) => s.score).filter((s): s is number => s !== null);
if (known.length === 0) return null;
return Math.floor((known.reduce((a, b) => a + b, 0) / known.length) * 10) / 10;
}
/** Build a result from rows and a predicate that returns the problem, or null when the row holds. */
export function judge<T>(
meta: Omit<CheckResult, 'checked' | 'passed' | 'failures' | 'kind'>,
rows: readonly T[],
problem: (row: T) => (Failure & { problem: string }) | null,
): CheckResult {
const failures = rows.map(problem).filter((f): f is Failure => f !== null);
return {
...meta,
kind: 'pure',
checked: rows.length,
passed: rows.length - failures.length,
failures,
};
}