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

Add status parameter to filter jobs #30

Merged
merged 26 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
22 changes: 20 additions & 2 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
## Release 0.3.0 (development release)

### New features since last release
HudZah marked this conversation as resolved.
Show resolved Hide resolved

* Job lists can now be filtered by status.
[(#30)](https://github.com/XanaduAI/xanadu-cloud-client/pull/30)

Using the CLI:

```bash
xcc job list --status '<Status>'
```

Using the Python API:

```python
xcc.Job.list(connection, status="<Status>")
```


### Contributors

This release contains contributions from (in alphabetical order):

[Hudhayfa Zaheem](https://github.com/HudZah).

## Release 0.2.1 (current release)

### New features since last release
Expand Down Expand Up @@ -38,8 +58,6 @@ This release contains contributions from (in alphabetical order):
## Release 0.2.0
### New features since last release

* The Connection class can now load a Connection from a Settings instance.
[(#22)](https://github.com/XanaduAI/xanadu-cloud-client/pull/22)
Expand Down
4 changes: 1 addition & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#
# The full version, including alpha/beta/rc tags.
import xcc

release = xcc.__version__

# The short X.Y version.
Expand Down Expand Up @@ -253,22 +254,19 @@
html_theme_options = {
"navbar_name": "Xanadu Cloud Client",
"navbar_logo_colour": "#78909c",

"navbar_right_links": [
{
"name": "GitHub",
"href": "https://github.com/XanaduAI/xanadu-cloud-client",
"icon": "fab fa-github",
}
],

"border_colour": "#78909c",
"prev_next_button_colour": "#607d8b",
"prev_next_button_hover_colour": "#34515e",
"toc_marker_colour": "#78909c",
"table_header_background_colour": "#78909c",
"text_accent_colour": "#78909c",

}

edit_on_github_project = "XanaduAI/xanadu-cloud-client"
Expand Down
3 changes: 2 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ docutils
m2r2
nbsphinx
sphinx-copybutton
sphinx>=3.1.0
Jinja2<3.1
sphinx==4.5.0
HudZah marked this conversation as resolved.
Show resolved Hide resolved
xanadu-sphinx-theme==0.1.0
2 changes: 1 addition & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MockJob(xcc.Job):
"""

@staticmethod
def list(connection, limit=10, ids=None):
def list(connection, limit=10, ids=None, status=None):
connection = xcc.commands.load_connection()
return [MockJob(id_, connection) for id_ in ("foo", "bar", "baz")][:limit]

Expand Down
29 changes: 28 additions & 1 deletion tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ def test_list(self, connection, add_response, limit, ids, want_params, want_name
have_params = responses.calls[0].request.params # pyright: reportGeneralTypeIssues=false
assert have_params == want_params

@pytest.mark.parametrize(
"limit, status, want_status_param",
[
(1, "queued", "queued"),
(2, None, None),
(2, "invalid", "invalid"),
(2, "complete", "complete"),
],
)
@responses.activate
def test_list_status(self, connection, add_response, limit, status, want_status_param):
"""Tests that the correct status parameter is encoded in the HTTP
request to the Xanadu Cloud platform when listing jobs.
"""
add_response(body={"data": []}, path="/jobs")
xcc.Job.list(connection, limit=limit, status=status)

have_params = responses.calls[0].request.params # pyright: reportGeneralTypeIssues=false
have_status_param = have_params.get("status")
assert have_status_param == want_status_param

@pytest.mark.parametrize("name", [None, "foo"])
@responses.activate
def test_submit(self, job_id, connection, name):
Expand Down Expand Up @@ -167,7 +188,13 @@ def test_result(self, connection, job):
with io.BytesIO() as buffer:
# The public savez() function does not allow pickling to be disabled.
savez = numpy.lib.npyio._savez # pylint: disable=protected-access
savez(file=buffer, args=output, kwds=metadata, compress=False, allow_pickle=False)
savez(
file=buffer,
args=output,
kwds=metadata,
compress=False,
allow_pickle=False,
)

buffer.seek(0)
body = buffer.read()
Expand Down
5 changes: 3 additions & 2 deletions xcc/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,19 @@ def get_job(


@beautify
def list_jobs(limit: int = 5, ids: List[str] = None) -> Sequence[Mapping]:
def list_jobs(limit: int = 5, ids: List[str] = None, status: str = None) -> Sequence[Mapping]:
"""Lists jobs submitted to the Xanadu Cloud.
Args:
limit (int): Maximum number of jobs to display.
ids (List[str], optional): IDs of the jobs to display. If at least one
ID is specified, the limit flag will be set to the number of IDs.
status (str, optional): If specificed, filter returned jobs by the given status.
Returns:
Sequence[Mapping]: Overview of each job submitted to the Xanadu Cloud.
"""
jobs = Job.list(connection=load_connection(), limit=limit, ids=ids)
jobs = Job.list(connection=load_connection(), limit=limit, ids=ids, status=status)
return [job.overview for job in jobs]


Expand Down
24 changes: 20 additions & 4 deletions xcc/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,30 @@ class Job:

@staticmethod
def list(
connection: Connection, limit: int = 5, ids: Optional[Collection[str]] = None
connection: Connection,
limit: int = 5,
ids: Optional[Collection[str]] = None,
status: Optional[str] = None,
) -> Sequence[Job]:
"""Returns jobs submitted to the Xanadu Cloud.
Args:
connection (Connection): connection to the Xanadu Cloud
limit (int): maximum number of jobs to retrieve
ids (Collection[str], optional): IDs of the jobs to retrieve; if at
least one ID is specified, ``limit`` will be set to the length
of the ID collection
status (str, optional): filter jobs by the given status (if not ``None``)
Returns:
Sequence[Job]: jobs which were submitted on the Xanadu Cloud by the
user associated with the Xanadu Cloud connection
"""
size = len(ids) if ids else limit
response = connection.request("GET", "/jobs", params={"size": size, "id": ids})

params = {"size": size, "id": ids, "status": status}
response = connection.request("GET", "/jobs", params=params)

jobs = []

Expand All @@ -122,7 +129,11 @@ def list(

@staticmethod
def submit(
connection: Connection, name: Optional[str], target: str, circuit: str, language: str
connection: Connection,
name: Optional[str],
target: str,
circuit: str,
language: str,
) -> Job:
"""Submits a job to the Xanadu Cloud.
Expand All @@ -136,7 +147,12 @@ def submit(
Returns:
Job: job submitted to the Xanadu Cloud
"""
payload = {"name": name, "target": target, "circuit": circuit, "language": language}
payload = {
"name": name,
"target": target,
"circuit": circuit,
"language": language,
}
response = connection.request("POST", "/jobs", json=payload)
details = response.json()

Expand Down