From 89a0db38473c5ca38455f781db1cf3dc1c21771b Mon Sep 17 00:00:00 2001 From: Matej Kubinec <32638572+matejkubinec@users.noreply.github.com> Date: Mon, 30 Sep 2024 09:48:08 +0200 Subject: [PATCH] PMM-13265 Fix custom fields for templated alerts (#765) * PMM-13265 Fix custom fields for templated alerts * PMM-13265 Add newline to betterer --- .../AlertRuleTemplate.types.ts | 22 +++++++++---------- .../AlertRuleTemplate.utils.test.ts | 4 ++-- .../TemplateForm/TemplateForm.constants.ts | 8 +++++++ .../TemplateForm/TemplateForm.utils.ts | 4 +++- public/app/percona/shared/core/types.ts | 3 ++- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/public/app/percona/integrated-alerting/components/AlertRuleTemplate/AlertRuleTemplate.types.ts b/public/app/percona/integrated-alerting/components/AlertRuleTemplate/AlertRuleTemplate.types.ts index fa94c0b279f9f..a8cd25a480947 100644 --- a/public/app/percona/integrated-alerting/components/AlertRuleTemplate/AlertRuleTemplate.types.ts +++ b/public/app/percona/integrated-alerting/components/AlertRuleTemplate/AlertRuleTemplate.types.ts @@ -35,23 +35,23 @@ export interface TemplatesList extends Omit { } export enum SourceDescription { - BUILT_IN = 'BUILT_IN', - SAAS = 'SAAS', - USER_FILE = 'USER_FILE', - USER_API = 'USER_API', + BUILT_IN = 'TEMPLATE_SOURCE_BUILT_IN', + SAAS = 'TEMPLATE_SOURCE_SAAS', + USER_FILE = 'TEMPLATE_SOURCE_USER_FILE', + USER_API = 'TEMPLATE_SOURCE_USER_API', } -// https://github.com/percona-platform/saas/blob/main/pkg/alert/type.go +// https://github.com/percona/saas/blob/main/pkg/alert/type.go export enum TemplateParamType { - FLOAT = 'FLOAT', - BOOL = 'BOOL', - STRING = 'STRING', + FLOAT = 'PARAM_TYPE_FLOAT', + BOOL = 'PARAM_TYPE_BOOL', + STRING = 'PARAM_TYPE_STRING', } -// https://github.com/percona-platform/saas/blob/main/pkg/alert/unit.go +// https://github.com/percona/saas/blob/main/pkg/alert/unit.go export enum TemplateParamUnit { - PERCENTAGE = 'PERCENTAGE', - SECONDS = 'SECONDS', + PERCENTAGE = 'PARAM_UNIT_PERCENTAGE', + SECONDS = 'PARAM_UNIT_SECONDS', } export interface TemplateFloatParamAPI { diff --git a/public/app/percona/integrated-alerting/components/AlertRuleTemplate/AlertRuleTemplate.utils.test.ts b/public/app/percona/integrated-alerting/components/AlertRuleTemplate/AlertRuleTemplate.utils.test.ts index cdcc8668e4fed..0206005bed674 100644 --- a/public/app/percona/integrated-alerting/components/AlertRuleTemplate/AlertRuleTemplate.utils.test.ts +++ b/public/app/percona/integrated-alerting/components/AlertRuleTemplate/AlertRuleTemplate.utils.test.ts @@ -50,7 +50,7 @@ describe('AlertRuleTemplatesTable utils', () => { }); test('formatSource', () => { - expect(formatSource(SourceDescription.BUILT_IN)).toBe(SOURCE_MAP.BUILT_IN); - expect(formatSource(SourceDescription.SAAS)).toBe(SOURCE_MAP.SAAS); + expect(formatSource(SourceDescription.BUILT_IN)).toBe(SOURCE_MAP.TEMPLATE_SOURCE_BUILT_IN); + expect(formatSource(SourceDescription.SAAS)).toBe(SOURCE_MAP.TEMPLATE_SOURCE_SAAS); }); }); diff --git a/public/app/percona/integrated-alerting/components/TemplateForm/TemplateForm.constants.ts b/public/app/percona/integrated-alerting/components/TemplateForm/TemplateForm.constants.ts index 91f164485e280..baab9857c42d1 100644 --- a/public/app/percona/integrated-alerting/components/TemplateForm/TemplateForm.constants.ts +++ b/public/app/percona/integrated-alerting/components/TemplateForm/TemplateForm.constants.ts @@ -1,6 +1,8 @@ import { SelectableValue } from '@grafana/data'; import { Severity } from 'app/percona/shared/core'; +import { TemplateParamType } from '../AlertRuleTemplate/AlertRuleTemplate.types'; + // TODO: generate SEVERITY_OPTIONS from its type definitions export const SEVERITY_OPTIONS: Array> = [ { @@ -40,3 +42,9 @@ export const SEVERITY_OPTIONS: Array> = [ // We define our default evaluation interval as 60s // 'for' can't be less than that, hence this minimum export const MINIMUM_DURATION_VALUE = 60; + +export const TYPE_TO_KEY_MAP: Record = { + PARAM_TYPE_BOOL: 'bool', + PARAM_TYPE_FLOAT: 'float', + PARAM_TYPE_STRING: 'string', +}; diff --git a/public/app/percona/integrated-alerting/components/TemplateForm/TemplateForm.utils.ts b/public/app/percona/integrated-alerting/components/TemplateForm/TemplateForm.utils.ts index 800a5d4de321d..fc26a6eb2b764 100644 --- a/public/app/percona/integrated-alerting/components/TemplateForm/TemplateForm.utils.ts +++ b/public/app/percona/integrated-alerting/components/TemplateForm/TemplateForm.utils.ts @@ -4,6 +4,8 @@ import { AlertRuleCreatePayload, AlertRulesListResponseChannel, Severity } from import { TemplatedAlertFormValues } from '../../types'; +import { TYPE_TO_KEY_MAP } from './TemplateForm.constants'; + export const formatChannelsOptions = (channels: string[]): Array> => channels ? channels.map((channel) => ({ @@ -48,7 +50,7 @@ export const formatCreateAPIPayload = (data: TemplatedAlertFormValues): AlertRul payload.params?.push({ name, type, - [type.toLowerCase()]: value, + [TYPE_TO_KEY_MAP[type]]: value, }); } }); diff --git a/public/app/percona/shared/core/types.ts b/public/app/percona/shared/core/types.ts index 24ab48478f9f7..e9f849c306982 100644 --- a/public/app/percona/shared/core/types.ts +++ b/public/app/percona/shared/core/types.ts @@ -95,6 +95,7 @@ export enum Severity { import { TemplateAnnotation, TemplateParam, + TemplateParamType, } from 'app/percona/integrated-alerting/components/AlertRuleTemplate/AlertRuleTemplate.types'; export enum AlertRuleParamType { @@ -159,7 +160,7 @@ export interface AlertRulesListPayloadFilter { export interface AlertRulesListResponseParam { name: string; - type: keyof typeof AlertRuleParamType; + type: TemplateParamType; [AlertRuleParamType.BOOL]?: boolean; [AlertRuleParamType.FLOAT]?: number; [AlertRuleParamType.STRING]?: string;