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/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/hatchAreas.spec.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/hatchAreas.spec.tsx new file mode 100644 index 000000000000..832b326548e1 --- /dev/null +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/hatchAreas.spec.tsx @@ -0,0 +1,65 @@ +import type {CustomSeriesRenderItemAPI, CustomSeriesRenderItemParams} from 'echarts'; + +import {HatchAreas} from './hatchAreas'; + +describe('HatchAreas', () => { + const config = {name: 'areas', color: '#f0f0f2', dataType: 'number' as const}; + + it('clips areas to the bounds it is plotted with', () => { + const areas = new HatchAreas( + [ + {start: -10, end: 10}, + {start: 40, end: 120}, + {start: 150, end: 160}, + ], + config + ); + + const [series] = areas.toSeries({bounds: {start: 0, end: 100}}); + + expect(series?.data).toEqual([ + [0, 10], + [40, 100], + ]); + }); + + it('is empty without ranges', () => { + expect(new HatchAreas([], config).isEmpty).toBe(true); + }); + + it('draws a hatched area across the full plot height behind the data', () => { + const [series] = new HatchAreas([{start: 1_000, end: 3_000}], config).toSeries(); + + expect(series).toEqual( + expect.objectContaining({ + data: [[1_000, 3_000]], + encode: {x: [0, 1]}, + silent: true, + tooltip: {show: false}, + z: 1, + }) + ); + + const values = [1_000, 3_000]; + const element = series!.renderItem!( + { + dataIndex: 0, + coordSys: {type: 'cartesian2d', x: 0, y: 10, width: 500, height: 200}, + } as unknown as CustomSeriesRenderItemParams, + { + value: (dimension: number) => values[dimension], + 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/hatchAreas.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/hatchAreas.tsx new file mode 100644 index 000000000000..0abb174f72e2 --- /dev/null +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/plottables/hatchAreas.tsx @@ -0,0 +1,146 @@ +import type {CustomSeriesOption, CustomSeriesRenderItem} from 'echarts'; + +import type {CartesianCoordSys} from 'sentry/components/charts/bands/bandShapes'; +import {defined} from 'sentry/utils/defined'; +import type {TimeRange} from 'sentry/utils/timeSeries/mergeTimeRanges'; + +import type { + Plottable, + PlottableConfidence, + PlottableTimeSeriesValueType, +} from './plottable'; + +type HatchAreasConfig = { + /** + * Hatch color. Areas are hatched over a transparent fill, so grid lines show + * through the gaps between stripes + */ + color: string; + /** + * Type of the Y axis to draw against. The areas don't read Y values, but the + * chart puts every plottable on an axis by its type, so this should match an + * axis the chart already has + */ + dataType: PlottableTimeSeriesValueType; + /** + * Identifies the plottable. It becomes the ECharts series ID and name, so it + * must be unique among the chart's plottables + */ + name: string; +}; + +type HatchAreasPlottingOptions = { + /** + * 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; +}; + +/** + * Hatched 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 HatchAreas implements Plottable { + ranges: readonly TimeRange[]; + config: Readonly; + + // Required by `Plottable`. The areas have no data, so none of it is extrapolated + confidence: PlottableConfidence = 'full'; + dataUnit = null; + needsColor = false; + + /** + * @param ranges Sorted ranges that don't overlap, such as the output of `mergeTimeRanges` + */ + constructor(ranges: readonly TimeRange[], config: HatchAreasConfig) { + this.ranges = ranges; + this.config = config; + } + + get name(): string { + return this.config.name; + } + + get dataType(): PlottableTimeSeriesValueType { + return this.config.dataType; + } + + 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({bounds}: HatchAreasPlottingOptions = {}): CustomSeriesOption[] { + const ranges = bounds + ? this.ranges + .map(({start, end}) => ({ + start: Math.max(start, bounds.start), + end: Math.min(end, bounds.end), + })) + .filter(({start, end}) => start < end) + : this.ranges; + + return [ + { + id: this.name, + name: this.name, + 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: 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 [left] = api.coord([api.value(0), 0]); + const [right] = api.coord([api.value(1), 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, + }, + }, + }; + }; +} diff --git a/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.spec.tsx b/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.spec.tsx index 8ec7aee1f072..395bf2fe7a37 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,7 +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 {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'); @@ -146,4 +150,37 @@ describe('TimeSeriesWidgetVisualization', () => { expect(fromAnnotations).toContain('Dropped Spans'); expect(fromAnnotations).toContain('Releases'); }); + + describe('low confidence areas', () => { + const lowConfidencePlottables = [ + new Line({ + ...sampleDurationTimeSeries, + values: sampleDurationTimeSeries.values.map((item, index) => ({ + ...item, + confidence: index === 3 ? ('low' as const) : ('high' as const), + })), + }), + ]; + + function getLowConfidenceSeries() { + const {series} = jest.mocked(BaseChart).mock.calls.at(-1)![0]; + return series?.find(({id}) => id === LOW_CONFIDENCE_AREAS_NAME); + } + + it('hatches low confidence data with the time series confidence feature', () => { + render(, { + organization: OrganizationFixture({ + features: ['data-browsing-time-series-confidence'], + }), + }); + + expect(getLowConfidenceSeries()).toBeDefined(); + }); + + it('needs the time series confidence feature', () => { + render(); + + expect(getLowConfidenceSeries()).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..b549d85b8bad 100644 --- a/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.tsx +++ b/static/app/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization.tsx @@ -45,6 +45,7 @@ import {escape} from 'sentry/utils'; import {getUserTimezone} from 'sentry/utils/dates'; import {defined} from 'sentry/utils/defined'; import {RangeMap, type Range} from 'sentry/utils/number/rangeMap'; +import {mergeTimeRanges} from 'sentry/utils/timeSeries/mergeTimeRanges'; import type {TimeSeriesSegmentField} from 'sentry/utils/timeSeries/segmentTimeSeriesByDisplayStyle'; import {useLocation} from 'sentry/utils/useLocation'; import {useNavigate} from 'sentry/utils/useNavigate'; @@ -70,6 +71,7 @@ import { ContinuousTimeSeries, isLowConfidenceDataPoint, } from './plottables/continuousTimeSeries'; +import {HatchAreas} from './plottables/hatchAreas'; import type {Plottable} from './plottables/plottable'; import {assignPlottablesToYAxes} from './assignPlottablesToYAxes'; import {createReleaseSeriesOptions} from './createReleaseSeriesOptions'; @@ -655,6 +657,41 @@ export function TimeSeriesWidgetVisualization(props: TimeSeriesWidgetVisualizati const thresholdMaxOffset = 10; + const hasConfidenceSegment = segmentBy.includes('confidence'); + const lowConfidenceAreas = useMemo( + () => + hasConfidenceSegment + ? new HatchAreas( + mergeTimeRanges( + props.plottables + // Series hidden from the legend don't mark their low confidence data + .filter( + plottable => !showLegend || legendSelection[plottable.name] !== false + ) + .flatMap(plottable => + plottable instanceof ContinuousTimeSeries + ? plottable.lowConfidenceRanges + : [] + ) + ), + { + name: LOW_CONFIDENCE_AREAS_NAME, + color: theme.tokens.background.tertiary, + // The first plottable's type always gets the left axis + dataType: props.plottables[0]?.dataType ?? FALLBACK_TYPE, + } + ) + : null, + [hasConfidenceSegment, props.plottables, showLegend, legendSelection, theme] + ); + + // Plottables the chart creates itself go last, so they stay out of the + // legend and the Y axis assignment, which only read `props.plottables` + const plottables: Plottable[] = + lowConfidenceAreas && !lowConfidenceAreas.isEmpty + ? [...props.plottables, lowConfidenceAreas] + : props.plottables; + // Keep track of which `Series[]` indexes correspond to which `Plottable` so // we can look up the types in the tooltip. We need this so we can find the // plottable responsible for a given value in the tooltip formatter. The only @@ -664,10 +701,15 @@ export function TimeSeriesWidgetVisualization(props: TimeSeriesWidgetVisualizati let seriesIndex = 0; const seriesIndexToPlottableMapRanges: Array> = []; + const dataBounds = + defined(earliestTimeStamp) && defined(latestTimeStamp) + ? {start: earliestTimeStamp, end: latestTimeStamp} + : undefined; + // Keep track of what color in the chosen palette we're assigning let seriesColorIndex = 0; const seriesFromPlottables: SeriesOption[] = []; - for (const plottable of props.plottables) { + for (const plottable of plottables) { let color: string | undefined; if (plottable.needsColor) { @@ -684,6 +726,7 @@ export function TimeSeriesWidgetVisualization(props: TimeSeriesWidgetVisualizati segmentBy, theme, maxOffset: thresholdMaxOffset, + bounds: dataBounds, }); seriesIndexToPlottableMapRanges.push({ @@ -913,6 +956,8 @@ function getBandBuckets(plottables: Plottable[]): BandBucket[] { .toSorted((a, b) => a.start - b.start); } +export const LOW_CONFIDENCE_AREAS_NAME = '__low_confidence__'; + // Hide every part of the axis so ECharts will remove those elements and also const X_AXIS_SPLIT_NUMBER = 10;