Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

fix(site): round network monitor res_avg and loss to two decimals

henrygdb3feff9a2827
diff --git a/internal/site/src/lib/network-monitor-utils.ts b/internal/site/src/lib/network-monitor-utils.ts
index 4f4e49f6c..dcbf2dffc 100644
--- a/internal/site/src/lib/network-monitor-utils.ts
+++ b/internal/site/src/lib/network-monitor-utils.ts
@@ -1,12 +1,16 @@
import type { MonitorCertInfo, MonitorStats, NetworkMonitorRecord, RawMonitorStatsRecord } from "@/types"
+import { toFixedFloat } from "./utils"
/** Derive chart metrics from the counts and response sum stored at every retention tier. */
export function getMonitorStats(record: RawMonitorStatsRecord): MonitorStats {
return {
- res_avg: record.success_count > 0 ? record.res_sum / record.success_count : 0,
+ res_avg: record.success_count > 0 ? toFixedFloat(record.res_sum / record.success_count, 2) : 0,
res_min: record.res_min,
res_max: record.res_max,
- loss: record.total_count > 0 ? ((record.total_count - record.success_count) / record.total_count) * 100 : 0,
+ loss:
+ record.total_count > 0
+ ? toFixedFloat(((record.total_count - record.success_count) / record.total_count) * 100, 2)
+ : 0,
}
}
diff --git a/internal/site/src/lib/utils.ts b/internal/site/src/lib/utils.ts
index f9050c4c5..4860c7060 100644
--- a/internal/site/src/lib/utils.ts
+++ b/internal/site/src/lib/utils.ts
@@ -111,18 +111,17 @@ export const updateFavicon = (() => {
</linearGradient>
</defs>
<path fill="url(#gradient)" d="M35 70H0V0h35q4.4 0 8.2 1.7a21.4 21.4 0 0 1 6.6 4.5q2.9 2.8 4.5 6.6Q56 16.7 56 21a15.4 15.4 0 0 1-.3 3.2 17.6 17.6 0 0 1-.2.8 19.4 19.4 0 0 1-1.5 4 17 17 0 0 1-2.4 3.4 13.5 13.5 0 0 1-2.6 2.3 12.5 12.5 0 0 1-.4.3q1.7 1 3 2.5Q53 39.1 54 41a18.3 18.3 0 0 1 1.5 4 17.4 17.4 0 0 1 .5 3 15.3 15.3 0 0 1 0 1q0 4.4-1.7 8.2a21.4 21.4 0 0 1-4.5 6.6q-2.8 2.9-6.6 4.6Q39.4 70 35 70ZM14 14v14h21a7 7 0 0 0 2.3-.3 6.6 6.6 0 0 0 .4-.2Q39 27 40 26a6.9 6.9 0 0 0 1.5-2.2q.5-1.3.5-2.8a7 7 0 0 0-.4-2.3 6.6 6.6 0 0 0-.1-.4Q40.9 17 40 16a7 7 0 0 0-2.3-1.4 6.9 6.9 0 0 0-2.5-.6 7.9 7.9 0 0 0-.2 0H14Zm0 28v14h21a7 7 0 0 0 2.3-.4 6.6 6.6 0 0 0 .4-.1Q39 54.9 40 54a7 7 0 0 0 1.5-2.2 6.9 6.9 0 0 0 .5-2.6 7.9 7.9 0 0 0 0-.2 7 7 0 0 0-.4-2.3 6.6 6.6 0 0 0-.1-.4Q40.9 45 40 44a7 7 0 0 0-2.3-1.5 6.9 6.9 0 0 0-2.5-.6 7.9 7.9 0 0 0-.2 0H14Z"/>
- ${
- downCount > 0 &&
- `
+ ${downCount > 0 &&
+ `
<circle cx="40" cy="50" r="22" fill="#f00"/>
<text x="40" y="60" font-size="34" text-anchor="middle" fill="#fff" font-family="Arial" font-weight="bold">${downCount}</text>
`
- }
+ }
</svg>
`
const blob = new Blob([svg], { type: "image/svg+xml" })
const url = URL.createObjectURL(blob)
- ;(document.querySelector("link[rel='icon']") as HTMLLinkElement).href = url
+ ; (document.querySelector("link[rel='icon']") as HTMLLinkElement).href = url
}
})()
diff --git a/internal/site/tests/network-monitor-utils.test.ts b/internal/site/tests/network-monitor-utils.test.ts
index 80aeec4fa..f58dd38eb 100644
--- a/internal/site/tests/network-monitor-utils.test.ts
+++ b/internal/site/tests/network-monitor-utils.test.ts
@@ -1,5 +1,11 @@
-import { describe, expect, test } from "bun:test"
-import { getMonitorStats } from "../src/lib/network-monitor-utils"
+import { describe, expect, mock, test } from "bun:test"
+
+mock.module("@lingui/core/macro", () => ({
+ t: (strings: string | TemplateStringsArray) => (typeof strings === "string" ? strings : (strings?.[0] ?? "")),
+ plural: (_count: number, forms: { other?: string }) => forms.other ?? "",
+}))
+
+const { getMonitorStats } = await import("../src/lib/network-monitor-utils")
describe("monitor stats derived from stored counts", () => {
test("retains probe weights and response precision", () => {
@@ -12,12 +18,26 @@ describe("monitor stats derived from stored counts", () => {
success_count: 6,
res_sum: 61,
})
- expect(stats.res_avg).toBeCloseTo(10.1666667)
- expect(stats.loss).toBeCloseTo(14.2857143)
+ expect(stats.res_avg).toBe(10.17)
+ expect(stats.loss).toBe(14.29)
expect(stats.res_min).toBe(5)
expect(stats.res_max).toBe(20)
})
+ test("limits average response and loss to two decimals", () => {
+ const stats = getMonitorStats({
+ monitor: "monitor1",
+ created: 1000,
+ res_min: 3,
+ res_max: 4,
+ total_count: 9,
+ success_count: 3,
+ res_sum: 10,
+ })
+ expect(stats.res_avg).toBe(3.33)
+ expect(stats.loss).toBe(66.67)
+ })
+
test.each([
{ total_count: 3, success_count: 0, loss: 100 },
{ total_count: 0, success_count: 0, loss: 0 },