Simplify telemetry to three game events
This commit is contained in:
@@ -1,15 +1,24 @@
|
||||
import { db } from '../db/index.js';
|
||||
import type { RoundTelemetry } from '../../telemetry.js';
|
||||
import type { GameTelemetry } from '../../telemetry.js';
|
||||
|
||||
export function listTelemetry(): RoundTelemetry[] {
|
||||
return db().prepare(`SELECT t.server, t.round, t.players, t.received_at AS receivedAt
|
||||
FROM server_telemetry t JOIN servers s ON s.name = t.server
|
||||
WHERE s.destroyed_at IS NULL`).all() as RoundTelemetry[];
|
||||
export function listTelemetry(): GameTelemetry[] {
|
||||
const rows = db().prepare(`SELECT t.payload FROM server_telemetry t JOIN servers s ON s.name = t.server
|
||||
WHERE s.destroyed_at IS NULL AND t.payload IS NOT NULL`).all() as Array<{ payload: string }>;
|
||||
return rows.map(row => JSON.parse(row.payload) as GameTelemetry);
|
||||
}
|
||||
|
||||
export function saveTelemetry(report: RoundTelemetry): void {
|
||||
db().prepare(`INSERT INTO server_telemetry (server, round, players, received_at)
|
||||
VALUES (?, ?, ?, ?) ON CONFLICT(server) DO UPDATE SET
|
||||
round = excluded.round, players = excluded.players, received_at = excluded.received_at`)
|
||||
.run(report.server, report.round, report.players, report.receivedAt);
|
||||
export function saveTelemetry(report: GameTelemetry): void {
|
||||
db().prepare(`INSERT INTO server_telemetry (server, round, players, received_at, payload)
|
||||
VALUES (?, ?, ?, ?, ?) ON CONFLICT(server) DO UPDATE SET
|
||||
round = excluded.round, players = excluded.players, received_at = excluded.received_at, payload = excluded.payload`)
|
||||
.run(report.server, report.event === 'game_start' ? 0 : report.round, report.players.length, report.receivedAt, JSON.stringify(report));
|
||||
if (report.event === 'game_start') {
|
||||
db().prepare('UPDATE servers SET game_started_at = ?, game_ended_at = NULL WHERE name = ?').run(report.receivedAt, report.server);
|
||||
} else if (report.event === 'game_end') {
|
||||
db().prepare('UPDATE servers SET game_ended_at = ? WHERE name = ?').run(report.receivedAt, report.server);
|
||||
// Legacy betas also take match completion directly from the script.
|
||||
db().prepare(`UPDATE beta_servers SET match_seen_at = ? WHERE server = ? AND stage = 'awaiting-match'
|
||||
AND healthy_at <= ? AND beta_id IN (SELECT id FROM betas WHERE state = 'running')`)
|
||||
.run(report.receivedAt, report.server, report.receivedAt);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user