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

feat: add custom filter function #632

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
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
55 changes: 54 additions & 1 deletion filters.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
const {
FilterByTypeOptions,
FilterByAccessOptions,
FILTER_VALUES
} = require('src/@types/aquarius/SearchQuery')

module.exports = {
filters: [],
filters: [
{
id: 'serviceType',
label: 'Service Type',
type: 'filterList',
queryPath: 'metadata.type',
options: [
{ label: 'datasets', value: FilterByTypeOptions.Data },
{ label: 'algorithms', value: FilterByTypeOptions.Algorithm },
{ label: 'saas', value: FilterByTypeOptions.Saas }
]
},
{
id: 'accessType',
label: 'Access Type',
type: 'filterList',
queryPath: 'services.type',
options: [
{ label: 'download', value: FilterByAccessOptions.Download },
{ label: 'compute', value: FilterByAccessOptions.Compute }
]
},
{
id: 'gaiax',
label: 'Gaia-X Service',
type: 'filterList',
options: [
{
label: 'Service SD',
value: FILTER_VALUES.MUST_EXISTS_AND_NON_EMPTY,
queryPath:
'metadata.additionalInformation.gaiaXInformation.serviceSD.url'
},
{
label: 'Terms and Conditions',
value: FILTER_VALUES.MUST_EXIST,
queryPath:
'metadata.additionalInformation.gaiaXInformation.termsAndConditions'
},
{
label: 'Verified',
value: FILTER_VALUES.MUST_EXIST,
queryPath:
'metadata.additionalInformation.gaiaXInformation.serviceSd.isValid'
}
]
}
],
filterSets: {}
}
4 changes: 3 additions & 1 deletion src/@context/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ function FilterProvider({ children }: { children: ReactNode }): ReactElement {
const [filters, setFilters] = useState<Filters>({
accessType: [],
serviceType: [],
filterSet: []
filterSet: [],
gaiax: [],
custom: []
})
const [ignorePurgatory, setIgnorePurgatory] = useState<boolean>(true)
const [sort, setSort] = useState<Sort>({
Expand Down
23 changes: 23 additions & 0 deletions src/@types/aquarius/BaseQueryParams.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,37 @@
size?: number
}

type FieldPath = string

interface BoolFilter<T extends FieldPath> {
bool: MustExistQuery<T> & MustNotTermQuery<T>
}

interface MustExistQuery<T extends FieldPath> {
must: {
exists: {
field: T
}
}
}

interface MustNotTermQuery<T extends FieldPath> {
must_not?: {
term: {
[field in T]: string
}
}
}

interface BaseQueryParams {
chainIds: number[]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
nestedQuery?: any
esPaginationOptions?: EsPaginationOptions
sortOptions?: SortOptions
aggs?: any

Check warning on line 34 in src/@types/aquarius/BaseQueryParams.d.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

Unexpected any. Specify a different type
filters?: FilterTerm[]
boolFilter?: BoolFilter
ignorePurgatory?: boolean
ignoreState?: boolean
showSaas?: boolean
Expand Down
5 changes: 5 additions & 0 deletions src/@types/aquarius/SearchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export enum FilterByAccessOptions {
Compute = 'compute'
}

export enum FILTER_VALUES {
MUST_EXIST = 'MUST_EXIST', // checks if the queryPath exists
MUST_EXISTS_AND_NON_EMPTY = 'MUST_EXISTS_AND_NON_EMPTY' // checks if the queryPath exists and is not empty
}

declare global {
interface SortOptions {
sortBy: SortTermOptions
Expand Down
82 changes: 69 additions & 13 deletions src/@utils/aquarius/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { OrdersData_orders as OrdersData } from '../../@types/subgraph/OrdersData'
import { metadataCacheUri, allowDynamicPricing } from '../../../app.config'
import {
FILTER_VALUES,
FilterByTypeOptions,
SortDirectionOptions,
SortTermOptions
Expand Down Expand Up @@ -49,36 +50,87 @@
): FilterTerm {
const isArray = Array.isArray(value)
const useKey = key === 'term' ? (isArray ? 'terms' : 'term') : key
return {
[useKey]: {
[filterField]: value
const filters = []
let field
if (isArray) {
for (const val of value) {
if (typeof val === 'string' && val.includes('=')) {
const [fieldFilter, values] = val.split('=') as [string, string]
field = fieldFilter
filters.push(values)
}
}
}
if (filters.length > 0) {
return {
[useKey]: {
[field]: filters
}
}
} else if (filterField) {
return {
[useKey]: {
[filterField]: value
}
}
}
}

function splitArg(arg: string) {
return arg.split('=')
}

export function getFilter(args: string | string[]) {
let filters = []
if (typeof args === 'object') {
for (const arg of args) {
filters = [...filters, splitArg(arg)]
}
} else {
filters = [...filters, splitArg(args)]
}

let filter: (MustNotTermQuery<string> & MustExistQuery<string>)[] = []
filters.forEach((filterItem) => {
let query: MustNotTermQuery<string> & MustExistQuery<string> = {
must: {
exists: { field: filterItem[0] }
}
}
if (filterItem[1] === FILTER_VALUES.MUST_EXISTS_AND_NON_EMPTY) {
query = {
...query,
must_not: {
term: { [filterItem[0] + '.keyword']: '' }
}
}
}
filter = [...filter, query]
})

return filter
}

export function parseFilters(
filtersList: Filters,
filterSets: { [key: string]: string[] }
): FilterTerm[] {
const filterQueryPath = {
accessType: 'services.type',
serviceType: 'metadata.type',
filterSet: 'metadata.tags.keyword'
}

const filterTerms = Object.keys(filtersList)?.map((key) => {
filtersList[key] = filtersList[key].filter(
(filter) =>
!filter.includes(FILTER_VALUES.MUST_EXISTS_AND_NON_EMPTY) &&
!filter.includes(FILTER_VALUES.MUST_EXIST)
)
if (key === 'filterSet') {
const tags = filtersList[key].reduce(
(acc, set) => [...acc, ...filterSets[set]],
[]
)
const uniqueTags = [...new Set(tags)]
return uniqueTags.length > 0
? getFilterTerm(filterQueryPath[key], uniqueTags)
: undefined
return uniqueTags.length > 0 ? getFilterTerm(null, uniqueTags) : undefined
}
if (filtersList[key].length > 0)
return getFilterTerm(filterQueryPath[key], filtersList[key])
return getFilterTerm(null, filtersList[key])

return undefined
})
Expand All @@ -90,7 +142,7 @@
const { whitelists } = addressConfig

const whitelistFilterTerms = Object.entries(whitelists)
.filter(([field, whitelist]) => whitelist.length > 0)

Check warning on line 145 in src/@utils/aquarius/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

'field' is defined but never used
.map(([field, whitelist]) =>
whitelist.map((address) => getFilterTerm(field, address, 'match'))
)
Expand Down Expand Up @@ -170,6 +222,10 @@
generatedQuery.aggs = baseQueryParams.aggs
}

if (baseQueryParams.boolFilter !== undefined) {
generatedQuery.query.bool.filter.push(...baseQueryParams.boolFilter)
}

if (baseQueryParams.sortOptions !== undefined)
generatedQuery.sort = {
[baseQueryParams.sortOptions.sortBy]:
Expand Down
80 changes: 47 additions & 33 deletions src/components/Search/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,15 @@ interface FilterStructure {
id: string
label: string
type: string
queryPath?: string
options: {
label: string
value: string
queryPath?: string
}[]
}

const filterList: FilterStructure[] = [
{
id: 'serviceType',
label: 'Service Type',
type: 'filterList',
options: [
{ label: 'datasets', value: FilterByTypeOptions.Data },
{ label: 'algorithms', value: FilterByTypeOptions.Algorithm },
{ label: 'saas', value: FilterByTypeOptions.Saas }
]
},
{
id: 'accessType',
label: 'Access Type',
type: 'filterList',
options: [
{ label: 'download', value: FilterByAccessOptions.Download },
{ label: 'compute', value: FilterByAccessOptions.Compute }
]
},
...(Array.isArray(customFilters?.filters) &&
customFilters?.filters?.length > 0 &&
customFilters?.filters.some((filter) => filter !== undefined)
Expand Down Expand Up @@ -114,10 +97,25 @@ export default function Filter({
router.push(urlLocation)
}

async function handleSelectedFilter(value: string, filterId: string) {
const updatedFilters = filters[filterId].includes(value)
? { ...filters, [filterId]: filters[filterId].filter((e) => e !== value) }
: { ...filters, [filterId]: [...filters[filterId], value] }
async function handleSelectedFilter(
option: { label: string; value: string; queryPath?: string },
filterId: string,
queryPath?: string
) {
const getFilterQueryString = `${option.queryPath || queryPath}=${
option.value
}`
const updatedFilters = filters[filterId].includes(getFilterQueryString)
? {
...filters,
[filterId]: filters[filterId].filter(
(filter) => filter !== getFilterQueryString
)
}
: {
...filters,
[filterId]: [...filters[filterId], getFilterQueryString]
}
setFilters(updatedFilters)

await applyFilter(updatedFilters[filterId], filterId)
Expand Down Expand Up @@ -172,20 +170,26 @@ export default function Filter({
}
>
<div className={styleClasses}>
{filterList.map((filter) => (
<div key={filter.id} className={styles.filterType}>
{filterList.map((filter, index) => (
<div key={filter.id + index} className={styles.filterType}>
<h5 className={styles.filterTypeLabel}>{filter.label}</h5>
{filter.options.map((option) => {
const isSelected = filters[filter.id].includes(option.value)
const isSelected = filters[filter.id].includes(
`${option.queryPath || filter.queryPath}=${option.value}`
)
return (
<Input
key={option.value}
key={option.value + option.queryPath}
name={option.label}
type="checkbox"
options={[option.label]}
checked={isSelected}
onChange={async () => {
handleSelectedFilter(option.value, filter.id)
handleSelectedFilter(
option,
filter.id,
filter?.queryPath
)
}}
/>
)
Expand All @@ -210,25 +214,35 @@ export default function Filter({
</Accordion>
</div>
<div className={styles.topPositioning}>
{filterList.map((filter) => (
<div key={filter.id} className={styles.compactFilterContainer}>
{filterList.map((filter, index) => (
<div
key={filter.id + index}
className={styles.compactFilterContainer}
>
<Accordion
title={filter.label}
badgeNumber={filters[filter.id].length}
compact
>
<div className={styles.compactOptionsContainer}>
{filter.options.map((option) => {
const isSelected = filters[filter.id].includes(option.value)
const isSelected = filters[filter.id].includes(
`${option.queryPath || filter.queryPath}=${option.value}`
)

return (
<Input
key={option.value}
key={option.value + option.queryPath}
name={option.label}
type="checkbox"
options={[option.label]}
checked={isSelected}
onChange={async () => {
handleSelectedFilter(option.value, filter.id)
handleSelectedFilter(
option,
filter.id,
filter?.queryPath
)
}}
/>
)
Expand Down
Loading
Loading