38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
import pool from '@/lib/db';
|
|
import type { SessionBoundary } from '@/lib/types';
|
|
|
|
/**
|
|
* Returns session-start timestamps where any gap > 30 min exists
|
|
* in the global tick_timing timeline.
|
|
*/
|
|
export async function getSessionBoundaries(
|
|
from: Date,
|
|
to: Date,
|
|
): Promise<SessionBoundary[]> {
|
|
const result = await pool.query<{ real_time: Date; game_tick: string }>(
|
|
`SELECT real_time, game_tick
|
|
FROM tick_timing
|
|
WHERE real_time BETWEEN $1 AND $2
|
|
ORDER BY real_time ASC`,
|
|
[from, to],
|
|
);
|
|
|
|
const rows = result.rows;
|
|
if (rows.length === 0) return [];
|
|
|
|
const boundaries: SessionBoundary[] = [{
|
|
real_time: rows[0].real_time.toISOString(),
|
|
game_tick: parseInt(rows[0].game_tick, 10),
|
|
}];
|
|
|
|
for (let i = 1; i < rows.length; i++) {
|
|
if (rows[i].real_time.getTime() - rows[i - 1].real_time.getTime() > 30 * 60 * 1000) {
|
|
boundaries.push({
|
|
real_time: rows[i].real_time.toISOString(),
|
|
game_tick: parseInt(rows[i].game_tick, 10),
|
|
});
|
|
}
|
|
}
|
|
|
|
return boundaries;
|
|
} |