-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtoday.ts
More file actions
459 lines (396 loc) · 16.3 KB
/
today.ts
File metadata and controls
459 lines (396 loc) · 16.3 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
// src/lib/today.ts
//
// Functions: computeTodayAtAGlance, backfillTrackerCheckpoints
import "server-only"
import { eq, gte, inArray, sql } from "drizzle-orm"
import { compareBigIntDesc, isUnixTimestampOnDate } from "@/lib/data-transforms"
import { db } from "@/lib/db"
import {
downloadClients,
torrentDailyCheckpoints,
trackerDailyCheckpoints,
trackerSnapshots,
trackers,
} from "@/lib/db/schema"
import { parseCachedTorrents } from "@/lib/download-clients"
import { parseTorrentTags } from "@/lib/fleet"
import { localDateStr } from "@/lib/formatters"
import { log } from "@/lib/logger"
import type { TodayAtAGlance } from "@/types/api"
interface TrackerDelta {
id: number
name: string
color: string | null
uploadDelta: bigint
downloadDelta: bigint
bufferDelta: bigint
ratioChange: number
seedbonusChange: number
}
function zeroDelta(tracker: { id: number; name: string; color: string | null }): TrackerDelta {
return {
id: tracker.id,
name: tracker.name,
color: tracker.color ?? null,
uploadDelta: 0n,
downloadDelta: 0n,
bufferDelta: 0n,
ratioChange: 0,
seedbonusChange: 0,
}
}
interface TorrentMover {
hash: string
name: string
qbtTag: string | null
trackerColor: string | null
clientName: string | null
uploadedToday: bigint
downloadedToday: bigint
}
/**
* Computes the entire TodayAtAGlance payload in a single call.
* Fetches all required data in parallel where possible, then aggregates
* fleet-level deltas, yesterday comparisons, torrent movers, and activity counts.
*/
export async function computeTodayAtAGlance(): Promise<TodayAtAGlance> {
// ── Date boundaries (local timezone) ──────────────────────────────────────
const now = Date.now()
const todayStr = localDateStr()
// Start of today in local timezone for snapshot filtering
const todayStart = new Date(`${todayStr}T00:00:00`)
const yesterdayStr = localDateStr(now - 86400000)
const dayBeforeStr = localDateStr(now - 172800000)
// ── Parallel data fetches ─────────────────────────────────────────────────
const [allTrackers, todaySnapshots, checkpointRows, torrentCps, clients] = await Promise.all([
db
.select({
id: trackers.id,
name: trackers.name,
color: trackers.color,
qbtTag: trackers.qbtTag,
})
.from(trackers)
.where(eq(trackers.isActive, true)),
db
.select({
trackerId: trackerSnapshots.trackerId,
polledAt: trackerSnapshots.polledAt,
uploadedBytes: trackerSnapshots.uploadedBytes,
downloadedBytes: trackerSnapshots.downloadedBytes,
bufferBytes: trackerSnapshots.bufferBytes,
ratio: trackerSnapshots.ratio,
seedbonus: trackerSnapshots.seedbonus,
})
.from(trackerSnapshots)
.where(gte(trackerSnapshots.polledAt, todayStart))
.orderBy(trackerSnapshots.polledAt),
db
.select({
trackerId: trackerDailyCheckpoints.trackerId,
checkpointDate: trackerDailyCheckpoints.checkpointDate,
uploadedBytesEnd: trackerDailyCheckpoints.uploadedBytesEnd,
downloadedBytesEnd: trackerDailyCheckpoints.downloadedBytesEnd,
bufferBytesEnd: trackerDailyCheckpoints.bufferBytesEnd,
})
.from(trackerDailyCheckpoints)
.where(inArray(trackerDailyCheckpoints.checkpointDate, [yesterdayStr, dayBeforeStr])),
db
.select({
clientId: torrentDailyCheckpoints.clientId,
hash: torrentDailyCheckpoints.hash,
uploadedStart: torrentDailyCheckpoints.uploadedStart,
downloadedStart: torrentDailyCheckpoints.downloadedStart,
})
.from(torrentDailyCheckpoints)
.where(eq(torrentDailyCheckpoints.checkpointDate, todayStr)),
db
.select({
id: downloadClients.id,
name: downloadClients.name,
cachedTorrents: downloadClients.cachedTorrents,
cachedTorrentsAt: downloadClients.cachedTorrentsAt,
})
.from(downloadClients)
.where(eq(downloadClients.enabled, true)),
])
const yesterdayCheckpoints = checkpointRows.filter((r) => r.checkpointDate === yesterdayStr)
const dayBeforeCheckpoints = checkpointRows.filter((r) => r.checkpointDate === dayBeforeStr)
// ── Per-tracker deltas from today's snapshots ─────────────────────────────
// Group snapshots by trackerId
const snapshotsByTracker = new Map<number, (typeof todaySnapshots)[number][]>()
for (const snap of todaySnapshots) {
const bucket = snapshotsByTracker.get(snap.trackerId) ?? []
bucket.push(snap)
snapshotsByTracker.set(snap.trackerId, bucket)
}
const trackerDeltas: TrackerDelta[] = []
for (const tracker of allTrackers) {
const snaps = snapshotsByTracker.get(tracker.id)
if (!snaps || snaps.length < 2) {
trackerDeltas.push(zeroDelta(tracker))
continue
}
const earliest = snaps[0]
const latest = snaps[snaps.length - 1]
try {
const uploadDelta = BigInt(latest.uploadedBytes) - BigInt(earliest.uploadedBytes)
const downloadDelta = BigInt(latest.downloadedBytes) - BigInt(earliest.downloadedBytes)
const latestBuffer = latest.bufferBytes ?? 0n
const earliestBuffer = earliest.bufferBytes ?? 0n
const bufferDelta = BigInt(latestBuffer) - BigInt(earliestBuffer)
const ratioChange = (latest.ratio ?? 0) - (earliest.ratio ?? 0)
const seedbonusChange = (latest.seedbonus ?? 0) - (earliest.seedbonus ?? 0)
trackerDeltas.push({
id: tracker.id,
name: tracker.name,
color: tracker.color ?? null,
uploadDelta,
downloadDelta,
bufferDelta,
ratioChange,
seedbonusChange,
})
} catch (err) {
log.warn(err, "BigInt conversion failed for tracker %d", tracker.id)
trackerDeltas.push(zeroDelta(tracker))
}
}
// ── Fleet aggregation ──────────────────────────────────────────────────────
let fleetUploadDelta = 0n
let fleetDownloadDelta = 0n
let fleetBufferDelta = 0n
let fleetSeedbonusChange = 0
// Weighted average of ratio change: sum(ratioChange * uploadDelta) / sum(uploadDelta)
let weightedRatioSum = 0
let totalUploadWeight = 0n
for (const delta of trackerDeltas) {
fleetUploadDelta += delta.uploadDelta
fleetDownloadDelta += delta.downloadDelta
fleetBufferDelta += delta.bufferDelta
fleetSeedbonusChange += delta.seedbonusChange
if (delta.uploadDelta > 0n) {
// NOTE: Number(delta.uploadDelta) can lose precision when uploadDelta
// exceeds Number.MAX_SAFE_INTEGER (~8 PiB). For realistic tracker upload
// volumes this is unlikely, but a full bigint weighted-average refactor
// would eliminate this limitation if ever needed.
const weight = Number(delta.uploadDelta)
weightedRatioSum += delta.ratioChange * weight
totalUploadWeight += delta.uploadDelta
}
}
const fleetRatioChange =
totalUploadWeight > 0n ? weightedRatioSum / Number(totalUploadWeight) : null
// ── Yesterday comparison from checkpoint tables ────────────────────────────
const yesterdayByTracker = new Map(yesterdayCheckpoints.map((cp) => [cp.trackerId, cp]))
const dayBeforeByTracker = new Map(dayBeforeCheckpoints.map((cp) => [cp.trackerId, cp]))
let yesterdayFleetUpload: bigint | null = null
let yesterdayFleetDownload: bigint | null = null
let yesterdayFleetBuffer: bigint | null = null
for (const tracker of allTrackers) {
const yesterday = yesterdayByTracker.get(tracker.id)
const dayBefore = dayBeforeByTracker.get(tracker.id)
if (!yesterday || !dayBefore) continue
try {
const trackerUploadYesterday =
BigInt(yesterday.uploadedBytesEnd) - BigInt(dayBefore.uploadedBytesEnd)
const trackerDownloadYesterday =
BigInt(yesterday.downloadedBytesEnd) - BigInt(dayBefore.downloadedBytesEnd)
const yesterdayBuffer = yesterday.bufferBytesEnd ?? 0n
const dayBeforeBuffer = dayBefore.bufferBytesEnd ?? 0n
const trackerBufferYesterday = BigInt(yesterdayBuffer) - BigInt(dayBeforeBuffer)
yesterdayFleetUpload = (yesterdayFleetUpload ?? 0n) + trackerUploadYesterday
yesterdayFleetDownload = (yesterdayFleetDownload ?? 0n) + trackerDownloadYesterday
yesterdayFleetBuffer = (yesterdayFleetBuffer ?? 0n) + trackerBufferYesterday
} catch (err) {
log.warn(err, "BigInt conversion failed for yesterday comparison, tracker %d", tracker.id)
}
}
// ── Torrent movers ─────────────────────────────────────────────────────────
// Build checkpoint lookup
const cpByKey = new Map(torrentCps.map((cp) => [`${cp.clientId}:${cp.hash}`, cp]))
// Build tracker tag → color lookup for matching torrent tags
const trackerTagToColor = new Map<string, string | null>()
for (const tracker of allTrackers) {
if (tracker.qbtTag) {
trackerTagToColor.set(tracker.qbtTag.toLowerCase(), tracker.color ?? null)
}
}
const movers: TorrentMover[] = []
let addedToday = 0
let completedToday = 0
for (const client of clients) {
const torrents = parseCachedTorrents(client.cachedTorrents)
for (const torrent of torrents) {
// Activity counts. addedAt and completedAt are unix timestamps (seconds)
if (isUnixTimestampOnDate(torrent.addedAt, todayStr)) {
addedToday++
}
if (torrent.completedAt !== -1 && isUnixTimestampOnDate(torrent.completedAt, todayStr)) {
completedToday++
}
// Compare current uploaded/downloaded to today's checkpoint
const key = `${client.id}:${torrent.hash}`
const checkpoint = cpByKey.get(key)
if (!checkpoint) continue
let uploadedToday: bigint
let downloadedToday: bigint
try {
uploadedToday = BigInt(torrent.uploaded) - BigInt(checkpoint.uploadedStart)
downloadedToday = BigInt(torrent.downloaded) - BigInt(checkpoint.downloadedStart)
} catch (err) {
log.warn(err, "BigInt conversion failed for torrent %s", torrent.hash)
continue
}
// Match first qbtTag found in the torrent's comma-separated tags field
let matchedTag: string | null = null
let matchedColor: string | null = null
if (torrent.tags) {
const torrentTags = parseTorrentTags(torrent.tags)
for (const tag of torrentTags) {
const tagLower = tag.toLowerCase()
if (trackerTagToColor.has(tagLower)) {
matchedTag = tag
matchedColor = trackerTagToColor.get(tagLower) ?? null
break
}
}
}
// Only include torrents that match a tracked tracker
if (!matchedTag) continue
movers.push({
hash: torrent.hash,
name: torrent.name,
qbtTag: matchedTag,
trackerColor: matchedColor,
clientName: client.name,
uploadedToday,
downloadedToday,
})
}
}
// Sort and take top 5 uploaders and downloaders
const topUploaders = movers
.filter((m) => m.uploadedToday > 0n)
.sort((a, b) => compareBigIntDesc(a.uploadedToday, b.uploadedToday))
.slice(0, 5)
const topDownloaders = movers
.filter((m) => m.downloadedToday > 0n)
.sort((a, b) => compareBigIntDesc(a.downloadedToday, b.downloadedToday))
.slice(0, 5)
// ── Assemble and return ───────────────────────────────────────────────────
const latestClientPoll = clients.reduce<Date | null>((latest, c) => {
if (c.cachedTorrentsAt && (!latest || c.cachedTorrentsAt > latest)) return c.cachedTorrentsAt
return latest
}, null)
return {
fleet: {
uploadDelta: fleetUploadDelta.toString(),
downloadDelta: fleetDownloadDelta.toString(),
bufferDelta: fleetBufferDelta.toString(),
ratioChange: fleetRatioChange,
seedbonusChange: fleetSeedbonusChange,
uploadDeltaYesterday: yesterdayFleetUpload !== null ? yesterdayFleetUpload.toString() : null,
downloadDeltaYesterday:
yesterdayFleetDownload !== null ? yesterdayFleetDownload.toString() : null,
bufferDeltaYesterday: yesterdayFleetBuffer !== null ? yesterdayFleetBuffer.toString() : null,
},
trackers: trackerDeltas.map((d) => ({
id: d.id,
name: d.name,
color: d.color,
uploadDelta: d.uploadDelta.toString(),
downloadDelta: d.downloadDelta.toString(),
bufferDelta: d.bufferDelta.toString(),
})),
activity: {
addedToday,
completedToday,
},
movers: {
topUploaders: topUploaders.map((t) => ({
hash: t.hash,
name: t.name,
qbtTag: t.qbtTag,
trackerColor: t.trackerColor,
clientName: t.clientName,
uploadedToday: t.uploadedToday.toString(),
})),
topDownloaders: topDownloaders.map((t) => ({
hash: t.hash,
name: t.name,
qbtTag: t.qbtTag,
trackerColor: t.trackerColor,
clientName: t.clientName,
downloadedToday: t.downloadedToday.toString(),
})),
},
trackerLastUpdated:
todaySnapshots.length > 0
? todaySnapshots[todaySnapshots.length - 1].polledAt.toISOString()
: null,
clientLastUpdated: latestClientPoll?.toISOString() ?? null,
}
}
// ── Backfill ───────────────────────────────────────────────────
/**
* One-time backfill: populates trackerDailyCheckpoints from existing trackerSnapshots.
* Should be called once when the checkpoint table is empty but snapshot data exists.
*/
export async function backfillTrackerCheckpoints(): Promise<number> {
// Check if backfill is needed
const [existing] = await db
.select({ id: trackerDailyCheckpoints.id })
.from(trackerDailyCheckpoints)
.limit(1)
if (existing) return 0 // Already has data, skip
// Check if there are snapshots to backfill from
const [hasSnapshots] = await db
.select({ id: trackerSnapshots.id })
.from(trackerSnapshots)
.limit(1)
if (!hasSnapshots) return 0 // No snapshots, nothing to backfill
const polledDate = sql<string>`DATE(${trackerSnapshots.polledAt})`
const rows = await db
.selectDistinctOn([trackerSnapshots.trackerId, polledDate], {
trackerId: trackerSnapshots.trackerId,
checkpointDate: polledDate.as("checkpoint_date"),
uploadedBytesEnd: trackerSnapshots.uploadedBytes,
downloadedBytesEnd: trackerSnapshots.downloadedBytes,
bufferBytesEnd: trackerSnapshots.bufferBytes,
ratioEnd: trackerSnapshots.ratio,
seedbonusEnd: trackerSnapshots.seedbonus,
})
.from(trackerSnapshots)
.orderBy(trackerSnapshots.trackerId, polledDate, sql`${trackerSnapshots.polledAt} DESC`)
if (rows.length === 0) return 0
const CHUNK_SIZE = 500
let inserted = 0
for (let i = 0; i < rows.length; i += CHUNK_SIZE) {
const chunk = rows.slice(i, i + CHUNK_SIZE)
await db
.insert(trackerDailyCheckpoints)
.values(
chunk.map((row) => ({
trackerId: row.trackerId,
checkpointDate: row.checkpointDate,
uploadedBytesEnd: row.uploadedBytesEnd,
downloadedBytesEnd: row.downloadedBytesEnd,
bufferBytesEnd: row.bufferBytesEnd,
ratioEnd: row.ratioEnd != null ? Number(row.ratioEnd) : null,
seedbonusEnd: row.seedbonusEnd != null ? Number(row.seedbonusEnd) : null,
// snapshotCount is hard-coded to 1 because backfill selects only the
// last snapshot per day — the actual count is not available without a
// separate COUNT query per (trackerId, date) pair. Acceptable for backfill.
snapshotCount: 1,
}))
)
.onConflictDoNothing()
// NOTE: inserted tracks chunk.length rather than actual DB rows written.
// onConflictDoNothing() silently skips duplicate rows, so this count may
// overstate the number of rows inserted. The return value is informational
// only (used for a log.info call) and does not affect correctness.
inserted += chunk.length
}
return inserted
}