Skip to content

Commit

Permalink
Fix task search problems (#1047)
Browse files Browse the repository at this point in the history
* Do not fail filtering by args:foo if task.args is None

* Do not fail filtering by kwargs:foo=bar if task.kwargs is None

* Do not fail filtering by result:foo if task.result is None

* Do not fail filtering by kwargs:partialkeyvalue until '=' is typed

* Add tests for changes in task filtering
  • Loading branch information
cb109 authored Dec 4, 2020
1 parent 1122a12 commit ef4c557
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
11 changes: 9 additions & 2 deletions flower/utils/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def parse_search_terms(raw_search_value):
elif query_part.startswith('kwargs:'):
if 'kwargs'not in parsed_search:
parsed_search['kwargs'] = {}
key, value = [p.strip() for p in query_part[len('kwargs:'):].split('=')]
try:
key, value = [p.strip() for p in query_part[len('kwargs:'):].split('=')]
except ValueError:
continue
parsed_search['kwargs'][key] = preprocess_search_value(value)
elif query_part.startswith('state'):
if 'state' not in parsed_search:
Expand All @@ -47,7 +50,7 @@ def satisfies_search_terms(task, search_terms):
filter(None, [task.name, task.uuid, task.state,
task.worker.hostname if task.worker else None,
task.args, task.kwargs, safe_str(task.result)])),
result_search_term and result_search_term in task.result,
result_search_term and task.result and result_search_term in task.result,
kwargs_search_terms and all(
stringified_dict_contains_value(k, v, task.kwargs) for k, v in kwargs_search_terms.items()
),
Expand All @@ -61,6 +64,8 @@ def stringified_dict_contains_value(key, value, str_dict):
key/value pair. This works faster, then creating actual dict
from string since this operation is called for each task in case
of kwargs search."""
if not str_dict:
return False
value = str(value)
try:
# + 3 for key right quote, one for colon and one for space
Expand All @@ -80,4 +85,6 @@ def preprocess_search_value(raw_value):


def task_args_contains_search_args(task_args, search_args):
if not task_args:
return False
return all(a in task_args for a in search_args)
25 changes: 25 additions & 0 deletions tests/unit/utils/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ def test_kwargs(self):
parse_search_terms('kwargs:some_kwarg1=some_value1 kwargs:some_kwarg2=some_value2')
)

def test_partial_kwargs(self):
self.assertEqual(
{'kwargs': {}},
parse_search_terms('kwargs:some_kwarg')
)
self.assertEqual(
{'kwargs': {'some_kwarg': ''}},
parse_search_terms('kwargs:some_kwarg=')
)

def test_args(self):
self.assertEqual(
{'args': ['some_value']},
Expand Down Expand Up @@ -65,6 +75,13 @@ def test_stringifies_args(self):
stringified_dict_contains_value('test', 5, "{'test': 5}")
)

def test_works_for_no_kwargs(self):
self.assertEqual(
False,
stringified_dict_contains_value('foo', 'bar', None)
)


def test_works_for_nonexisting_kwargs(self):
self.assertEqual(
False,
Expand All @@ -89,6 +106,7 @@ def setUp(self):
self.task = self._create_task(
args=['arg1'],
kwargs="{'kwarg1': 1, 'kwarg2': 22, 'kwarg3': '345'}",
result=None,
)

def test_kwarg_search_works(self):
Expand Down Expand Up @@ -124,5 +142,12 @@ def test_args_search_works(self):
)


def test_result_search_handles_none(self):
self.assertEqual(
False,
satisfies_search_terms(self.task, dict(result=['result1']))
)


if __name__ == '__main__':
unittest.main()

0 comments on commit ef4c557

Please sign in to comment.