Repository navigation
[BUGFIX] (table): expand time series for PivotByLabel transforms #830
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
Open
colivi
wants to merge
16
commits into
perses:main
Choose a base branch
from
colivi:fix/table-pivot-time-expansion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ea10772
fix(table): expand time series for PivotByLabel transforms
colivi dcbc9ac
style(table): oxfmt table-data-utils
colivi 39f2235
Merge branch 'main' into fix/table-pivot-time-expansion
colivi f857d09
Merge branch 'main' into fix/table-pivot-time-expansion
colivi 7af6de5
Merge branch 'main' into fix/table-pivot-time-expansion
shahrokni bd8a829
fix(table): gate pivot expansion on last transform + options
colivi ac8a4aa
Merge branch 'main' into fix/table-pivot-time-expansion
colivi f04e2ca
Merge branch 'main' into fix/table-pivot-time-expansion
colivi a7bbf3a
Merge branch 'main' into fix/table-pivot-time-expansion
colivi 77192c3
Merge branch 'main' into fix/table-pivot-time-expansion
colivi 69c3973
fix(table): expand when any transform needs each sample
colivi b86d726
Merge branch 'main' into fix/table-pivot-time-expansion
colivi 8d7865b
Merge branch 'main' into fix/table-pivot-time-expansion
colivi 387db38
fix(table): drop the components probe and restore table docs
colivi 16d454a
Merge branch 'main' into fix/table-pivot-time-expansion
colivi 3ef7888
Merge branch 'main' into fix/table-pivot-time-expansion
colivi 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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ import type { Labels, PanelData } from '@perses-dev/plugin-system'; | |
| import type { JsonData, TimeSeries, TimeSeriesData } from '@perses-dev/spec'; | ||
|
|
||
| import type { TableOptions } from './models'; | ||
| import { transformRequiresEachSample } from './transform-requirements'; | ||
|
|
||
| /** | ||
| * Options for building raw table data. | ||
|
|
@@ -25,14 +26,31 @@ export interface BuildRawTableDataOptions { | |
| * When false, plugin columns will contain embedded PanelData objects (for rendering). | ||
| */ | ||
| forExport?: boolean; | ||
| /** One row per sample. Defaults to needsTimeSeriesExpansion. */ | ||
| expandTimeSeries?: boolean; | ||
| /** Query mode already resolved for this panel. Defaults to getTablePanelQueryMode. */ | ||
| queryMode?: 'instant' | 'range'; | ||
| } | ||
|
|
||
| /** True when an enabled transform needs one row per timestamp. */ | ||
| export function needsTimeSeriesExpansion(spec: TableOptions): boolean { | ||
| return (spec.transforms ?? []).some( | ||
| (transform) => transform.spec?.disabled !== true && transformRequiresEachSample(transform.kind), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Determines the query mode based on table options. | ||
| * If any column has a plugin (embedded panel), use range mode; otherwise instant. | ||
| * If any column has a plugin (embedded panel), or a transform needs every sample, use range mode; otherwise instant. | ||
| */ | ||
| export function getTablePanelQueryMode(spec: TableOptions): 'instant' | 'range' { | ||
| return (spec.columnSettings ?? []).some((c) => c.plugin) ? 'range' : 'instant'; | ||
| if ((spec.columnSettings ?? []).some((c) => c.plugin)) { | ||
| return 'range'; | ||
| } | ||
| if (needsTimeSeriesExpansion(spec)) { | ||
| return 'range'; | ||
| } | ||
| return 'instant'; | ||
| } | ||
|
|
||
| /** | ||
|
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. Why comment is deleted? |
||
|
|
@@ -67,21 +85,11 @@ function buildTimeSeriesTableData( | |
| options: BuildRawTableDataOptions = {}, | ||
| ): Array<Record<string, unknown>> { | ||
| const { forExport = false } = options; | ||
| const queryMode = getTablePanelQueryMode(spec); | ||
|
|
||
| return queryResults | ||
| .flatMap((data: PanelData<TimeSeriesData>, queryIndex: number) => | ||
| (data.data?.series ?? []).map((ts: TimeSeries) => ({ data, ts, queryIndex })), | ||
| ) | ||
| .map(({ data, ts, queryIndex }: { data: PanelData<TimeSeriesData>; ts: TimeSeries; queryIndex: number }) => { | ||
| // Pick the last (most recent) data point. For range responses the last | ||
| // value covers the window ending at the selected time range's end. | ||
| const lastPoint = ts.values[ts.values.length - 1]; | ||
| if (lastPoint === undefined) { | ||
| return { ...ts.labels }; | ||
| } | ||
| const queryMode = options.queryMode ?? getTablePanelQueryMode(spec); | ||
| const expandTime = options.expandTimeSeries ?? needsTimeSeriesExpansion(spec); | ||
|
|
||
| // If there are multiple queries, add query index to value key and label keys to avoid conflicts | ||
| return queryResults.flatMap((data: PanelData<TimeSeriesData>, queryIndex: number) => | ||
| (data.data?.series ?? []).flatMap((ts: TimeSeries) => { | ||
| const valueColumnName = queryResults.length === 1 ? 'value' : `value #${queryIndex + 1}`; | ||
| const labels = | ||
| queryResults.length === 1 | ||
|
|
@@ -91,8 +99,24 @@ function buildTimeSeriesTableData( | |
| return acc; | ||
| }, {} as Labels); | ||
|
|
||
| // For export: always use raw scalar values | ||
| // For rendering: plugin columns get embedded PanelData objects | ||
| const points = ts.values ?? []; | ||
| if (points.length === 0) { | ||
| return [{ ...labels }]; | ||
| } | ||
|
|
||
| if (expandTime) { | ||
| return points.map(([tsMs, value]) => ({ | ||
| timestamp: tsMs, | ||
| [valueColumnName]: value, | ||
| ...labels, | ||
| })); | ||
| } | ||
|
|
||
| const lastPoint = points[points.length - 1]; | ||
| if (lastPoint === undefined) { | ||
| return [{ ...labels }]; | ||
| } | ||
|
|
||
| let columnValue: unknown; | ||
| if (forExport) { | ||
| columnValue = lastPoint[1]; | ||
|
|
@@ -104,11 +128,11 @@ function buildTimeSeriesTableData( | |
| } | ||
|
|
||
| if (queryMode === 'instant') { | ||
| return { timestamp: lastPoint[0], [valueColumnName]: columnValue, ...labels }; | ||
| } else { | ||
| return { [valueColumnName]: columnValue, ...labels }; | ||
| return [{ timestamp: lastPoint[0], [valueColumnName]: columnValue, ...labels }]; | ||
| } | ||
| }); | ||
| return [{ [valueColumnName]: columnValue, ...labels }]; | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| function safeStringify(v: object): string { | ||
|
|
||
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,18 @@ | ||
| // 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. | ||
|
|
||
| const TRANSFORMS_REQUIRING_EACH_SAMPLE = new Set(['PivotByLabel']); | ||
|
|
||
| export function transformRequiresEachSample(kind: string): boolean { | ||
| return TRANSFORMS_REQUIRING_EACH_SAMPLE.has(kind); | ||
| } |
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.
One concern I have about combining multiple transforms. Since enabled
PivotByLabelmakesneedsTimeSeriesExpansionreturntrue, the whole table switches to range mode. Could this change the input shape seen by other transforms when are combined withPivotByLable? Sicne transform order matters, I wonder whether combinations such as MergeSeries -> PivotByLable or JoinByColumnValue -> PivotByLabel could behave differently because they now receive expanded time-series dat.