Skip to content

Commit 0c2d6ef

Browse files
refactor: migrate scorecards and pricing to createLazyStatements
Replace manual 'let _stmts = null; function getStatements()' pattern with the shared createLazyStatements() factory from lib/lazy-statements.js. This aligns scorecards.js and pricing.js with the convention already used in analytics.js, sessions.js, leaderboard.js, and postmortem.js, reducing boilerplate and eliminating the direct getDb() import in scorecards.js (pricing.js still needs it for transactions/seeding).
1 parent 789c249 commit 0c2d6ef

2 files changed

Lines changed: 8 additions & 25 deletions

File tree

backend/routes/pricing.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const { getDb } = require("../db");
1010
const { sanitizeString } = require("../lib/validation");
1111
const { requireSessionId, wrapRoute } = require("../lib/request-helpers");
1212
const { invalidatePricingCache } = require("../lib/pricing");
13+
const { createLazyStatements } = require("../lib/lazy-statements");
1314

1415
const router = express.Router();
1516

@@ -33,13 +34,7 @@ const DEFAULT_PRICING = {
3334
};
3435

3536
// ── Cached prepared statements ──────────────────────────────────────
36-
let _pricingStmts = null;
37-
38-
function getPricingStatements() {
39-
if (_pricingStmts) return _pricingStmts;
40-
const db = getDb();
41-
42-
_pricingStmts = {
37+
const getPricingStatements = createLazyStatements((db) => ({
4338
getAll: db.prepare("SELECT * FROM model_pricing ORDER BY model ASC"),
4439
getByModel: db.prepare("SELECT * FROM model_pricing WHERE model = ?"),
4540
upsert: db.prepare(`
@@ -56,10 +51,7 @@ function getPricingStatements() {
5651
getSessionEvents: db.prepare(
5752
"SELECT event_id, event_type, model, tokens_in, tokens_out, duration_ms, timestamp FROM events WHERE session_id = ? ORDER BY timestamp ASC"
5853
),
59-
};
60-
61-
return _pricingStmts;
62-
}
54+
}));
6355

6456
/**
6557
* Ensure default pricing entries exist in the DB.

backend/routes/scorecards.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const express = require("express");
2-
const { getDb } = require("../db");
32
const { wrapRoute, parseDays, daysAgoCutoff } = require("../lib/request-helpers");
3+
const { createLazyStatements } = require("../lib/lazy-statements");
44

55
const router = express.Router();
66

@@ -32,15 +32,9 @@ function clamp(v, lo, hi) { return Math.max(lo, Math.min(hi, v)); }
3232
function round2(v) { return Math.round(v * 100) / 100; }
3333

3434
// ── Cached prepared statements for scorecards ───────────────────────
35-
// Lazily initialized once, reused across all requests to avoid
36-
// re-compiling SQL on every call.
37-
let _scorecardStmts = null;
38-
39-
function getScorecardStatements() {
40-
if (_scorecardStmts) return _scorecardStmts;
41-
const db = getDb();
42-
43-
_scorecardStmts = {
35+
// Uses createLazyStatements for consistent lazy-init pattern across
36+
// all route files (same approach as analytics.js, sessions.js, etc.).
37+
const getScorecardStatements = createLazyStatements((db) => ({
4438
agentStats: db.prepare(`
4539
SELECT
4640
agent_name,
@@ -127,10 +121,7 @@ function getScorecardStatements() {
127121
GROUP BY day
128122
ORDER BY day
129123
`),
130-
};
131-
132-
return _scorecardStmts;
133-
}
124+
}));
134125

135126
// ── GET /scorecards ─────────────────────────────────────────────────
136127
// Returns per-agent scorecards with composite score, letter grade,

0 commit comments

Comments
 (0)