-
Notifications
You must be signed in to change notification settings - Fork 1.9k
perf: getLabelName & store legends graph efficiently #9967
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ import { DataSource } from 'types/common/queryBuilder'; | |
|
|
||
| import { GraphClickProps } from '../useGraphClickToShowButton'; | ||
| import { NavigateToExplorerPagesProps } from '../useNavigateToExplorerPages'; | ||
| import { LegendEntryProps } from './FullView/types'; | ||
| import { LegendEntryProps, SavedLegendGraph } from './FullView/types'; | ||
| import { | ||
| showAllDataSet, | ||
| showAllDataSetFromApiResponse, | ||
|
|
@@ -44,14 +44,12 @@ export const getLocalStorageGraphVisibilityState = ({ | |
| ], | ||
| }; | ||
|
|
||
| if (localStorage.getItem(LOCALSTORAGE.GRAPH_VISIBILITY_STATES) !== null) { | ||
| const legendGraphFromLocalStore = localStorage.getItem( | ||
| LOCALSTORAGE.GRAPH_VISIBILITY_STATES, | ||
| ); | ||
| let legendFromLocalStore: { | ||
| name: string; | ||
| dataIndex: LegendEntryProps[]; | ||
| }[] = []; | ||
| const legendGraphFromLocalStore = localStorage.getItem( | ||
| LOCALSTORAGE.GRAPH_VISIBILITY_STATES, | ||
| ); | ||
|
|
||
| if (legendGraphFromLocalStore !== null) { | ||
| let legendFromLocalStore: SavedLegendGraph[] = []; | ||
|
|
||
| try { | ||
| legendFromLocalStore = JSON.parse(legendGraphFromLocalStore || '[]'); | ||
|
|
@@ -62,7 +60,6 @@ export const getLocalStorageGraphVisibilityState = ({ | |
| ); | ||
| } | ||
|
|
||
| const newGraphVisibilityStates = Array(apiResponse.length + 1).fill(true); | ||
| legendFromLocalStore.forEach((item) => { | ||
| const newName = name; | ||
| if (item.name === newName) { | ||
|
|
@@ -73,11 +70,10 @@ export const getLocalStorageGraphVisibilityState = ({ | |
| dataKey.label === | ||
| getLabelName(datasets.metric, datasets.queryName, datasets.legend || ''), | ||
| ); | ||
| if (index !== -1) { | ||
| newGraphVisibilityStates[i + 1] = item.dataIndex[index].show; | ||
| } | ||
| visibilityStateAndLegendEntry.graphVisibilityStates[i + 1] = item.inverted | ||
| ? index === -1 | ||
| : index !== -1; | ||
|
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. Visibility state restoration logic is completely invertedHigh Severity The visibility state restoration from localStorage produces the opposite of the saved state. When π¬ Verification TestWhy verification test was not possible: This is a frontend React application that requires a browser environment and localStorage. The bug can be verified by tracing the logic manually:
Why this proves the bug: The ternary conditions are swapped. The correct logic would be 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. Old localStorage format ignored during migrationMedium Severity When reading localStorage data saved in the old format (which included π¬ Verification TestWhy verification test was not possible: This requires a browser environment with localStorage. The bug can be verified by tracing the logic:
Why this proves the bug: The old |
||
| }); | ||
| visibilityStateAndLegendEntry.graphVisibilityStates = newGraphVisibilityStates; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
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.
Legend entries replaced with incomplete subset from storage
Medium Severity
The
legendEntryproperty is directly assigneditem.dataIndex, which only contains a subset of legends (either visible or hidden items, depending on theinvertedflag) stored for efficiency. This replaces the complete legend list (initialized fromshowAllDataSetFromApiResponse) with an incomplete subset, potentially causing missing legend entries in the UI. ThelegendEntryneeds to be reconstructed to include all entries, not just the stored subset.π¬ Verification Test
Why verification test was not possible: This requires a browser environment with localStorage and the full React application context. The bug can be verified by tracing the logic:
legendEntryis set with ALL legends from API response (lines 38-44)item.dataIndexcontains only a subset (e.g., if 3/10 items are hidden andinverted=false, only those 3 hidden items are stored)visibilityStateAndLegendEntry.legendEntry = item.dataIndexlegendEntrynow only has 3 items instead of 10Why this proves the bug: The storage optimization stores only a subset of legends, but the restore logic incorrectly uses this subset as the complete legend list.