Skip to content

Commit

Permalink
coinciding filters results
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpkane committed Nov 15, 2024
1 parent 4ec4acc commit 3f03ea5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,17 @@ export const stringSearchResults = selectorFamily<
};

if (!modal && get(fos.queryPerformance)) {
const filters = Object.fromEntries(
Object.entries(get(fos.filters) || {}).filter(([p]) => p !== path)
);

return {
values: get(
fos.lightningStringResults({
path,
search,
exclude: [...selected.filter((s) => s !== null)] as string[],
filters,
})
)?.map((value) => ({ value, count: null })),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ export default function (
const shown =
(!modal && queryPerformance) ||
(resultsLoadable.state !== "loading" && (length >= CHECKBOX_LIMIT || id));
const isFrameField = useRecoilValue(fos.isFrameField(path));

return {
results,

useSearch:
path === "_label_tags" && queryPerformance && !modal
path === "_label_tags" && queryPerformance && !isFrameField && !modal
? undefined
: useSearch,
showSearch: Boolean(shown) && !boolean,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/packages/state/src/recoil/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { indexedPaths } from "./queryPerformance";
import { expandPath, fields } from "./schema";
import { hiddenLabelIds } from "./selectors";
import { sidebarExpandedStore } from "./sidebarExpanded";
import { State } from "./types";
import type { State } from "./types";

export const modalFilters = sessionAtom({
key: "modalFilters",
Expand Down
8 changes: 7 additions & 1 deletion app/packages/state/src/recoil/pathData/lightningString.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import type { SerializableParam } from "recoil";
import { selectorFamily } from "recoil";
import { lightningQuery } from "../queryPerformance";

export const lightningStringResults = selectorFamily<
string[],
{ path: string; search?: string; exclude?: string[] }
{
path: string;
search?: string;
exclude?: string[];
filters: SerializableParam;
}
>({
key: "lightningStringResults",
get:
Expand Down
21 changes: 6 additions & 15 deletions app/packages/state/src/recoil/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,6 @@ export namespace State {
info: { [key: string]: string };
}

/**
* @hidden
*/
export interface CategoricalFilter<T> {
values: T[];
isMatching: boolean;
exclude: boolean;
}

/**
* @hidden
*/
export type Filter = CategoricalFilter<string>;

export interface SortBySimilarityParameters {
brainKey: string;
distField?: string;
Expand All @@ -178,8 +164,13 @@ export namespace State {
queryIds?: string[];
}

type FilterValues = string | boolean | number | null | undefined;

export interface Filter {
[key: string]: FilterValues | Array<FilterValues>;
}

export interface Filters {
_label_tags?: CategoricalFilter<string>;
[key: string]: Filter;
}

Expand Down
1 change: 1 addition & 0 deletions app/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ input LightningPathInput {
exclude: [String!] = null
first: Int = 200
search: String = null
filters: BSON = null
}

interface LightningResult {
Expand Down
44 changes: 37 additions & 7 deletions fiftyone/server/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

import fiftyone.server.constants as foc
from fiftyone.server.data import Info
from fiftyone.server.scalars import BSON
from fiftyone.server.utils import meets_type
from fiftyone.server.view import get_view


_TWENTY_FOUR = 24
Expand All @@ -37,6 +39,7 @@ class LightningPathInput:
)
first: t.Optional[int] = foc.LIST_LIMIT
search: t.Optional[str] = None
filters: t.Optional[BSON] = None


@gql.input
Expand Down Expand Up @@ -135,7 +138,7 @@ async def lightning_resolver(
for collection, sublist in zip(collections, queries)
for item in sublist
]
result = await _do_async_pooled_queries(flattened)
result = await _do_async_pooled_queries(dataset, flattened)

results = []
offset = 0
Expand All @@ -154,6 +157,7 @@ class DistinctQuery:
is_object_id_field: bool
exclude: t.Optional[t.List[str]] = None
search: t.Optional[str] = None
filters: t.Optional[BSON] = None


def _resolve_lightning_path_queries(
Expand Down Expand Up @@ -258,6 +262,7 @@ def _resolve_object_id(results):
d["has_list"] = _has_list(dataset, field_path, is_frame_field)
d["is_object_id_field"] = True
d["path"] = field_path
d["filters"] = path.filters
return (
collection,
[DistinctQuery(**d)],
Expand All @@ -273,6 +278,7 @@ def _resolve_string(results):
d["has_list"] = _has_list(dataset, field_path, is_frame_field)
d["is_object_id_field"] = False
d["path"] = field_path
d["filters"] = path.filters
return (
collection,
[DistinctQuery(**d)],
Expand All @@ -283,24 +289,29 @@ def _resolve_string(results):


async def _do_async_pooled_queries(
dataset: fo.Dataset,
queries: t.List[
t.Tuple[AsyncIOMotorCollection, t.Union[DistinctQuery, t.List[t.Dict]]]
]
],
):
return await asyncio.gather(
*[_do_async_query(collection, query) for collection, query in queries]
*[
_do_async_query(dataset, collection, query)
for collection, query in queries
]
)


async def _do_async_query(
dataset: fo.Dataset,
collection: AsyncIOMotorCollection,
query: t.Union[DistinctQuery, t.List[t.Dict]],
):
if isinstance(query, DistinctQuery):
if query.has_list:
if query.has_list and not query.filters:
return await _do_distinct_query(collection, query)

return await _do_distinct_pipeline(collection, query)
return await _do_distinct_pipeline(dataset, collection, query)

return [i async for i in collection.aggregate(query)]

Expand Down Expand Up @@ -336,9 +347,28 @@ async def _do_distinct_query(


async def _do_distinct_pipeline(
collection: AsyncIOMotorCollection, query: DistinctQuery
dataset: fo.Dataset,
collection: AsyncIOMotorCollection,
query: DistinctQuery,
):
pipeline = [{"$sort": {query.path: 1}}]
pipeline = []
if query.filters:
pipeline += get_view(dataset, filters=query.filters)._pipeline()

pipeline += [{"$sort": {query.path: 1}}]

if query.search:
if query.is_object_id_field:
add = (_TWENTY_FOUR - len(query.search)) * "0"
value = {"$gte": ObjectId(f"{query.search}{add}")}
else:
value = Regex(f"^{query.search}")
pipeline.append({"$match": {query.path: value}})

pipeline += _match_arrays(dataset, query.path, False) + _unwind(
dataset, query.path, False
)

if query.search:
if query.is_object_id_field:
add = (_TWENTY_FOUR - len(query.search)) * "0"
Expand Down

0 comments on commit 3f03ea5

Please sign in to comment.