forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart-transforms.ts
More file actions
141 lines (127 loc) · 4.56 KB
/
Copy pathchart-transforms.ts
File metadata and controls
141 lines (127 loc) · 4.56 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
// src/components/charts/lib/chart-transforms.ts
//
// Functions: buildActivityMatrix, computeDailyDeltas, carryForwardValues, buildTimeSeriesData, carryForwardTimeSeries, collectUnifiedTimestamps
import { localDateStr } from "@/lib/formatters"
import type { Snapshot } from "@/types/api"
import type { TrackerSnapshotSeries } from "@/types/charts"
export interface DailyBucket {
label: string
uploadDelta: number
downloadDelta: number
}
/** Compute per-day upload/download deltas (in GiB) from a sorted snapshot list. */
// INVARIANT: snapshots arrive sorted ascending by polledAt from the API
export function computeDailyDeltas(snapshots: Snapshot[]): DailyBucket[] {
if (snapshots.length < 2) return []
const bucketMap = new Map<string, { upload: number; download: number }>()
for (let i = 1; i < snapshots.length; i++) {
const prev = snapshots[i - 1]
const curr = snapshots[i]
const uploadDiff = Number(BigInt(curr.uploadedBytes) - BigInt(prev.uploadedBytes))
const downloadDiff = Number(BigInt(curr.downloadedBytes) - BigInt(prev.downloadedBytes))
const dayKey = localDateStr(new Date(curr.polledAt))
const existing = bucketMap.get(dayKey) ?? { upload: 0, download: 0 }
existing.upload += uploadDiff
existing.download += downloadDiff
bucketMap.set(dayKey, existing)
}
return Array.from(bucketMap.entries()).map(([label, { upload, download }]) => ({
label,
uploadDelta: upload / 1024 ** 3,
downloadDelta: download / 1024 ** 3,
}))
}
/**
* Build a 7x24 activity matrix from a list of Unix epoch timestamps (seconds).
* Returns the flattened [hour, day, count] data array and the maximum count.
*/
export function buildActivityMatrix(addedOnSeconds: number[]): {
data: [number, number, number][]
maxCount: number
} {
const grid = Array.from({ length: 7 }, () => Array(24).fill(0) as number[])
for (const ts of addedOnSeconds) {
const d = new Date(ts * 1000)
grid[d.getDay()][d.getHours()]++
}
let maxCount = 0
const data: [number, number, number][] = []
for (let hour = 0; hour < 24; hour++) {
for (let day = 0; day < 7; day++) {
const count = grid[day][hour]
if (count > maxCount) maxCount = count
data.push([hour, day, count])
}
}
return { data, maxCount }
}
/**
* Map a pre-built Map<timestamp, number> onto a string timestamp axis, carrying
* forward the last known value at timestamps where there is no data.
* O(T) where T = timestamps.length. Used by charts that pre-index values
* before mapping to the unified axis (e.g. SeedbonusRiverChart).
*/
export function carryForwardValues(
timestamps: string[],
valueMap: Map<string, number>,
initialValue: number | null = null
): (number | null)[] {
let lastValue = initialValue
return timestamps.map((ts) => {
const val = valueMap.get(ts)
if (val !== undefined) lastValue = val
return lastValue
})
}
/**
* Build [timestamp_ms, value][] pairs for a time-axis series.
* Skips snapshots where fieldFn returns null.
*/
export function buildTimeSeriesData(
snapshots: Snapshot[],
fieldFn: (s: Snapshot) => number | null
): [number, number][] {
const result: [number, number][] = []
for (const s of snapshots) {
const val = fieldFn(s)
if (val !== null) result.push([new Date(s.polledAt).getTime(), val])
}
return result
}
/**
* Carry-forward variant for time-axis. Returns [timestamp_ms, value][] pairs
* where gaps are filled with the last known value from each tracker's own snapshots.
* Used for stacked/summed multi-tracker charts on a time axis.
*/
export function carryForwardTimeSeries(
allTimestamps: number[],
snapshots: Snapshot[],
fieldFn: (s: Snapshot) => number | null
): [number, number][] {
const snapByTs = new Map<number, Snapshot>()
for (const snap of snapshots) {
snapByTs.set(new Date(snap.polledAt).getTime(), snap)
}
let lastValue: number | null = null
const result: [number, number][] = []
for (const ts of allTimestamps) {
const snap = snapByTs.get(ts)
if (snap) {
const raw = fieldFn(snap)
if (raw !== null) lastValue = raw
}
if (lastValue !== null) result.push([ts, lastValue])
}
return result
}
/**
* Collect the union of all polledAt timestamps across multiple tracker series,
* sorted ascending. Returns millisecond timestamps for use with time-axis charts.
*/
export function collectUnifiedTimestamps(trackerData: TrackerSnapshotSeries[]): number[] {
const set = new Set<number>()
for (const { snapshots } of trackerData) {
for (const s of snapshots) set.add(new Date(s.polledAt).getTime())
}
return [...set].sort((a, b) => a - b)
}