Files
grafana/public/app/features/alerting/unified/api/alertmanagerApi.ts
T
Virginia CepedaandSonia Aguilar 7338164612 Alerting: Add alert instance picker (#67138)
* Add Preview template and payload editor to templates form

* Add TemplatePreview test and update css

* Preview errors for each template that is wrong

* Enable preview templating only for Grafana Alert Manager

* Use harcoded default payload instead of requesting it to the backend

* Update error response in the api definition

* Add spinner when loading result for preview

* Update api request followind DD changes

* Use pre instead of TextArea to render the preview

* Fix tests

* Add alert list editor

* Add start and end time for alert generator

* Add preview for data list added in the modal

* Update copies and move submit button in alert generator to the bottom

* Copy updates

* Refactor

* Use tab instead of button to preview

* Move payload editor next to the content

* Copy update

* Refactor

* Adress PR review comments

* Fix wrong json format throwing an exception when adding more data

* Use monaco editor for payload

* Only show text 'Preview for...'  when we have more than one define

* Fix some errors

* Update CollapseSection style

* Add tooltip for the Payload info icon explaining the available list of alert data fields in preview

* Set payload as invalid if it's not an array

* Fix test

* Update text in AlertTemplateDataTable

* Add separators to distinguish lines that belong to the preview

* Use harcoded default payload instead of requesting it to the backend

* Add alert instance picker

* Add rule search capability and cleanup

* Display alert instance extra information on hover

* Rebase and integrate with existing view

* Display folder under rule name

* Display unique labels for alert instances

* Remove unneeded interface

* Reset state after closing the modal

* Refactor useEffect and useMemo

* Move common code to variable

* Refactor to avoid setting filtered rules as state

* Disable instance selector button when there are errors in the payload

* Validate payload on button click

* Change warning text

* Add support for state filters in alertmanager alerts request

* Use RTK Query to fetch alert instances

* Address review comments

* Fix lint

---------

Co-authored-by: Sonia Aguilar <soniaaguilarpeiron@gmail.com>
2023-04-28 12:58:15 -03:00

104 lines
3.6 KiB
TypeScript

import {
AlertmanagerAlert,
AlertmanagerChoice,
AlertManagerCortexConfig,
ExternalAlertmanagerConfig,
ExternalAlertmanagers,
ExternalAlertmanagersResponse,
Matcher,
} from '../../../../plugins/datasource/alertmanager/types';
import { matcherToOperator } from '../utils/alertmanager';
import { getDatasourceAPIUid, GRAFANA_RULES_SOURCE_NAME } from '../utils/datasource';
import { alertingApi } from './alertingApi';
const LIMIT_TO_SUCCESSFULLY_APPLIED_AMS = 10;
export interface AlertmanagersChoiceResponse {
alertmanagersChoice: AlertmanagerChoice;
numExternalAlertmanagers: number;
}
interface AlertmanagerAlertsFilter {
active?: boolean;
silenced?: boolean;
inhibited?: boolean;
unprocessed?: boolean;
matchers?: Matcher[];
}
// Based on https://github.com/prometheus/alertmanager/blob/main/api/v2/openapi.yaml
export const alertmanagerApi = alertingApi.injectEndpoints({
endpoints: (build) => ({
getAlertmanagerAlerts: build.query<
AlertmanagerAlert[],
{ amSourceName: string; filter?: AlertmanagerAlertsFilter }
>({
query: ({ amSourceName, filter }) => {
// TODO Add support for active, silenced, inhibited, unprocessed filters
const filterMatchers = filter?.matchers
?.filter((matcher) => matcher.name && matcher.value)
.map((matcher) => `${matcher.name}${matcherToOperator(matcher)}${matcher.value}`);
const { silenced, inhibited, unprocessed, active } = filter || {};
const stateParams = Object.fromEntries(
Object.entries({ silenced, active, inhibited, unprocessed }).filter(([_, value]) => value !== undefined)
);
const params: Record<string, unknown> | undefined = { filter: filterMatchers };
if (stateParams) {
Object.keys(stateParams).forEach((key: string) => {
params[key] = stateParams[key];
});
}
return {
url: `/api/alertmanager/${getDatasourceAPIUid(amSourceName)}/api/v2/alerts`,
params,
};
},
}),
getAlertmanagerChoiceStatus: build.query<AlertmanagersChoiceResponse, void>({
query: () => ({ url: '/api/v1/ngalert' }),
providesTags: ['AlertmanagerChoice'],
}),
getExternalAlertmanagerConfig: build.query<ExternalAlertmanagerConfig, void>({
query: () => ({ url: '/api/v1/ngalert/admin_config' }),
providesTags: ['AlertmanagerChoice'],
}),
getExternalAlertmanagers: build.query<ExternalAlertmanagers, void>({
query: () => ({ url: '/api/v1/ngalert/alertmanagers' }),
transformResponse: (response: ExternalAlertmanagersResponse) => response.data,
}),
saveExternalAlertmanagersConfig: build.mutation<{ message: string }, ExternalAlertmanagerConfig>({
query: (config) => ({ url: '/api/v1/ngalert/admin_config', method: 'POST', data: config }),
invalidatesTags: ['AlertmanagerChoice'],
}),
getValidAlertManagersConfig: build.query<AlertManagerCortexConfig[], void>({
//this is only available for the "grafana" alert manager
query: () => ({
url: `/api/alertmanager/${getDatasourceAPIUid(
GRAFANA_RULES_SOURCE_NAME
)}/config/history?limit=${LIMIT_TO_SUCCESSFULLY_APPLIED_AMS}`,
}),
}),
resetAlertManagerConfigToOldVersion: build.mutation<{ message: string }, { id: number }>({
//this is only available for the "grafana" alert manager
query: (config) => ({
url: `/api/alertmanager/${getDatasourceAPIUid(GRAFANA_RULES_SOURCE_NAME)}/config/history/${
config.id
}/_activate`,
method: 'POST',
}),
}),
}),
});