-
Notifications
You must be signed in to change notification settings - Fork 67
[BUGFIX] StatChart: derive query mode from the spec instead of always using range #746
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright The Perses Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import { CalculationType } from '@perses-dev/core'; | ||
| import { createInitialStatChartOptions, getStatChartQueryMode, StatChartOptions } from './stat-chart-model'; | ||
|
|
||
| function options(overrides: Partial<StatChartOptions> = {}): StatChartOptions { | ||
| return { | ||
| calculation: 'last-number', | ||
| format: { unit: 'decimal' }, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe('getStatChartQueryMode', () => { | ||
| it('uses instant when there is no sparkline and the calculation only needs the latest point', () => { | ||
| expect(getStatChartQueryMode(options({ calculation: 'last' }))).toBe('instant'); | ||
| expect(getStatChartQueryMode(options({ calculation: 'last-number' }))).toBe('instant'); | ||
| }); | ||
|
|
||
| // Regression: requesting instant unconditionally broke the sparkline, which draws the | ||
| // series itself. See perses/plugins#738, which was reverted for this reason. | ||
| it('uses range whenever a sparkline is configured', () => { | ||
| expect(getStatChartQueryMode(options({ sparkline: {} }))).toBe('range'); | ||
| expect(getStatChartQueryMode(options({ sparkline: { color: '#fff', width: 2 } }))).toBe('range'); | ||
| // even for a calculation that would otherwise be instant | ||
| expect(getStatChartQueryMode(options({ calculation: 'last', sparkline: {} }))).toBe('range'); | ||
| }); | ||
|
|
||
| it.each<CalculationType>(['mean', 'sum', 'min', 'max', 'first', 'first-number'])( | ||
| 'uses range for %s, which aggregates over the whole series', | ||
| (calculation) => { | ||
| expect(getStatChartQueryMode(options({ calculation }))).toBe('range'); | ||
| } | ||
| ); | ||
|
|
||
| it('uses range for the default options, which enable the sparkline', () => { | ||
| expect(getStatChartQueryMode(createInitialStatChartOptions())).toBe('range'); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,3 +78,32 @@ export function createInitialStatChartOptions(): StatChartOptions { | |
| legendMode: 'auto', | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Calculations that only need the most recent point of a series. Everything else | ||
| * (`mean`, `sum`, `min`, `max`, `first`, `first-number`) is computed over the whole | ||
| * series in `StatChartPanel`, so it needs range data to be correct. | ||
| */ | ||
| const LATEST_POINT_CALCULATIONS: ReadonlySet<CalculationType> = new Set<CalculationType>(['last', 'last-number']); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not expert in that part but what is the difference between last and last-number. If last number means last result of number type or something, could the range query and instance query give different results ?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great question — and you're right, they are not interchangeable here. |
||
|
|
||
| /** | ||
| * A stat panel usually shows a single aggregate value, for which an instant query is | ||
| * enough and much cheaper than a range query — especially against backends that split | ||
| * range queries into many sub-queries. | ||
| * | ||
| * It cannot always be instant though: | ||
| * - the sparkline draws the series itself, so it needs range data; | ||
| * - calculations other than `last`/`last-number` aggregate over the series, and with a | ||
| * single instant point they would silently collapse to that point. | ||
| * | ||
| * Requesting instant unconditionally breaks both (see perses/plugins#738, reverted for | ||
| * the sparkline case), so the mode is derived from the spec. | ||
| */ | ||
| export function getStatChartQueryMode(spec: StatChartOptions): 'instant' | 'range' { | ||
| if (spec.sparkline !== undefined) return 'range'; | ||
| return LATEST_POINT_CALCULATIONS.has(spec.calculation) ? 'instant' : 'range'; | ||
| } | ||
|
|
||
| export function getStatChartQueryOptions(spec: StatChartOptions): { mode: 'instant' | 'range' } { | ||
| return { mode: getStatChartQueryMode(spec) }; | ||
| } | ||
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.
please avoid to use the package
@perses-dev/corebut@perses-dev/specinstead.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.
Fixed in 3ab3dbe. I used
@perses-dev/plugin-systemrather than@perses-dev/spec—CalculationTypeis re-exported there, and it is what every other consumer in the repo uses, includingstat-chart-model.tsright next to this test. That test file was the only place reaching into@perses-dev/coredirectly. Happy to switch to@perses-dev/specif that is the direction you want these to move.