31 lines
1.4 KiB
JavaScript
31 lines
1.4 KiB
JavaScript
import { redactInstance, summarizeInstanceSnapshot } from '../core.js'
|
|
|
|
export async function collectInstanceStatus(client) {
|
|
const result = {
|
|
...redactInstance(client.instance), reachable: false, authenticated: null, latencyMs: null,
|
|
statusCode: null, error: null, authStatus: null, summary: summarizeInstanceSnapshot({}), raw: {}, checkedAt: new Date().toISOString(),
|
|
}
|
|
try {
|
|
const health = await client.fetchJson('/api/health')
|
|
result.latencyMs = health.latencyMs
|
|
result.statusCode = health.status
|
|
result.raw.health = health.data || health.text || null
|
|
const auth = await client.ensureAuthenticated()
|
|
result.authenticated = auth.authenticated
|
|
result.authStatus = auth
|
|
const endpoints = { device: '/api/device', sim: '/api/sim', network: '/api/network', smsStats: '/api/sms/stats', data: '/api/data', ota: '/api/ota/status', stats: '/api/stats', calls: '/api/calls' }
|
|
await Promise.all(Object.entries(endpoints).map(async ([key, endpoint]) => {
|
|
try {
|
|
const response = await client.fetchJson(endpoint)
|
|
if (response.ok) { result.raw[key] = response.data; result.reachable = true }
|
|
if (response.status === 401) result.authenticated = false
|
|
} catch {}
|
|
}))
|
|
result.reachable ||= health.ok
|
|
result.summary = summarizeInstanceSnapshot(result.raw)
|
|
} catch (error) {
|
|
result.error = error?.name === 'AbortError' ? 'timeout' : String(error?.message || error)
|
|
}
|
|
return result
|
|
}
|