unrip/test-deltas.mjs
philipp fb547f24d9
All checks were successful
deploy / deploy (push) Successful in 48s
fix: scope inventory fetch by submission time window to prevent OOM
Instead of fetching up to 50k inventory snapshots (~442MB), scope the
query to only the time range of the current submission batch with a
15-minute buffer. For a typical 1-hour batch this drops from 50k rows
to ~300 rows, well within the 1280Mi pod memory limit.

The coalesced_at_idx on intent_inventory_snapshots covers the BETWEEN
clause so this remains efficient.

Proof: history-writer OOM kills from refreshQuoteOutcomes inventory fetch
Assumptions: 15min buffer covers the attribution window for all submissions
Still fake: heuristic gap outcomes may attribute trades imprecisely
2026-06-16 17:17:19 +02:00

38 lines
1.4 KiB
JavaScript

import { createPostgresPool } from './src/lib/postgres.mjs';
async function main() {
const pool = createPostgresPool({ connectionString: process.env.POSTGRES_URL });
const inventoryRows = await pool.query(`
SELECT event_id as inventory_id, observed_at, payload
FROM intent_inventory_snapshots
ORDER BY COALESCE(observed_at, ingested_at) DESC
LIMIT 2000
`);
const snapshots = inventoryRows.rows.map(r => ({
inventory_id: r.inventory_id,
observed_at: r.observed_at,
spendable: r.payload?.spendable || {},
})).sort((a,b) => new Date(a.observed_at) - new Date(b.observed_at));
const activeAssetIds = Object.keys(snapshots[0].spendable);
const deltas = [];
for (let i = 1; i < snapshots.length; i++) {
const prev = snapshots[i-1].spendable;
const curr = snapshots[i].spendable;
const delta = {};
for (const a of activeAssetIds) {
const p = BigInt(prev[a] || 0);
const c = BigInt(curr[a] || 0);
if (c - p !== 0n) delta[a] = (c - p).toString();
}
if (Object.keys(delta).length > 0) {
deltas.push({ observed_at: snapshots[i].observed_at, delta });
}
}
console.log("Total deltas:", deltas.length);
for (let i = 0; i < Math.min(5, deltas.length); i++) {
console.log(deltas[i].observed_at, Object.entries(deltas[i].delta).filter(x => x[1] !== '0'));
}
process.exit(0);
}
main().catch(console.error);