Repository navigation
fix: annotations with a null time_end or a partial overlap are not shown on time series charts
#10027
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix: annotations with a null time_end or a partial overlap are not shown on time series charts
#10027
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
runtime/resolvers/testdata/metrics_annotations_time_end.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| project_files: | ||
| duckdb_data.yaml: | ||
| type: model | ||
| connector: duckdb | ||
| sql: | | ||
| select '2024-01-01T00:00:00Z'::TIMESTAMP as time, 1 as val union all | ||
| select '2024-02-01T00:00:00Z'::TIMESTAMP as time, 2 as val union all | ||
| select '2024-03-01T00:00:00Z'::TIMESTAMP as time, 3 as val | ||
| time_end_duckdb_annotation.yaml: | ||
| type: model | ||
| connector: duckdb | ||
| sql: | | ||
| select '2024-01-05T00:00:00Z'::TIMESTAMP as time, NULL::TIMESTAMP as time_end, 'point before' as description | ||
| union all | ||
| select '2024-01-20T00:00:00Z'::TIMESTAMP as time, '2024-02-05T00:00:00Z'::TIMESTAMP as time_end, 'range crossing start' as description | ||
| union all | ||
| select '2024-02-10T00:00:00Z'::TIMESTAMP as time, NULL::TIMESTAMP as time_end, 'point inside' as description | ||
| union all | ||
| select '2024-02-16T00:00:00Z'::TIMESTAMP as time, '2024-02-20T00:00:00Z'::TIMESTAMP as time_end, 'range inside' as description | ||
| union all | ||
| select '2024-02-25T00:00:00Z'::TIMESTAMP as time, '2024-03-10T00:00:00Z'::TIMESTAMP as time_end, 'range crossing end' as description | ||
| union all | ||
| select '2024-03-05T00:00:00Z'::TIMESTAMP as time, NULL::TIMESTAMP as time_end, 'point after' as description | ||
| union all | ||
| select '2024-03-06T00:00:00Z'::TIMESTAMP as time, '2024-03-08T00:00:00Z'::TIMESTAMP as time_end, 'range after' as description | ||
| duckdb_metrics.yaml: | ||
| type: metrics_view | ||
| model: duckdb_data | ||
| timeseries: time | ||
| measures: | ||
| - name: sum | ||
| expression: sum(val) | ||
| annotations: | ||
| - model: time_end_duckdb_annotation | ||
| tests: | ||
| # Point annotations have a null time_end and are selected when their time falls in the range. | ||
| # Range annotations are selected when they overlap the range, even when only partially. | ||
| - name: annotations_overlapping_the_time_range | ||
| resolver: metrics_annotations | ||
| properties: | ||
| metrics_view: duckdb_metrics | ||
| time_range: | ||
| start: 2024-02-01T00:00:00Z | ||
| end: 2024-03-01T00:00:00Z | ||
| result: | ||
| - time: "2024-01-20T00:00:00Z" | ||
| time_end: "2024-02-05T00:00:00Z" | ||
| description: "range crossing start" | ||
| for_measures: ['sum'] | ||
| - time: "2024-02-10T00:00:00Z" | ||
| time_end: null | ||
| description: "point inside" | ||
| for_measures: ['sum'] | ||
| - time: "2024-02-16T00:00:00Z" | ||
| time_end: "2024-02-20T00:00:00Z" | ||
| description: "range inside" | ||
| for_measures: ['sum'] | ||
| - time: "2024-02-25T00:00:00Z" | ||
| time_end: "2024-03-10T00:00:00Z" | ||
| description: "range crossing end" | ||
| for_measures: ['sum'] | ||
| # A range that ends exactly when the time range starts does not overlap it. | ||
| - name: range_ending_at_the_start_is_excluded | ||
| resolver: metrics_annotations | ||
| properties: | ||
| metrics_view: duckdb_metrics | ||
| time_range: | ||
| start: 2024-02-05T00:00:00Z | ||
| end: 2024-02-09T00:00:00Z | ||
| result: [] | ||
| # Without a time range, the metrics view's full time range is used. | ||
| - name: annotations_without_time_range | ||
| resolver: metrics_annotations | ||
| properties: | ||
| metrics_view: duckdb_metrics | ||
| result: | ||
| - time: "2024-01-05T00:00:00Z" | ||
| time_end: null | ||
| description: "point before" | ||
| for_measures: ['sum'] | ||
| - time: "2024-01-20T00:00:00Z" | ||
| time_end: "2024-02-05T00:00:00Z" | ||
| description: "range crossing start" | ||
| for_measures: ['sum'] | ||
| - time: "2024-02-10T00:00:00Z" | ||
| time_end: null | ||
| description: "point inside" | ||
| for_measures: ['sum'] | ||
| - time: "2024-02-16T00:00:00Z" | ||
| time_end: "2024-02-20T00:00:00Z" | ||
| description: "range inside" | ||
| for_measures: ['sum'] | ||
| - time: "2024-02-25T00:00:00Z" | ||
| time_end: "2024-03-10T00:00:00Z" | ||
| description: "range crossing end" | ||
| for_measures: ['sum'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
web-common/src/features/dashboards/time-series/measure-chart/annotation-utils.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import { V1TimeGrain } from "@rilldata/web-common/runtime-client"; | ||
| import { scaleLinear } from "d3-scale"; | ||
| import { DateTime } from "luxon"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| type Annotation, | ||
| AnnotationWidth, | ||
| groupAnnotations, | ||
| } from "./annotation-utils"; | ||
| import type { ChartConfig, ChartScales, TimeSeriesPoint } from "./types"; | ||
|
|
||
| // Four daily data points from Feb 1 to Feb 4, 2024, plotted from x=0 to x=300. | ||
| const data: TimeSeriesPoint[] = [1, 2, 3, 4].map((day) => ({ | ||
| ts: DateTime.utc(2024, 2, day), | ||
| value: day, | ||
| })); | ||
|
|
||
| const plotBounds = { | ||
| left: 0, | ||
| right: 300, | ||
| top: 0, | ||
| bottom: 100, | ||
| width: 300, | ||
| height: 100, | ||
| }; | ||
| const config: ChartConfig = { | ||
| width: 300, | ||
| height: 100, | ||
| margin: { top: 0, right: 0, bottom: 0, left: 0 }, | ||
| plotBounds, | ||
| }; | ||
| const scales: ChartScales = { | ||
| x: scaleLinear() | ||
| .domain([0, data.length - 1]) | ||
| .range([0, 300]), | ||
| y: scaleLinear().domain([0, 4]).range([100, 0]), | ||
| }; | ||
|
|
||
| function annotation(time: string, timeEnd?: string): Annotation { | ||
| return { | ||
| time, | ||
| timeEnd, | ||
| description: time, | ||
| startTime: DateTime.fromISO(time, { zone: "UTC" }), | ||
| endTime: timeEnd ? DateTime.fromISO(timeEnd, { zone: "UTC" }) : undefined, | ||
| formattedTimeOrRange: "", | ||
| }; | ||
| } | ||
|
|
||
| function group(annotations: Annotation[]) { | ||
| return groupAnnotations( | ||
| annotations, | ||
| scales, | ||
| data, | ||
| config, | ||
| V1TimeGrain.TIME_GRAIN_DAY, | ||
| ); | ||
| } | ||
|
|
||
| describe("groupAnnotations", () => { | ||
| it("positions a point annotation at its grain bucket", () => { | ||
| const groups = group([annotation("2024-02-03T06:00:00Z")]); | ||
|
|
||
| expect(groups).toHaveLength(1); | ||
| expect(groups[0].index).toBe(2); | ||
| expect(groups[0].left).toBe(200); | ||
| expect(groups[0].right).toBe(200 + AnnotationWidth); | ||
| expect(groups[0].hasRange).toBe(false); | ||
| }); | ||
|
|
||
| it("groups annotations that fall in the same bucket", () => { | ||
| const groups = group([ | ||
| annotation("2024-02-02T01:00:00Z"), | ||
| annotation("2024-02-02T23:00:00Z"), | ||
| annotation("2024-02-04T00:00:00Z"), | ||
| ]); | ||
|
|
||
| expect(groups.map((g) => g.index)).toEqual([1, 3]); | ||
| expect(groups[0].items).toHaveLength(2); | ||
| }); | ||
|
|
||
| it("spans a range annotation from its start to its end bucket", () => { | ||
| const groups = group([ | ||
| annotation("2024-02-02T00:00:00Z", "2024-02-03T00:00:00Z"), | ||
| ]); | ||
|
|
||
| expect(groups).toHaveLength(1); | ||
| expect(groups[0].left).toBe(100); | ||
| expect(groups[0].right).toBe(200); | ||
| expect(groups[0].hasRange).toBe(true); | ||
| }); | ||
|
|
||
| it("anchors a range that starts before the data to the first data point", () => { | ||
| const groups = group([ | ||
| annotation("2024-01-30T00:00:00Z", "2024-02-03T00:00:00Z"), | ||
| ]); | ||
|
|
||
| expect(groups).toHaveLength(1); | ||
| expect(groups[0].index).toBe(0); | ||
| expect(groups[0].left).toBe(0); | ||
| expect(groups[0].right).toBe(200); | ||
| expect(groups[0].hasRange).toBe(true); | ||
| }); | ||
|
|
||
| it("drops a range that ends exactly where the data starts", () => { | ||
| const groups = group([ | ||
| annotation("2024-01-30T00:00:00Z", "2024-02-01T00:00:00Z"), | ||
| ]); | ||
|
|
||
| expect(groups).toEqual([]); | ||
| }); | ||
|
|
||
| it("extends a range that ends after the data to the last data point", () => { | ||
| const groups = group([ | ||
| annotation("2024-02-03T00:00:00Z", "2024-02-10T00:00:00Z"), | ||
| ]); | ||
|
|
||
| expect(groups).toHaveLength(1); | ||
| expect(groups[0].left).toBe(200); | ||
| expect(groups[0].right).toBe(300); | ||
| }); | ||
|
|
||
| it("drops annotations that do not overlap the data", () => { | ||
| const groups = group([ | ||
| annotation("2024-01-30T00:00:00Z"), | ||
| annotation("2024-01-20T00:00:00Z", "2024-01-25T00:00:00Z"), | ||
| annotation("2024-02-05T00:00:00Z"), | ||
| ]); | ||
|
|
||
| expect(groups).toEqual([]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesnt this need
startOf(unit)?