Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PMM-13265 Fix custom fields for templated alerts #765

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ export interface TemplatesList extends Omit<TemplatesListAPI, 'templates'> {
}

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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<SelectableValue<keyof typeof Severity>> = [
{
Expand Down Expand Up @@ -40,3 +42,9 @@ export const SEVERITY_OPTIONS: Array<SelectableValue<keyof typeof Severity>> = [
// 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<TemplateParamType, 'bool' | 'float' | 'string'> = {
PARAM_TYPE_BOOL: 'bool',
PARAM_TYPE_FLOAT: 'float',
PARAM_TYPE_STRING: 'string',
};
Original file line number Diff line number Diff line change
Expand Up @@ -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<SelectableValue<string>> =>
channels
? channels.map((channel) => ({
Expand Down Expand Up @@ -48,7 +50,7 @@ export const formatCreateAPIPayload = (data: TemplatedAlertFormValues): AlertRul
payload.params?.push({
name,
type,
[type.toLowerCase()]: value,
[TYPE_TO_KEY_MAP[type]]: value,
});
}
});
Expand Down
3 changes: 2 additions & 1 deletion public/app/percona/shared/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export enum Severity {
import {
TemplateAnnotation,
TemplateParam,
TemplateParamType,
} from 'app/percona/integrated-alerting/components/AlertRuleTemplate/AlertRuleTemplate.types';

export enum AlertRuleParamType {
Expand Down Expand Up @@ -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;
Expand Down
Loading