Fix financial taxonomy snapshot normalization

This commit is contained in:
2026-03-13 19:01:56 -04:00
parent b1c9c0ef08
commit 30977dc15f
16 changed files with 1273 additions and 156 deletions

View File

@@ -2,6 +2,7 @@ import { hydrateFilingTaxonomySnapshot } from '@/lib/server/taxonomy/engine';
import { listFilingsRecords, updateFilingMetricsById } from '@/lib/server/repos/filings';
import {
getFilingTaxonomySnapshotByFilingId,
normalizeFilingTaxonomySnapshotPayload,
upsertFilingTaxonomySnapshot
} from '@/lib/server/repos/filing-taxonomy';
@@ -186,8 +187,12 @@ async function runBackfill(options: ScriptOptions): Promise<ScriptSummary> {
summary.wouldWrite += 1;
if (options.apply) {
await upsertFilingTaxonomySnapshot(snapshot);
await updateFilingMetricsById(row.id, snapshot.derived_metrics);
const normalizedSnapshot = {
...snapshot,
...normalizeFilingTaxonomySnapshotPayload(snapshot)
};
await upsertFilingTaxonomySnapshot(normalizedSnapshot);
await updateFilingMetricsById(row.id, normalizedSnapshot.derived_metrics);
summary.written += 1;
}
} catch (error) {

View File

@@ -312,12 +312,14 @@ function relativeDiff(left: number | null, right: number | null) {
return Math.abs(left - right) / baseline;
}
function periodStart(period: ResultPeriod) {
return period.periodStart ?? period.period_start ?? null;
function periodStart(period: ResultPeriod): string | null {
const start = ('periodStart' in period ? period.periodStart : undefined) ?? period.period_start ?? null;
return typeof start === 'string' ? start : null;
}
function periodEnd(period: ResultPeriod) {
return period.periodEnd ?? period.period_end ?? null;
function periodEnd(period: ResultPeriod): string | null {
const end = ('periodEnd' in period ? period.periodEnd : undefined) ?? period.period_end ?? null;
return typeof end === 'string' ? end : null;
}
function chooseDurationPeriodId(result: TaxonomyHydrationResult) {