From 0baa0e9b34810269e0c53598bdbeb262db00c079 Mon Sep 17 00:00:00 2001 From: Hansen1018 Date: Sat, 24 Oct 2026 18:01:05 +0000 Subject: [PATCH 1/2] Read Nezha v2 monitor names for the latency rows Nezha v2 renamed the service fields: model.ServiceInfos is serialised with the JSON tags "monitor_id" / "monitor_name" (model/service_history_api.go, v2.0.0 onwards). Nezha v1 sent them as "service_id" / "service_name". The adapter read the v1 spelling, so on a v2 panel every latency row had an undefined name and an undefined id: the rows showed a bare colour dash, and all points collapsed onto one series in the chart. Both spellings are read now, with the v1 names kept as a fallback. --- src/adapters/nezha/index.ts | 23 ++++++++--- tests/nezha-ping.test.ts | 80 +++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 tests/nezha-ping.test.ts diff --git a/src/adapters/nezha/index.ts b/src/adapters/nezha/index.ts index 235b86e..a74ebd6 100644 --- a/src/adapters/nezha/index.ts +++ b/src/adapters/nezha/index.ts @@ -74,8 +74,16 @@ interface NezhaSetting { } interface NezhaServiceInfo { - service_id: number - service_name: string + /** + * Nezha v2 renamed these two fields: the JSON tags of model.ServiceInfos are + * "monitor_id" / "monitor_name" (model/service_history_api.go, v2.0.0 onwards). + * Nezha v1 shipped them as "service_id" / "service_name", so both spellings are + * read here. The monitor name is the latency target the row is labelled with. + */ + monitor_id?: number + monitor_name?: string + service_id?: number + service_name?: string created_at: number[] | null avg_delay: number[] | null } @@ -399,20 +407,23 @@ export const nezha: MonitorAdapter = { const res = await request>(`/api/v1/server/${encodeURIComponent(id)}/service`) const tasks: PingData['tasks'] = [] const points: PingData['points'] = [] - for (const s of res.data ?? []) { + for (const [index, s] of (res.data ?? []).entries()) { const at = s.created_at ?? [] const delay = s.avg_delay ?? [] const values = delay.filter((v) => v > 0) + // Nezha v2 calls a latency target a "monitor" and names it monitor_name. + const id = s.monitor_id ?? s.service_id ?? index + const name = s.monitor_name ?? s.service_name ?? '' tasks.push({ - id: s.service_id, - name: s.service_name, + id, + name, interval: 0, loss: 0, avg: values.length ? values.reduce((a, b) => a + b, 0) / values.length : 0, min: values.length ? Math.min(...values) : 0, max: values.length ? Math.max(...values) : 0, }) - at.forEach((t, i) => points.push({ at: t, task: s.service_id, value: delay[i] ?? -1 })) + at.forEach((t, i) => points.push({ at: t, task: id, value: delay[i] ?? -1 })) } points.sort((a, b) => a.at - b.at) return { tasks, points } diff --git a/tests/nezha-ping.test.ts b/tests/nezha-ping.test.ts new file mode 100644 index 0000000..6246a35 --- /dev/null +++ b/tests/nezha-ping.test.ts @@ -0,0 +1,80 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { nezha } from '../src/adapters/nezha' + +/** + * Nezha v2 renamed the service fields: model.ServiceInfos is serialised with the + * JSON tags "monitor_id" / "monitor_name" (model/service_history_api.go). + * The latency rows are labelled with that name, so a wrong field name leaves + * every row without a label. + */ +const v2Payload = { + success: true, + data: [ + { + monitor_id: 7, + server_id: 1, + monitor_name: '电信-河北', + server_name: 'NanoPi R5S', + display_index: 0, + created_at: [1, 2, 3], + avg_delay: [10, 20, 30], + }, + { + monitor_id: 8, + server_id: 1, + monitor_name: '联通-河北廊坊', + server_name: 'NanoPi R5S', + display_index: 0, + created_at: [1, 2], + avg_delay: [5, 15], + }, + ], +} + +/** Nezha v1 sent the same list as "service_id" / "service_name". */ +const v1Payload = { + success: true, + data: [{ service_id: 3, service_name: '移动-河北', created_at: [1], avg_delay: [7] }], +} + +function stubService(payload: unknown) { + vi.stubGlobal('fetch', async (url: string) => + new Response(JSON.stringify(url.includes('/service') ? payload : { success: true, data: [] }), { status: 200 }), + ) +} + +beforeEach(() => { + vi.stubGlobal('window', { location: { protocol: 'https:', host: 'status.test' }, ZoolConfig: {} }) +}) + +afterEach(() => { + vi.unstubAllGlobals() +}) + +describe('nezha loadPing', () => { + it('labels every row with the v2 monitor name', async () => { + stubService(v2Payload) + const data = await nezha.loadPing('1') + expect(data.tasks.map((t) => t.name)).toEqual(['电信-河北', '联通-河北廊坊']) + expect(data.tasks.map((t) => t.id)).toEqual([7, 8]) + expect(data.tasks[0].avg).toBe(20) + expect(data.tasks[0].min).toBe(10) + expect(data.tasks[0].max).toBe(30) + }) + + it('keeps the v1 service name working', async () => { + stubService(v1Payload) + const data = await nezha.loadPing('1') + expect(data.tasks.map((t) => t.name)).toEqual(['移动-河北']) + expect(data.tasks.map((t) => t.id)).toEqual([3]) + }) + + it('keeps each point attached to its own task', async () => { + stubService(v2Payload) + const data = await nezha.loadPing('1') + const byTask = new Map() + for (const p of data.points) byTask.set(p.task, [...(byTask.get(p.task) ?? []), p.value]) + expect(byTask.get(7)).toEqual([10, 20, 30]) + expect(byTask.get(8)).toEqual([5, 15]) + }) +}) From 460ac5bf77250965c7a976bcccb37f5173fbb4be Mon Sep 17 00:00:00 2001 From: Hansen1018 Date: Sat, 24 Oct 2026 18:09:24 +0000 Subject: [PATCH 2/2] Give loadPing the hours argument its type requires --- tests/nezha-ping.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/nezha-ping.test.ts b/tests/nezha-ping.test.ts index 6246a35..cb72bf0 100644 --- a/tests/nezha-ping.test.ts +++ b/tests/nezha-ping.test.ts @@ -54,7 +54,7 @@ afterEach(() => { describe('nezha loadPing', () => { it('labels every row with the v2 monitor name', async () => { stubService(v2Payload) - const data = await nezha.loadPing('1') + const data = await nezha.loadPing('1', 24) expect(data.tasks.map((t) => t.name)).toEqual(['电信-河北', '联通-河北廊坊']) expect(data.tasks.map((t) => t.id)).toEqual([7, 8]) expect(data.tasks[0].avg).toBe(20) @@ -64,14 +64,14 @@ describe('nezha loadPing', () => { it('keeps the v1 service name working', async () => { stubService(v1Payload) - const data = await nezha.loadPing('1') + const data = await nezha.loadPing('1', 24) expect(data.tasks.map((t) => t.name)).toEqual(['移动-河北']) expect(data.tasks.map((t) => t.id)).toEqual([3]) }) it('keeps each point attached to its own task', async () => { stubService(v2Payload) - const data = await nezha.loadPing('1') + const data = await nezha.loadPing('1', 24) const byTask = new Map() for (const p of data.points) byTask.set(p.task, [...(byTask.get(p.task) ?? []), p.value]) expect(byTask.get(7)).toEqual([10, 20, 30])