-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.ts
More file actions
466 lines (419 loc) · 12.5 KB
/
db.ts
File metadata and controls
466 lines (419 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import { DatabaseSync } from "node:sqlite";
import { DB_PATH, DATA_DIR } from "./config.ts";
import { ensureDir } from "./util.ts";
export type SiteRow = {
id: number;
name: string;
domain: string;
enabled: number;
archived: number;
username_enc: string | null;
password_enc: string | null;
cookies_enc: string | null;
created_at: string;
updated_at: string;
archived_at: string | null;
last_run_at: string | null;
last_success_at: string | null;
last_status: string | null;
last_error: string | null;
};
export type ScriptRow = {
id: number;
site_id: number;
content: string;
created_at: string;
};
export type RunRow = {
id: number;
site_id: number;
status: string;
error: string | null;
started_at: string;
finished_at: string | null;
duration_ms: number | null;
};
export type ScreenshotRow = {
id: number;
run_id: number;
data: Uint8Array;
mime_type: string;
created_at: string;
};
export type CaptchaTraceRow = {
id: number;
run_id: number;
attempt: number;
sequence: number;
model: string;
prompt: string;
response: string | null;
error: string | null;
created_at: string;
};
export type RunEventRow = {
id: number;
run_id: number;
type: string;
payload: string;
created_at: string;
};
export type RunUptimeRow = {
site_id: number;
completed_runs_30d: number;
successful_runs_30d: number;
completed_runs_90d: number;
successful_runs_90d: number;
completed_runs_all: number;
successful_runs_all: number;
};
export const sqlite = createSqlite();
function createSqlite() {
ensureDir(DATA_DIR);
return new DatabaseSync(DB_PATH);
}
export function closeDb() {
sqlite.close();
}
export async function initDb() {
sqlite.exec("PRAGMA journal_mode=WAL;");
sqlite.exec("PRAGMA busy_timeout=5000;");
sqlite.exec(`
create table if not exists sites (
id integer primary key autoincrement,
name text not null,
domain text not null,
enabled integer not null default 1,
archived integer not null default 0,
username_enc text,
password_enc text,
cookies_enc text,
created_at text not null,
updated_at text not null,
archived_at text,
last_run_at text,
last_success_at text,
last_status text,
last_error text
);
`);
ensureSitesCookiesColumn();
ensureSitesArchiveColumns();
sqlite.exec(`
create table if not exists scripts (
id integer primary key autoincrement,
site_id integer not null,
content text not null,
created_at text not null
);
`);
sqlite.exec(`
create table if not exists runs (
id integer primary key autoincrement,
site_id integer not null,
status text not null,
error text,
started_at text not null,
finished_at text,
duration_ms integer
);
`);
sqlite.exec(`
create table if not exists screenshots (
id integer primary key autoincrement,
run_id integer not null,
data blob not null,
mime_type text not null,
created_at text not null
);
`);
sqlite.exec(`
create table if not exists captcha_traces (
id integer primary key autoincrement,
run_id integer not null,
attempt integer not null,
sequence integer not null,
model text not null,
prompt text not null,
response text,
error text,
created_at text not null
);
`);
sqlite.exec(`
create table if not exists run_events (
id integer primary key autoincrement,
run_id integer not null,
type text not null,
payload text not null,
created_at text not null
);
`);
}
export async function listSites() {
return sqlite.prepare("select * from sites order by id").all() as SiteRow[];
}
export async function getSiteByDomain(domain: string) {
return sqlite
.prepare("select * from sites where domain = ?")
.get(domain) as SiteRow | undefined;
}
export async function getSiteById(siteId: number) {
return sqlite
.prepare("select * from sites where id = ?")
.get(siteId) as SiteRow | undefined;
}
export async function getSiteIdByDomain(domain: string) {
const row = sqlite
.prepare("select id from sites where domain = ?")
.get(domain) as { id: number } | undefined;
return row?.id ?? null;
}
export async function insertSite(values: Omit<SiteRow, "id">) {
const result = sqlite
.prepare(
`insert into sites
(name, domain, enabled, archived, username_enc, password_enc, cookies_enc, created_at, updated_at, archived_at, last_run_at, last_success_at, last_status, last_error)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
)
.run(
values.name,
values.domain,
values.enabled,
values.archived,
values.username_enc,
values.password_enc,
values.cookies_enc,
values.created_at,
values.updated_at,
values.archived_at,
values.last_run_at,
values.last_success_at,
values.last_status,
values.last_error,
);
return Number(result.lastInsertRowid);
}
export async function updateSite(siteId: number, values: Partial<SiteRow>) {
const sets: string[] = [];
const params: unknown[] = [];
for (const [key, value] of Object.entries(values)) {
if (key === "id") continue;
sets.push(`${key} = ?`);
params.push(value);
}
if (!sets.length) return;
params.push(siteId);
sqlite.prepare(`update sites set ${sets.join(", ")} where id = ?`).run(...params);
}
function ensureSitesCookiesColumn() {
try {
const columns = sqlite
.prepare("pragma table_info(sites)")
.all() as Array<{ name: string }>;
if (columns.some((col) => col.name === "cookies_enc")) return;
sqlite.exec("alter table sites add column cookies_enc text;");
} catch {
// ignore
}
}
function ensureSitesArchiveColumns() {
try {
const columns = sqlite
.prepare("pragma table_info(sites)")
.all() as Array<{ name: string }>;
if (!columns.some((col) => col.name === "archived")) {
sqlite.exec("alter table sites add column archived integer not null default 0;");
}
if (!columns.some((col) => col.name === "archived_at")) {
sqlite.exec("alter table sites add column archived_at text;");
}
} catch {
// ignore
}
}
export async function getLatestScriptForSite(siteId: number) {
return sqlite
.prepare(
"select * from scripts where site_id = ? order by created_at desc limit 1",
)
.get(siteId) as ScriptRow | undefined;
}
export async function listScriptsForSite(siteId: number) {
return sqlite
.prepare("select * from scripts where site_id = ? order by created_at desc")
.all(siteId) as ScriptRow[];
}
export async function insertScript(siteId: number, content: string, createdAt: string) {
const result = sqlite
.prepare("insert into scripts (site_id, content, created_at) values (?, ?, ?)")
.run(siteId, content, createdAt);
return Number(result.lastInsertRowid);
}
export async function insertRun(values: Omit<RunRow, "id">) {
const result = sqlite
.prepare(
`insert into runs
(site_id, status, error, started_at, finished_at, duration_ms)
values (?, ?, ?, ?, ?, ?)`,
)
.run(
values.site_id,
values.status,
values.error,
values.started_at,
values.finished_at,
values.duration_ms,
);
return Number(result.lastInsertRowid);
}
export async function updateRun(runId: number, values: Partial<RunRow>) {
const sets: string[] = [];
const params: unknown[] = [];
for (const [key, value] of Object.entries(values)) {
if (key === "id") continue;
sets.push(`${key} = ?`);
params.push(value);
}
if (!sets.length) return;
params.push(runId);
sqlite.prepare(`update runs set ${sets.join(", ")} where id = ?`).run(...params);
}
export async function listRunsForSite(siteId: number, limit = 10) {
return sqlite
.prepare(
"select * from runs where site_id = ? order by started_at desc limit ?",
)
.all(siteId, limit) as RunRow[];
}
export async function listRunsBySite(siteId: number) {
return sqlite.prepare("select * from runs where site_id = ?").all(siteId) as RunRow[];
}
export async function listRunUptimeBySite(
startedAfter30d: string,
startedAfter90d: string,
) {
return sqlite
.prepare(
`select
site_id,
sum(case when status != 'running' and started_at >= ? then 1 else 0 end) as completed_runs_30d,
sum(case when status = 'success' and started_at >= ? then 1 else 0 end) as successful_runs_30d,
sum(case when status != 'running' and started_at >= ? then 1 else 0 end) as completed_runs_90d,
sum(case when status = 'success' and started_at >= ? then 1 else 0 end) as successful_runs_90d,
sum(case when status != 'running' then 1 else 0 end) as completed_runs_all,
sum(case when status = 'success' then 1 else 0 end) as successful_runs_all
from runs
group by site_id`,
)
.all(
startedAfter30d,
startedAfter30d,
startedAfter90d,
startedAfter90d,
) as RunUptimeRow[];
}
export async function getLatestRunId() {
const row = sqlite
.prepare("select id from runs order by id desc limit 1")
.get() as { id: number } | undefined;
return row?.id ?? null;
}
export async function getRunById(runId: number) {
return sqlite
.prepare("select * from runs where id = ?")
.get(runId) as RunRow | undefined;
}
export async function listScreenshotsForRuns(runIds: number[]) {
if (!runIds.length) return [];
const placeholders = runIds.map(() => "?").join(", ");
return sqlite
.prepare(
`select id, run_id, created_at from screenshots where run_id in (${placeholders}) order by created_at desc`,
)
.all(...runIds) as Array<{ id: number; run_id: number; created_at: string }>;
}
export async function getLatestScreenshotForRun(runId: number) {
return sqlite
.prepare(
"select id, created_at from screenshots where run_id = ? order by created_at desc limit 1",
)
.get(runId) as { id: number; created_at: string } | undefined;
}
export async function getScreenshotById(shotId: number) {
return sqlite
.prepare("select * from screenshots where id = ?")
.get(shotId) as ScreenshotRow | undefined;
}
export async function insertScreenshot(
runId: number,
data: Uint8Array,
mimeType: string,
createdAt: string,
) {
const result = sqlite
.prepare(
"insert into screenshots (run_id, data, mime_type, created_at) values (?, ?, ?, ?)",
)
.run(runId, data, mimeType, createdAt);
return Number(result.lastInsertRowid);
}
export async function listCaptchaTracesForRun(runId: number) {
return sqlite
.prepare("select * from captcha_traces where run_id = ? order by created_at asc")
.all(runId) as CaptchaTraceRow[];
}
export async function insertCaptchaTrace(values: Omit<CaptchaTraceRow, "id">) {
const result = sqlite
.prepare(
`insert into captcha_traces
(run_id, attempt, sequence, model, prompt, response, error, created_at)
values (?, ?, ?, ?, ?, ?, ?, ?)`,
)
.run(
values.run_id,
values.attempt,
values.sequence,
values.model,
values.prompt,
values.response,
values.error,
values.created_at,
);
return Number(result.lastInsertRowid);
}
export async function listRunEventsForRun(runId: number) {
return sqlite
.prepare("select * from run_events where run_id = ? order by created_at asc")
.all(runId) as RunEventRow[];
}
export async function insertRunEvent(values: Omit<RunEventRow, "id">) {
const result = sqlite
.prepare(
"insert into run_events (run_id, type, payload, created_at) values (?, ?, ?, ?)",
)
.run(values.run_id, values.type, values.payload, values.created_at);
return Number(result.lastInsertRowid);
}
export async function deleteScreenshotsByRunIds(runIds: number[]) {
if (!runIds.length) return;
const placeholders = runIds.map(() => "?").join(", ");
sqlite
.prepare(`delete from screenshots where run_id in (${placeholders})`)
.run(...runIds);
}
export async function deleteRunsBySiteId(siteId: number) {
sqlite.prepare("delete from runs where site_id = ?").run(siteId);
}
export async function deleteScriptsBySiteId(siteId: number) {
sqlite.prepare("delete from scripts where site_id = ?").run(siteId);
}
export async function deleteSitesById(siteId: number) {
sqlite.prepare("delete from sites where id = ?").run(siteId);
}
export async function deleteAllData() {
sqlite.exec("delete from screenshots;");
sqlite.exec("delete from runs;");
sqlite.exec("delete from scripts;");
sqlite.exec("delete from sites;");
}