From f0b801b92f58bc08094e824fb39228a86fc76660 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 04:49:16 +0000 Subject: [PATCH 1/5] feat(dashboards): Hatch the background behind low confidence data Time series charts with the data-browsing-time-series-confidence feature now hatch the full plot height behind every stretch of time where a visible series has a low confidence bucket. Ranges from all series are merged, so the treatment is per chart rather than per series. Fixes BROWSE-908 Co-Authored-By: George Gritsouk --- .../lowConfidenceBackground.spec.tsx | 149 ++++++++++++++++++ .../lowConfidenceBackground.tsx | 130 +++++++++++++++ .../timeSeriesWidgetVisualization.spec.tsx | 56 +++++++ .../timeSeriesWidgetVisualization.stories.tsx | 4 +- .../timeSeriesWidgetVisualization.tsx | 25 +++ 5 files changed, 363 insertions(+), 1 deletion(-) create mode 100644 static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.spec.tsx create mode 100644 static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.tsx diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.spec.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.spec.tsx new file mode 100644 index 000000000000..1bb398c23091 --- /dev/null +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.spec.tsx @@ -0,0 +1,149 @@ +import type {CustomSeriesRenderItemAPI, CustomSeriesRenderItemParams} from 'echarts'; +import {ThemeFixture} from 'sentry-fixture/theme'; +import {TimeSeriesFixture} from 'sentry-fixture/timeSeries'; + +import type {TimeSeriesItem} from 'sentry/views/dashboards/widgets/common/types'; + +import {Bars} from './plottables/bars'; +import {Line} from './plottables/line'; +import {Thresholds} from './plottables/thresholds'; +import { + createLowConfidenceBackgroundSeries, + getLowConfidenceRanges, +} from './lowConfidenceBackground'; + +const INTERVAL = 1_000; +const HALF = INTERVAL / 2; + +function makeLine(items: Array>) { + return new Line( + TimeSeriesFixture({ + meta: {valueType: 'number', valueUnit: null, interval: INTERVAL}, + values: items.map((item, index) => ({ + timestamp: index * INTERVAL, + value: 1, + ...item, + })), + }) + ); +} + +const BOUNDS = {start: 0, end: 9 * INTERVAL}; + +describe('getLowConfidenceRanges', () => { + it('covers half an interval either side of each low confidence bucket', () => { + const line = makeLine([ + {}, + {}, + {confidence: 'low'}, + {}, + {}, + {confidence: 'low'}, + {}, + {}, + {}, + {}, + ]); + + expect(getLowConfidenceRanges([line], BOUNDS)).toEqual([ + {start: 2 * INTERVAL - HALF, end: 2 * INTERVAL + HALF}, + {start: 5 * INTERVAL - HALF, end: 5 * INTERVAL + HALF}, + ]); + }); + + it('merges consecutive low confidence buckets into one range', () => { + const line = makeLine([ + {}, + {confidence: 'low'}, + {confidence: 'low'}, + {confidence: 'low'}, + {}, + ]); + + expect(getLowConfidenceRanges([line], BOUNDS)).toEqual([ + {start: INTERVAL - HALF, end: 3 * INTERVAL + HALF}, + ]); + }); + + it('merges ranges from every plottable', () => { + const first = makeLine([{}, {confidence: 'low'}, {}, {}, {}, {}]); + const second = new Bars( + makeLine([{}, {}, {confidence: 'low'}, {}, {}, {confidence: 'low'}]).timeSeries + ); + + expect(getLowConfidenceRanges([first, second], BOUNDS)).toEqual([ + {start: INTERVAL - HALF, end: 2 * INTERVAL + HALF}, + {start: 5 * INTERVAL - HALF, end: 5 * INTERVAL + HALF}, + ]); + }); + + it('clamps ranges to the chart bounds', () => { + const line = makeLine([{confidence: 'low'}, {}, {confidence: 'low'}]); + + expect(getLowConfidenceRanges([line], {start: 0, end: 2 * INTERVAL})).toEqual([ + {start: 0, end: HALF}, + {start: 2 * INTERVAL - HALF, end: 2 * INTERVAL}, + ]); + }); + + it('ignores high, null, empty, and incomplete buckets', () => { + const line = makeLine([ + {confidence: 'high'}, + {confidence: null}, + {confidence: 'low', value: null}, + {confidence: 'low', incomplete: true}, + ]); + + expect(getLowConfidenceRanges([line], BOUNDS)).toEqual([]); + }); + + it('ignores plottables that are not time series', () => { + const thresholds = new Thresholds({ + thresholds: {max_values: {max1: 10, max2: 20}, unit: null}, + }); + + expect(getLowConfidenceRanges([thresholds], BOUNDS)).toEqual([]); + }); +}); + +describe('createLowConfidenceBackgroundSeries', () => { + const theme = ThemeFixture(); + + it('hatches the full plot height behind each range', () => { + const series = createLowConfidenceBackgroundSeries( + [{start: 1_000, end: 3_000}], + theme + ); + + expect(series).toEqual( + expect.objectContaining({ + data: [[1_000, 3_000]], + encode: {x: [0, 1]}, + silent: true, + tooltip: {show: false}, + z: 1, + }) + ); + + const renderItem = series.renderItem!; + const element = renderItem( + { + dataIndex: 0, + coordSys: {type: 'cartesian2d', x: 0, y: 10, width: 500, height: 200}, + } as unknown as CustomSeriesRenderItemParams, + { + coord: ([time]: number[]) => [(time ?? 0) / 10, 0], + } as unknown as CustomSeriesRenderItemAPI + ); + + expect(element).toEqual( + expect.objectContaining({ + type: 'rect', + shape: {x: 100, y: 10, width: 200, height: 200}, + style: expect.objectContaining({ + decal: expect.objectContaining({color: theme.tokens.background.tertiary}), + }), + }) + ); + }); +}); diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.tsx new file mode 100644 index 000000000000..b91d5e1a7a5c --- /dev/null +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.tsx @@ -0,0 +1,130 @@ +import type {Theme} from '@emotion/react'; +import type {CustomSeriesOption, CustomSeriesRenderItem} from 'echarts'; + +import type {CartesianCoordSys} from 'sentry/components/charts/bands/bandShapes'; +import {defined} from 'sentry/utils/defined'; + +import { + ContinuousTimeSeries, + isLowConfidenceDataPoint, +} from './plottables/continuousTimeSeries'; +import type {Plottable} from './plottables/plottable'; + +export const LOW_CONFIDENCE_BACKGROUND_SERIES_ID = 'low-confidence-background'; + +interface TimeRange { + end: number; + start: number; +} + +/** + * Finds the stretches of time where any plottable has a low confidence bucket. + * A bucket spans half an interval either side of its timestamp, the slot a bar + * fills, so touching buckets merge into one range. Ranges are clamped to the + * chart's bounds, since the x axis grows to fit any series that runs past them. + */ +export function getLowConfidenceRanges( + plottables: Plottable[], + bounds: TimeRange +): TimeRange[] { + const slots: TimeRange[] = []; + + for (const plottable of plottables) { + if (!(plottable instanceof ContinuousTimeSeries)) { + continue; + } + + const {meta, values} = plottable.timeSeries; + for (const item of values) { + // Incomplete buckets are drawn in the incomplete style, which hides low + // confidence, and don't count towards `ContinuousTimeSeries.confidence` + if (item.value === null || item.incomplete || !isLowConfidenceDataPoint(item)) { + continue; + } + + const start = Math.max(item.timestamp - meta.interval / 2, bounds.start); + const end = Math.min(item.timestamp + meta.interval / 2, bounds.end); + if (start < end) { + slots.push({start, end}); + } + } + } + + const ranges: TimeRange[] = []; + for (const slot of slots.toSorted((a, b) => a.start - b.start)) { + const previous = ranges.at(-1); + if (previous && slot.start <= previous.end) { + previous.end = Math.max(previous.end, slot.end); + } else { + ranges.push({...slot}); + } + } + + return ranges; +} + +/** + * Hatches the full height of the plot behind each range. A custom series, + * rather than a `markArea`, because only custom series render a `decal`, which + * ECharts redraws at the device pixel ratio so the stripes stay crisp. + */ +export function createLowConfidenceBackgroundSeries( + ranges: TimeRange[], + theme: Theme +): CustomSeriesOption { + return { + id: LOW_CONFIDENCE_BACKGROUND_SERIES_ID, + name: LOW_CONFIDENCE_BACKGROUND_SERIES_ID, + type: 'custom', + data: ranges.map(({start, end}) => [start, end]), + // Mapping only `x` keeps the timestamps out of the y axis extent + encode: {x: [0, 1]}, + renderItem: lowConfidenceBackgroundRenderItem(ranges, theme), + // Behind line, area, and bar series, which default to 2 + z: 1, + clip: true, + silent: true, + animation: false, + legendHoverLink: false, + tooltip: {show: false}, + }; +} + +function lowConfidenceBackgroundRenderItem( + ranges: TimeRange[], + theme: Theme +): CustomSeriesRenderItem { + return function renderLowConfidenceBackgroundItem(params, api) { + const range = ranges[params.dataIndex]; + if (!range) { + return null; + } + + const [left] = api.coord([range.start, 0]); + const [right] = api.coord([range.end, 0]); + if (!defined(left) || !defined(right)) { + return null; + } + + const {y, height} = params.coordSys as CartesianCoordSys; + + return { + type: 'rect', + silent: true, + shape: {x: left, y, width: right - left, height}, + style: { + // A decal only draws over a fill. A transparent one keeps grid lines + // visible through the gaps between stripes + fill: 'transparent', + decal: { + symbol: 'rect', + symbolSize: 1, + dashArrayX: [1, 0], + dashArrayY: [2, 2], + rotation: -Math.PI / 4, + color: theme.tokens.background.tertiary, + }, + }, + }; + }; +} diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.spec.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.spec.tsx index 8ec7aee1f072..0f8ad246922a 100644 --- a/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.spec.tsx +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.spec.tsx @@ -1,4 +1,5 @@ import type {CallbackDataParams} from 'echarts/types/dist/shared'; +import {OrganizationFixture} from 'sentry-fixture/organization'; import {OutcomeRowFixture} from 'sentry-fixture/outcomeRow'; import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary'; @@ -13,6 +14,7 @@ import {DiscoverDatasets} from 'sentry/utils/discover/types'; import {sampleDurationTimeSeries} from './fixtures/sampleDurationTimeSeries'; import {sampleThroughputTimeSeries} from './fixtures/sampleThroughputTimeSeries'; import {Line} from './plottables/line'; +import {LOW_CONFIDENCE_BACKGROUND_SERIES_ID} from './lowConfidenceBackground'; import {TimeSeriesWidgetVisualization} from './timeSeriesWidgetVisualization'; jest.mock('sentry/components/charts/baseChart', () => { @@ -146,4 +148,58 @@ describe('TimeSeriesWidgetVisualization', () => { expect(fromAnnotations).toContain('Dropped Spans'); expect(fromAnnotations).toContain('Releases'); }); + + describe('low confidence background', () => { + const lowConfidenceTimeSeries = { + ...sampleDurationTimeSeries, + values: sampleDurationTimeSeries.values.map((item, index) => ({ + ...item, + confidence: index === 3 || index === 4 ? ('low' as const) : ('high' as const), + })), + }; + const lowConfidencePlottables = [ + new Line(lowConfidenceTimeSeries), + new Line(sampleThroughputTimeSeries), + ]; + const confidenceOrganization = OrganizationFixture({ + features: ['data-browsing-time-series-confidence'], + }); + + function getBackgroundSeries() { + const {series} = jest.mocked(BaseChart).mock.calls.at(-1)![0]; + return series?.find(({id}) => id === LOW_CONFIDENCE_BACKGROUND_SERIES_ID); + } + + it('hatches the merged low confidence buckets', () => { + render(, { + organization: confidenceOrganization, + }); + + const {interval} = lowConfidenceTimeSeries.meta; + expect(getBackgroundSeries()?.data).toEqual([ + [ + lowConfidenceTimeSeries.values[3]!.timestamp - interval / 2, + lowConfidenceTimeSeries.values[4]!.timestamp + interval / 2, + ], + ]); + }); + + it('skips series hidden from the legend', () => { + render( + , + {organization: confidenceOrganization} + ); + + expect(getBackgroundSeries()).toBeUndefined(); + }); + + it('needs the time series confidence feature', () => { + render(); + + expect(getBackgroundSeries()).toBeUndefined(); + }); + }); }); diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.stories.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.stories.tsx index f37345763441..b3c46f5fb601 100644 --- a/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.stories.tsx +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.stories.tsx @@ -862,7 +862,9 @@ export default Storybook.story('TimeSeriesWidgetVisualization', story => {

Data points with confidence: 'low' are estimated from too few samples to be reliable. TimeSeriesWidgetVisualization draws them - differently from high-confidence data, and the tooltip labels them. + differently from high-confidence data, hatches the chart background behind any + stretch of time where a visible series has low confidence data, and the tooltip + labels them.

diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.tsx index 60ab1ca9ead9..d92d32e4cbf4 100644 --- a/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.tsx +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.tsx @@ -75,6 +75,10 @@ import {assignPlottablesToYAxes} from './assignPlottablesToYAxes'; import {createReleaseSeriesOptions} from './createReleaseSeriesOptions'; import {createTimeSeriesWidgetYAxisOptions} from './createTimeSeriesWidgetYAxisOptions'; import {generateTimezoneAlignedTicks} from './generateTimezoneAlignedTicks'; +import { + createLowConfidenceBackgroundSeries, + getLowConfidenceRanges, +} from './lowConfidenceBackground'; import {FALLBACK_TYPE} from './settings'; const {warn} = Sentry.logger; @@ -738,10 +742,31 @@ export function TimeSeriesWidgetVisualization(props: TimeSeriesWidgetVisualizati {} ); + // Series hidden from the legend don't hatch the background + const lowConfidenceRanges = + segmentBy.includes('confidence') && + defined(earliestTimeStamp) && + defined(latestTimeStamp) + ? getLowConfidenceRanges( + props.plottables.filter( + plottable => normalizedLegendSelection[plottable.name] !== false + ), + {start: earliestTimeStamp, end: latestTimeStamp} + ) + : []; + + const lowConfidenceBackgroundSeries = + lowConfidenceRanges.length > 0 + ? createLowConfidenceBackgroundSeries(lowConfidenceRanges, theme) + : null; + + // Plottable series come first, so `seriesIndexToPlottableRangeMap` indexes + // still line up const allSeries = [ ...seriesFromPlottables, releaseSeries, annotationMarkerSeries, + lowConfidenceBackgroundSeries, ].filter(defined); const runHandler = ( From 21e75533c23a3d8f06afd4bc489cf49df6c17100 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 20:19:36 +0000 Subject: [PATCH 2/5] ref(dashboards): Draw low confidence hatching with a FullHeightAreas plottable ContinuousTimeSeries now reports its own low confidence ranges, and a generic FullHeightAreas plottable draws hatched areas across the full plot height. TimeSeriesWidgetVisualization creates that plottable at runtime and plots it after the plottables it was given. Refs BROWSE-908 --- .../utils/timeSeries/mergeTimeRanges.spec.tsx | 43 +++++ .../app/utils/timeSeries/mergeTimeRanges.tsx | 22 +++ .../lowConfidenceBackground.spec.tsx | 149 ------------------ .../lowConfidenceBackground.tsx | 130 --------------- .../plottables/continuousTimeSeries.spec.tsx | 55 +++++++ .../plottables/continuousTimeSeries.tsx | 22 +++ .../plottables/fullHeightAreas.spec.tsx | 68 ++++++++ .../plottables/fullHeightAreas.tsx | 139 ++++++++++++++++ .../timeSeriesWidgetVisualization.spec.tsx | 38 ++++- .../timeSeriesWidgetVisualization.tsx | 54 ++++--- 10 files changed, 411 insertions(+), 309 deletions(-) create mode 100644 static/app/utils/timeSeries/mergeTimeRanges.spec.tsx create mode 100644 static/app/utils/timeSeries/mergeTimeRanges.tsx delete mode 100644 static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.spec.tsx delete mode 100644 static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.tsx create mode 100644 static/app/views/dashboards/widgets/timeSeriesWidget/plottables/fullHeightAreas.spec.tsx create mode 100644 static/app/views/dashboards/widgets/timeSeriesWidget/plottables/fullHeightAreas.tsx diff --git a/static/app/utils/timeSeries/mergeTimeRanges.spec.tsx b/static/app/utils/timeSeries/mergeTimeRanges.spec.tsx new file mode 100644 index 000000000000..6600f15f7570 --- /dev/null +++ b/static/app/utils/timeSeries/mergeTimeRanges.spec.tsx @@ -0,0 +1,43 @@ +import {mergeTimeRanges} from './mergeTimeRanges'; + +describe('mergeTimeRanges', () => { + it('merges ranges that overlap or touch, in any order', () => { + expect( + mergeTimeRanges([ + {start: 50, end: 60}, + {start: 10, end: 20}, + {start: 20, end: 30}, + {start: 25, end: 28}, + ]) + ).toEqual([ + {start: 10, end: 30}, + {start: 50, end: 60}, + ]); + }); + + it('keeps ranges with a gap between them apart', () => { + expect( + mergeTimeRanges([ + {start: 0, end: 10}, + {start: 11, end: 20}, + ]) + ).toEqual([ + {start: 0, end: 10}, + {start: 11, end: 20}, + ]); + }); + + it('does not change the ranges it was given', () => { + const ranges = [ + {start: 0, end: 10}, + {start: 5, end: 20}, + ]; + + mergeTimeRanges(ranges); + + expect(ranges).toEqual([ + {start: 0, end: 10}, + {start: 5, end: 20}, + ]); + }); +}); diff --git a/static/app/utils/timeSeries/mergeTimeRanges.tsx b/static/app/utils/timeSeries/mergeTimeRanges.tsx new file mode 100644 index 000000000000..bf43cb63410c --- /dev/null +++ b/static/app/utils/timeSeries/mergeTimeRanges.tsx @@ -0,0 +1,22 @@ +export interface TimeRange { + end: number; + start: number; +} + +/** + * Sorts ranges by start and merges any that overlap or touch. + */ +export function mergeTimeRanges(ranges: readonly TimeRange[]): TimeRange[] { + const merged: TimeRange[] = []; + + for (const range of ranges.toSorted((a, b) => a.start - b.start)) { + const previous = merged.at(-1); + if (previous && range.start <= previous.end) { + previous.end = Math.max(previous.end, range.end); + } else { + merged.push({...range}); + } + } + + return merged; +} diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.spec.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.spec.tsx deleted file mode 100644 index 1bb398c23091..000000000000 --- a/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.spec.tsx +++ /dev/null @@ -1,149 +0,0 @@ -import type {CustomSeriesRenderItemAPI, CustomSeriesRenderItemParams} from 'echarts'; -import {ThemeFixture} from 'sentry-fixture/theme'; -import {TimeSeriesFixture} from 'sentry-fixture/timeSeries'; - -import type {TimeSeriesItem} from 'sentry/views/dashboards/widgets/common/types'; - -import {Bars} from './plottables/bars'; -import {Line} from './plottables/line'; -import {Thresholds} from './plottables/thresholds'; -import { - createLowConfidenceBackgroundSeries, - getLowConfidenceRanges, -} from './lowConfidenceBackground'; - -const INTERVAL = 1_000; -const HALF = INTERVAL / 2; - -function makeLine(items: Array>) { - return new Line( - TimeSeriesFixture({ - meta: {valueType: 'number', valueUnit: null, interval: INTERVAL}, - values: items.map((item, index) => ({ - timestamp: index * INTERVAL, - value: 1, - ...item, - })), - }) - ); -} - -const BOUNDS = {start: 0, end: 9 * INTERVAL}; - -describe('getLowConfidenceRanges', () => { - it('covers half an interval either side of each low confidence bucket', () => { - const line = makeLine([ - {}, - {}, - {confidence: 'low'}, - {}, - {}, - {confidence: 'low'}, - {}, - {}, - {}, - {}, - ]); - - expect(getLowConfidenceRanges([line], BOUNDS)).toEqual([ - {start: 2 * INTERVAL - HALF, end: 2 * INTERVAL + HALF}, - {start: 5 * INTERVAL - HALF, end: 5 * INTERVAL + HALF}, - ]); - }); - - it('merges consecutive low confidence buckets into one range', () => { - const line = makeLine([ - {}, - {confidence: 'low'}, - {confidence: 'low'}, - {confidence: 'low'}, - {}, - ]); - - expect(getLowConfidenceRanges([line], BOUNDS)).toEqual([ - {start: INTERVAL - HALF, end: 3 * INTERVAL + HALF}, - ]); - }); - - it('merges ranges from every plottable', () => { - const first = makeLine([{}, {confidence: 'low'}, {}, {}, {}, {}]); - const second = new Bars( - makeLine([{}, {}, {confidence: 'low'}, {}, {}, {confidence: 'low'}]).timeSeries - ); - - expect(getLowConfidenceRanges([first, second], BOUNDS)).toEqual([ - {start: INTERVAL - HALF, end: 2 * INTERVAL + HALF}, - {start: 5 * INTERVAL - HALF, end: 5 * INTERVAL + HALF}, - ]); - }); - - it('clamps ranges to the chart bounds', () => { - const line = makeLine([{confidence: 'low'}, {}, {confidence: 'low'}]); - - expect(getLowConfidenceRanges([line], {start: 0, end: 2 * INTERVAL})).toEqual([ - {start: 0, end: HALF}, - {start: 2 * INTERVAL - HALF, end: 2 * INTERVAL}, - ]); - }); - - it('ignores high, null, empty, and incomplete buckets', () => { - const line = makeLine([ - {confidence: 'high'}, - {confidence: null}, - {confidence: 'low', value: null}, - {confidence: 'low', incomplete: true}, - ]); - - expect(getLowConfidenceRanges([line], BOUNDS)).toEqual([]); - }); - - it('ignores plottables that are not time series', () => { - const thresholds = new Thresholds({ - thresholds: {max_values: {max1: 10, max2: 20}, unit: null}, - }); - - expect(getLowConfidenceRanges([thresholds], BOUNDS)).toEqual([]); - }); -}); - -describe('createLowConfidenceBackgroundSeries', () => { - const theme = ThemeFixture(); - - it('hatches the full plot height behind each range', () => { - const series = createLowConfidenceBackgroundSeries( - [{start: 1_000, end: 3_000}], - theme - ); - - expect(series).toEqual( - expect.objectContaining({ - data: [[1_000, 3_000]], - encode: {x: [0, 1]}, - silent: true, - tooltip: {show: false}, - z: 1, - }) - ); - - const renderItem = series.renderItem!; - const element = renderItem( - { - dataIndex: 0, - coordSys: {type: 'cartesian2d', x: 0, y: 10, width: 500, height: 200}, - } as unknown as CustomSeriesRenderItemParams, - { - coord: ([time]: number[]) => [(time ?? 0) / 10, 0], - } as unknown as CustomSeriesRenderItemAPI - ); - - expect(element).toEqual( - expect.objectContaining({ - type: 'rect', - shape: {x: 100, y: 10, width: 200, height: 200}, - style: expect.objectContaining({ - decal: expect.objectContaining({color: theme.tokens.background.tertiary}), - }), - }) - ); - }); -}); diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.tsx deleted file mode 100644 index b91d5e1a7a5c..000000000000 --- a/static/app/views/dashboards/widgets/timeSeriesWidget/lowConfidenceBackground.tsx +++ /dev/null @@ -1,130 +0,0 @@ -import type {Theme} from '@emotion/react'; -import type {CustomSeriesOption, CustomSeriesRenderItem} from 'echarts'; - -import type {CartesianCoordSys} from 'sentry/components/charts/bands/bandShapes'; -import {defined} from 'sentry/utils/defined'; - -import { - ContinuousTimeSeries, - isLowConfidenceDataPoint, -} from './plottables/continuousTimeSeries'; -import type {Plottable} from './plottables/plottable'; - -export const LOW_CONFIDENCE_BACKGROUND_SERIES_ID = 'low-confidence-background'; - -interface TimeRange { - end: number; - start: number; -} - -/** - * Finds the stretches of time where any plottable has a low confidence bucket. - * A bucket spans half an interval either side of its timestamp, the slot a bar - * fills, so touching buckets merge into one range. Ranges are clamped to the - * chart's bounds, since the x axis grows to fit any series that runs past them. - */ -export function getLowConfidenceRanges( - plottables: Plottable[], - bounds: TimeRange -): TimeRange[] { - const slots: TimeRange[] = []; - - for (const plottable of plottables) { - if (!(plottable instanceof ContinuousTimeSeries)) { - continue; - } - - const {meta, values} = plottable.timeSeries; - for (const item of values) { - // Incomplete buckets are drawn in the incomplete style, which hides low - // confidence, and don't count towards `ContinuousTimeSeries.confidence` - if (item.value === null || item.incomplete || !isLowConfidenceDataPoint(item)) { - continue; - } - - const start = Math.max(item.timestamp - meta.interval / 2, bounds.start); - const end = Math.min(item.timestamp + meta.interval / 2, bounds.end); - if (start < end) { - slots.push({start, end}); - } - } - } - - const ranges: TimeRange[] = []; - for (const slot of slots.toSorted((a, b) => a.start - b.start)) { - const previous = ranges.at(-1); - if (previous && slot.start <= previous.end) { - previous.end = Math.max(previous.end, slot.end); - } else { - ranges.push({...slot}); - } - } - - return ranges; -} - -/** - * Hatches the full height of the plot behind each range. A custom series, - * rather than a `markArea`, because only custom series render a `decal`, which - * ECharts redraws at the device pixel ratio so the stripes stay crisp. - */ -export function createLowConfidenceBackgroundSeries( - ranges: TimeRange[], - theme: Theme -): CustomSeriesOption { - return { - id: LOW_CONFIDENCE_BACKGROUND_SERIES_ID, - name: LOW_CONFIDENCE_BACKGROUND_SERIES_ID, - type: 'custom', - data: ranges.map(({start, end}) => [start, end]), - // Mapping only `x` keeps the timestamps out of the y axis extent - encode: {x: [0, 1]}, - renderItem: lowConfidenceBackgroundRenderItem(ranges, theme), - // Behind line, area, and bar series, which default to 2 - z: 1, - clip: true, - silent: true, - animation: false, - legendHoverLink: false, - tooltip: {show: false}, - }; -} - -function lowConfidenceBackgroundRenderItem( - ranges: TimeRange[], - theme: Theme -): CustomSeriesRenderItem { - return function renderLowConfidenceBackgroundItem(params, api) { - const range = ranges[params.dataIndex]; - if (!range) { - return null; - } - - const [left] = api.coord([range.start, 0]); - const [right] = api.coord([range.end, 0]); - if (!defined(left) || !defined(right)) { - return null; - } - - const {y, height} = params.coordSys as CartesianCoordSys; - - return { - type: 'rect', - silent: true, - shape: {x: left, y, width: right - left, height}, - style: { - // A decal only draws over a fill. A transparent one keeps grid lines - // visible through the gaps between stripes - fill: 'transparent', - decal: { - symbol: 'rect', - symbolSize: 1, - dashArrayX: [1, 0], - dashArrayY: [2, 2], - rotation: -Math.PI / 4, - color: theme.tokens.background.tertiary, - }, - }, - }; - }; -} diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/continuousTimeSeries.spec.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/continuousTimeSeries.spec.tsx index ff11b001b981..6de253f60fe2 100644 --- a/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/continuousTimeSeries.spec.tsx +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/continuousTimeSeries.spec.tsx @@ -1,6 +1,7 @@ import {TimeSeriesFixture} from 'sentry-fixture/timeSeries'; import {createLineSeries} from 'sentry/components/charts/series/lineSeries'; +import type {TimeSeriesItem} from 'sentry/views/dashboards/widgets/common/types'; import {ContinuousTimeSeries} from './continuousTimeSeries'; import type {Plottable} from './plottable'; @@ -201,6 +202,60 @@ describe('ContinuousTimeSeries', () => { }); }); + describe('lowConfidenceRanges', () => { + const interval = 1_000; + + function timeSeriesWith(items: Array>) { + return new Dots( + TimeSeriesFixture({ + meta: {valueType: 'number', valueUnit: null, interval}, + values: items.map((item, index) => ({ + timestamp: index * interval, + value: 1, + ...item, + })), + }) + ); + } + + it('covers half an interval either side of each low confidence bucket', () => { + const plottable = timeSeriesWith([ + {}, + {confidence: 'low'}, + {}, + {confidence: 'low'}, + ]); + + expect(plottable.lowConfidenceRanges).toEqual([ + {start: 500, end: 1_500}, + {start: 2_500, end: 3_500}, + ]); + }); + + it('merges consecutive low confidence buckets', () => { + const plottable = timeSeriesWith([ + {}, + {confidence: 'low'}, + {confidence: 'low'}, + {confidence: 'low'}, + {}, + ]); + + expect(plottable.lowConfidenceRanges).toEqual([{start: 500, end: 3_500}]); + }); + + it('ignores high, null, empty, and incomplete buckets', () => { + const plottable = timeSeriesWith([ + {confidence: 'high'}, + {confidence: null}, + {confidence: 'low', value: null}, + {confidence: 'low', incomplete: true}, + ]); + + expect(plottable.lowConfidenceRanges).toEqual([]); + }); + }); + describe('name', () => { it('name is properly formatted', () => { const timeSeries = TimeSeriesFixture({ diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/continuousTimeSeries.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/continuousTimeSeries.tsx index 62c3f229d788..1254f477d50b 100644 --- a/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/continuousTimeSeries.tsx +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/continuousTimeSeries.tsx @@ -1,6 +1,7 @@ import type {SeriesOption} from 'echarts'; import {defined} from 'sentry/utils/defined'; +import {mergeTimeRanges, type TimeRange} from 'sentry/utils/timeSeries/mergeTimeRanges'; import {scaleTimeSeriesData} from 'sentry/utils/timeSeries/scaleTimeSeriesData'; import type {TimeSeriesSegmentField} from 'sentry/utils/timeSeries/segmentTimeSeriesByDisplayStyle'; import {timeSeriesItemToEChartsDataPoint} from 'sentry/utils/timeSeries/timeSeriesItemToEChartsDataPoint'; @@ -110,6 +111,27 @@ export abstract class ContinuousTimeSeries< return values.some(isExtrapolated) ? 'high' : 'full'; } + /** + * Stretches of time covered by low confidence buckets. A bucket spans half an + * interval either side of its timestamp, the slot a bar fills, so consecutive + * buckets merge into one range. Incomplete buckets don't count, as in `confidence` + */ + get lowConfidenceRanges(): TimeRange[] { + const {interval} = this.timeSeries.meta; + + return mergeTimeRanges( + this.timeSeries.values + .filter( + datum => + datum.value !== null && !datum.incomplete && isLowConfidenceDataPoint(datum) + ) + .map(({timestamp}) => ({ + start: timestamp - interval / 2, + end: timestamp + interval / 2, + })) + ); + } + get needsColor(): boolean { return !this.config?.color; } diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/fullHeightAreas.spec.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/fullHeightAreas.spec.tsx new file mode 100644 index 000000000000..26265f396df7 --- /dev/null +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/fullHeightAreas.spec.tsx @@ -0,0 +1,68 @@ +import type {CustomSeriesRenderItemAPI, CustomSeriesRenderItemParams} from 'echarts'; + +import {FullHeightAreas} from './fullHeightAreas'; + +describe('FullHeightAreas', () => { + const config = {name: 'areas', color: '#f0f0f2'}; + + it('merges overlapping ranges and clips them to the bounds', () => { + const areas = new FullHeightAreas({ + ...config, + ranges: [ + {start: 40, end: 60}, + {start: -10, end: 10}, + {start: 50, end: 120}, + {start: 150, end: 160}, + ], + bounds: {start: 0, end: 100}, + }); + + expect(areas.ranges).toEqual([ + {start: 0, end: 10}, + {start: 40, end: 100}, + ]); + expect(areas.start).toBe(0); + expect(areas.end).toBe(100); + }); + + it('is empty without ranges', () => { + expect(new FullHeightAreas({...config, ranges: []}).isEmpty).toBe(true); + }); + + it('draws a hatched area across the full plot height behind the data', () => { + const [series] = new FullHeightAreas({ + ...config, + ranges: [{start: 1_000, end: 3_000}], + }).toSeries(); + + expect(series).toEqual( + expect.objectContaining({ + data: [[1_000, 3_000]], + encode: {x: [0, 1]}, + silent: true, + tooltip: {show: false}, + z: 1, + }) + ); + + const element = series!.renderItem!( + { + dataIndex: 0, + coordSys: {type: 'cartesian2d', x: 0, y: 10, width: 500, height: 200}, + } as unknown as CustomSeriesRenderItemParams, + { + coord: ([time]: number[]) => [(time ?? 0) / 10, 0], + } as unknown as CustomSeriesRenderItemAPI + ); + + expect(element).toEqual( + expect.objectContaining({ + type: 'rect', + shape: {x: 100, y: 10, width: 200, height: 200}, + style: expect.objectContaining({ + decal: expect.objectContaining({color: '#f0f0f2'}), + }), + }) + ); + }); +}); diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/fullHeightAreas.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/fullHeightAreas.tsx new file mode 100644 index 000000000000..a4a650103ef2 --- /dev/null +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/fullHeightAreas.tsx @@ -0,0 +1,139 @@ +import type {CustomSeriesOption, CustomSeriesRenderItem} from 'echarts'; + +import type {CartesianCoordSys} from 'sentry/components/charts/bands/bandShapes'; +import {defined} from 'sentry/utils/defined'; +import {mergeTimeRanges, type TimeRange} from 'sentry/utils/timeSeries/mergeTimeRanges'; + +import type { + Plottable, + PlottableConfidence, + PlottableTimeSeriesValueType, +} from './plottable'; + +type FullHeightAreasConfig = { + /** + * Hatch color. Areas are hatched over a transparent fill, so grid lines show + * through the gaps between stripes + */ + color: string; + name: string; + /** + * Ranges that overlap or touch are drawn as one area + */ + ranges: readonly TimeRange[]; + /** + * Areas are clipped to these bounds. The x axis grows to fit any series that + * runs past the plotted data, so pass the data's first and last timestamps + */ + bounds?: TimeRange; +}; + +/** + * Areas that span the full height of the plot over stretches of time, drawn + * behind the data. Unlike time series plottables, it has no data of its own, + * so it never takes up a y axis, a legend item, or a tooltip row. + */ +export class FullHeightAreas implements Plottable { + config: Readonly; + ranges: readonly TimeRange[]; + + confidence: PlottableConfidence = 'full'; + dataType: PlottableTimeSeriesValueType = 'number'; + dataUnit = null; + needsColor = false; + + constructor(config: FullHeightAreasConfig) { + this.config = config; + this.ranges = mergeTimeRanges(config.ranges) + .map(range => clampRange(range, config.bounds)) + .filter(range => range.start < range.end); + } + + get name(): string { + return this.config.name; + } + + get label(): string { + return this.config.name; + } + + get isEmpty(): boolean { + return this.ranges.length === 0; + } + + get start(): number | null { + return this.ranges.at(0)?.start ?? null; + } + + get end(): number | null { + return this.ranges.at(-1)?.end ?? null; + } + + toSeries(): CustomSeriesOption[] { + return [ + { + id: this.name, + name: this.name, + type: 'custom', + data: this.ranges.map(({start, end}) => [start, end]), + // Mapping only `x` keeps the timestamps out of the y axis extent + encode: {x: [0, 1]}, + renderItem: this.renderItem, + // Behind line, area, and bar series, which default to 2 + z: 1, + clip: true, + silent: true, + animation: false, + legendHoverLink: false, + tooltip: {show: false}, + }, + ]; + } + + // A custom series rather than a `markArea`, because only custom series render + // a `decal`, which ECharts redraws at the device pixel ratio so the stripes + // stay crisp + private renderItem: CustomSeriesRenderItem = (params, api) => { + const range = this.ranges[params.dataIndex]; + if (!range) { + return null; + } + + const [left] = api.coord([range.start, 0]); + const [right] = api.coord([range.end, 0]); + if (!defined(left) || !defined(right)) { + return null; + } + + const {y, height} = params.coordSys as CartesianCoordSys; + + return { + type: 'rect', + silent: true, + shape: {x: left, y, width: right - left, height}, + style: { + // A decal only draws over a fill + fill: 'transparent', + decal: { + symbol: 'rect', + symbolSize: 1, + dashArrayX: [1, 0], + dashArrayY: [2, 2], + rotation: -Math.PI / 4, + color: this.config.color, + }, + }, + }; + }; +} + +function clampRange(range: TimeRange, bounds?: TimeRange): TimeRange { + if (!bounds) { + return range; + } + + return { + start: Math.max(range.start, bounds.start), + end: Math.min(range.end, bounds.end), + }; +} diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.spec.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.spec.tsx index 0f8ad246922a..2b0d92b2acdd 100644 --- a/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.spec.tsx +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.spec.tsx @@ -14,8 +14,10 @@ import {DiscoverDatasets} from 'sentry/utils/discover/types'; import {sampleDurationTimeSeries} from './fixtures/sampleDurationTimeSeries'; import {sampleThroughputTimeSeries} from './fixtures/sampleThroughputTimeSeries'; import {Line} from './plottables/line'; -import {LOW_CONFIDENCE_BACKGROUND_SERIES_ID} from './lowConfidenceBackground'; -import {TimeSeriesWidgetVisualization} from './timeSeriesWidgetVisualization'; +import { + LOW_CONFIDENCE_AREAS_NAME, + TimeSeriesWidgetVisualization, +} from './timeSeriesWidgetVisualization'; jest.mock('sentry/components/charts/baseChart', () => { const actual = jest.requireActual('sentry/components/charts/baseChart'); @@ -149,7 +151,7 @@ describe('TimeSeriesWidgetVisualization', () => { expect(fromAnnotations).toContain('Releases'); }); - describe('low confidence background', () => { + describe('low confidence areas', () => { const lowConfidenceTimeSeries = { ...sampleDurationTimeSeries, values: sampleDurationTimeSeries.values.map((item, index) => ({ @@ -167,7 +169,7 @@ describe('TimeSeriesWidgetVisualization', () => { function getBackgroundSeries() { const {series} = jest.mocked(BaseChart).mock.calls.at(-1)![0]; - return series?.find(({id}) => id === LOW_CONFIDENCE_BACKGROUND_SERIES_ID); + return series?.find(({id}) => id === LOW_CONFIDENCE_AREAS_NAME); } it('hatches the merged low confidence buckets', () => { @@ -184,6 +186,34 @@ describe('TimeSeriesWidgetVisualization', () => { ]); }); + it('merges low confidence buckets from every series', () => { + const throughputWithLowConfidence = { + ...sampleThroughputTimeSeries, + values: sampleThroughputTimeSeries.values.map((item, index) => ({ + ...item, + confidence: index === 5 ? ('low' as const) : ('high' as const), + })), + }; + + render( + , + {organization: confidenceOrganization} + ); + + const {interval} = lowConfidenceTimeSeries.meta; + expect(getBackgroundSeries()?.data).toEqual([ + [ + lowConfidenceTimeSeries.values[3]!.timestamp - interval / 2, + throughputWithLowConfidence.values[5]!.timestamp + interval / 2, + ], + ]); + }); + it('skips series hidden from the legend', () => { render(