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

UBER-828: Fix slow value filter #3676

Merged
merged 1 commit into from
Sep 11, 2023
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
69 changes: 41 additions & 28 deletions plugins/view-resources/src/components/filter/ValueFilter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
import core, { Class, Doc, FindResult, getObjectValue, Ref, SortingOrder, Space } from '@hcengineering/core'
import presentation, { getClient } from '@hcengineering/presentation'
import ui, {
deviceOptionsStore,
EditWithIcon,
Icon,
IconSearch,
IconCheck,
IconSearch,
Label,
Loading,
resizeObserver,
deviceOptionsStore
resizeObserver
} from '@hcengineering/ui'
import { Filter } from '@hcengineering/view'
import { createEventDispatcher } from 'svelte'
import { getPresenter } from '../../utils'
import { FILTER_DEBOUNCE_MS, sortFilterValues } from '../../filter'
import view from '../../plugin'
import { getPresenter } from '../../utils'

export let _class: Ref<Class<Doc>>
export let space: Ref<Space> | undefined = undefined
Expand Down Expand Up @@ -79,36 +79,49 @@
? []
: (await client.findAll(core.class.Space, { archived: true }, { projection: { _id: 1 } })).map((it) => it._id)

objectsPromise = client.findAll(
_class,
{
...resultQuery,
...(space ? { space } : isDerivedFromSpace ? { archived: false } : { space: { $nin: archived } })
},
{
sort: { [filter.key.key]: SortingOrder.Ascending },
projection: { [prefix + filter.key.key]: 1, space: 1 },
...(space || isDerivedFromSpace ? {} : { lookup: { space: core.class.Space } })
async function doQuery (limit: number | undefined, first1000?: any[]): Promise<void> {
const p = client.findAll(
_class,
{
...resultQuery,
...(space ? { space } : isDerivedFromSpace ? { archived: false } : { space: { $nin: archived } }),
...(first1000 ? { [filter.key.key]: { $nin: first1000 } } : {})
},
{
sort: { modifiedOn: SortingOrder.Descending },
projection: { [prefix + filter.key.key]: 1 },
...(limit !== undefined ? { limit } : {})
}
)
if (limit !== undefined) {
objectsPromise = p
}
)
const res = await objectsPromise

for (const object of res) {
let asDoc = object
if (hierarchy.isMixin(filter.key._class)) {
asDoc = hierarchy.as(object, filter.key._class)
const res = await p

for (const object of res) {
let asDoc = object
if (hierarchy.isMixin(filter.key._class)) {
asDoc = hierarchy.as(object, filter.key._class)
}
const realValue = getObjectValue(filter.key.key, asDoc)
const value = getValue(realValue)
values.add(value)
realValues.set(value, (realValues.get(value) ?? new Set()).add(realValue))
}
for (const object of filter.value.map((p) => p[0])) {
values.add(object)
}
const realValue = getObjectValue(filter.key.key, asDoc)
const value = getValue(realValue)
values.add(value)
realValues.set(value, (realValues.get(value) ?? new Set()).add(realValue))
}
for (const object of filter.value.map((p) => p[0])) {
values.add(object)
}
await doQuery(1000)
values = values
sortedValues = sortFilterValues([...values.keys()], (v) => isSelected(v, selectedValues))
objectsPromise = undefined

// Check if we have all possible values, in case of enumeration
await doQuery(undefined, Array.from(values))

sortedValues = sortFilterValues([...values.keys()], (v) => isSelected(v, selectedValues))
objectsPromise = undefined
}

function getValue (obj: any): any {
Expand Down