Skip to content

Commit

Permalink
feat(@xen-orchestra/rest-api): expose hosts/:id/stats
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuRA committed Feb 28, 2025
1 parent dcc512a commit 0ecb942
Show file tree
Hide file tree
Showing 7 changed files with 603 additions and 6 deletions.
34 changes: 34 additions & 0 deletions @vates/types/src/common.mts
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,37 @@ export const CERTIFICATE_TYPE = {
} as const

export type CERTIFICATE_TYPE = (typeof CERTIFICATE_TYPE)[keyof typeof CERTIFICATE_TYPE]

// ----- XAPI Stats

type XapiStatsResponse<T> = {
endTimestamp: number
interval: number
stats: T
}

export type XapiStatsGranularity = 'seconds' | 'minutes' | 'hours' | 'days'

export type XapiHostStats = XapiStatsResponse<{
cpus: Record<string, number[]>
ioThroughput: {
r: Record<string, number[]>
w: Record<string, number[]>
}
iops: {
r: Record<string, number[]>
w: Record<string, number[]>
}
iowait: Record<string, number[]>
latency: {
r: Record<string, number[]>
w: Record<string, number[]>
}
load: number[]
memory: number[]
memoryFree: number[]
pifs: {
rx: Record<string, number[]>
tx: Record<string, number[]>
}
}>
18 changes: 16 additions & 2 deletions @xen-orchestra/rest-api/src/hosts/host.controller.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Example, Get, Path, Query, Request, Response, Route, Security, Tags } from 'tsoa'
import type { Request as ExRequest } from 'express'
import { inject } from 'inversify'
import { invalidParameters } from 'xo-common/api-errors.js'
import { provide } from 'inversify-binding-decorators'
import type { XoHost } from '@vates/types'
import type { XapiHostStats, XapiStatsGranularity, XoHost } from '@vates/types'

import { host, hostIds, partialHosts } from '../open-api/oa-examples/host.oa-example.mjs'
import { host, hostIds, hostStats, partialHosts } from '../open-api/oa-examples/host.oa-example.mjs'
import { RestApi } from '../rest-api/rest-api.mjs'
import type { Unbrand, WithHref } from '../helpers/helper.type.mjs'
import { XapiXoController } from '../abstract-classes/xapi-xo-controller.mjs'
Expand Down Expand Up @@ -45,4 +46,17 @@ export class HostController extends XapiXoController<XoHost> {
getHost(@Path() id: string): Unbrand<XoHost> {
return this.getObject(id as XoHost['id'])
}

/**
* Host must be running
* @example id "b61a5c92-700e-4966-a13b-00633f03eea8"
*/
@Example(hostStats)
@Get('{id}/stats')
@Response(404, 'Host not found')
@Response(422, 'Invalid granularity')
@Response(500, 'Internal Server Error')
async getHostStats(@Path() id: string, @Query() granularity?: XapiStatsGranularity): Promise<XapiHostStats> {
return await this.restApi.xoApp.getXapiHostStats(id as XoHost['id'], granularity)
}
}
542 changes: 542 additions & 0 deletions @xen-orchestra/rest-api/src/open-api/oa-examples/host.oa-example.mts

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions @xen-orchestra/rest-api/src/rest-api/rest-api.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export class RestApi {
this.#xoApp = xoApp
}

get xoApp() {
return this.#xoApp
}

authenticateUser(...args: Parameters<XoApp['authenticateUser']>) {
return this.#xoApp.authenticateUser(...args)
}
Expand Down
4 changes: 3 additions & 1 deletion @xen-orchestra/rest-api/src/rest-api/rest-api.type.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { XoUser, XapiXoRecord } from '@vates/types/xo'
import { XapiHostStats, XapiStatsGranularity } from '@vates/types'
import type { XoUser, XapiXoRecord, XoHost } from '@vates/types/xo'

export type XoApp = {
authenticateUser: (
Expand All @@ -11,5 +12,6 @@ export type XoApp = {
type: T['type'],
opts?: { filter?: string; limit?: number }
) => Record<T['id'], T>
getXapiHostStats: (hostId: XoHost['id'], granularity?: XapiStatsGranularity) => Promise<XapiHostStats>
runWithApiContext: (user: XoUser, fn: () => void) => Promise<unknown>
}
2 changes: 2 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- **Migrated REST API endpoints**:
- `/rest/v0/hosts` (PR [#8372](https://github.com/vatesfr/xen-orchestra/pull/8372))
- `/rest/v0/hosts/<host-id>` (PR [#8372](https://github.com/vatesfr/xen-orchestra/pull/8372))
- `/rest/v0/hosts/<host-id>/stats` (PR [#8372](https://github.com/vatesfr/xen-orchestra/pull/8372))

> Users must be able to say: “Nice enhancement, I'm eager to test it”
Expand All @@ -40,5 +41,6 @@
- @vates/types minor
- @xen-orchestra/rest-api minor
- @xen-orchestra/web minor
- xo-server patch

<!--packages-end-->
5 changes: 2 additions & 3 deletions packages/xo-server/src/xo-mixins/rest-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1357,13 +1357,12 @@ export default class RestApi {
)

api.get(
'/:collection(vms|hosts)/:object/stats',
'/:collection(vms)/:object/stats',
wrap(async (req, res) => {
const object = req.object
const method = object.type === 'VM' ? 'getXapiVmStats' : 'getXapiHostStats'
const granularity = req.query.granularity

const result = await app[method](object.id, granularity)
const result = await app.getXapiVmStats(object.id, granularity)
return res.json(result)
})
)
Expand Down

0 comments on commit 0ecb942

Please sign in to comment.