Skip to content

Commit

Permalink
more filtering options for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetretto committed Sep 15, 2023
1 parent 434becc commit 7bcd12e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
19 changes: 11 additions & 8 deletions src/jobflow_remote/cli/flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from datetime import datetime, timedelta

import typer
from rich.prompt import Confirm
from rich.text import Text
Expand All @@ -16,6 +14,7 @@
flow_ids_opt,
flow_state_opt,
force_opt,
hours_opt,
job_flow_id_flag_opt,
job_ids_opt,
max_results_opt,
Expand All @@ -31,6 +30,7 @@
exit_with_error_msg,
exit_with_warning_msg,
get_job_db_ids,
get_start_date,
loading_spinner,
out_console,
)
Expand All @@ -52,6 +52,7 @@ def flows_list(
end_date: end_date_opt = None,
name: name_opt = None,
days: days_opt = None,
hours: hours_opt = None,
verbosity: verbosity_opt = 0,
max_results: max_results_opt = 100,
sort: sort_opt = SortOption.UPDATED_ON.value,
Expand All @@ -60,13 +61,12 @@ def flows_list(
"""
Get the list of Jobs in the database
"""
check_incompatible_opt({"start_date": start_date, "days": days})
check_incompatible_opt({"end_date": end_date, "days": days})
check_incompatible_opt({"start_date": start_date, "days": days, "hours": hours})
check_incompatible_opt({"end_date": end_date, "days": days, "hours": hours})

jc = JobController()

if days:
start_date = datetime.now() - timedelta(days=days)
start_date = get_start_date(start_date, days, hours)

sort = [(sort.query_field, 1 if reverse_sort else -1)]

Expand Down Expand Up @@ -105,13 +105,16 @@ def delete(
end_date: end_date_opt = None,
name: name_opt = None,
days: days_opt = None,
hours: hours_opt = None,
force: force_opt = False,
):
"""
Permanently delete Flows from the database
"""
check_incompatible_opt({"start_date": start_date, "days": days})
check_incompatible_opt({"end_date": end_date, "days": days})
check_incompatible_opt({"start_date": start_date, "days": days, "hours": hours})
check_incompatible_opt({"end_date": end_date, "days": days, "hours": hours})

start_date = get_start_date(start_date, days, hours)

jc = JobController()

Expand Down
11 changes: 6 additions & 5 deletions src/jobflow_remote/cli/job.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import io
from datetime import datetime, timedelta
from pathlib import Path

import typer
Expand All @@ -14,6 +13,7 @@
db_ids_opt,
end_date_opt,
flow_ids_opt,
hours_opt,
job_db_id_arg,
job_ids_indexes_opt,
job_index_arg,
Expand All @@ -38,6 +38,7 @@
exit_with_warning_msg,
get_job_db_ids,
get_job_ids_indexes,
get_start_date,
loading_spinner,
out_console,
print_success_msg,
Expand Down Expand Up @@ -65,6 +66,7 @@ def jobs_list(
name: name_opt = None,
metadata: metadata_opt = None,
days: days_opt = None,
hours: hours_opt = None,
verbosity: verbosity_opt = 0,
max_results: max_results_opt = 100,
sort: sort_opt = SortOption.UPDATED_ON.value,
Expand All @@ -76,16 +78,15 @@ def jobs_list(
Get the list of Jobs in the database
"""
check_incompatible_opt({"state": state, "remote-state": remote_state})
check_incompatible_opt({"start_date": start_date, "days": days})
check_incompatible_opt({"end_date": end_date, "days": days})
check_incompatible_opt({"start_date": start_date, "days": days, "hours": hours})
check_incompatible_opt({"end_date": end_date, "days": days, "hours": hours})
metadata_dict = convert_metadata(metadata)

job_ids_indexes = get_job_ids_indexes(job_id)

jc = JobController()

if days:
start_date = datetime.now() - timedelta(days=days)
start_date = get_start_date(start_date, days, hours)

sort = [(sort.query_field, 1 if reverse_sort else -1)]

Expand Down
11 changes: 11 additions & 0 deletions src/jobflow_remote/cli/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"--start-date",
"-sdate",
help="Initial date for last update field",
formats=["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d", "%H:%M:%S", "%H:%M:%S"],
),
]

Expand All @@ -140,6 +141,16 @@
]


hours_opt = Annotated[
Optional[int],
typer.Option(
"--hours",
"-hs",
help="Last update field is in the last hours",
),
]


verbosity_opt = Annotated[
int,
typer.Option(
Expand Down
18 changes: 18 additions & 0 deletions src/jobflow_remote/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import uuid
from contextlib import contextmanager
from datetime import datetime, timedelta
from enum import Enum

import typer
Expand Down Expand Up @@ -201,3 +202,20 @@ def convert_metadata(string_metadata: str | None) -> dict | None:
metadata = {split[0]: split[1]}

return metadata


def get_start_date(start_date: datetime | None, days: int | None, hours: int | None):

if start_date and (start_date.year, start_date.month, start_date.day) == (
1900,
1,
1,
):
now = datetime.now()
start_date = start_date.replace(year=now.year, month=now.month, day=now.day)
elif days:
start_date = datetime.now() - timedelta(days=days)
elif hours:
start_date = datetime.now() - timedelta(hours=hours)

return start_date
4 changes: 2 additions & 2 deletions src/jobflow_remote/jobs/jobcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ def _build_query_wf(

# at variance with Firework doc, the dates in the Workflow are Date objects
if start_date:
start_date_str = start_date.astimezone(timezone.utc)
start_date_str = start_date.astimezone(timezone.utc).isoformat()
query["updated_on"] = {"$gte": start_date_str}
if end_date:
end_date_str = end_date.astimezone(timezone.utc)
end_date_str = end_date.astimezone(timezone.utc).isoformat()
query["updated_on"] = {"$lte": end_date_str}

if name:
Expand Down

0 comments on commit 7bcd12e

Please sign in to comment.