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

[batch] Fix list batches query and test #13237

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions batch/batch/front_end/front_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,11 @@ async def _query_batches(request, user: str, q: str, version: int, last_batch_id
async def get_batches_v1(request, userdata): # pylint: disable=unused-argument
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should fix the types so that we cannot make this mistake in the future. There's a couple things we need to do:

  1. add type annotations here: request: web.Request (userdata is a little harder to type, but maybe just Dict[str, str] for now?)
  2. Fix _handle_api_error to properly use types. It needs to use ParamSpec (see also the ParamSpec docs). This seems to type correctly and also cause mypy errors where it should:
T = TypeVar('T')
P = ParamSpec('P')

async def _handle_api_error(f: Callable[P, Awaitable[T]], *args: P.args, **kwargs: P.kwargs) -> Optional[T]:

user = userdata['username']
q = request.query.get('q', f'user:{user}')

last_batch_id = request.query.get('last_batch_id')
if last_batch_id is not None:
last_batch_id = int(last_batch_id)

batches, last_batch_id = await _handle_api_error(_query_batches, request, user, q, 1, last_batch_id)
body = {'batches': batches}
if last_batch_id is not None:
Expand All @@ -649,6 +653,8 @@ async def get_batches_v2(request, userdata): # pylint: disable=unused-argument
user = userdata['username']
q = request.query.get('q', f'user = {user}')
last_batch_id = request.query.get('last_batch_id')
if last_batch_id is not None:
last_batch_id = int(last_batch_id)
batches, last_batch_id = await _handle_api_error(_query_batches, request, user, q, 2, last_batch_id)
body = {'batches': batches}
if last_batch_id is not None:
Expand Down
38 changes: 32 additions & 6 deletions batch/test/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,24 +481,50 @@ def assert_batch_ids(expected: Set[int], q=None):

assert_batch_ids(
{b1.id, b2.id},
'''
f'''
start_time >= 2023-02-24T17:15:25Z
end_time < 3000-02-24T17:15:25Z
tag = {tag}
''',
)

assert_batch_ids(
set(),
'''
f'''
start_time >= 2023-02-24T17:15:25Z
end_time == 2023-02-24T17:15:25Z
tag = {tag}
''',
)

assert_batch_ids(set(), 'duration > 50000')
assert_batch_ids(set(), 'cost > 1000')
assert_batch_ids({b1.id}, f'batch_id = {b1.id}')
assert_batch_ids({b1.id}, f'batch_id == {b1.id}')
assert_batch_ids(
set(),
f'''
duration > 50000
tag = {tag}
''',
)
assert_batch_ids(
set(),
f'''
cost > 1000
tag = {tag}
''',
)
assert_batch_ids(
{b1.id},
f'''
batch_id = {b1.id}
tag = {tag}
''',
)
assert_batch_ids(
{b1.id},
f'''
batch_id == {b1.id}
tag = {tag}
''',
)

with pytest.raises(httpx.ClientResponseError, match='could not parse term'):
assert_batch_ids(batch_id_test_universe, 'batch_id >= 1 abcde')
Expand Down