From 7bcd12e43c366dada43a125d591cf0187611479f Mon Sep 17 00:00:00 2001 From: Guido Petretto Date: Fri, 15 Sep 2023 17:24:33 +0200 Subject: [PATCH] more filtering options for CLI --- src/jobflow_remote/cli/flow.py | 19 +++++++++++-------- src/jobflow_remote/cli/job.py | 11 ++++++----- src/jobflow_remote/cli/types.py | 11 +++++++++++ src/jobflow_remote/cli/utils.py | 18 ++++++++++++++++++ src/jobflow_remote/jobs/jobcontroller.py | 4 ++-- 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/jobflow_remote/cli/flow.py b/src/jobflow_remote/cli/flow.py index 9057d3a0..a13a2a6e 100644 --- a/src/jobflow_remote/cli/flow.py +++ b/src/jobflow_remote/cli/flow.py @@ -1,5 +1,3 @@ -from datetime import datetime, timedelta - import typer from rich.prompt import Confirm from rich.text import Text @@ -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, @@ -31,6 +30,7 @@ exit_with_error_msg, exit_with_warning_msg, get_job_db_ids, + get_start_date, loading_spinner, out_console, ) @@ -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, @@ -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)] @@ -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() diff --git a/src/jobflow_remote/cli/job.py b/src/jobflow_remote/cli/job.py index 0519fc8c..ebcea6ae 100644 --- a/src/jobflow_remote/cli/job.py +++ b/src/jobflow_remote/cli/job.py @@ -1,5 +1,4 @@ import io -from datetime import datetime, timedelta from pathlib import Path import typer @@ -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, @@ -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, @@ -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, @@ -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)] diff --git a/src/jobflow_remote/cli/types.py b/src/jobflow_remote/cli/types.py index 2a2fbff8..b17d8ec0 100644 --- a/src/jobflow_remote/cli/types.py +++ b/src/jobflow_remote/cli/types.py @@ -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"], ), ] @@ -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( diff --git a/src/jobflow_remote/cli/utils.py b/src/jobflow_remote/cli/utils.py index 8b3ff10b..ad280d81 100644 --- a/src/jobflow_remote/cli/utils.py +++ b/src/jobflow_remote/cli/utils.py @@ -4,6 +4,7 @@ import json import uuid from contextlib import contextmanager +from datetime import datetime, timedelta from enum import Enum import typer @@ -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 diff --git a/src/jobflow_remote/jobs/jobcontroller.py b/src/jobflow_remote/jobs/jobcontroller.py index d5478a75..3740092e 100644 --- a/src/jobflow_remote/jobs/jobcontroller.py +++ b/src/jobflow_remote/jobs/jobcontroller.py @@ -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: