From f60b79c9116989c7ee9b0da8037e092350eff0ad Mon Sep 17 00:00:00 2001 From: Lorenzo Gaifas Date: Tue, 8 Oct 2024 14:56:07 +0200 Subject: [PATCH] fix time wasted --- src/stemia/cryosparc/time_wasted.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/stemia/cryosparc/time_wasted.py b/src/stemia/cryosparc/time_wasted.py index 79a7154..979ebb0 100644 --- a/src/stemia/cryosparc/time_wasted.py +++ b/src/stemia/cryosparc/time_wasted.py @@ -25,12 +25,20 @@ def cli(project_dirs, useful_jobs): ) import json - from datetime import timedelta + import logging + from datetime import datetime, timedelta from pathlib import Path from rich import print + from rich.logging import RichHandler from rich.progress import Progress + logging.basicConfig( + format="%(message)s", + datefmt="[%X]", + handlers=[RichHandler()], + ) + with Progress() as prog: for proj in prog.track(project_dirs, description="Reading projects..."): proj = Path(proj) @@ -63,7 +71,7 @@ def get_parents(job, proj, parents): for job in prog.track(jobs, description="Reading jobs..."): job_meta = job / "job.json" if not job_meta.exists(): - # print(f'Missing job metadata for {job.name}, skipping.') + logging.log(f"Missing job metadata for {job.name}, skipping.") skipped += 1 continue with open(job_meta) as f: @@ -71,7 +79,7 @@ def get_parents(job, proj, parents): start = meta["started_at"] if start is None: - # print(f'job {job.name} was not started, skipping') + logging.info(f"job {job.name} was not started, skipping") skipped += 1 continue @@ -81,12 +89,20 @@ def get_parents(job, proj, parents): if end is None: end = meta["failed_at"] if end is None: - # print(f'job {job.name} was not finished, skipping') + logging.info(f"job {job.name} was not finished, skipping") skipped += 1 continue - running = timedelta(milliseconds=end["$date"] - start["$date"]) - queued = timedelta(milliseconds=start["$date"] - launch["$date"]) + if isinstance(end["$date"], int): + running = timedelta(milliseconds=end["$date"] - start["$date"]) + queued = timedelta(milliseconds=start["$date"] - launch["$date"]) + elif isinstance(end["$date"], str): + running = datetime.fromisoformat( + end["$date"] + ) - datetime.fromisoformat(start["$date"]) + queued = datetime.fromisoformat( + start["$date"] + ) - datetime.fromisoformat(launch["$date"]) total_queued += queued