Skip to content

Commit

Permalink
fix: dashboard support new explore filter format
Browse files Browse the repository at this point in the history
  • Loading branch information
filipgutica committed Dec 19, 2024
1 parent a0bf3fe commit ab52c53
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type AnalyticsBridge,
type ExploreAggregations,
type ExploreFilter,
type ExploreFilterAll,
type FilterableExploreDimensions,
type QueryDatasource, stripUnknownFilters,
type Timeframe,
Expand Down Expand Up @@ -30,7 +31,7 @@ interface FetcherOptions {
datasource: Ref<QueryDatasource>
dimension?: FilterableExploreDimensions
dimensionFilterValue?: string
additionalFilter: Ref<ExploreFilter[] | undefined>
additionalFilter: Ref<ExploreFilterAll[] | undefined>
queryReady: Ref<boolean>
timeframe: Ref<Timeframe>
tz: Ref<string>
Expand Down
18 changes: 10 additions & 8 deletions packages/analytics/analytics-utilities/src/types/explore/all.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { type BasicExploreAggregations, type BasicExploreFilter, filterableBasicExploreDimensions } from './basic'
import { type AiExploreAggregations, type AiExploreFilter, filterableAiExploreDimensions } from './ai'
import { type ExploreAggregations, type ExploreFilter, filterableExploreDimensions } from './advanced'
import { type BasicExploreAggregations, type BasicExploreFilterAll, filterableBasicExploreDimensions } from './basic'
import { type AiExploreAggregations, type AiExploreFilterAll, filterableAiExploreDimensions } from './ai'
import { type ExploreAggregations, type ExploreFilterAll, filterableExploreDimensions } from './advanced'

export type AllAggregations = BasicExploreAggregations | AiExploreAggregations | ExploreAggregations
export type AllFilters = BasicExploreFilter | AiExploreFilter | ExploreFilter
export type AllFilters = BasicExploreFilterAll | ExploreFilterAll | AiExploreFilterAll

export const queryDatasources = ['basic', 'advanced', 'ai'] as const

export type QueryDatasource = typeof queryDatasources[number]

export interface FilterTypeMap extends Record<QueryDatasource, AllFilters> {
basic: BasicExploreFilter,
advanced: ExploreFilter,
ai: AiExploreFilter,
basic: BasicExploreFilterAll,
advanced: ExploreFilterAll,
ai: AiExploreFilterAll,
}

export const datasourceToFilterableDimensions: Record<QueryDatasource, Set<string>> = {
Expand All @@ -25,7 +25,9 @@ export const datasourceToFilterableDimensions: Record<QueryDatasource, Set<strin
export const stripUnknownFilters = <K extends keyof typeof datasourceToFilterableDimensions>(datasource: K, filters: AllFilters[]): FilterTypeMap[K][] => {
// Note: once we extend API request filters, this may need to look at more than just dimensions.
// Note the cast; we could potentially try to derive the type, but it doesn't seem worth it.
return filters.filter(f => datasourceToFilterableDimensions[datasource].has(f.dimension)) as FilterTypeMap[K][]
return filters.filter(f => {
'dimension' in f ? datasourceToFilterableDimensions[datasource].has(f['dimension']) : datasourceToFilterableDimensions[datasource].has(f['field'])
}) as FilterTypeMap[K][]
}

// TODO: Add utility func for marking unknown filters (but not stripping them).
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import composables from '../composables'
import {
type AllFilters,
type AnalyticsBridge,
type DatasourceAwareQuery, type ExploreFilter,
type DatasourceAwareQuery,
type ExploreFilterAll,
type ExploreQuery, type ExploreResultV4, type FilterTypeMap, type QueryDatasource, stripUnknownFilters,
} from '@kong-ui-public/analytics-utilities'
import { INJECT_QUERY_PROVIDER } from '../constants'
Expand Down Expand Up @@ -101,7 +102,7 @@ const { data: v4Data, error, isValidating } = useSWRV(queryKey, async () => {
datasource = 'basic'
}
const mergedFilters = deriveFilters(datasource, props.query.filters, props.context.filters)
const mergedFilters = deriveFilters(datasource, props.query.filters as AllFilters[], props.context.filters)
// TODO: similar to other places, consider adding a type guard to ensure the query
// matches the datasource. Currently, this block effectively pretends all queries
Expand All @@ -114,7 +115,7 @@ const { data: v4Data, error, isValidating } = useSWRV(queryKey, async () => {
...props.context.timeSpec,
tz: props.context.tz,
},
filters: mergedFilters as ExploreFilter[],
filters: mergedFilters as ExploreFilterAll[],
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
queryableBasicExploreDimensions,
queryableExploreDimensions,
relativeTimeRangeValuesV4,
requestFilterTypeEmptyV2,
} from '@kong-ui-public/analytics-utilities'

export interface DashboardRendererContext {
Expand Down Expand Up @@ -306,32 +307,78 @@ const dimensionsFn = <T extends readonly string[]>(dimensions: T) => ({

const filtersFn = <T extends readonly string[]>(filterableDimensions: T) => ({
type: 'array',
description: 'A list of filters to apply to the query.',
description: 'A list of filters to apply to the query',
items: {
type: 'object',
description: 'A filter that specifies which data to include in the query',
properties: {
dimension: {
type: 'string',
enum: filterableDimensions,
anyOf: [
{
type: 'object',
description: 'A filter that specifies which data to include in the query',
properties: {
dimension: {
type: 'string',
enum: filterableDimensions,
},
type: {
type: 'string',
enum: exploreFilterTypesV2,
},
values: {
type: 'array',
items: {
type: ['string', 'number', 'null'],
},
},
},
required: [
'dimension',
'type',
'values',
],
additionalProperties: false,
},
type: {
type: 'string',
enum: exploreFilterTypesV2,
{
type: 'object',
description: 'In filter',
properties: {
field: {
type: 'string',
enum: filterableDimensions,
},
operator: {
type: 'string',
enum: exploreFilterTypesV2,
},
value: {
type: ['string', 'number', 'null'],
},
},
required: [
'field',
'operator',
'value',
],
additionalProperties: false,
},
values: {
type: 'array',
items: {
type: ['string', 'number', 'null'],
{
type: 'object',
description: 'Empty filter',
properties: {
field: {
type: 'string',
enum: filterableDimensions,
},
operator: {
type: 'string',
enum: requestFilterTypeEmptyV2,
},
},
required: [
'field',
'operator',
],
additionalProperties: false,
},
},
required: [
'dimension',
'type',
'values',
],
additionalProperties: false,
},
} as const satisfies JSONSchema)

Expand Down

0 comments on commit ab52c53

Please sign in to comment.