28 lines
880 B
TypeScript
28 lines
880 B
TypeScript
import vine from '@vinejs/vine'
|
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
import metricsService, { type HostSample } from '#services/metrics_service'
|
|
import { openSse } from '#services/sse'
|
|
|
|
const historyValidator = vine.create({
|
|
range: vine.enum(['1h', '24h', '7d', '30d'] as const),
|
|
})
|
|
|
|
export default class MetricsController {
|
|
/**
|
|
* Live samples (every 5 s) for the charts of the overview.
|
|
*/
|
|
async live(ctx: HttpContext) {
|
|
const channel = openSse(ctx)
|
|
channel.send('init', metricsService.live)
|
|
|
|
const onSample = (sample: HostSample) => channel.send('sample', sample)
|
|
metricsService.on('sample', onSample)
|
|
channel.onClose(() => metricsService.off('sample', onSample))
|
|
}
|
|
|
|
async history({ request }: HttpContext) {
|
|
const { range } = await historyValidator.validate(request.qs())
|
|
return metricsService.history(range)
|
|
}
|
|
}
|