:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(insights): place annotations correctly on sparse SQL charts (#125829)
Sam Pennington911a51899420
diff --git a/frontend/src/lib/components/AnnotationsOverlay/annotationsOverlayLogic.test.ts b/frontend/src/lib/components/AnnotationsOverlay/annotationsOverlayLogic.test.tsindex 3d9b0d126261..a1f795744021 100644--- a/frontend/src/lib/components/AnnotationsOverlay/annotationsOverlayLogic.test.ts+++ b/frontend/src/lib/components/AnnotationsOverlay/annotationsOverlayLogic.test.ts@@ -896,6 +896,12 @@ describe('annotationsOverlayLogic', () => {dates: ['2022-08-08', '2022-08-15', '2022-08-22', '2022-08-29', '2022-09-05', '2022-09-12'],expected: { '2022-08-10 00:00:00+0000': 2 / 7, '2022-09-10 00:00:00+0000': 4 + 5 / 7 },},+ {+ // Regression: SQL charts default to a day interval but can have sparser points.+ interval: 'day',+ dates: ['2022-08-01', '2022-08-08', '2022-08-15'],+ expected: { '2022-08-10 00:00:00+0000': 1 + 2 / 7, '2022-08-11 00:00:00+0000': 1 + 3 / 7 },+ },])('$interval chart → fractional indices', async ({ interval, dates, expected }) => {useInsightMocks(interval)logic = annotationsOverlayLogic({diff --git a/frontend/src/lib/components/AnnotationsOverlay/annotationsOverlayLogic.ts b/frontend/src/lib/components/AnnotationsOverlay/annotationsOverlayLogic.tsindex 436c497e94e7..2988bac36000 100644--- a/frontend/src/lib/components/AnnotationsOverlay/annotationsOverlayLogic.ts+++ b/frontend/src/lib/components/AnnotationsOverlay/annotationsOverlayLogic.ts@@ -475,22 +475,35 @@ export const annotationsOverlayLogic = kea<annotationsOverlayLogicType>([if (dates.length === 0) {return []}- // Don't startOf(intervalUnit) here — dayjs uses Sunday-start weeks, which would- // drift Monday-aligned dates backward and bias every fractional index by 1/7.- const firstDate = parseDateInTimezone(dates[0], timezone)+ // Interpolate between the actual data points instead of assuming one point per interval:+ // SQL charts can skip dates or bucket coarser than the insight interval.+ // SQL charts can have tens of thousands of points, so binary search and parse only the dates it visits.+ const pointTime = (index: number): number => parseDateInTimezone(dates[index], timezone).valueOf()const lastIndex = dates.length - 1return Object.entries(groupedAnnotations).map(([dateKey, annotations]) => {const date = annotations[0].date_marker.startOf(getGroupingUnit(intervalUnit))- const wholeIndex = date.diff(firstDate, intervalUnit)- const intervalStart = firstDate.add(wholeIndex, intervalUnit)- const intervalEnd = intervalStart.add(1, intervalUnit)- const intervalSpanMs = intervalEnd.valueOf() - intervalStart.valueOf()- const fraction =- intervalSpanMs > 0 ? (date.valueOf() - intervalStart.valueOf()) / intervalSpanMs : 0- // Clamp to lastIndex so annotations past the final data point snap onto- // it rather than extrapolating off the canvas.- const dataIndex = Math.min(wholeIndex + fraction, lastIndex)+ const time = date.valueOf()+ let nextIndex = 0+ let end = dates.length+ while (nextIndex < end) {+ const middle = (nextIndex + end) >> 1+ if (pointTime(middle) > time) {+ end = middle+ } else {+ nextIndex = middle + 1+ }+ }+ // Before the first point → negative, filtered out below. Past the final point → snap+ // onto it rather than extrapolating off the canvas.+ if (nextIndex === dates.length) {+ return { dateKey, date, dataIndex: lastIndex }+ }+ if (nextIndex === 0) {+ return { dateKey, date, dataIndex: -1 }+ }+ const previousTime = pointTime(nextIndex - 1)+ const dataIndex = nextIndex - 1 + (time - previousTime) / (pointTime(nextIndex) - previousTime)return { dateKey, date, dataIndex }}).filter(({ dataIndex }) => dataIndex >= 0)diff --git a/frontend/src/queries/nodes/DataVisualization/Components/DisplayTab.tsx b/frontend/src/queries/nodes/DataVisualization/Components/DisplayTab.tsxindex b69d3b079853..8bdbd7c6d522 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/DisplayTab.tsx+++ b/frontend/src/queries/nodes/DataVisualization/Components/DisplayTab.tsx@@ -92,10 +92,9 @@ export const DisplayTab = (): JSX.Element => {effectiveVisualizationType === ChartDisplayType.ActionsAreaGraphconst isDateXAxis = xData?.column.type.name === 'DATE' || xData?.column.type.name === 'DATETIME'const supportsAnnotations =- isDateXAxis &&- (isLineChart ||- effectiveVisualizationType === ChartDisplayType.ActionsBar ||- effectiveVisualizationType === ChartDisplayType.ActionsStackedBar)+ isLineChart ||+ effectiveVisualizationType === ChartDisplayType.ActionsBar ||+ effectiveVisualizationType === ChartDisplayType.ActionsStackedBarconst renderYAxisSettings = (name: 'leftYAxisSettings' | 'rightYAxisSettings'): JSX.Element => {const leftPlaceholder = isHorizontalBarChart@@ -295,7 +294,10 @@ export const DisplayTab = (): JSX.Element => {className="flex-1 w-full"data-attr="data-visualization-show-annotations"label="Show annotations"- checked={chartSettings.showAnnotations ?? false}+ disabledReason={+ !isDateXAxis && 'Annotations need a Date or DateTime column on the X-axis'+ }+ checked={isDateXAxis && (chartSettings.showAnnotations ?? false)}onChange={(value) => {updateChartSettings({ showAnnotations: value })}}