Intelligence, Advanced Charting & Trading Platform

1 const BASE = Object.freeze({ GOOD: 100, DEGRADED: 80, STALE: 30, INVALID: 0 });
2
3 export function qualityScore({ state, flags = [], ageRatio = 0 }) {
4 if (!(state in BASE)) throw new RangeError(`unsupported quality state: ${state}`);
5 if (!Array.isArray(flags)) throw new TypeError('flags must be an array');
6 if (!Number.isFinite(ageRatio) || ageRatio < 0) throw new RangeError('ageRatio must be non-negative and finite');
7 if (state === 'INVALID') return 0;
8
9 let score = BASE[state];
10 if (flags.includes('PROVIDER_DIVERGENCE')) score -= 15;
11 if (flags.includes('STALE_SOURCE')) score -= 10;
12 if (ageRatio >= 0.75 && ageRatio <= 1) score -= 10;
13 if (ageRatio > 1) score -= Math.min(20, Math.floor(ageRatio - 1) * 5);
14
15 if (state === 'STALE') score = Math.min(score, 30);
16 return Math.max(0, Math.min(100, score));
17 }