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/mocks/alertRuleApi.ts

248 lines
6.3 KiB

import { http, HttpResponse } from 'msw';
Alerting: Add notification policies preview in alert creation (#68839) * Add notification policies preview in alert rule form Co-authored-by: Konrad Lalik <konrad.lalik@grafana.com> * Refactor using new useGetPotentialInstances hook and apply some style changes * Add notification policy detail modal * Use backtesting api for simulating potential alert instances * Fix logic to travserse all the children from the root route * Split notification preview by alert manager * Add instance count to matching policy header and fix some styles * Move some logic to a new hook useGetAlertManagersSourceNames to make the code more clean * Fix some tests * Add initial test for NotificationPreview * Use button to preview potential instances * Add link to contact point details * Add route matching result details * Show AlertManager image in the routing preview list * Add tests setup, add single AM preview test * Handle no matchers and no labels use case * Update some style in collapse component and fix policy path in modal * Update modal styles * Update styles * Update collapse header styling * Normalize tree nodes should happen before findMatchingRoutes call * Fix findMatchingRoutes and findMatchingAlertGroups methods after reabasing * Move instances matching to the web worker code * Fix config fetching for vanilla prometheus AMs * Add tests * Add tests mocks * Fix tests after adding web worker * Display matching labels for each matching alert instance * Add minor css improvements * Revert changes added in Collapse component as we don't use it anymore * Move the route details modal to a separate file * Move NotificationRoute and preview hook into separate files * Fix Alertmanager preview tests * Fix tests * Move matcher code to a separate file, improve matcher mock * Add permissions control for contact point edit view link * Fix from and to for the temporal use of backtesting api * Fix tests, add lazy loading of the preview component Co-authored-by: Sonia Aguilar <soniaaguilarpeiron@gmail.com> * Fix preview test * Add onclick on the header div so it collapse and expands when clicking on it, and update styles to be consistent with the rest of tables * Adapt the code to the new rule testing endpoint definition * Fix tests * small changes after reviewing the final code * compute entire inherited tree before computing the routes map * Throw error in case of not having receiver in routesByIdMap and add test for the use case of inheriting receiver from parent to check UI throws no errors * Add list of labels in the policy route path that produces the policy matchers to match potential instances * Use color determined by the key, in label tags when hovering matchers in the policy tree * Remove labels in modal and handle empty string as receiver to inherit from parent as we do with undefined * Revert "Add list of labels in the policy route path that produces the policy matchers to match potential instances" This reverts commit ee73ae9cf9caf10c33bcd58973279f23437a9146. * fix inheritance for computeInheritedTree * Fix message shown when preview has not been executed yet * First round for adressing PR review comments * Adress the rest of PR review commments * Update texts and rename id prop in NotificaitonStep to alertUid --------- Co-authored-by: Konrad Lalik <konrad.lalik@grafana.com> Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com>
2 years ago
import { SetupServer } from 'msw/node';
import { FieldType } from '@grafana/data';
import {
GrafanaAlertStateDecision,
PromRulesResponse,
RulerGrafanaRuleDTO,
RulerRuleGroupDTO,
} from 'app/types/unified-alerting-dto';
import { PREVIEW_URL, PreviewResponse, PROM_RULES_URL } from '../api/alertRuleApi';
import { Annotation } from '../utils/constants';
Alerting: Add notification policies preview in alert creation (#68839) * Add notification policies preview in alert rule form Co-authored-by: Konrad Lalik <konrad.lalik@grafana.com> * Refactor using new useGetPotentialInstances hook and apply some style changes * Add notification policy detail modal * Use backtesting api for simulating potential alert instances * Fix logic to travserse all the children from the root route * Split notification preview by alert manager * Add instance count to matching policy header and fix some styles * Move some logic to a new hook useGetAlertManagersSourceNames to make the code more clean * Fix some tests * Add initial test for NotificationPreview * Use button to preview potential instances * Add link to contact point details * Add route matching result details * Show AlertManager image in the routing preview list * Add tests setup, add single AM preview test * Handle no matchers and no labels use case * Update some style in collapse component and fix policy path in modal * Update modal styles * Update styles * Update collapse header styling * Normalize tree nodes should happen before findMatchingRoutes call * Fix findMatchingRoutes and findMatchingAlertGroups methods after reabasing * Move instances matching to the web worker code * Fix config fetching for vanilla prometheus AMs * Add tests * Add tests mocks * Fix tests after adding web worker * Display matching labels for each matching alert instance * Add minor css improvements * Revert changes added in Collapse component as we don't use it anymore * Move the route details modal to a separate file * Move NotificationRoute and preview hook into separate files * Fix Alertmanager preview tests * Fix tests * Move matcher code to a separate file, improve matcher mock * Add permissions control for contact point edit view link * Fix from and to for the temporal use of backtesting api * Fix tests, add lazy loading of the preview component Co-authored-by: Sonia Aguilar <soniaaguilarpeiron@gmail.com> * Fix preview test * Add onclick on the header div so it collapse and expands when clicking on it, and update styles to be consistent with the rest of tables * Adapt the code to the new rule testing endpoint definition * Fix tests * small changes after reviewing the final code * compute entire inherited tree before computing the routes map * Throw error in case of not having receiver in routesByIdMap and add test for the use case of inheriting receiver from parent to check UI throws no errors * Add list of labels in the policy route path that produces the policy matchers to match potential instances * Use color determined by the key, in label tags when hovering matchers in the policy tree * Remove labels in modal and handle empty string as receiver to inherit from parent as we do with undefined * Revert "Add list of labels in the policy route path that produces the policy matchers to match potential instances" This reverts commit ee73ae9cf9caf10c33bcd58973279f23437a9146. * fix inheritance for computeInheritedTree * Fix message shown when preview has not been executed yet * First round for adressing PR review comments * Adress the rest of PR review commments * Update texts and rename id prop in NotificaitonStep to alertUid --------- Co-authored-by: Konrad Lalik <konrad.lalik@grafana.com> Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com>
2 years ago
export function mockPreviewApiResponse(server: SetupServer, result: PreviewResponse) {
server.use(http.post(PREVIEW_URL, () => HttpResponse.json(result)));
Alerting: Add notification policies preview in alert creation (#68839) * Add notification policies preview in alert rule form Co-authored-by: Konrad Lalik <konrad.lalik@grafana.com> * Refactor using new useGetPotentialInstances hook and apply some style changes * Add notification policy detail modal * Use backtesting api for simulating potential alert instances * Fix logic to travserse all the children from the root route * Split notification preview by alert manager * Add instance count to matching policy header and fix some styles * Move some logic to a new hook useGetAlertManagersSourceNames to make the code more clean * Fix some tests * Add initial test for NotificationPreview * Use button to preview potential instances * Add link to contact point details * Add route matching result details * Show AlertManager image in the routing preview list * Add tests setup, add single AM preview test * Handle no matchers and no labels use case * Update some style in collapse component and fix policy path in modal * Update modal styles * Update styles * Update collapse header styling * Normalize tree nodes should happen before findMatchingRoutes call * Fix findMatchingRoutes and findMatchingAlertGroups methods after reabasing * Move instances matching to the web worker code * Fix config fetching for vanilla prometheus AMs * Add tests * Add tests mocks * Fix tests after adding web worker * Display matching labels for each matching alert instance * Add minor css improvements * Revert changes added in Collapse component as we don't use it anymore * Move the route details modal to a separate file * Move NotificationRoute and preview hook into separate files * Fix Alertmanager preview tests * Fix tests * Move matcher code to a separate file, improve matcher mock * Add permissions control for contact point edit view link * Fix from and to for the temporal use of backtesting api * Fix tests, add lazy loading of the preview component Co-authored-by: Sonia Aguilar <soniaaguilarpeiron@gmail.com> * Fix preview test * Add onclick on the header div so it collapse and expands when clicking on it, and update styles to be consistent with the rest of tables * Adapt the code to the new rule testing endpoint definition * Fix tests * small changes after reviewing the final code * compute entire inherited tree before computing the routes map * Throw error in case of not having receiver in routesByIdMap and add test for the use case of inheriting receiver from parent to check UI throws no errors * Add list of labels in the policy route path that produces the policy matchers to match potential instances * Use color determined by the key, in label tags when hovering matchers in the policy tree * Remove labels in modal and handle empty string as receiver to inherit from parent as we do with undefined * Revert "Add list of labels in the policy route path that produces the policy matchers to match potential instances" This reverts commit ee73ae9cf9caf10c33bcd58973279f23437a9146. * fix inheritance for computeInheritedTree * Fix message shown when preview has not been executed yet * First round for adressing PR review comments * Adress the rest of PR review commments * Update texts and rename id prop in NotificaitonStep to alertUid --------- Co-authored-by: Konrad Lalik <konrad.lalik@grafana.com> Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com>
2 years ago
}
export function mockPromRulesApiResponse(server: SetupServer, result: PromRulesResponse) {
server.use(http.get(PROM_RULES_URL, () => HttpResponse.json(result)));
}
export const grafanaRulerGroupName = 'grafana-group-1';
export const grafanaRulerNamespace = { name: 'test-folder-1', uid: 'uuid020c61ef' };
export const grafanaRulerNamespace2 = { name: 'test-folder-2', uid: '6abdb25bc1eb' };
export const grafanaRulerRule: RulerGrafanaRuleDTO = {
for: '5m',
labels: {
severity: 'critical',
region: 'nasa',
},
annotations: {
[Annotation.summary]: 'Test alert',
},
grafana_alert: {
uid: '4d7125fee983',
title: 'Grafana-rule',
namespace_uid: 'uuid020c61ef',
rule_group: grafanaRulerGroupName,
data: [
{
refId: 'A',
datasourceUid: 'datasource-uid',
queryType: 'alerting',
relativeTimeRange: { from: 1000, to: 2000 },
model: {
refId: 'A',
expression: 'vector(1)',
queryType: 'alerting',
datasource: { uid: 'datasource-uid', type: 'prometheus' },
},
},
],
condition: 'A',
no_data_state: GrafanaAlertStateDecision.NoData,
exec_err_state: GrafanaAlertStateDecision.Error,
is_paused: false,
notification_settings: undefined,
},
};
export const grafanaRulerGroup: RulerRuleGroupDTO = {
name: grafanaRulerGroupName,
interval: '1m',
rules: [grafanaRulerRule],
};
export const grafanaRulerEmptyGroup: RulerRuleGroupDTO = {
name: 'empty-group',
interval: '1m',
rules: [],
};
export const namespaceByUid: Record<string, { name: string; uid: string }> = {
[grafanaRulerNamespace.uid]: grafanaRulerNamespace,
[grafanaRulerNamespace2.uid]: grafanaRulerNamespace2,
};
export const namespaces: Record<string, RulerRuleGroupDTO[]> = {
[grafanaRulerNamespace.uid]: [grafanaRulerGroup],
[grafanaRulerNamespace2.uid]: [grafanaRulerEmptyGroup],
};
//-------------------- for alert history tests we reuse these constants --------------------
export const time_0 = 1718368710000;
// time1 + 30 seg
export const time_plus_30 = 1718368740000;
// time1 + 5 seg
export const time_plus_5 = 1718368715000;
// time1 + 15 seg
export const time_plus_15 = 1718368725000;
// time1 + 10 seg
export const time_plus_10 = 1718368720000;
// returns 4 transitions. times is an array of 4 timestamps.
export const getHistoryResponse = (times: number[]) => ({
schema: {
fields: [
{
name: 'time',
type: FieldType.time,
labels: {},
},
{
name: 'line',
type: FieldType.other,
labels: {},
},
{
name: 'labels',
type: FieldType.other,
labels: {},
},
],
},
data: {
values: [
[...times],
[
{
schemaVersion: 1,
previous: 'Pending',
current: 'Alerting',
value: {
A: 1,
B: 1,
C: 1,
},
condition: 'C',
dashboardUID: '',
panelID: 0,
fingerprint: '141da2d491f61029',
ruleTitle: 'alert1',
ruleID: 7,
ruleUID: 'adnpo0g62bg1sb',
labels: {
alertname: 'alert1',
grafana_folder: 'FOLDER A',
handler: '/alerting/*',
},
},
{
schemaVersion: 1,
previous: 'Pending',
current: 'Alerting',
value: {
A: 1,
B: 1,
C: 1,
},
condition: 'C',
dashboardUID: '',
panelID: 0,
fingerprint: '141da2d491f61029',
ruleTitle: 'alert2',
ruleID: 3,
ruleUID: 'adna1xso80hdsd',
labels: {
alertname: 'alert2',
grafana_folder: 'FOLDER A',
handler: '/alerting/*',
},
},
{
schemaVersion: 1,
previous: 'Pending',
current: 'Alerting',
value: {
A: 1,
B: 1,
C: 1,
},
condition: 'C',
dashboardUID: '',
panelID: 0,
fingerprint: '141da2d491f61029',
ruleTitle: 'alert1',
ruleID: 7,
ruleUID: 'adnpo0g62bg1sb',
labels: {
alertname: 'alert1',
grafana_folder: 'FOLDER A',
handler: '/alerting/*',
},
},
{
schemaVersion: 1,
previous: 'Pending',
current: 'Alerting',
value: {
A: 1,
B: 1,
C: 1,
},
condition: 'C',
dashboardUID: '',
panelID: 0,
fingerprint: '5d438530c73fc657',
ruleTitle: 'alert2',
ruleID: 3,
ruleUID: 'adna1xso80hdsd',
labels: {
alertname: 'alert2',
grafana_folder: 'FOLDER A',
handler: '/alerting/*',
},
},
],
[
{
folderUID: 'edlvwh5881z40e',
from: 'state-history',
group: 'GROUP111',
level: 'info',
orgID: '1',
service_name: 'unknown_service',
},
{
folderUID: 'edlvwh5881z40e',
from: 'state-history',
group: 'GROUP111',
level: 'info',
orgID: '1',
service_name: 'unknown_service',
},
{
folderUID: 'edlvwh5881z40e',
from: 'state-history',
group: 'GROUP111',
level: 'info',
orgID: '1',
service_name: 'unknown_service',
},
{
folderUID: 'edlvwh5881z40e',
from: 'state-history',
group: 'GROUP111',
level: 'info',
orgID: '1',
service_name: 'unknown_service',
},
],
],
},
});