Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit ea6d332

Browse files
committed
Add jobs list command
1 parent d877bec commit ea6d332

File tree

4 files changed

+953
-3
lines changed

4 files changed

+953
-3
lines changed

paperspace/cli/jobs.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import click
22

33
from paperspace import client, config
4-
from paperspace.cli.common import api_key_option
4+
from paperspace.cli import common
55
from paperspace.commands import jobs as jobs_commands
66

77

@@ -16,7 +16,7 @@ def jobs_group():
1616
"job_id",
1717
required=True,
1818
)
19-
@api_key_option
19+
@common.api_key_option
2020
def delete_job(job_id, api_key=None):
2121
jobs_api = client.API(config.CONFIG_HOST, api_key=api_key)
2222
command = jobs_commands.DeleteJobCommand(api=jobs_api)
@@ -29,8 +29,16 @@ def delete_job(job_id, api_key=None):
2929
"job_id",
3030
required=True,
3131
)
32-
@api_key_option
32+
@common.api_key_option
3333
def stop_job(job_id, api_key=None):
3434
jobs_api = client.API(config.CONFIG_HOST, api_key=api_key)
3535
command = jobs_commands.StopJobCommand(api=jobs_api)
3636
command.execute(job_id)
37+
38+
39+
@jobs_group.command("list", help="List jobs with optional filtering")
40+
@common.api_key_option
41+
def list_jobs(api_key):
42+
jobs_api = client.API(config.CONFIG_HOST, api_key=api_key)
43+
command = jobs_commands.ListJobsCommand(api=jobs_api)
44+
command.execute()

paperspace/commands/jobs.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
import pydoc
2+
3+
import terminaltables
4+
15
from paperspace.commands import CommandBase
6+
from paperspace.utils import get_terminal_lines
27

38

49
class JobsCommandBase(CommandBase):
@@ -35,3 +40,44 @@ def execute(self, job_id):
3540
self._log_message(response,
3641
"Job stopped",
3742
"Unknown error while stopping job")
43+
44+
45+
class ListJobsCommand(JobsCommandBase):
46+
def execute(self):
47+
response = self.api.get("/jobs/getJobs/", json=None)
48+
49+
try:
50+
data = response.json()
51+
if not response.ok:
52+
self.logger.log_error_response(data)
53+
return
54+
except (ValueError, KeyError) as e:
55+
self.logger.log("Error while parsing response data: {}".format(e))
56+
else:
57+
self._log_jobs_list(data)
58+
59+
def _log_jobs_list(self, data):
60+
if not data:
61+
self.logger.log("No jobs found")
62+
else:
63+
table_str = self._make_table(data)
64+
if len(table_str.splitlines()) > get_terminal_lines():
65+
pydoc.pager(table_str)
66+
else:
67+
self.logger.log(table_str)
68+
69+
@staticmethod
70+
def _make_table(jobs):
71+
data = [("ID", "Name", "Project", "Cluster", "Machine Type", "Created")]
72+
for job in jobs:
73+
id_ = job.get("id")
74+
name = job.get("name")
75+
project = job.get("project")
76+
cluster = job.get("cluster")
77+
machine_type = job.get("machineType")
78+
created = job.get("dtCreated")
79+
data.append((id_, name, project, cluster, machine_type, created))
80+
81+
ascii_table = terminaltables.AsciiTable(data)
82+
table_string = ascii_table.table
83+
return table_string

0 commit comments

Comments
 (0)