fix: use async postgres for autonomy http routes
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const path = require('path');
|
||||
const { openRuntimeDb, isPostgresEnabled } = require('../db/runtime');
|
||||
const pg = require('../db/pgAsync');
|
||||
|
||||
const intelligencePath = process.env.INTELLIGENCE_DB || path.resolve(process.cwd(), 'intelligence.sqlite');
|
||||
const db = openRuntimeDb(intelligencePath, { schema: 'intelligence', readonly: true });
|
||||
@@ -8,6 +9,19 @@ async function autonomyRoutes(fastify) {
|
||||
fastify.get('/health', async () => ({ ok: true, service: 'duriin-api' }));
|
||||
|
||||
fastify.get('/autonomy/status', async () => {
|
||||
if (isPostgresEnabled()) {
|
||||
const exists = await pg.get('intelligence', "SELECT 1 FROM information_schema.tables WHERE table_schema = $1 AND table_name = $2", ['intelligence', 'autonomy_jobs']);
|
||||
if (!exists) return { enabled: false, reason: 'autonomy schema is not initialized' };
|
||||
const [jobs, predictions, decisions, outcomes, legacy, instruments] = await Promise.all([
|
||||
pg.all('intelligence', 'SELECT lane, status, COUNT(*) AS count FROM autonomy_jobs GROUP BY lane, status ORDER BY lane, status'),
|
||||
pg.all('intelligence', 'SELECT status, COUNT(*) AS count FROM autonomy_predictions GROUP BY status ORDER BY status'),
|
||||
pg.all('intelligence', 'SELECT action, COUNT(*) AS count FROM autonomy_decisions GROUP BY action ORDER BY action'),
|
||||
pg.get('intelligence', 'SELECT COUNT(*) AS total, SUM(direction_correct) AS correct, AVG(excess_return) AS average_excess_return FROM autonomy_outcomes'),
|
||||
pg.get('intelligence', 'SELECT COUNT(*) AS count FROM autonomy_legacy_records'),
|
||||
pg.get('intelligence', 'SELECT COUNT(*) AS count FROM autonomy_instruments WHERE active=1 AND tradable=1'),
|
||||
]);
|
||||
return { enabled: true, jobs, predictions, decisions, outcomes, legacyRecords: legacy.count, allowlistedInstruments: instruments.count };
|
||||
}
|
||||
const exists = isPostgresEnabled()
|
||||
? db.prepare("SELECT 1 FROM information_schema.tables WHERE table_schema = ? AND table_name = ?").get('intelligence', 'autonomy_jobs')
|
||||
: db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='autonomy_jobs'").get();
|
||||
|
||||
Reference in New Issue
Block a user