SLA: support multiple SLA in one query, fixes #1603

This commit is contained in:
Alexander Zobnin
2023-08-01 15:04:35 +02:00
parent cef0a2e157
commit 873947ca2e
2 changed files with 78 additions and 4 deletions

View File

@@ -540,6 +540,74 @@ export function handleSLIResponse(response: any, itservices: any[], target: Zabb
});
}
export function handleMultiSLIResponse(response: any[], itservices: any[], slas: any[], target: ZabbixMetricsQuery) {
if (!response || response?.length === 0) {
return new MutableDataFrame({
refId: target.refId,
name: 'SLI',
fields: [],
});
}
const firstResponse = response[0];
const timestamps = [];
for (let i = 0; i < firstResponse?.periods?.length; i++) {
const period = firstResponse.periods[i];
timestamps.push(period.period_from * 1000);
}
const timeFiled: Field = {
name: TIME_SERIES_TIME_FIELD_NAME,
type: FieldType.time,
config: {
custom: {},
},
values: new ArrayVector<number>(timestamps),
};
const valueFields: Field[] = [];
const slaProperty = mapLegacySLAProperty(target.slaProperty);
for (let respIdx = 0; respIdx < response.length; respIdx++) {
const res = response[respIdx];
const values: number[][] = [];
for (let i = 0; i < res?.sli?.length; i++) {
const slis = res.sli[i];
for (let j = 0; j < slis.length; j++) {
const sli = slis[j];
const value = sli[slaProperty];
if (!values[j]) {
values[j] = [];
}
values[j].push(value);
}
}
for (let i = 0; i < res?.serviceids?.length; i++) {
const serviceId = res?.serviceids[i].toString();
const service = itservices.find((s) => s.serviceid === serviceId);
let name = `${service?.name || serviceId}`;
if (response.length > 1) {
const sla = slas[respIdx];
name = `${name}: ${sla.name}`;
}
valueFields.push({
name,
type: FieldType.number,
config: {},
values: new ArrayVector<number>(values[i]),
});
}
}
return new MutableDataFrame({
refId: target.refId,
name: 'SLI',
fields: [timeFiled, ...valueFields],
});
}
function mapLegacySLAProperty(property: string) {
switch (property) {
case 'sla':