Skip to content

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

Fix some events not shown on charts when grouping with different units (#64801)

Mahatthana (Kelvin) Nomsawadi205bd075a9fa
diff --git a/e2e/test/scenarios/organization/timelines-question.cy.spec.js b/e2e/test/scenarios/organization/timelines-question.cy.spec.js
index f90526dd923..c55cdd8a5b5 100644
--- a/e2e/test/scenarios/organization/timelines-question.cy.spec.js
+++ b/e2e/test/scenarios/organization/timelines-question.cy.spec.js
@@ -336,7 +336,7 @@ describe("scenarios > organization > timelines > question", () => {
// should show a newly created event
cy.button("Create event").click();
cy.findByLabelText("Event name").type("RC2");
- cy.findByLabelText("Date").type("10/20/2023");
+ cy.findByLabelText("Date").clear().type("10/20/2023");
cy.button("Create").click();
cy.wait("@createEvent");
@@ -394,6 +394,32 @@ describe("scenarios > organization > timelines > question", () => {
toggleEventVisibility("TC1");
H.echartsIcon("warning").should("not.exist");
+
+ /**
+ * This tests the case where group by unit with the event in the end range bucket
+ * not included when it's the only event selected.
+ *
+ * e.g. group by month from 2024-01-01 to 2025-01-01 with event at 2025-01-15,
+ * it should be included, but was not previously
+ */
+ cy.log(
+ "test single event after the starting end range unit (metabase#56290)",
+ );
+ cy.findByText("Create event").click();
+
+ H.modal().within(() => {
+ cy.findByLabelText("Event name").type("Event at the end of range");
+ cy.findByLabelText("Date").clear().type("10/20/2026");
+
+ cy.button("Create").click();
+ });
+
+ cy.log("remove all other events except the new one");
+ toggleEventVisibility("RC1").should("not.be.checked");
+ toggleEventVisibility("RC2").should("not.be.checked");
+
+ cy.log("the new event should be visible in the chart");
+ H.echartsIcon("star").should("be.visible");
});
});
@@ -557,7 +583,7 @@ function timelineEventCard(eventName) {
}
function toggleEventVisibility(eventName) {
- timelineEventVisibility(eventName).click();
+ return timelineEventVisibility(eventName).click();
}
function timelineEventVisibility(eventName) {
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/timeline-events/model.ts b/frontend/src/metabase/visualizations/echarts/cartesian/timeline-events/model.ts
index 29f16b8ebee..c450e1d7637 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/timeline-events/model.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/timeline-events/model.ts
@@ -1,5 +1,6 @@
import type { OpUnitType } from "dayjs";
import dayjs from "dayjs";
+import type { SupportedUnit } from "types/dayjs";
import _ from "underscore";
import { CHART_STYLE } from "metabase/visualizations/echarts/cartesian/constants/style";
@@ -29,7 +30,7 @@ const getIntervalWidth = (
const groupEventsByUnitStart = (
events: TimelineEvent[],
- unit: string = "day",
+ unit: SupportedUnit = "day",
): TimelineEventGroup[] => {
const groupedEvents = events.reduce<Map<string, TimelineEvent[]>>(
(acc, event) => {
@@ -128,13 +129,12 @@ export const mergeOverlappingTimelineEventGroups = (
const getTimelineEventsInsideRange = (
timelineEvents: TimelineEvent[],
range: DateRange,
+ unit: SupportedUnit,
) => {
const [min, max] = range;
+
return timelineEvents.filter((event) => {
- return (
- (min.isSame(event.timestamp) || min.isBefore(event.timestamp)) &&
- (max.isSame(event.timestamp) || max.isAfter(event.timestamp))
- );
+ return dayjs(event.timestamp).isBetween(min, max, unit, "[]");
});
};
@@ -156,6 +156,7 @@ export const getTimelineEventsModel = (
const visibleTimelineEvents = getTimelineEventsInsideRange(
timelineEvents,
dimensionRange,
+ chartModel.xAxisModel.interval.unit,
);
const hasTimelineEvents = visibleTimelineEvents.length !== 0;
@@ -164,7 +165,7 @@ export const getTimelineEventsModel = (
}
const timelineEventsByUnitStart = groupEventsByUnitStart(
- timelineEvents,
+ visibleTimelineEvents,
chartModel.xAxisModel.interval.unit,
);