codexmeter 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -0
- package/bin/codexmeter.js +57 -0
- package/dist/assets/index-DJWqyRDh.css +1 -0
- package/dist/assets/index-DZKogILW.js +113 -0
- package/dist/index.html +16 -0
- package/package.json +43 -0
- package/server/aggregator.js +428 -0
- package/server/cost-catalog.js +85 -0
- package/server/day-key.js +16 -0
- package/server/index.js +134 -0
- package/server/ingest.js +595 -0
- package/server/live-state.js +464 -0
- package/server/normalize.js +72 -0
- package/server/pricing-fetch.js +59 -0
- package/server/rollout-reader.js +108 -0
- package/server/rollout-worker-pool.js +159 -0
- package/server/rollout-worker.js +21 -0
- package/server/sqlite-reader.js +59 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import os from 'os';
|
|
2
|
+
import { Worker } from 'worker_threads';
|
|
3
|
+
|
|
4
|
+
export function createRolloutWorkerPool(opts = {}) {
|
|
5
|
+
const size = normalizePoolSize(opts.size);
|
|
6
|
+
if (size <= 1) {
|
|
7
|
+
return createInlinePool();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const workers = new Set();
|
|
11
|
+
const idleWorkers = [];
|
|
12
|
+
const inflight = new Map();
|
|
13
|
+
const queuedTasks = [];
|
|
14
|
+
let nextId = 1;
|
|
15
|
+
let closed = false;
|
|
16
|
+
|
|
17
|
+
const spawnWorker = () => {
|
|
18
|
+
const worker = new Worker(new URL('./rollout-worker.js', import.meta.url), {
|
|
19
|
+
type: 'module',
|
|
20
|
+
execArgv: [],
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
worker.on('message', (message) => {
|
|
24
|
+
const task = inflight.get(message?.id);
|
|
25
|
+
if (!task) return;
|
|
26
|
+
inflight.delete(message.id);
|
|
27
|
+
if (!closed) idleWorkers.push(worker);
|
|
28
|
+
drainQueue();
|
|
29
|
+
if (message?.ok === false) {
|
|
30
|
+
task.resolve({ ok: false, data: null, error: message?.error || 'Unknown worker error' });
|
|
31
|
+
} else {
|
|
32
|
+
task.resolve({ ok: true, data: message?.data ?? null, error: null });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
worker.on('error', (error) => {
|
|
37
|
+
failWorker(worker, error);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
worker.on('exit', (code) => {
|
|
41
|
+
if (closed) return;
|
|
42
|
+
if (code !== 0) {
|
|
43
|
+
failWorker(worker, new Error(`Worker exited with code ${code}`));
|
|
44
|
+
} else {
|
|
45
|
+
removeWorker(worker);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
workers.add(worker);
|
|
50
|
+
idleWorkers.push(worker);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
for (let i = 0; i < size; i += 1) {
|
|
54
|
+
spawnWorker();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function removeWorker(worker) {
|
|
58
|
+
workers.delete(worker);
|
|
59
|
+
const idleIndex = idleWorkers.indexOf(worker);
|
|
60
|
+
if (idleIndex >= 0) idleWorkers.splice(idleIndex, 1);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function failWorker(worker, error) {
|
|
64
|
+
removeWorker(worker);
|
|
65
|
+
|
|
66
|
+
for (const [id, task] of inflight.entries()) {
|
|
67
|
+
if (task.worker !== worker) continue;
|
|
68
|
+
inflight.delete(id);
|
|
69
|
+
task.resolve({
|
|
70
|
+
ok: false,
|
|
71
|
+
data: null,
|
|
72
|
+
error: error instanceof Error ? error.message : String(error),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!closed) {
|
|
77
|
+
spawnWorker();
|
|
78
|
+
drainQueue();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function drainQueue() {
|
|
83
|
+
while (!closed && idleWorkers.length > 0 && queuedTasks.length > 0) {
|
|
84
|
+
const worker = idleWorkers.pop();
|
|
85
|
+
const task = queuedTasks.shift();
|
|
86
|
+
const id = nextId++;
|
|
87
|
+
inflight.set(id, { worker, resolve: task.resolve });
|
|
88
|
+
worker.postMessage({
|
|
89
|
+
id,
|
|
90
|
+
rolloutPath: task.rolloutPath,
|
|
91
|
+
timezone: task.timezone,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function runTask(rolloutPath, timezone) {
|
|
97
|
+
if (closed) {
|
|
98
|
+
return Promise.resolve({ ok: false, data: null, error: 'Worker pool is closed' });
|
|
99
|
+
}
|
|
100
|
+
return new Promise((resolve) => {
|
|
101
|
+
queuedTasks.push({ rolloutPath, timezone, resolve });
|
|
102
|
+
drainQueue();
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function mapRollouts(rolloutPaths, timezone) {
|
|
107
|
+
return Promise.all(rolloutPaths.map((rolloutPath) => runTask(rolloutPath, timezone)));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function close() {
|
|
111
|
+
closed = true;
|
|
112
|
+
while (queuedTasks.length > 0) {
|
|
113
|
+
const task = queuedTasks.shift();
|
|
114
|
+
task.resolve({ ok: false, data: null, error: 'Worker pool closed before task started' });
|
|
115
|
+
}
|
|
116
|
+
for (const [, task] of inflight.entries()) {
|
|
117
|
+
task.resolve({ ok: false, data: null, error: 'Worker pool closed before task finished' });
|
|
118
|
+
}
|
|
119
|
+
inflight.clear();
|
|
120
|
+
await Promise.allSettled([...workers].map((worker) => worker.terminate()));
|
|
121
|
+
workers.clear();
|
|
122
|
+
idleWorkers.length = 0;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return { mapRollouts, close, size };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function createInlinePool() {
|
|
129
|
+
return {
|
|
130
|
+
size: 1,
|
|
131
|
+
async mapRollouts(rolloutPaths, timezone) {
|
|
132
|
+
const { enrichFromRollout } = await import('./rollout-reader.js');
|
|
133
|
+
return Promise.all(
|
|
134
|
+
rolloutPaths.map(async (rolloutPath) => {
|
|
135
|
+
try {
|
|
136
|
+
const data = await enrichFromRollout(rolloutPath, { timezone });
|
|
137
|
+
return { ok: true, data, error: null };
|
|
138
|
+
} catch (error) {
|
|
139
|
+
return {
|
|
140
|
+
ok: false,
|
|
141
|
+
data: null,
|
|
142
|
+
error: error instanceof Error ? error.message : String(error),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
);
|
|
147
|
+
},
|
|
148
|
+
async close() {},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function normalizePoolSize(size) {
|
|
153
|
+
if (size != null) {
|
|
154
|
+
return Math.max(1, Number(size) || 1);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const cpuCount = os.cpus()?.length || 4;
|
|
158
|
+
return Math.max(2, Math.min(cpuCount - 1, 8));
|
|
159
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { parentPort } from 'worker_threads';
|
|
2
|
+
import { enrichFromRollout } from './rollout-reader.js';
|
|
3
|
+
|
|
4
|
+
if (!parentPort) {
|
|
5
|
+
throw new Error('rollout-worker requires a parentPort');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
parentPort.on('message', async (message) => {
|
|
9
|
+
const { id, rolloutPath, timezone } = message || {};
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
const result = await enrichFromRollout(rolloutPath, { timezone });
|
|
13
|
+
parentPort.postMessage({ id, ok: true, data: result });
|
|
14
|
+
} catch (error) {
|
|
15
|
+
parentPort.postMessage({
|
|
16
|
+
id,
|
|
17
|
+
ok: false,
|
|
18
|
+
error: error instanceof Error ? error.message : String(error),
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
import { existsSync } from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
|
|
7
|
+
export function readThreads(codexHome, onProgress) {
|
|
8
|
+
const dbPath = path.join(codexHome, 'state_5.sqlite');
|
|
9
|
+
if (!existsSync(dbPath)) {
|
|
10
|
+
throw new Error(`SQLite database not found at ${dbPath}`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const Database = require('better-sqlite3');
|
|
14
|
+
const db = new Database(dbPath, { readonly: true });
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const count = db.prepare('SELECT count(*) as c FROM threads').get().c;
|
|
18
|
+
if (onProgress) onProgress({ total: count, read: 0 });
|
|
19
|
+
|
|
20
|
+
const stmt = db.prepare(`
|
|
21
|
+
SELECT
|
|
22
|
+
id, rollout_path, created_at, updated_at,
|
|
23
|
+
source, model_provider, cwd, title,
|
|
24
|
+
tokens_used, agent_nickname, agent_role,
|
|
25
|
+
cli_version, git_branch, git_origin_url
|
|
26
|
+
FROM threads
|
|
27
|
+
ORDER BY created_at ASC
|
|
28
|
+
`);
|
|
29
|
+
|
|
30
|
+
const threads = [];
|
|
31
|
+
let read = 0;
|
|
32
|
+
for (const row of stmt.iterate()) {
|
|
33
|
+
threads.push({
|
|
34
|
+
thread_id: row.id,
|
|
35
|
+
rollout_path: row.rollout_path,
|
|
36
|
+
created_at: row.created_at,
|
|
37
|
+
updated_at: row.updated_at,
|
|
38
|
+
source: row.source,
|
|
39
|
+
model_provider: row.model_provider,
|
|
40
|
+
cwd_raw: row.cwd,
|
|
41
|
+
title: row.title ? row.title.slice(0, 200) : '',
|
|
42
|
+
tokens_used: row.tokens_used || 0,
|
|
43
|
+
agent_nickname: row.agent_nickname || null,
|
|
44
|
+
agent_role: row.agent_role || null,
|
|
45
|
+
cli_version: row.cli_version || '',
|
|
46
|
+
git_branch: row.git_branch || null,
|
|
47
|
+
});
|
|
48
|
+
read++;
|
|
49
|
+
if (onProgress && read % 200 === 0) {
|
|
50
|
+
onProgress({ total: count, read });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (onProgress) onProgress({ total: count, read });
|
|
55
|
+
return threads;
|
|
56
|
+
} finally {
|
|
57
|
+
db.close();
|
|
58
|
+
}
|
|
59
|
+
}
|