feat(time_comparison): Support all date formats when computing custom and inherit offsets (#30002)

This commit is contained in:
Antonio Rivero
2024-08-23 15:32:25 +02:00
committed by GitHub
parent ce72a0ac27
commit bc6d2dba37
10 changed files with 216 additions and 167 deletions

View File

@@ -26,7 +26,7 @@ import {
t,
useTheme,
} from '@superset-ui/core';
import { Tooltip } from '@superset-ui/chart-controls';
import { DEFAULT_DATE_PATTERN, Tooltip } from '@superset-ui/chart-controls';
import { isEmpty } from 'lodash';
import {
ColorSchemeEnum,
@@ -90,27 +90,33 @@ export default function PopKPI(props: PopKPIProps) {
if (!currentTimeRangeFilter || (!shift && !startDateOffset)) {
setComparisonRange('');
} else if (!isEmpty(shift) || startDateOffset) {
const newShift = getTimeOffset({
timeRangeFilter: {
...currentTimeRangeFilter,
comparator:
dashboardTimeRange ?? (currentTimeRangeFilter as any).comparator,
},
shifts: ensureIsArray(shift),
startDate: startDateOffset || '',
});
const promise: any = fetchTimeRange(
dashboardTimeRange ?? (currentTimeRangeFilter as any).comparator,
currentTimeRangeFilter.subject,
newShift || [],
);
Promise.resolve(promise).then((res: any) => {
const response: string[] = ensureIsArray(res.value);
const firstRange: string = response.flat()[0];
const rangeText = firstRange.split('vs\n');
setComparisonRange(
rangeText.length > 1 ? rangeText[1].trim() : rangeText[0],
);
const dates = res?.value?.match(DEFAULT_DATE_PATTERN);
const [parsedStartDate, parsedEndDate] = dates ?? [];
const newShift = getTimeOffset({
timeRangeFilter: {
...currentTimeRangeFilter,
comparator: `${parsedStartDate} : ${parsedEndDate}`,
},
shifts: ensureIsArray(shift),
startDate: startDateOffset || '',
});
fetchTimeRange(
dashboardTimeRange ?? (currentTimeRangeFilter as any).comparator,
currentTimeRangeFilter.subject,
ensureIsArray(newShift),
).then(res => {
const response: string[] = ensureIsArray(res.value);
const firstRange: string = response.flat()[0];
const rangeText = firstRange.split('vs\n');
setComparisonRange(
rangeText.length > 1 ? rangeText[1].trim() : rangeText[0],
);
});
});
}
}, [currentTimeRangeFilter, shift, startDateOffset, dashboardTimeRange]);

View File

@@ -21,9 +21,6 @@ import {
QueryFormData,
PostProcessingRule,
ensureIsArray,
SimpleAdhocFilter,
getTimeOffset,
parseDttmToDate,
} from '@superset-ui/core';
import {
isTimeComparison,
@@ -37,43 +34,30 @@ export default function buildQuery(formData: QueryFormData) {
const queryContextA = buildQueryContext(formData, baseQueryObject => {
const postProcessing: PostProcessingRule[] = [];
postProcessing.push(timeCompareOperator(formData, baseQueryObject));
const TimeRangeFilters =
formData.adhoc_filters?.filter(
(filter: SimpleAdhocFilter) => filter.operator === 'TEMPORAL_RANGE',
) || [];
// In case the viz is using all version of controls, we try to load them
const previousCustomTimeRangeFilters: any =
formData.adhoc_custom?.filter(
(filter: SimpleAdhocFilter) => filter.operator === 'TEMPORAL_RANGE',
) || [];
const nonCustomNorInheritShifts = ensureIsArray(
formData.time_compare,
).filter((shift: string) => shift !== 'custom' && shift !== 'inherit');
const customOrInheritShifts = ensureIsArray(formData.time_compare).filter(
(shift: string) => shift === 'custom' || shift === 'inherit',
);
let previousCustomStartDate = '';
if (
!isEmpty(previousCustomTimeRangeFilters) &&
previousCustomTimeRangeFilters[0]?.comparator !== 'No Filter'
) {
previousCustomStartDate =
previousCustomTimeRangeFilters[0]?.comparator.split(' : ')[0];
let timeOffsets: string[] = [];
// Shifts for non-custom or non inherit time comparison
if (!isEmpty(nonCustomNorInheritShifts)) {
timeOffsets = nonCustomNorInheritShifts;
}
const timeOffsets = ensureIsArray(
isTimeComparison(formData, baseQueryObject)
? getTimeOffset({
timeRangeFilter: {
...TimeRangeFilters[0],
comparator:
baseQueryObject?.time_range ??
(TimeRangeFilters[0] as any)?.comparator,
},
shifts: formData.time_compare,
startDate:
previousCustomStartDate && !formData.start_date_offset
? parseDttmToDate(previousCustomStartDate)?.toUTCString()
: formData.start_date_offset,
})
: [],
);
// Shifts for custom or inherit time comparison
if (!isEmpty(customOrInheritShifts)) {
if (customOrInheritShifts.includes('custom')) {
timeOffsets = timeOffsets.concat([formData.start_date_offset]);
}
if (customOrInheritShifts.includes('inherit')) {
timeOffsets = timeOffsets.concat(['inherit']);
}
}
return [
{
...baseQueryObject,

View File

@@ -24,10 +24,7 @@ import {
getNumberFormatter,
SimpleAdhocFilter,
ensureIsArray,
getTimeOffset,
parseDttmToDate,
} from '@superset-ui/core';
import { isEmpty } from 'lodash';
import { getComparisonFontSize, getHeaderFontSize } from './utils';
export const parseMetricValue = (metricValue: number | string | null) => {
@@ -99,37 +96,16 @@ export default function transformProps(chartProps: ChartProps) {
(adhoc_filter: SimpleAdhocFilter) =>
adhoc_filter.operator === 'TEMPORAL_RANGE',
)?.[0];
// In case the viz is using all version of controls, we try to load them
const previousCustomTimeRangeFilters: any =
chartProps.rawFormData?.adhoc_custom?.filter(
(filter: SimpleAdhocFilter) => filter.operator === 'TEMPORAL_RANGE',
) || [];
let previousCustomStartDate = '';
if (
!isEmpty(previousCustomTimeRangeFilters) &&
previousCustomTimeRangeFilters[0]?.comparator !== 'No Filter'
) {
previousCustomStartDate =
previousCustomTimeRangeFilters[0]?.comparator.split(' : ')[0];
}
const isCustomOrInherit =
timeComparison === 'custom' || timeComparison === 'inherit';
let dataOffset: string[] = [];
if (isCustomOrInherit) {
dataOffset = getTimeOffset({
timeRangeFilter: {
...currentTimeRangeFilter,
comparator:
formData?.extraFormData?.time_range ??
(currentTimeRangeFilter as any)?.comparator,
},
shifts: ensureIsArray(timeComparison),
startDate:
previousCustomStartDate && !startDateOffset
? parseDttmToDate(previousCustomStartDate)?.toUTCString()
: startDateOffset,
});
if (timeComparison && timeComparison === 'custom') {
dataOffset = [startDateOffset];
} else {
dataOffset = ensureIsArray(timeComparison) || [];
}
}
const { value1, value2 } = data.reduce(