Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/adapters/nezha/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -399,20 +407,23 @@ export const nezha: MonitorAdapter = {
const res = await request<Common<NezhaServiceInfo[]>>(`/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 }
Expand Down
80 changes: 80 additions & 0 deletions tests/nezha-ping.test.ts
Original file line number Diff line number Diff line change
@@ -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', 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)
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', 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', 24)
const byTask = new Map<number, number[]>()
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])
})
})