-
Notifications
You must be signed in to change notification settings - Fork 186
feat: default sort config for tables in canvas #9648
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
djbarnwal
wants to merge
4
commits into
main
Choose a base branch
from
feat/default-sort-pivot-canvas
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
4 commits
Select commit
Hold shift + click to select a range
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
208 changes: 208 additions & 0 deletions
208
web-common/src/features/canvas/components/pivot/default-sort.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,208 @@ | ||
| import { | ||
| extractNumbers, | ||
| getTimeGrainFromDimension, | ||
| isTimeDimension, | ||
| } from "@rilldata/web-common/features/dashboards/pivot/pivot-utils"; | ||
| import { | ||
| COMPARISON_DELTA, | ||
| COMPARISON_PERCENT, | ||
| COMPARISON_VALUE, | ||
| type PivotDataStoreConfig, | ||
| } from "@rilldata/web-common/features/dashboards/pivot/types"; | ||
| import { TIME_GRAIN } from "@rilldata/web-common/lib/time/config"; | ||
| import { convertISOStringToJSDateWithSameTimeAsSelectedTimeZone } from "@rilldata/web-common/lib/time/timezone"; | ||
| import { timeFormat } from "d3-time-format"; | ||
| import type { DefaultSort } from "./index"; | ||
|
|
||
| /** Separator between column-dimension values in a nested-sort label. */ | ||
| const LABEL_SEPARATOR = " › "; | ||
|
|
||
| /** Matches a nested pivot leaf accessor, e.g. "c0v2_c1v0m0" or "c0v2m0". */ | ||
| const NESTED_ACCESSOR_RE = /^(c\d+v\d+_?)+m\d+$/; | ||
|
|
||
| /** | ||
| * Builds the human-readable label for a sort id. For stable ids (measure, | ||
| * dimension, time grain) this resolves the display name from the metrics view. | ||
| * For nested pivot leaf accessors it decodes the column-dimension value path | ||
| * and joins it with the measure display name, e.g. "US › 2024 › Sales". | ||
| * | ||
| * Returns the raw id as a fallback when nothing better can be resolved. | ||
| */ | ||
| export function getSortLabel( | ||
| id: string, | ||
| config: PivotDataStoreConfig, | ||
| columnDimensionAxes: Record<string, string[]> = {}, | ||
| ): string { | ||
| if (NESTED_ACCESSOR_RE.test(id)) { | ||
| return getNestedAccessorLabel(id, config, columnDimensionAxes); | ||
| } | ||
| return getFieldLabel(id, config); | ||
| } | ||
|
|
||
| export type SortFieldType = "measure" | "dimension" | "time"; | ||
|
|
||
| export interface SortChip { | ||
| label: string; | ||
| type: SortFieldType; | ||
| } | ||
|
|
||
| /** | ||
| * Breaks a sort id into the chips shown in the inspector. A stable field is a | ||
| * single chip; a nested pivot leaf accessor becomes one chip per column- | ||
| * dimension value followed by the measure chip, e.g. [US, 2024, Sales]. | ||
| */ | ||
| export function getSortChips( | ||
| id: string, | ||
| config: PivotDataStoreConfig, | ||
| columnDimensionAxes: Record<string, string[]> = {}, | ||
| ): SortChip[] { | ||
| if (NESTED_ACCESSOR_RE.test(id)) { | ||
| const [colPart, measureIndexPart] = id.split("m"); | ||
| const parts = colPart.split("_").filter(Boolean); | ||
|
|
||
| const chips: SortChip[] = parts.map((part) => { | ||
| const { c, v } = extractNumbers(part); | ||
| const dimensionName = config.colDimensionNames[c]; | ||
| const value = columnDimensionAxes[dimensionName]?.[v] ?? ""; | ||
| const isTime = isTimeDimension(dimensionName, config.time.timeDimension); | ||
| return { | ||
| label: isTime | ||
| ? formatColumnTimeValue(value, dimensionName, config) | ||
| : value, | ||
| type: isTime ? "time" : "dimension", | ||
| }; | ||
| }); | ||
|
|
||
| const measureName = config.measureNames[parseInt(measureIndexPart)]; | ||
| if (measureName) { | ||
| chips.push({ | ||
| label: getFieldLabel(measureName, config), | ||
| type: "measure", | ||
| }); | ||
| } | ||
| return chips; | ||
| } | ||
|
|
||
| return [ | ||
| { label: getFieldLabel(id, config), type: getSortFieldType(id, config) }, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Whether all chip values for a sort id can be resolved from the currently | ||
| * loaded data. Nested pivot leaf accessors reference column-dimension values | ||
| * that arrive asynchronously; until they load, chips would render blank or, | ||
| * for time dimensions, as "NaN". Stable fields resolve synchronously. | ||
| */ | ||
| export function areSortChipsReady( | ||
| id: string, | ||
| config: PivotDataStoreConfig, | ||
| columnDimensionAxes: Record<string, string[]> = {}, | ||
| ): boolean { | ||
| if (!NESTED_ACCESSOR_RE.test(id)) return true; | ||
|
|
||
| const [colPart] = id.split("m"); | ||
| const parts = colPart.split("_").filter(Boolean); | ||
|
|
||
| return parts.every((part) => { | ||
| const { c, v } = extractNumbers(part); | ||
| const dimensionName = config.colDimensionNames[c]; | ||
| return columnDimensionAxes[dimensionName]?.[v] != null; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the field type of a sort id, used to color the inspector chip. | ||
| * A nested pivot leaf accessor sorts by its measure, so it reads as "measure". | ||
| */ | ||
| export function getSortFieldType( | ||
| id: string, | ||
| config: PivotDataStoreConfig, | ||
| ): SortFieldType { | ||
| if (NESTED_ACCESSOR_RE.test(id)) return "measure"; | ||
| if (isTimeDimension(id, config.time.timeDimension)) return "time"; | ||
| if (config.allMeasures.some((m) => m.name === id)) return "measure"; | ||
| return "dimension"; | ||
| } | ||
|
|
||
| /** Human-readable suffix appended to a base measure for comparison columns. */ | ||
| const COMPARISON_MODIFIER: Record<string, string> = { | ||
| [COMPARISON_VALUE]: " (previous)", | ||
| [COMPARISON_DELTA]: " Δ", | ||
| [COMPARISON_PERCENT]: " Δ %", | ||
| }; | ||
|
|
||
| function formatColumnTimeValue( | ||
| value: string, | ||
| dimensionName: string, | ||
| config: PivotDataStoreConfig, | ||
| ): string { | ||
| const grain = getTimeGrainFromDimension(dimensionName); | ||
| const dt = convertISOStringToJSDateWithSameTimeAsSelectedTimeZone( | ||
| value, | ||
| config.time.timeZone || "UTC", | ||
| ); | ||
| const formatter = timeFormat(grain ? TIME_GRAIN[grain].d3format : "%H:%M"); | ||
| return formatter(dt); | ||
| } | ||
|
|
||
| function getFieldLabel(field: string, config: PivotDataStoreConfig): string { | ||
| if (isTimeDimension(field, config.time.timeDimension)) { | ||
| const grain = getTimeGrainFromDimension(field); | ||
| return `Time ${TIME_GRAIN[grain]?.label ?? grain}`; | ||
| } | ||
|
|
||
| // Comparison columns are the base measure plus a suffix (e.g. "sales__delta_rel"). | ||
| for (const [suffix, modifier] of Object.entries(COMPARISON_MODIFIER)) { | ||
| if (field.endsWith(suffix)) { | ||
| const baseName = field.slice(0, -suffix.length); | ||
| return getFieldLabel(baseName, config) + modifier; | ||
| } | ||
| } | ||
|
|
||
| const measure = config.allMeasures.find((m) => m.name === field); | ||
| if (measure) return measure.displayName || (measure.name as string); | ||
|
|
||
| const dimension = config.allDimensions.find( | ||
| (d) => (d.name || d.column) === field, | ||
| ); | ||
| if (dimension) { | ||
| return ( | ||
| dimension.displayName || dimension.name || (dimension.column as string) | ||
| ); | ||
| } | ||
|
|
||
| return field; | ||
| } | ||
|
|
||
| function getNestedAccessorLabel( | ||
| accessor: string, | ||
| config: PivotDataStoreConfig, | ||
| columnDimensionAxes: Record<string, string[]>, | ||
| ): string { | ||
| const [colPart, measureIndexPart] = accessor.split("m"); | ||
| const parts = colPart.split("_").filter(Boolean); | ||
|
|
||
| const valueLabels = parts.map((part) => { | ||
| const { c, v } = extractNumbers(part); | ||
| const dimensionName = config.colDimensionNames[c]; | ||
| return columnDimensionAxes[dimensionName]?.[v] ?? ""; | ||
| }); | ||
|
|
||
| const measureName = config.measureNames[parseInt(measureIndexPart)]; | ||
| const measureLabel = measureName ? getFieldLabel(measureName, config) : ""; | ||
|
|
||
| return [...valueLabels, measureLabel].filter(Boolean).join(LABEL_SEPARATOR); | ||
| } | ||
|
|
||
| /** | ||
| * Whether two sort configs target the same column and direction. Used to | ||
| * disable the "Make default" button when the active sort already is the default. | ||
| */ | ||
| export function sortEquals( | ||
| a: DefaultSort | undefined, | ||
| b: { id: string; desc: boolean } | undefined, | ||
| ): boolean { | ||
| if (!a || !b) return false; | ||
| return a.id === b.id && a.desc === b.desc; | ||
| } |
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
Oops, something went wrong.
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.
When a saved default sort points at a field that is no longer part of the current table (for example, set the default on
sales, then removesalesfrommeasures/columnsor change the metrics view), this always rehydrates that stale id intopivotState.sorting. For nested pivots,getSortForAccessoronly treats ids in the currentmeasureNamesor row dimension as stable; a stale stable id falls through to nested-accessor parsing and can indexcolumnDimensionAxes[undefined][NaN], crashing the table, while flat tables send a sort for a non-selected field. Please clear or validatedefault_sortagainst the current fields before applying it.Useful? React with 👍 / 👎.