Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix SmartScalar comparison labels across daylight saving changes (#82457)

Fraser08a69693f036
diff --git a/frontend/src/metabase/visualizations/visualizations/SmartScalar/compute.ts b/frontend/src/metabase/visualizations/visualizations/SmartScalar/compute.ts
index db2afde4bad..861da4abb6a 100644
--- a/frontend/src/metabase/visualizations/visualizations/SmartScalar/compute.ts
+++ b/frontend/src/metabase/visualizations/visualizations/SmartScalar/compute.ts
@@ -706,15 +706,15 @@ function computeComparisonStrPreviousValue({
prevDate: string;
nextDate: string | undefined;
}) {
- const parsedPrevDate = dayjs.parseZone(prevDate);
- const parsedNextDate = dayjs.parseZone(nextDate);
-
- const isSameDay = parsedPrevDate.isSame(parsedNextDate, "day");
- const isSameYear = parsedPrevDate.isSame(parsedNextDate, "year");
+ const prevDateTime = dayjs.parseZone(prevDate);
+ const nextDateTime = dayjs.parseZone(nextDate);
+ // isSame checks whether nextDate falls inside prevDate's day as worked out in prevDate's offset,
+ // so it gives the wrong answer when the two timestamps carry different offsets.
const options = {
- removeDay: isSameDay,
- removeYear: isSameYear,
+ removeDay:
+ prevDateTime.format("YYYY-MM-DD") === nextDateTime.format("YYYY-MM-DD"),
+ removeYear: prevDateTime.format("YYYY") === nextDateTime.format("YYYY"),
};
const formattedDateStr = formatDateStr({
diff --git a/frontend/src/metabase/visualizations/visualizations/SmartScalar/compute.unit.spec.ts b/frontend/src/metabase/visualizations/visualizations/SmartScalar/compute.unit.spec.ts
index e5170e83d16..b6d517c7b9a 100644
--- a/frontend/src/metabase/visualizations/visualizations/SmartScalar/compute.unit.spec.ts
+++ b/frontend/src/metabase/visualizations/visualizations/SmartScalar/compute.unit.spec.ts
@@ -1659,6 +1659,52 @@ describe("SmartScalar > compute", () => {
},
},
},
+ {
+ description:
+ "should remove year when previous hour and current hour are in different days with differing offsets",
+ rows: [
+ ["2024-03-10T00:00:00-05:00", 100],
+ ["2024-03-11T00:00:00-04:00", 300],
+ ],
+ dateUnit: "hour",
+ expected: {
+ ...getMetricProperties({
+ dateStr: "Mar 11, 2024, 12:00–59 AM",
+ metricValue: 300,
+ }),
+ comparison: {
+ ...getComparisonProperties({
+ changeType: "increase",
+ comparisonValue: 100,
+ dateStr: "Mar 10, 12:00–59 AM",
+ metricValue: 300,
+ }),
+ },
+ },
+ },
+ {
+ description:
+ "should remove year and day when previous hour and current hour are in the same day with differing offsets",
+ rows: [
+ ["2024-11-03T00:00:00-04:00", 100],
+ ["2024-11-03T23:00:00-05:00", 300],
+ ],
+ dateUnit: "hour",
+ expected: {
+ ...getMetricProperties({
+ dateStr: "Nov 3, 2024, 11:00–59 PM",
+ metricValue: 300,
+ }),
+ comparison: {
+ ...getComparisonProperties({
+ changeType: "increase",
+ comparisonValue: 100,
+ dateStr: "12:00–59 AM",
+ metricValue: 300,
+ }),
+ },
+ },
+ },
{
description:
"should not remove year when previous minute and current minute are in different years",
@@ -2093,6 +2139,30 @@ describe("SmartScalar > compute", () => {
},
},
},
+ {
+ description:
+ "should not remove year when comparing months in different years",
+ rows: [
+ ["2019-07-01T00:00:00+10:00", 100],
+ ["2020-01-01T00:00:00+11:00", 300],
+ ],
+ dateUnit: "month",
+ periodsAgo: 6,
+ expected: {
+ ...getMetricProperties({
+ dateStr: "Jan 2020",
+ metricValue: 300,
+ }),
+ comparison: {
+ ...getComparisonProperties({
+ changeType: "increase",
+ comparisonValue: 100,
+ dateStr: "Jul 2019",
+ metricValue: 300,
+ }),
+ },
+ },
+ },
{
description: "should handle comparisons by weeks",
rows: [