perf: avoid sort-for-single selection

This commit is contained in:
Peter Steinberger
2026-04-20 23:19:20 +01:00
parent 2d010306e4
commit f1a544ef6d
7 changed files with 156 additions and 34 deletions

View File

@@ -199,10 +199,15 @@ function resolveDreamingNextCycle(
if (!status?.phases) {
return null;
}
const nextRunAtMs = Object.values(status.phases)
.filter((phase) => phase.enabled && typeof phase.nextRunAtMs === "number")
.map((phase) => phase.nextRunAtMs as number)
.toSorted((a, b) => a - b)[0];
let nextRunAtMs: number | undefined;
for (const phase of Object.values(status.phases)) {
if (!phase.enabled || typeof phase.nextRunAtMs !== "number") {
continue;
}
if (nextRunAtMs === undefined || phase.nextRunAtMs < nextRunAtMs) {
nextRunAtMs = phase.nextRunAtMs;
}
}
return formatDreamNextCycle(nextRunAtMs);
}

View File

@@ -579,15 +579,25 @@ const buildUsageInsightStats = (
const errorRate = aggregates.messages.total
? aggregates.messages.errors / aggregates.messages.total
: 0;
const peakErrorDay = aggregates.daily
.filter((day) => day.messages > 0 && day.errors > 0)
.map((day) => ({
let peakErrorDay: UsageInsightStats["peakErrorDay"];
for (const day of aggregates.daily) {
if (day.messages <= 0 || day.errors <= 0) {
continue;
}
const candidate = {
date: day.date,
errors: day.errors,
messages: day.messages,
rate: day.errors / day.messages,
}))
.toSorted((a, b) => b.rate - a.rate || b.errors - a.errors)[0];
};
if (
!peakErrorDay ||
candidate.rate > peakErrorDay.rate ||
(candidate.rate === peakErrorDay.rate && candidate.errors > peakErrorDay.errors)
) {
peakErrorDay = candidate;
}
}
return {
durationSumMs,