From e8dfb9869673897c4bd9bce37120c0abacac9985 Mon Sep 17 00:00:00 2001 From: hackerstanislav Date: Thu, 14 Nov 2024 08:49:58 +0100 Subject: [PATCH] fix: repairs related to operator and comparison risk: low JIRA: F1-911 --- .../AlertComparisonPeriodSelect.tsx | 6 ++-- .../InsightAlertConfig/utils/items.ts | 30 ++++++++++--------- .../utils/transformation.ts | 2 +- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/libs/sdk-ui-dashboard/src/presentation/widget/insight/configuration/InsightAlertConfig/AlertComparisonPeriodSelect.tsx b/libs/sdk-ui-dashboard/src/presentation/widget/insight/configuration/InsightAlertConfig/AlertComparisonPeriodSelect.tsx index 061ad6286b7..6eecffe3fe5 100644 --- a/libs/sdk-ui-dashboard/src/presentation/widget/insight/configuration/InsightAlertConfig/AlertComparisonPeriodSelect.tsx +++ b/libs/sdk-ui-dashboard/src/presentation/widget/insight/configuration/InsightAlertConfig/AlertComparisonPeriodSelect.tsx @@ -1,6 +1,6 @@ // (C) 2022-2024 GoodData Corporation import React, { useMemo } from "react"; -import { IAutomationMetadataObject } from "@gooddata/sdk-model"; +import { DateGranularity, IAutomationMetadataObject } from "@gooddata/sdk-model"; import { Button, Dropdown, List, OverlayPositionType, SingleSelectListItem } from "@gooddata/sdk-ui-kit"; import { FormattedMessage, useIntl } from "react-intl"; import cx from "classnames"; @@ -44,7 +44,7 @@ export const AlertComparisonPeriodSelect = (props: IAlertComparisonPeriodSelectP ); return [ - sp?.granularity + sp?.granularity && pp?.granularity !== DateGranularity.year ? { title: intl.formatMessage( { id: "insightAlert.config.compare_with_sp_granularity" }, @@ -151,7 +151,7 @@ const DropdownButtonLabel = (props: DropdownButtonLabelProps) => { return (
{" "} - {selectedOperator.granularity ? ( + {selectedOperator.granularity && selectedOperator.granularity !== DateGranularity.year ? ( { - const prev = metric.comparators.find( + const previousPeriod = metric.comparators.find( (c) => c.comparator === AlertMetricComparatorType.PreviousPeriod, ); - if (!prev) { + if (!previousPeriod) { //PP metric.comparators.push({ measure: newPreviousPeriodMeasure( @@ -452,7 +452,12 @@ function fillComparators(simpleMetrics: AlertMetric[], datasets: ICatalogDateDat dataset: undefined, granularity: undefined, }); + } + const samePeriodPrevYear = metric.comparators.find( + (c) => c.comparator === AlertMetricComparatorType.SamePeriodPreviousYear, + ); + if (!samePeriodPrevYear) { //PoP metric.comparators.push({ measure: newPopMeasure( @@ -483,14 +488,11 @@ function fillGranularity(simpleMetrics: AlertMetric[], datasets: ICatalogDateDat const dataset = datasets.find((d) => { return d.dateAttributes.some((a) => areObjRefsEqual(a.attribute.ref, attr)); }); - const dateAttribute = dataset?.dateAttributes.find((a) => - areObjRefsEqual(a.attribute.ref, attr), - ); if (dataset) { + const lowest = sortDateAttributes(dataset)[0]; comparator.dataset = dataset.dataSet; - //comparator.dateAttribute = dateAttribute?.attribute; - comparator.granularity = dateAttribute?.granularity; + comparator.granularity = lowest?.granularity; } } if (isPreviousPeriodMeasureDefinition(def)) { @@ -501,13 +503,7 @@ function fillGranularity(simpleMetrics: AlertMetric[], datasets: ICatalogDateDat }); if (dataset) { - const sorted = dataset.dateAttributes.slice().sort((a, b) => { - return ( - SortedGranularities.indexOf(b.granularity) - - SortedGranularities.indexOf(a.granularity) - ); - }); - const lowest = sorted[0]; + const lowest = sortDateAttributes(dataset)[0]; comparator.dataset = dataset.dataSet; comparator.granularity = lowest?.granularity; } @@ -516,6 +512,12 @@ function fillGranularity(simpleMetrics: AlertMetric[], datasets: ICatalogDateDat }); } +function sortDateAttributes(dataset: ICatalogDateDataset) { + return dataset.dateAttributes.slice().sort((a, b) => { + return SortedGranularities.indexOf(b.granularity) - SortedGranularities.indexOf(a.granularity); + }); +} + function removeInvalidComparators(simpleMetrics: AlertMetric[], insightFilters: IFilter[]) { const dateFilters = insightFilters.filter((filter) => isRelativeDateFilter(filter), diff --git a/libs/sdk-ui-dashboard/src/presentation/widget/insight/configuration/InsightAlertConfig/utils/transformation.ts b/libs/sdk-ui-dashboard/src/presentation/widget/insight/configuration/InsightAlertConfig/utils/transformation.ts index 8b2ef65eaa5..ff1dcf564a0 100644 --- a/libs/sdk-ui-dashboard/src/presentation/widget/insight/configuration/InsightAlertConfig/utils/transformation.ts +++ b/libs/sdk-ui-dashboard/src/presentation/widget/insight/configuration/InsightAlertConfig/utils/transformation.ts @@ -234,7 +234,7 @@ export function transformAlertByRelativeOperator( comparatorType?: AlertMetricComparatorType, ): IAutomationMetadataObject { const periodMeasure = measure.comparators.filter((c) => - comparatorType ? c.comparator === comparatorType : true, + comparatorType !== undefined ? c.comparator === comparatorType : true, ); const cond = transformToRelativeCondition(alert.alert!.condition);