Fixes responsiveness on the charts

This commit is contained in:
litoral05
2026-06-01 12:23:37 +01:00
parent 6e44522782
commit 99924ac1df
4 changed files with 101 additions and 119 deletions
@@ -130,59 +130,4 @@ function rangeToMs(range: WorkspaceChartTimeRange) {
case "30d":
return 30 * 24 * 60 * 60 * 1000;
}
}
function intervalToMs(interval: WorkspaceChartInterval) {
switch (interval) {
case "1m":
return 60 * 1000;
case "5m":
return 5 * 60 * 1000;
case "15m":
return 15 * 60 * 1000;
case "1h":
return 60 * 60 * 1000;
}
}
function aggregatePoints(
points: WorkspaceChartPoint[],
interval: WorkspaceChartInterval,
): WorkspaceChartPoint[] {
const bucketMs = intervalToMs(interval);
if (bucketMs === 0) {
return points;
}
const buckets = new Map<number, number[]>();
for (const point of points) {
const time = new Date(point.timestamp).getTime();
if (!Number.isFinite(time)) {
continue;
}
const bucketTime = Math.floor(time / bucketMs) * bucketMs;
const values = buckets.get(bucketTime) ?? [];
if (point.value !== null && Number.isFinite(point.value)) {
values.push(point.value);
}
buckets.set(bucketTime, values);
}
return Array.from(buckets.entries())
.sort(([a], [b]) => a - b)
.map(([bucketTime, values]) => ({
timestamp: new Date(bucketTime).toISOString(),
value: average(values),
}));
}
function average(values: number[]) {
return values.reduce((sum, value) => sum + value, 0) / values.length;
}