-
Notifications
You must be signed in to change notification settings - Fork 605
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
ObjectId Sidebar Filter Fixes #5415
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -345,8 +345,13 @@ async def _do_distinct_query( | |
query: DistinctQuery, | ||
): | ||
match = None | ||
matcher = lambda v: False | ||
if query.search: | ||
match = query.search | ||
matcher = lambda v: match not in v | ||
if query.is_object_id_field: | ||
match = match[:_TWENTY_FOUR] | ||
matcher = lambda v: v < match | ||
benjaminpkane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
try: | ||
result = await collection.distinct(query.path) | ||
|
@@ -358,10 +363,13 @@ async def _do_distinct_query( | |
exclude = set(query.exclude or []) | ||
|
||
for value in result: | ||
if query.is_object_id_field: | ||
value = str(value) | ||
|
||
if value in exclude: | ||
continue | ||
|
||
if not value or (match and match not in value): | ||
if not value or matcher(value): | ||
continue | ||
|
||
values.append(value) | ||
|
@@ -386,25 +394,19 @@ async def _do_distinct_pipeline( | |
|
||
pipeline.append({"$sort": {query.path: 1}}) | ||
|
||
match_search = None | ||
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}}) | ||
match_search = _add_search(query) | ||
pipeline.append(match_search) | ||
|
||
pipeline += _match_arrays(dataset, query.path, False) + _unwind( | ||
match_arrays = _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" | ||
value = {"$gte": ObjectId(f"{query.search}{add}")} | ||
else: | ||
value = Regex(f"^{query.search}") | ||
pipeline.append({"$match": {query.path: value}}) | ||
if match_arrays: | ||
pipeline += match_arrays | ||
if match_search: | ||
# match again after unwinding list fields | ||
pipeline.append(match_search) | ||
|
||
pipeline += [{"$group": {"_id": f"${query.path}"}}] | ||
|
||
|
@@ -423,6 +425,23 @@ async def _do_distinct_pipeline( | |
return values | ||
|
||
|
||
def _add_search(query: DistinctQuery): | ||
# strip chars after 24 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean sidebar only filters by first 24 chars? So long file path filters could potentially not include the file name? What's the purpose of limiting to the first 24 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good comment. I refactored this incorrectly. It should only apply to ObjectId fields I think I will add frontend validation to omit these queries completely. That is a more holistic approach. An object id is always a 24 char hexadecimal string. The search results can be omitted when the search does not comply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will undraft when I'm happy with the solution |
||
if query.is_object_id_field: | ||
search = query.search[:_TWENTY_FOUR] | ||
add = (_TWENTY_FOUR - len(search)) * "0" | ||
if add: | ||
search = f"{search}{add}" | ||
try: | ||
value = {"$gte": ObjectId(search)} | ||
except: | ||
# search is not valid | ||
value = {"$lt": ObjectId("0" * _TWENTY_FOUR)} | ||
else: | ||
value = Regex(f"^{search}") | ||
return {"$match": {query.path: value}} | ||
benjaminpkane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def _first( | ||
path: str, | ||
dataset: fo.Dataset, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve the ObjectId validation logic.
The current implementation has a few issues that could be improved:
Apply this diff to improve the implementation:
This implementation:
📝 Committable suggestion