Intelligence, Advanced Charting & Trading Platform

1 import { analyzeObservationSeries } from './data-proof.mjs';
2
3 function nonNegative(value, name) {
4 if (!Number.isFinite(value) || value < 0) throw new RangeError(`${name} must be non-negative and finite`);
5 }
6
7 export function buildDataProofReport({ providerCode, canonicalSymbol, observations, checkedAt, thresholds }) {
8 if (!providerCode || !canonicalSymbol) throw new TypeError('providerCode and canonicalSymbol are required');
9 if (!Array.isArray(observations) || observations.length === 0) throw new RangeError('at least one observation is required');
10 if (!(checkedAt instanceof Date) || Number.isNaN(checkedAt.valueOf())) throw new TypeError('checkedAt must be a valid Date');
11 if (!thresholds || typeof thresholds !== 'object') throw new TypeError('thresholds are required');
12 for (const key of ['maxGapMs','maxFreshnessMs','maxDuplicateCount','maxOutOfOrderCount']) nonNegative(thresholds[key], key);
13
14 const series = analyzeObservationSeries(observations);
15 const latestMs = new Date(series.lastTimestamp).valueOf();
16 const freshnessMs = checkedAt.valueOf() - latestMs;
17 if (freshnessMs < 0) throw new RangeError('checkedAt cannot be before the latest observation');
18
19 const failures = [];
20 if (series.maxGapMs !== null && series.maxGapMs > thresholds.maxGapMs) failures.push('MAX_GAP_EXCEEDED');
21 if (freshnessMs > thresholds.maxFreshnessMs) failures.push('STALE_LATEST_OBSERVATION');
22 if (series.duplicateTimestampCount > thresholds.maxDuplicateCount) failures.push('DUPLICATE_TIMESTAMPS');
23 if (series.outOfOrderCount > thresholds.maxOutOfOrderCount) failures.push('OUT_OF_ORDER_OBSERVATIONS');
24
25 return {
26 providerCode,
27 canonicalSymbol,
28 checkedAt: checkedAt.toISOString(),
29 status: failures.length === 0 ? 'PASS' : 'FAIL',
30 failures,
31 thresholds: { ...thresholds },
32 metrics: { ...series, freshnessMs },
33 };
34 }