Repository navigation
feat: Render static value list filter inputs #3022
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@hyperdx/app": patch | ||
| --- | ||
|
|
||
| feat: Display static value filters |
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
154 changes: 154 additions & 0 deletions
154
packages/app/src/hooks/__tests__/useDashboardFilterValues.test.tsx
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,154 @@ | ||
| import React from 'react'; | ||
| import { | ||
| DashboardFilter, | ||
| SourceKind, | ||
| StaticListDashboardFilter, | ||
| TSource, | ||
| } from '@hyperdx/common-utils/dist/types'; | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import { renderHook, waitFor } from '@testing-library/react'; | ||
|
|
||
| import { useDashboardFilterValues } from '@/hooks/useDashboardFilterValues'; | ||
|
|
||
| const mockGetKeyValues = jest.fn(); | ||
| let mockSourcesData: Partial<TSource>[]; | ||
|
|
||
| // Untyped factories, so partial mocks need no unsafe type assertions. | ||
| jest.mock('@/source', () => ({ | ||
| useSources: () => ({ data: mockSourcesData, isLoading: false }), | ||
| })); | ||
| jest.mock('../useMetadata', () => ({ | ||
| useMetadataWithSettings: () => ({ getKeyValues: mockGetKeyValues }), | ||
| })); | ||
| jest.mock('@hyperdx/common-utils/dist/core/materializedViews', () => ({ | ||
| optimizeGetKeyValuesCalls: jest | ||
| .fn() | ||
| .mockImplementation(async ({ keys, chartConfig }) => [ | ||
| { keys, chartConfig }, | ||
| ]), | ||
| optimizeFacetedKeyValuesConfig: jest | ||
| .fn() | ||
| .mockImplementation(async ({ chartConfig }) => chartConfig), | ||
| })); | ||
|
|
||
| describe('useDashboardFilterValues', () => { | ||
| let queryClient: QueryClient; | ||
| let wrapper: React.ComponentType<{ children: React.ReactNode }>; | ||
|
|
||
| // Deliberately not alphabetical: options must keep definition order. | ||
| const staticFilter: StaticListDashboardFilter = { | ||
| id: 'static1', | ||
| type: 'STATIC_LIST', | ||
| name: 'Tier', | ||
| options: ['low', 'medium', 'high'], | ||
| isBroadcastEnabled: false, | ||
| isVariableEnabled: true, | ||
| variableName: 'tier', | ||
| }; | ||
|
|
||
| const queriedFilter: DashboardFilter = { | ||
| id: 'queried1', | ||
| type: 'QUERY_EXPRESSION', | ||
| name: 'Environment', | ||
| expression: 'environment', | ||
| source: 'logs-source', | ||
| }; | ||
|
|
||
| const mockDateRange: [Date, Date] = [ | ||
| new Date('2024-01-01'), | ||
| new Date('2024-01-02'), | ||
| ]; | ||
|
|
||
| beforeEach(() => { | ||
| mockSourcesData = [ | ||
| { | ||
| id: 'logs-source', | ||
| kind: SourceKind.Log, | ||
| name: 'Logs', | ||
| timestampValueExpression: 'timestamp', | ||
| connection: 'clickhouse-conn', | ||
| from: { | ||
| databaseName: 'telemetry', | ||
| tableName: 'logs', | ||
| }, | ||
| }, | ||
| ]; | ||
| mockGetKeyValues.mockReset(); | ||
| mockGetKeyValues.mockImplementation(({ keys }: { keys: string[] }) => | ||
| Promise.resolve( | ||
| keys.map(key => ({ key, value: ['production', 'staging'] })), | ||
| ), | ||
| ); | ||
|
|
||
| queryClient = new QueryClient({ | ||
| defaultOptions: { queries: { retry: false } }, | ||
| }); | ||
| wrapper = ({ children }) => ( | ||
| <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
| ); | ||
| }); | ||
|
|
||
| it('returns static options in definition order without loading or fetching', () => { | ||
|
pulpdrew marked this conversation as resolved.
Resolved
|
||
| const { result } = renderHook( | ||
| () => | ||
| useDashboardFilterValues({ | ||
| filters: [staticFilter], | ||
| dateRange: mockDateRange, | ||
| }), | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| // Static values resolve synchronously, and the queried hook's perpetual | ||
| // "loading" for an empty filter list must not leak through. | ||
| expect(result.current.isLoading).toBe(false); | ||
| expect(result.current.isFetching).toBe(false); | ||
| expect(result.current.isError).toBe(false); | ||
| expect(result.current.data).toEqual( | ||
| new Map([ | ||
| ['static1', { values: ['low', 'medium', 'high'], isLoading: false }], | ||
| ]), | ||
| ); | ||
| expect(mockGetKeyValues).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('merges static and queried values keyed by filter id', async () => { | ||
| const { result } = renderHook( | ||
| () => | ||
| useDashboardFilterValues({ | ||
| filters: [staticFilter, queriedFilter], | ||
| dateRange: mockDateRange, | ||
| }), | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| await waitFor(() => expect(result.current.isFetching).toBe(false)); | ||
|
|
||
| expect(result.current.data).toEqual( | ||
| new Map([ | ||
| ['queried1', { values: ['production', 'staging'], isLoading: false }], | ||
| ['static1', { values: ['low', 'medium', 'high'], isLoading: false }], | ||
| ]), | ||
| ); | ||
| }); | ||
|
|
||
| it('surfaces queried errors without implicating static filters', async () => { | ||
| mockGetKeyValues.mockRejectedValue(new Error('query failed')); | ||
|
|
||
| const { result } = renderHook( | ||
| () => | ||
| useDashboardFilterValues({ | ||
| filters: [staticFilter, queriedFilter], | ||
| dateRange: mockDateRange, | ||
| }), | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| await waitFor(() => expect(result.current.isError).toBe(true)); | ||
|
|
||
| expect(result.current.erroredFilterIds).toEqual(new Set(['queried1'])); | ||
| expect(result.current.data.get('static1')).toEqual({ | ||
| values: ['low', 'medium', 'high'], | ||
| isLoading: false, | ||
| }); | ||
| }); | ||
| }); | ||
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,78 @@ | ||
| import { useMemo } from 'react'; | ||
| import { FilterSelection } from '@hyperdx/common-utils/dist/dashboardFilterValues'; | ||
| import { | ||
| isQueryExpressionFilter, | ||
| isStaticListFilter, | ||
| } from '@hyperdx/common-utils/dist/filters'; | ||
| import { | ||
| ChartVariable, | ||
| DashboardFilter, | ||
| } from '@hyperdx/common-utils/dist/types'; | ||
|
|
||
| import { useQueriedDashboardFilterValues } from './useQueriedDashboardFilterValues'; | ||
| import { useStaticDashboardFilterValues } from './useStaticDashboardFilterValues'; | ||
|
|
||
| export type DashboardFilterValuesResult = { | ||
| /** Dropdown values keyed by `filter.id` */ | ||
| data: ReadonlyMap<string, { values: string[]; isLoading: boolean }>; | ||
| /** Filter IDs whose values lookup failed, or never resolved. */ | ||
| erroredFilterIds: ReadonlySet<string>; | ||
| /** Error messages, when known, for filters in `erroredFilterIds`. */ | ||
| filterErrorMessages: ReadonlyMap<string, string>; | ||
| isLoading: boolean; | ||
| isFetching: boolean; | ||
| isError: boolean; | ||
| }; | ||
|
|
||
| /** Dropdown values for a dashboard's filters. */ | ||
| export function useDashboardFilterValues({ | ||
|
Contributor
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. The intention is that this will be a "router" hook that partitions filters by type, delegates to type-specific hooks that fetch values for the type, and then merges the results. This should make it more straightforward to add new filter types in the near future. |
||
| filters, | ||
| dateRange, | ||
| selectionByFilterId, | ||
| variables, | ||
| }: { | ||
| filters: DashboardFilter[]; | ||
| dateRange: [Date, Date]; | ||
| /** Each filter's current selection, keyed by `filter.id`. */ | ||
| selectionByFilterId?: ReadonlyMap<string, FilterSelection>; | ||
| /** The dashboard's variables and their current selections, if any */ | ||
| variables?: ChartVariable[]; | ||
| }): DashboardFilterValuesResult { | ||
| const [queriedFilters, staticFilters] = useMemo( | ||
| () => [ | ||
| filters.filter(isQueryExpressionFilter), | ||
| filters.filter(isStaticListFilter), | ||
| ], | ||
| [filters], | ||
| ); | ||
|
|
||
| const queriedValues = useQueriedDashboardFilterValues({ | ||
| filters: queriedFilters, | ||
| dateRange, | ||
| selectionByFilterId, | ||
| variables, | ||
| }); | ||
|
|
||
| const staticValues = useStaticDashboardFilterValues({ | ||
| filters: staticFilters, | ||
| }); | ||
|
|
||
| const data = useMemo( | ||
| () => new Map([...queriedValues.data, ...staticValues.data]), | ||
| [queriedValues.data, staticValues.data], | ||
| ); | ||
|
|
||
| // The queried hook reports isLoading/isFetching true while sources load even | ||
| // when it was handed no filters at all; only let it speak for itself when it | ||
| // actually has filters to resolve. | ||
| const hasQueriedFilters = queriedFilters.length > 0; | ||
|
|
||
| return { | ||
| data, | ||
| erroredFilterIds: queriedValues.erroredFilterIds, | ||
| filterErrorMessages: queriedValues.filterErrorMessages, | ||
| isLoading: hasQueriedFilters && queriedValues.isLoading, | ||
| isFetching: hasQueriedFilters && queriedValues.isFetching, | ||
| isError: queriedValues.isError, | ||
| }; | ||
| } | ||
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,29 @@ | ||
| import { useMemo } from 'react'; | ||
| import { StaticListDashboardFilter } from '@hyperdx/common-utils/dist/types'; | ||
|
|
||
| /** Stable identities: static filters never error, so these never change. */ | ||
| const EMPTY_ERRORED_IDS: ReadonlySet<string> = new Set(); | ||
| const EMPTY_ERROR_MESSAGES: ReadonlyMap<string, string> = new Map(); | ||
|
|
||
| export function useStaticDashboardFilterValues({ | ||
| filters, | ||
| }: { | ||
| filters: StaticListDashboardFilter[]; | ||
| }) { | ||
| const data = useMemo(() => { | ||
| const byId = new Map<string, { values: string[]; isLoading: boolean }>(); | ||
| for (const filter of filters) { | ||
| byId.set(filter.id, { values: filter.options, isLoading: false }); | ||
| } | ||
| return byId; | ||
| }, [filters]); | ||
|
|
||
| return { | ||
|
pulpdrew marked this conversation as resolved.
Resolved
|
||
| data, | ||
| erroredFilterIds: EMPTY_ERRORED_IDS, | ||
| filterErrorMessages: EMPTY_ERROR_MESSAGES, | ||
| isLoading: false, | ||
| isFetching: false, | ||
| isError: false, | ||
| }; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.