Skip to content

Commit 711a5d1

Browse files
committed
Enrich example list_jobs.py with more info
1 parent b2f54c9 commit 711a5d1

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

ravenpackapi/examples/list_jobs.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,46 @@
1+
"""
2+
Example that lists jobs (processing, completed or in error) that have been
3+
updated in a time range, and prints them.
4+
"""
5+
6+
import datetime
17
from ravenpackapi import RPApi
28

39
# initialize the API (here we use the RP_API_KEY in os.environ)
4-
api = RPApi()
10+
PRODUCT = "rpa" # Or PRODUCT = "edge"
11+
api = RPApi(product=PRODUCT)
12+
13+
14+
def print_jobs(jobs):
15+
for job in jobs:
16+
print_job(job)
17+
18+
19+
def print_job(job):
20+
status = "failed" if job.status == "error" else job.status
21+
start, end = [job._data[date] for date in ("start_date", "end_date")]
22+
id = job.token
23+
print(f"Job {status} with ID {id} with data ranging from {start} to {end}")
24+
25+
26+
if __name__ == "__main__":
27+
# Define start_date and end_date or use the current date
28+
# start_date = "2022-11-22"
29+
# end_date = "2022-11-23"
30+
DAYS_BACK = 1
31+
end_date = datetime.datetime.utcnow().replace(microsecond=0)
32+
start_date = end_date - datetime.timedelta(days=DAYS_BACK)
33+
34+
# Print all finished_jobs: completed or in error
35+
finished_jobs = api.list_jobs(start_date, end_date, status=["completed", "error"])
36+
print(
37+
f"{len(finished_jobs)} jobs completed or in error between {start_date} and {end_date}:"
38+
)
39+
print_jobs(finished_jobs)
540

6-
for job in api.list_jobs('2020-01-01', '2020-10-10', status=['COMPLETED']):
7-
print(job)
41+
# Print running jobs
42+
jobs = api.list_jobs(start_date, end_date, status=["processing"])
43+
print(
44+
f"{len(jobs)} jobs processing between {start_date} and {end_date} with tokens:"
45+
)
46+
print_jobs(jobs)

0 commit comments

Comments
 (0)