Skip to content

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

fix(ui): sort monitor targets without URL scheme, show HTTPS in badge (#2501)

Sven van Ginkel032c03b0f7c6
diff --git a/internal/site/src/components/network-monitors-table/monitor-dialog.tsx b/internal/site/src/components/network-monitors-table/monitor-dialog.tsx
index 6a0b91166..07eeac337 100644
--- a/internal/site/src/components/network-monitors-table/monitor-dialog.tsx
+++ b/internal/site/src/components/network-monitors-table/monitor-dialog.tsx
@@ -28,7 +28,7 @@ import { ChevronDownIcon, GlobeIcon, ListIcon, type LucideIcon, PlusIcon, Search
import { useToast } from "@/components/ui/use-toast"
import { $systems } from "@/lib/stores"
import { cn, supportsNetworkMonitors } from "@/lib/utils"
-import { getMonitorTarget } from "@/lib/network-monitor-utils"
+import { getMonitorTarget, isHttpsTarget } from "@/lib/network-monitor-utils"
import type { NetworkMonitorRecord } from "@/types"
import * as v from "valibot"
@@ -126,10 +126,6 @@ const BulkMonitorSchema = v.object({
server: v.optional(v.pipe(v.string(), v.trim())),
})
-function isHttpsTarget(target: string) {
- return /^https:\/\//i.test(target)
-}
-
function normalizeHttpTarget(target: string, port = 0) {
const useExplicitPort = port > 0 && port !== 80 && port !== 443
const hasOriginOnlyTarget = /^https?:\/\/[^/?#]+$/i.test(target)
@@ -281,8 +277,18 @@ export function MonitorMultiSelect({
canSelectMore?: boolean
}) {
const { t } = useLingui()
+ const labelCounts = new Map<string, number>()
+ for (const monitor of monitors) {
+ const label = getMonitorTarget(monitor)
+ labelCounts.set(label, (labelCounts.get(label) ?? 0) + 1)
+ }
const options = monitors
- .map((monitor) => ({ id: monitor.id, label: getMonitorTarget(monitor), server: monitor.server }))
+ .map((monitor) => {
+ let label = getMonitorTarget(monitor)
+ // Show the URL scheme when it's all that tells http and https monitors apart.
+ if (monitor.protocol === "http" && (labelCounts.get(label) ?? 0) > 1) label = monitor.target
+ return { id: monitor.id, label, server: monitor.server }
+ })
.sort((a, b) => a.label.localeCompare(b.label))
return (
<MultiSelect
diff --git a/internal/site/src/components/network-monitors-table/network-monitors-columns.tsx b/internal/site/src/components/network-monitors-table/network-monitors-columns.tsx
index e8b3e26e8..ece7ea62e 100644
--- a/internal/site/src/components/network-monitors-table/network-monitors-columns.tsx
+++ b/internal/site/src/components/network-monitors-table/network-monitors-columns.tsx
@@ -36,7 +36,12 @@ import { Checkbox } from "@/components/ui/checkbox"
import { useMemo } from "react"
import { formatBulkMonitorLine } from "@/components/network-monitors-table/monitor-dialog"
import { Badge } from "../ui/badge"
-import { getCertDaysLeft, getCertExpiryLevel, getMonitorTarget } from "@/lib/network-monitor-utils"
+import {
+ getCertDaysLeft,
+ getCertExpiryLevel,
+ getMonitorProtocolLabel,
+ getMonitorTarget,
+} from "@/lib/network-monitor-utils"
const certExpiryDotColors = { ok: "bg-green-500", warning: "bg-yellow-500", critical: "bg-red-500" }
@@ -49,7 +54,8 @@ declare module "@tanstack/react-table" {
const protocolColors: Record<string, string> = {
icmp: "bg-blue-500/15! text-blue-600 dark:text-blue-400",
tcp: "bg-purple-500/15! text-purple-600 dark:text-purple-400",
- http: "bg-green-500/15! text-green-700 dark:text-green-400",
+ http: "bg-teal-500/15! text-teal-700 dark:text-teal-400",
+ https: "bg-green-500/15! text-green-700 dark:text-green-400",
dns: "bg-amber-500/15! text-amber-600 dark:text-amber-400",
// HTTP monitors that skip TLS certificate verification
insecure: "bg-orange-500/15! text-orange-600 dark:text-orange-400",
@@ -145,7 +151,8 @@ export function getMonitorColumns(
{
id: "target",
meta: { label: t`Target` },
- sortingFn: (a, b) => a.original.target.localeCompare(b.original.target),
+ sortingFn: (a, b) =>
+ (a.getValue("target") as string).localeCompare(b.getValue("target") as string, undefined, { numeric: true }),
accessorFn: (record) => getMonitorTarget(record),
header: ({ column }) => <HeaderButton column={column} name={t`Target`} Icon={GlobeIcon} />,
cell: ({ row, getValue }) => {
@@ -176,7 +183,7 @@ export function getMonitorColumns(
{
id: "protocol",
meta: { label: t`Protocol` },
- accessorFn: (record) => record.protocol,
+ accessorFn: (record) => getMonitorProtocolLabel(record),
header: ({ column }) => <HeaderButton column={column} name={t`Protocol`} Icon={ArrowLeftRightIcon} />,
cell: ({ row, getValue }) => {
const protocol = getValue() as string
diff --git a/internal/site/src/components/network-monitors-table/network-monitors-table.tsx b/internal/site/src/components/network-monitors-table/network-monitors-table.tsx
index 9162fb3bb..e6729f240 100644
--- a/internal/site/src/components/network-monitors-table/network-monitors-table.tsx
+++ b/internal/site/src/components/network-monitors-table/network-monitors-table.tsx
@@ -1,4 +1,9 @@
-import { getCertDaysLeft, getCertExpiryLevel, getMonitorTarget } from "@/lib/network-monitor-utils"
+import {
+ getCertDaysLeft,
+ getCertExpiryLevel,
+ getMonitorProtocolLabel,
+ getMonitorTarget,
+} from "@/lib/network-monitor-utils"
import { t } from "@lingui/core/macro"
import { Trans } from "@lingui/react/macro"
import {
@@ -304,7 +309,9 @@ export default function NetworkMonitorsTableNew({
if (!value) return true
const monitor = row.original
const systemName = $allSystemsById.get()[monitor.system]?.name ?? ""
- const searchString = `${getMonitorTarget(monitor)}${monitor.protocol}${systemName}`.toLocaleLowerCase()
+ // Include the raw target so searches for the hidden URL scheme still match.
+ const searchString =
+ `${getMonitorTarget(monitor)}${monitor.target}${monitor.protocol}${systemName}`.toLocaleLowerCase()
return matchesFilterGroups(searchString, parseFilterGroups(value))
},
})
@@ -779,7 +786,7 @@ function NetworkMonitorSheetContent({
</Link>
<Separator orientation="vertical" className="h-2.5 bg-muted-foreground opacity-70" />
<ArrowLeftRightIcon className="size-3.5 text-muted-foreground -me-0.5" />
- {monitor.protocol.toUpperCase()}
+ {getMonitorProtocolLabel(monitor).toUpperCase()}
{monitor.protocol === "tcp" && monitor.port > 0 && (
<>
<Separator orientation="vertical" className="h-2.5 bg-muted-foreground opacity-70" />
diff --git a/internal/site/src/lib/monitor-compare.test.ts b/internal/site/src/lib/monitor-compare.test.ts
index 434fc663a..924391f5e 100644
--- a/internal/site/src/lib/monitor-compare.test.ts
+++ b/internal/site/src/lib/monitor-compare.test.ts
@@ -3,6 +3,7 @@ import type { NetworkMonitorRecord } from "@/types"
import {
getMonitorCompareState,
getMonitorIdentityKey,
+ getMonitorProtocolLabel,
getMonitorTarget,
MAX_COMPARE_MONITORS,
} from "./monitor-compare"
@@ -43,6 +44,10 @@ test("formats tcp targets with their port", () => {
expect(getMonitorTarget({ target: "example.com", protocol: "icmp", port: 0 })).toBe("example.com")
expect(getMonitorTarget({ target: "example.com", protocol: "tcp", port: 443 })).toBe("example.com:443")
expect(getMonitorTarget({ target: "::1", protocol: "tcp", port: 22 })).toBe("[::1]:22")
+ expect(getMonitorTarget({ target: "https://example.com/health", protocol: "http", port: 0 })).toBe(
+ "example.com/health"
+ )
+ expect(getMonitorTarget({ target: "http://example.com", protocol: "http", port: 0 })).toBe("example.com")
})
test("identity ignores the system but not protocol, port or server", () => {
@@ -118,6 +123,23 @@ test("DNS lookups of the same name against different servers get the server in t
expect(s.getLabel(d2)).toBe("example.com (8.8.8.8)")
})
+test("http and https URLs that are otherwise the same keep their scheme in their label", () => {
+ const h1 = mon("h1", "a", "http://example.com/health", { protocol: "http" })
+ const h2 = mon("h2", "a", "https://example.com/health", { protocol: "http" })
+ const h3 = mon("h3", "a", "https://other.example", { protocol: "http" })
+ const s = state(h1, [h1, h2, h3], [], ["h2", "h3"])
+ expect(s.getLabel(h1)).toBe("http://example.com/health")
+ expect(s.getLabel(h2)).toBe("https://example.com/health")
+ expect(s.getLabel(h3)).toBe("other.example")
+})
+
+test("shows https as the protocol for https targets", () => {
+ expect(getMonitorProtocolLabel({ target: "https://example.com", protocol: "http" })).toBe("https")
+ expect(getMonitorProtocolLabel({ target: "HTTPS://example.com", protocol: "http" })).toBe("https")
+ expect(getMonitorProtocolLabel({ target: "http://example.com", protocol: "http" })).toBe("http")
+ expect(getMonitorProtocolLabel({ target: "example.com", protocol: "tcp" })).toBe("tcp")
+})
+
test("uses separate local and fetched monitor lists on single-system pages", () => {
const s = getMonitorCompareState({
monitor: a1,
diff --git a/internal/site/src/lib/monitor-compare.ts b/internal/site/src/lib/monitor-compare.ts
index 9c709aabc..1a7350247 100644
--- a/internal/site/src/lib/monitor-compare.ts
+++ b/internal/site/src/lib/monitor-compare.ts
@@ -8,11 +8,22 @@ export const MAX_COMPARE_MONITORS = 24
type MonitorTarget = Pick<NetworkMonitorRecord, "target" | "protocol" | "port">
export function getMonitorTarget(monitor: MonitorTarget) {
+ // The protocol column already says http, so the URL scheme is redundant.
+ if (monitor.protocol === "http") return monitor.target.replace(/^https?:\/\//i, "")
if (monitor.protocol !== "tcp") return monitor.target
const host = monitor.target.includes(":") && !monitor.target.startsWith("[") ? `[${monitor.target}]` : monitor.target
return `${host}:${monitor.port}`
}
+export function isHttpsTarget(target: string) {
+ return /^https:\/\//i.test(target)
+}
+
+/** The protocol to show next to a target, since getMonitorTarget hides whether an HTTP monitor uses https. */
+export function getMonitorProtocolLabel(monitor: Pick<NetworkMonitorRecord, "target" | "protocol">) {
+ return monitor.protocol === "http" && isHttpsTarget(monitor.target) ? "https" : monitor.protocol
+}
+
/** Identifies what a monitor probes, regardless of which system probes it. */
export function getMonitorIdentityKey({
protocol,
@@ -98,17 +109,22 @@ export function getMonitorCompareState({
const multiTarget = targetIds.size > 0
const labels = new Map<string, string>()
const counts = new Map<string, number>()
- for (const m of compareMonitors) {
+ const getSeriesLabel = (m: NetworkMonitorRecord, target: string) => {
const systemName = getSystemName(m.system)
- const target = getMonitorTarget(m)
- const label = multiSystem && multiTarget ? `${systemName} · ${target}` : multiTarget ? target : systemName
+ return multiSystem && multiTarget ? `${systemName} · ${target}` : multiTarget ? target : systemName
+ }
+ for (const m of compareMonitors) {
+ const label = getSeriesLabel(m, getMonitorTarget(m))
labels.set(m.id, label)
counts.set(label, (counts.get(label) ?? 0) + 1)
}
- // DNS lookups of the same name against different servers would otherwise share a label.
+ // DNS lookups of the same name against different servers, and http and https URLs that are otherwise
+ // the same, would share a label.
for (const m of compareMonitors) {
const label = labels.get(m.id) as string
- if ((counts.get(label) ?? 0) > 1 && m.server) labels.set(m.id, `${label} (${m.server})`)
+ if ((counts.get(label) ?? 0) < 2) continue
+ if (m.server) labels.set(m.id, `${label} (${m.server})`)
+ else if (m.protocol === "http") labels.set(m.id, getSeriesLabel(m, m.target))
}
return {
diff --git a/internal/site/src/lib/network-monitor-utils.ts b/internal/site/src/lib/network-monitor-utils.ts
index c2623f92f..d76d52cf6 100644
--- a/internal/site/src/lib/network-monitor-utils.ts
+++ b/internal/site/src/lib/network-monitor-utils.ts
@@ -98,7 +98,7 @@ export function mergeMonitorStats(rawRecords: RawMonitorStatsRecord[], bucketMs
.map(([created, stats]) => ({ created, stats }))
}
-export { getMonitorTarget } from "./monitor-compare"
+export { getMonitorProtocolLabel, getMonitorTarget, isHttpsTarget } from "./monitor-compare"
/** Whole days until the certificate expires; negative once expired. */
export function getCertDaysLeft(cert: Pick<MonitorCertInfo, "expires">, now = Date.now()) {