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 18, 2024
1 parent a0bf3fe commit 2087111
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
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 @@ -306,32 +306,59 @@ 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,
},
type: {
type: 'string',
enum: exploreFilterTypesV2,
oneOf: [
{
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,
},
values: {
type: 'array',
items: {
type: ['string', 'number', 'null'],
{
type: 'object',
description: 'A filter that specifies which data to include in the query',
properties: {
field: {
type: 'string',
enum: filterableDimensions,
},
type: {
type: 'string',
enum: exploreFilterTypesV2,
},
value: {
type: ['string', 'number', 'null'],
},
},
required: [
'field',
'type',
'value',
],
additionalProperties: false,
},
},
required: [
'dimension',
'type',
'values',
],
additionalProperties: false,
},
} as const satisfies JSONSchema)

Expand Down

0 comments on commit 2087111

Please sign in to comment.