The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/alerting/unified/components/rule-editor/preview.ts

46 lines
1.5 KiB

import { DataFrame } from '@grafana/data';
import { GrafanaAlertState, isGrafanaAlertState, Labels } from '../../../../../types/unified-alerting-dto';
interface AlertPreviewInstance {
state: GrafanaAlertState;
info?: string;
labels: Labels;
}
interface AlertPreview {
instances: AlertPreviewInstance[];
}
// Alerts previews come in a DataFrame format which is more suited for displaying time series data
// In order to display a list of tags we need to transform DataFrame into set of labels
export function mapDataFrameToAlertPreview({ fields }: DataFrame): AlertPreview {
const labelFields = fields.filter((field) => !['State', 'Info'].includes(field.name));
const stateFieldIndex = fields.findIndex((field) => field.name === 'State');
const infoFieldIndex = fields.findIndex((field) => field.name === 'Info');
const labelIndexes = labelFields.map((labelField) => fields.indexOf(labelField));
const instanceStatusCount = fields[stateFieldIndex]?.values.length ?? 0;
const instances: AlertPreviewInstance[] = [];
for (let index = 0; index < instanceStatusCount; index++) {
const labelValues = labelIndexes.map((labelIndex) => [
fields[labelIndex].name,
fields[labelIndex].values.get(index),
]);
const state = fields[stateFieldIndex]?.values?.get(index);
const info = fields[infoFieldIndex]?.values?.get(index);
if (isGrafanaAlertState(state)) {
instances.push({
state: state,
info: info,
labels: Object.fromEntries(labelValues),
});
}
}
return { instances };
}