Skip to content
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

[Example app] Use 'in' operator in facets for date and location attributes #634

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 5 additions & 70 deletions front/example-app/src/services/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ import {
} from '@elastic-suite/gally-admin-shared'

import { IActiveFilters } from '../types'
import { add, format, parse } from 'date-fns'

type DurationKey = 'y' | 'M' | 'd'

const durationFormat: Record<DurationKey, string> = {
y: 'years',
M: 'months',
d: 'days',
}

/* eslint-disable no-underscore-dangle */
export function getProductFilters(
Expand Down Expand Up @@ -44,7 +35,11 @@ export function getProductFilters(
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
lte: Number(activeFilter.value),
} as IEntityIntegerTypeFilterInput
} else if (activeFilter.filter.type === AggregationType.CHECKBOX) {
} else if (
activeFilter.filter.type === AggregationType.CHECKBOX ||
activeFilter.filter.type === AggregationType.HISTOGRAM_DATE ||
activeFilter.filter.type === AggregationType.HISTOGRAM
) {
if (!(activeFilter.filter.field in acc)) {
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
in: [],
Expand All @@ -55,66 +50,6 @@ export function getProductFilters(
activeFilter.filter.field as keyof IProductFieldFilterInput
] as ISelectTypeDefaultFilterInputType
).in.push(activeFilter.value)
} else if (activeFilter.filter.type === AggregationType.HISTOGRAM_DATE) {
if (!acc.boolFilter?._should) {
acc.boolFilter = { _should: [] }
}
const field = activeFilter.filter
.field as keyof IProductFieldFilterInput
const date = parse(
activeFilter.value,
activeFilter.filter.date_format,
new Date()
)

const incrementString = activeFilter.filter.date_range_interval
const incrementNumber = Number(
incrementString.substring(0, incrementString.length - 1)
)
const incrementType = incrementString.substring(
incrementString.length - 1
) as DurationKey

const gt = format(date, activeFilter.filter.date_format)
const lt = format(
add(date, {
[durationFormat[incrementType]]: incrementNumber,
}),
activeFilter.filter.date_format
)

acc.boolFilter._should.push({
[field]: {
gt,
lt,
},
})
} else if (activeFilter.filter.type === AggregationType.HISTOGRAM) {
if (!acc.boolFilter?._should) {
acc.boolFilter = { _should: [] }
}
const field = activeFilter.filter
.field as keyof IProductFieldFilterInput
const arrayValue = activeFilter.value.split('-')
const [firstValue, secondValue] = arrayValue

if (arrayValue.length > 1) {
const isFirstValueIsAsterisk = firstValue === '*'
const isSecondValueIsAsterisk = secondValue === '*'
let data: Record<string, number> = {}

if (isFirstValueIsAsterisk && !isSecondValueIsAsterisk) {
data = { lt: Number(secondValue) }
} else if (!isFirstValueIsAsterisk && !isSecondValueIsAsterisk) {
data = { gte: Number(firstValue), lte: Number(secondValue) }
} else {
data = { gt: Number(firstValue) }
}

acc.boolFilter._should.push({
[field]: data,
})
}
}

return data
Expand Down
Loading