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 4 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-->
142 changes: 105 additions & 37 deletions packages/xo-server/src/xo-mixins/rest-api.mjs
Original file line number Diff line number Diff line change
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,15 @@ 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 messagesFilter =
({ onlyAlarms = false, excludeAlarms = false } = {}) =>
_ =>
_.type === 'message' && (onlyAlarms ? _.name === 'ALARM' : excludeAlarms ? _.name !== 'ALARM' : true)

fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
export default class RestApi {
#api

Expand Down Expand Up @@ -477,7 +447,11 @@ export default class RestApi {
await sendObjects(
Object.values(
app.getObjects({
filter: every(_ => _.type === 'message' && _.$object === id, handleOptionalUserFilter(query.filter)),
filter: every(
messagesFilter({ excludeAlarms: true }),
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
_ => _.$object === id,
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
handleOptionalUserFilter(query.filter)
),
limit: ifDef(query.limit, Number),
})
),
Expand All @@ -486,10 +460,39 @@ export default class RestApi {
'/messages'
)
}

async function alarms(req, res) {
const {
object: { id },
query,
} = req
await sendObjects(
Object.values(
app.getObjects({
filter: every(
messagesFilter({ onlyAlarms: true }),
_ => _.$object === id,
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
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 +740,71 @@ export default class RestApi {
},
}
collections.dashboard = {}
collections.messages = {
async getObject(id) {
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
const message = app.getObject(id, 'message')
if (message === 'ALARM') {
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
throw noSuchObject(id, 'message')
}

return message
},
async getObjects(filter, limit) {
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
return handleArray(
await app.getObjects({
filter: every(messagesFilter({ excludeAlarms: true }), filter),
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
limit,
})
)
},
}
collections.alarms = {
async getObject(id, req) {
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
const alarm = app.getObject(id, 'message')
if (alarm.name !== '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: +value,
name,
},
object: {
type: object.type,
uuid: object.uuid,
href,
},
}
},
async getObjects(filter, limit) {
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
return handleArray(
Object.values(
await app.getObjects({
filter: every(messagesFilter({ onlyAlarms: true }), filter),
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
limit,
})
)
)
},
}

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