diff --git a/ee/query-service/rules/anomaly.go b/ee/query-service/rules/anomaly.go index 53f205e8d004..af988b5d6777 100644 --- a/ee/query-service/rules/anomaly.go +++ b/ee/query-service/rules/anomaly.go @@ -251,7 +251,7 @@ func (r *AnomalyRule) buildAndRunQuery(ctx context.Context, orgID valuer.UUID, t continue } } - results, err := r.Threshold.ShouldAlert(*series) + results, err := r.Threshold.ShouldAlert(*series, r.Unit()) if err != nil { return nil, err } @@ -301,7 +301,7 @@ func (r *AnomalyRule) buildAndRunQueryV5(ctx context.Context, orgID valuer.UUID, continue } } - results, err := r.Threshold.ShouldAlert(*series) + results, err := r.Threshold.ShouldAlert(*series, r.Unit()) if err != nil { return nil, err } @@ -336,14 +336,19 @@ func (r *AnomalyRule) Eval(ctx context.Context, ts time.Time) (interface{}, erro resultFPs := map[uint64]struct{}{} var alerts = make(map[uint64]*ruletypes.Alert, len(res)) + ruleReceivers := r.Threshold.GetRuleReceivers() + ruleReceiverMap := make(map[string][]string) + for _, value := range ruleReceivers { + ruleReceiverMap[value.Name] = value.Channels + } + for _, smpl := range res { l := make(map[string]string, len(smpl.Metric)) for _, lbl := range smpl.Metric { l[lbl.Name] = lbl.Value } - value := valueFormatter.Format(smpl.V, r.Unit()) - threshold := valueFormatter.Format(r.TargetVal(), r.Unit()) + threshold := valueFormatter.Format(smpl.Target, smpl.TargetUnit) r.logger.DebugContext(ctx, "Alert template data for rule", "rule_name", r.Name(), "formatter", valueFormatter.Name(), "value", value, "threshold", threshold) tmplData := ruletypes.AlertTemplateData(l, value, threshold) @@ -408,13 +413,12 @@ func (r *AnomalyRule) Eval(ctx context.Context, ts time.Time) (interface{}, erro State: model.StatePending, Value: smpl.V, GeneratorURL: r.GeneratorURL(), - Receivers: r.PreferredChannels(), + Receivers: ruleReceiverMap[lbs.Map()[ruletypes.LabelThresholdName]], Missing: smpl.IsMissing, } } r.logger.InfoContext(ctx, "number of alerts found", "rule_name", r.Name(), "alerts_count", len(alerts)) - // alerts[h] is ready, add or update active list now for h, a := range alerts { // Check whether we already have alerting state for the identifying label set. @@ -423,7 +427,9 @@ func (r *AnomalyRule) Eval(ctx context.Context, ts time.Time) (interface{}, erro alert.Value = a.Value alert.Annotations = a.Annotations - alert.Receivers = r.PreferredChannels() + if v, ok := alert.Labels.Map()[ruletypes.LabelThresholdName]; ok { + alert.Receivers = ruleReceiverMap[v] + } continue } diff --git a/ee/query-service/rules/manager.go b/ee/query-service/rules/manager.go index 3212031f9f3f..31009b3c3091 100644 --- a/ee/query-service/rules/manager.go +++ b/ee/query-service/rules/manager.go @@ -126,7 +126,6 @@ func TestNotification(opts baserules.PrepareTestRuleOptions) (int, *basemodel.Ap if parsedRule.RuleType == ruletypes.RuleTypeThreshold { // add special labels for test alerts - parsedRule.Annotations[labels.AlertSummaryLabel] = fmt.Sprintf("The rule threshold is set to %.4f, and the observed metric value is {{$value}}.", *parsedRule.RuleCondition.Target) parsedRule.Labels[labels.RuleSourceLabel] = "" parsedRule.Labels[labels.AlertRuleIdLabel] = "" diff --git a/frontend/src/container/CreateAlertV2/AlertCondition/AlertCondition.tsx b/frontend/src/container/CreateAlertV2/AlertCondition/AlertCondition.tsx index 174af6e93de2..e25142deb482 100644 --- a/frontend/src/container/CreateAlertV2/AlertCondition/AlertCondition.tsx +++ b/frontend/src/container/CreateAlertV2/AlertCondition/AlertCondition.tsx @@ -13,14 +13,12 @@ import APIError from 'types/api/error'; import { useCreateAlertState } from '../context'; import AdvancedOptions from '../EvaluationSettings/AdvancedOptions'; import Stepper from '../Stepper'; -import { showCondensedLayout } from '../utils'; import AlertThreshold from './AlertThreshold'; import AnomalyThreshold from './AnomalyThreshold'; import { ANOMALY_TAB_TOOLTIP, THRESHOLD_TAB_TOOLTIP } from './constants'; function AlertCondition(): JSX.Element { const { alertType, setAlertType } = useCreateAlertState(); - const showCondensedLayoutFlag = showCondensedLayout(); const { data, @@ -108,11 +106,9 @@ function AlertCondition(): JSX.Element { refreshChannels={refreshChannels} /> )} - {showCondensedLayoutFlag ? ( -
- -
- ) : null} +
+ +
); } diff --git a/frontend/src/container/CreateAlertV2/AlertCondition/AlertThreshold.tsx b/frontend/src/container/CreateAlertV2/AlertCondition/AlertThreshold.tsx index c67eebf77490..95663daa518c 100644 --- a/frontend/src/container/CreateAlertV2/AlertCondition/AlertThreshold.tsx +++ b/frontend/src/container/CreateAlertV2/AlertCondition/AlertThreshold.tsx @@ -4,8 +4,10 @@ import '../EvaluationSettings/styles.scss'; import { Button, Select, Tooltip, Typography } from 'antd'; import classNames from 'classnames'; import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder'; +import getRandomColor from 'lib/getRandomColor'; import { Plus } from 'lucide-react'; import { useEffect } from 'react'; +import { v4 } from 'uuid'; import { useCreateAlertState } from '../context'; import { @@ -16,7 +18,6 @@ import { THRESHOLD_OPERATOR_OPTIONS, } from '../context/constants'; import EvaluationSettings from '../EvaluationSettings/EvaluationSettings'; -import { showCondensedLayout } from '../utils'; import ThresholdItem from './ThresholdItem'; import { AnomalyAndThresholdProps, UpdateThreshold } from './types'; import { @@ -41,8 +42,6 @@ function AlertThreshold({ setNotificationSettings, } = useCreateAlertState(); - const showCondensedLayoutFlag = showCondensedLayout(); - const { currentQuery } = useQueryBuilder(); const queryNames = getQueryNames(currentQuery); @@ -68,11 +67,15 @@ function AlertThreshold({ const addThreshold = (): void => { let newThreshold; if (thresholdState.thresholds.length === 1) { - newThreshold = INITIAL_WARNING_THRESHOLD; + newThreshold = { ...INITIAL_WARNING_THRESHOLD, id: v4() }; } else if (thresholdState.thresholds.length === 2) { - newThreshold = INITIAL_INFO_THRESHOLD; + newThreshold = { ...INITIAL_INFO_THRESHOLD, id: v4() }; } else { - newThreshold = INITIAL_RANDOM_THRESHOLD; + newThreshold = { + ...INITIAL_RANDOM_THRESHOLD, + id: v4(), + color: getRandomColor(), + }; } setThresholdState({ type: 'SET_THRESHOLDS', @@ -157,17 +160,12 @@ function AlertThreshold({ }), ); - const evaluationWindowContext = showCondensedLayoutFlag ? ( - - ) : ( - Evaluation Window. - ); - return (
{/* Main condition sentence */}
@@ -213,7 +211,7 @@ function AlertThreshold({ options={matchTypeOptionsWithTooltips} /> - during the {evaluationWindowContext} + during the
diff --git a/frontend/src/container/CreateAlertV2/AlertCondition/__tests__/AlertThreshold.test.tsx b/frontend/src/container/CreateAlertV2/AlertCondition/__tests__/AlertThreshold.test.tsx index ab91089e205a..468d67b8a840 100644 --- a/frontend/src/container/CreateAlertV2/AlertCondition/__tests__/AlertThreshold.test.tsx +++ b/frontend/src/container/CreateAlertV2/AlertCondition/__tests__/AlertThreshold.test.tsx @@ -110,7 +110,6 @@ jest.mock('container/NewWidget/RightContainer/alertFomatCategories', () => ({ jest.mock('container/CreateAlertV2/utils', () => ({ ...jest.requireActual('container/CreateAlertV2/utils'), - showCondensedLayout: jest.fn().mockReturnValue(false), })); const TEST_STRINGS = { @@ -159,7 +158,9 @@ describe('AlertThreshold', () => { expect(screen.getByText('Send a notification when')).toBeInTheDocument(); expect(screen.getByText('the threshold(s)')).toBeInTheDocument(); expect(screen.getByText('during the')).toBeInTheDocument(); - expect(screen.getByText('Evaluation Window.')).toBeInTheDocument(); + expect( + screen.getByTestId('condensed-evaluation-settings-container'), + ).toBeInTheDocument(); }); it('renders query selection dropdown', async () => { diff --git a/frontend/src/container/CreateAlertV2/CreateAlertV2.tsx b/frontend/src/container/CreateAlertV2/CreateAlertV2.tsx index dc60cedb7c93..e4f87d95e481 100644 --- a/frontend/src/container/CreateAlertV2/CreateAlertV2.tsx +++ b/frontend/src/container/CreateAlertV2/CreateAlertV2.tsx @@ -8,12 +8,11 @@ import AlertCondition from './AlertCondition'; import { CreateAlertProvider } from './context'; import { buildInitialAlertDef } from './context/utils'; import CreateAlertHeader from './CreateAlertHeader'; -import EvaluationSettings from './EvaluationSettings'; import Footer from './Footer'; import NotificationSettings from './NotificationSettings'; import QuerySection from './QuerySection'; import { CreateAlertV2Props } from './types'; -import { showCondensedLayout, Spinner } from './utils'; +import { Spinner } from './utils'; function CreateAlertV2({ alertType }: CreateAlertV2Props): JSX.Element { const queryToRedirect = buildInitialAlertDef(alertType); @@ -23,8 +22,6 @@ function CreateAlertV2({ alertType }: CreateAlertV2Props): JSX.Element { useShareBuilderUrl({ defaultValue: currentQueryToRedirect }); - const showCondensedLayoutFlag = showCondensedLayout(); - return ( @@ -32,7 +29,6 @@ function CreateAlertV2({ alertType }: CreateAlertV2Props): JSX.Element { - {!showCondensedLayoutFlag ? : null}