Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(xo-server/rest-api): remove alarms information in the dashboard endpoint #7959

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

> Users must be able to say: “Nice enhancement, I'm eager to test it”

- [REST API] Add `/alarms` endpoint and remove `alarms` information in the `/dashboard` endpoint (PR [#7959](https://github.com/vatesfr/xen-orchestra/pull/7959))

### Bug fixes

> Users must be able to say: “I had this issue, happy to know it's fixed”
Expand All @@ -31,4 +33,6 @@

<!--packages-start-->

- xo-server minor

<!--packages-end-->
4 changes: 4 additions & 0 deletions packages/xo-server/src/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,7 @@ export const unboxIdsFromPattern = pattern => {
// -------------------------------------------------------------------

export const isSrWritable = sr => sr !== undefined && sr.content_type !== 'iso' && sr.size > 0

// -------------------------------------------------------------------

export const isAlarm = alarm => alarm.type === 'message' && alarm.name === 'ALARM'
136 changes: 97 additions & 39 deletions packages/xo-server/src/xo-mixins/rest-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import * as CM from 'complex-matcher'
import { VDI_FORMAT_RAW, VDI_FORMAT_VHD } from '@xen-orchestra/xapi'
import { parse } from 'xo-remote-parser'

import { getUserPublicProperties, isSrWritable } from '../utils.mjs'
import { getUserPublicProperties, isAlarm, isSrWritable } from '../utils.mjs'
import { compileXoJsonSchema } from './_xoJsonSchema.mjs'

// E.g: 'value: 0.6\nconfig:\n<variable>\n<name value="cpu_usage"/>\n<alarm_trigger_level value="0.4"/>\n<alarm_trigger_period value ="60"/>\n</variable>';
const ALARM_BODY_REGEX = /^value:\s*(\d+(?:\.\d+)?)\s*config:\s*<variable>\s*<name value="(.*?)"/
const ALARM_BODY_REGEX = /^value:\s*(Infinity|NaN|-Infinity|\d+(?:\.\d+)?)\s*config:\s*<variable>\s*<name value="(.*?)"/

const { join } = path.posix
const noop = Function.prototype
Expand Down Expand Up @@ -174,7 +174,6 @@ async function _getDashboardStats(app) {
const poolIds = new Set()
const hosts = []
const writableSrs = []
const alarms = []

for (const obj of app.objects.values()) {
if (obj.type === 'host') {
Expand All @@ -190,10 +189,6 @@ async function _getDashboardStats(app) {
writableSrs.push(obj)
}
}

if (obj.type === 'message' && obj.name === 'ALARM') {
alarms.push(obj)
}
}

dashboard.nPools = poolIds.size
Expand Down Expand Up @@ -360,40 +355,11 @@ async function _getDashboardStats(app) {
console.error(error)
}

dashboard.alarms = alarms.reduce((acc, { $object, body, time }) => {
try {
const [, value, name] = body.match(ALARM_BODY_REGEX)

let object
try {
object = app.getObject($object)
} catch (error) {
console.error(error)
object = {
type: 'unknown',
uuid: $object,
}
}

acc.push({
name,
object: {
type: object.type,
uuid: object.uuid,
},
timestamp: time,
value: +value,
})
} catch (error) {
console.error(error)
}

return acc
}, [])
return dashboard
}
const getDashboardStats = throttle(_getDashboardStats, 6e4, { trailing: false, leading: true })

const keepNonAlarmMessages = message => message.type === 'message' && !isAlarm(message)
export default class RestApi {
#api

Expand Down Expand Up @@ -477,7 +443,7 @@ export default class RestApi {
await sendObjects(
Object.values(
app.getObjects({
filter: every(_ => _.type === 'message' && _.$object === id, handleOptionalUserFilter(query.filter)),
filter: every(_ => _.$object === id, keepNonAlarmMessages, handleOptionalUserFilter(query.filter)),
limit: ifDef(query.limit, Number),
})
),
Expand All @@ -486,10 +452,35 @@ export default class RestApi {
'/messages'
)
}

async function alarms(req, res) {
const {
object: { id },
query,
} = req
await sendObjects(
Object.values(
app.getObjects({
filter: every(_ => _.$object === id, isAlarm, handleOptionalUserFilter(query.filter)),
limit: ifDef(query.limit, Number),
})
),
req,
res,
'/alarms'
)
}

for (const type of types) {
const id = type.toLocaleLowerCase() + 's'

collections[id] = { getObject, getObjects, routes: { messages }, isCorrectType: _ => _.type === type, type }
collections[id] = {
getObject,
getObjects,
routes: { messages, alarms },
isCorrectType: _ => _.type === type,
type,
}
}

collections.hosts.routes = {
Expand Down Expand Up @@ -737,6 +728,73 @@ export default class RestApi {
},
}
collections.dashboard = {}
collections.messages = {
getObject(id) {
const message = app.getObject(id, 'message')
if (isAlarm(message)) {
throw noSuchObject(id, 'message')
}

return message
},
getObjects(filter, limit) {
return handleArray(
Object.values(
app.getObjects({
filter: every(keepNonAlarmMessages, filter),
limit,
})
)
)
},
}
collections.alarms = {
getObject(id, req) {
const alarm = app.getObject(id, 'message')
if (!isAlarm(alarm)) {
throw noSuchObject(id, 'alarm')
}

const { $object, body } = alarm
let object = {}
try {
object = app.getObject($object)
} catch (error) {
object = {
type: 'unknown',
uuid: $object,
}
}

const { baseUrl } = req
const objType = object.type.toLowerCase() + 's'
const href = collections[objType] === undefined ? undefined : `${baseUrl}/${objType}/${object.uuid}`
const [, value, name] = body.match(ALARM_BODY_REGEX)

return {
...alarm,
body: {
value, // Keep the value as a string because NaN, Infinity, -Infinity is not valid JSON
name,
},
object: {
type: object.type,
uuid: object.uuid,
href,
},
}
},
getObjects(filter, limit) {
return handleArray(
Object.values(
app.getObjects({
filter: every(isAlarm, filter),
limit,
})
)
)
},
}

// normalize collections
for (const id of Object.keys(collections)) {
Expand Down
Loading