Files
Sonia Aguilar 2014d27def Alerting: Add alert rule version history - part1 (#99490)
* Add alertingRuleVersionHistory feature toggle

* WIP: Add version history tab

* revert temp change in index.ts

* wip2

* --wip--

* sync code with the BE changes in the endpoint

* add translations

* Add translations

* use ff only for restore feature

* WIP: Add tracking, make version required, and start mapping dif results
Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>

* Tweak more translations and improve types

* Add button to show/hide JSON diff

* update type for top level rule fields

* Create types

* Make updated_by/version properties optional

* Update mocks to remove updated by and version

* add comments to restore code

* rename fetature flag, as we use this one only for the restore feature

* Update version history to handle special cases

* Add diff numbers

* Fix conflicts

* Move generic computeVersionDiff to a utils file

* Update DOM structure of version summary and tidy up types

* Add tests for version comparison logic

* Lint fix utils file

* Rename props and add docs

* Change to EmptyState and log when no versions

* Remove CreatedBy component and simplify

* Add missing i18n for version history

* add test for computeVersionDiff

* update test

* fix number diff order and add a test

* fix prettier

* fix prettier

* Add promise resolve back in

* Rename to humanReadableDiff and tweak translation

* Show tab for recording rules as well

* Split components out to separate files

* Add optional interval seconds

* Update i18n

* Remove commented code

* Remove value

* Remove unneeded version

* Consistent rendering of updated by

* Mode parseVersionInfo to a separate pure function

* update invalidate/provide tags for getAlertVersionHistory

* Use checkedVersions state only in the parent component

* update getSpecialUidMap name and create an interface

* Fix prettier

* update tab description

* use set instead of map for checkedVersions

---------

Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2025-02-17 13:25:32 +01:00

133 lines
3.1 KiB
TypeScript

import { DataSourceJsonData, PluginMeta } from '@grafana/data';
import { ExpressionDatasourceUID } from 'app/features/expressions/types';
import { CombinedRule } from 'app/types/unified-alerting';
import { GrafanaAlertStateDecision } from 'app/types/unified-alerting-dto';
import { GRAFANA_RULES_SOURCE_NAME } from './datasource';
import { alertRuleToQueries } from './query';
describe('alertRuleToQueries', () => {
it('it should convert grafana alert', () => {
const combinedRule: CombinedRule = {
name: 'Test alert',
query: 'up',
labels: {},
annotations: {},
group: {
name: 'Prom up alert',
rules: [],
totals: {},
},
namespace: {
rulesSource: GRAFANA_RULES_SOURCE_NAME,
name: 'Alerts',
groups: [],
},
rulerRule: {
for: '',
annotations: {},
labels: {},
grafana_alert: grafanaAlert,
},
instanceTotals: {},
filteredInstanceTotals: {},
};
const result = alertRuleToQueries(combinedRule);
expect(result).toEqual(grafanaAlert.data);
});
it('shoulds convert cloud alert', () => {
const combinedRule: CombinedRule = {
name: 'cloud test',
labels: {},
query: 'up == 0',
annotations: {},
group: {
name: 'test',
rules: [],
totals: {},
},
namespace: {
name: 'prom test alerts',
groups: [],
rulesSource: {
name: 'prom test',
type: 'prometheus',
uid: 'asdf23',
id: 1,
access: 'proxy',
meta: {} as PluginMeta,
jsonData: {} as DataSourceJsonData,
readOnly: false,
},
},
instanceTotals: {},
filteredInstanceTotals: {},
};
const result = alertRuleToQueries(combinedRule);
expect(result).toEqual([
{
refId: 'A',
datasourceUid: 'asdf23',
queryType: '',
model: {
refId: 'A',
expr: 'up == 0',
},
relativeTimeRange: {
from: 360,
to: 0,
},
},
]);
});
});
const grafanaAlert = {
condition: 'B',
exec_err_state: GrafanaAlertStateDecision.Alerting,
namespace_uid: 'namespaceuid123',
rule_group: 'my-group',
no_data_state: GrafanaAlertStateDecision.NoData,
title: 'Test alert',
uid: 'asdf23',
version: 1,
data: [
{
refId: 'A',
queryType: '',
relativeTimeRange: { from: 600, to: 0 },
datasourceUid: 'asdf51',
model: {
expr: 'up',
refId: 'A',
},
},
{
refId: 'B',
queryType: '',
relativeTimeRange: { from: 0, to: 0 },
datasourceUid: '__expr__',
model: {
conditions: [
{
evaluator: { params: [1], type: 'lt' },
operator: { type: 'and' },
query: { params: ['A'] },
reducer: { params: [], type: 'last' },
type: 'query',
},
],
datasource: {
uid: ExpressionDatasourceUID,
},
hide: false,
refId: 'B',
type: 'classic_conditions',
},
},
],
};