@davoodmehraban89 / VELYQOR / commits / 5394304
Fork 0

Intelligence, Advanced Charting & Trading Platform

Phase 1: add data proof quality metrics

* test: define data proof cadence and integrity metrics * feat: implement data proof cadence and integrity metrics

davoodmehraban89 committed Oct 5, 2026 at 16:29 UTC 5394304c9d248a56e670e2fd986684c446c8b532
2 files changed +104
packages/market-domain/src/data-proof.mjs new
+59
@@ -0,0 +1,59 @@
1 +function parseObservation(observation, index) {
2 + if (!observation || typeof observation !== 'object' || Array.isArray(observation)) {
3 + throw new TypeError(`observation[${index}] must be an object`);
4 + }
5 +
6 + const timestampMs = new Date(observation.timestamp).valueOf();
7 + if (!Number.isFinite(timestampMs)) {
8 + throw new TypeError(`observation[${index}].timestamp must be valid`);
9 + }
10 +
11 + if (!Number.isFinite(observation.price) || observation.price <= 0) {
12 + throw new RangeError(`observation[${index}].price must be positive and finite`);
13 + }
14 +
15 + return { timestampMs, price: observation.price };
16 +}
17 +
18 +function median(values) {
19 + if (values.length === 0) return null;
20 + const sorted = [...values].sort((a, b) => a - b);
21 + const middle = Math.floor(sorted.length / 2);
22 + if (sorted.length % 2 === 1) return sorted[middle];
23 + return (sorted[middle - 1] + sorted[middle]) / 2;
24 +}
25 +
26 +export function analyzeObservationSeries(observations) {
27 + if (!Array.isArray(observations)) throw new TypeError('observations must be an array');
28 +
29 + const parsed = observations.map(parseObservation);
30 + let outOfOrderCount = 0;
31 + for (let i = 1; i < parsed.length; i += 1) {
32 + if (parsed[i].timestampMs < parsed[i - 1].timestampMs) outOfOrderCount += 1;
33 + }
34 +
35 + const timestampCounts = new Map();
36 + for (const item of parsed) {
37 + timestampCounts.set(item.timestampMs, (timestampCounts.get(item.timestampMs) ?? 0) + 1);
38 + }
39 +
40 + const uniqueTimestamps = [...timestampCounts.keys()].sort((a, b) => a - b);
41 + const gaps = [];
42 + for (let i = 1; i < uniqueTimestamps.length; i += 1) {
43 + gaps.push(uniqueTimestamps[i] - uniqueTimestamps[i - 1]);
44 + }
45 +
46 + const duplicateTimestampCount = [...timestampCounts.values()]
47 + .reduce((total, count) => total + Math.max(0, count - 1), 0);
48 +
49 + return {
50 + count: parsed.length,
51 + uniqueTimestampCount: uniqueTimestamps.length,
52 + duplicateTimestampCount,
53 + outOfOrderCount,
54 + maxGapMs: gaps.length ? Math.max(...gaps) : null,
55 + medianCadenceMs: median(gaps),
56 + firstTimestamp: uniqueTimestamps.length ? new Date(uniqueTimestamps[0]).toISOString() : null,
57 + lastTimestamp: uniqueTimestamps.length ? new Date(uniqueTimestamps.at(-1)).toISOString() : null,
58 + };
59 +}
packages/market-domain/test/data-proof.test.mjs new
+45
@@ -0,0 +1,45 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { analyzeObservationSeries } from '../src/data-proof.mjs';
4 +
5 +test('measures cadence, max gap and duplicate timestamps', () => {
6 + const result = analyzeObservationSeries([
7 + { timestamp: '2026-10-05T12:00:00Z', price: 100 },
8 + { timestamp: '2026-10-05T12:01:00Z', price: 101 },
9 + { timestamp: '2026-10-05T12:01:00Z', price: 101 },
10 + { timestamp: '2026-10-05T12:03:00Z', price: 102 },
11 + ]);
12 +
13 + assert.equal(result.count, 4);
14 + assert.equal(result.uniqueTimestampCount, 3);
15 + assert.equal(result.duplicateTimestampCount, 1);
16 + assert.equal(result.maxGapMs, 120_000);
17 + assert.equal(result.medianCadenceMs, 90_000);
18 +});
19 +
20 +test('reports out-of-order input without silently reclassifying it as clean', () => {
21 + const result = analyzeObservationSeries([
22 + { timestamp: '2026-10-05T12:02:00Z', price: 100 },
23 + { timestamp: '2026-10-05T12:01:00Z', price: 101 },
24 + { timestamp: '2026-10-05T12:03:00Z', price: 102 },
25 + ]);
26 + assert.equal(result.outOfOrderCount, 1);
27 +});
28 +
29 +test('rejects invalid timestamps and prices', () => {
30 + assert.throws(() => analyzeObservationSeries([
31 + { timestamp: 'bad', price: 100 },
32 + ]), /timestamp/i);
33 +
34 + assert.throws(() => analyzeObservationSeries([
35 + { timestamp: '2026-10-05T12:00:00Z', price: 0 },
36 + ]), /price/i);
37 +});
38 +
39 +test('returns null cadence metrics when fewer than two unique timestamps exist', () => {
40 + const result = analyzeObservationSeries([
41 + { timestamp: '2026-10-05T12:00:00Z', price: 100 },
42 + ]);
43 + assert.equal(result.maxGapMs, null);
44 + assert.equal(result.medianCadenceMs, null);
45 +});