From 9d3153b8779edc5f2337355685d790f6ceb5a6d4 Mon Sep 17 00:00:00 2001 From: Leonardo Schwarz Date: Mon, 12 Aug 2024 14:47:21 +0200 Subject: [PATCH] make use of entities api to make the script nicer --- ...list_not_available_proteomics_workunits.py | 31 +++++++++---------- docs/changelog.md | 2 +- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/bfabric/scripts/bfabric_list_not_available_proteomics_workunits.py b/bfabric/scripts/bfabric_list_not_available_proteomics_workunits.py index 61de4fc7..f50d035a 100755 --- a/bfabric/scripts/bfabric_list_not_available_proteomics_workunits.py +++ b/bfabric/scripts/bfabric_list_not_available_proteomics_workunits.py @@ -14,7 +14,6 @@ from argparse import ArgumentParser from datetime import datetime, timedelta -from typing import Any from loguru import logger from rich.console import Console @@ -22,14 +21,14 @@ from bfabric import Bfabric from bfabric.cli_formatting import setup_script_logging -from bfabric.entities import Parameter +from bfabric.entities import Parameter, Workunit, Application -def render_output(workunits_by_status: dict[str, list[dict[str, Any]]], client: Bfabric) -> None: +def render_output(workunits_by_status: dict[str, list[Workunit]], client: Bfabric) -> None: """Renders the output as a table.""" table = Table( - Column("AID", no_wrap=True), - Column("WUID", no_wrap=True), + Column("Application", no_wrap=True), + Column("WU ID", no_wrap=True), Column("Created", no_wrap=True), Column("Status", no_wrap=True), Column("Created by", no_wrap=True, max_width=12), @@ -37,9 +36,12 @@ def render_output(workunits_by_status: dict[str, list[dict[str, Any]]], client: Column("Nodelist", no_wrap=False), ) - workunit_ids = [wu["id"] for wu_list in workunits_by_status.values() for wu in wu_list] + workunit_ids = [wu.id for wu_list in workunits_by_status.values() for wu in wu_list] + app_ids = {wu["application"]["id"] for wu_list in workunits_by_status.values() for wu in wu_list} + nodelist_params = Parameter.find_by({"workunitid": workunit_ids, "key": "nodelist"}, client) nodelist_values = {param["workunit"]["id"]: param.value for param in nodelist_params.values()} + application_values = Application.find_all(ids=sorted(app_ids), client=client) for status, workunits_all in workunits_by_status.items(): workunits = [x for x in workunits_all if x["createdby"] not in ["gfeeder", "itfeeder"]] @@ -50,17 +52,15 @@ def render_output(workunits_by_status: dict[str, list[dict[str, Any]]], client: }.get(status, "black") for wu in workunits: - app_url = f"{client.config.base_url}/application/show.html?id={wu['application']['id']}" - wu_url = f"{client.config.base_url}/workunit/show.html?id={wu['id']}&tab=details" - + app = application_values[wu["application"]["id"]] table.add_row( - f"[link={app_url}]A{wu['application']['id']:3}[/link]", - f"[link={wu_url}]WU{wu['id']}[/link]", + f"[link={app.web_url}]A{wu['application']['id']:3} {app['name']}[/link]", + f"[link={wu.web_url}&tab=details]WU{wu['id']}[/link]", wu["created"], f"[{status_color}]{status}[/{status_color}]", wu["createdby"], wu["name"], - nodelist_values.get(wu["id"], "N/A"), + nodelist_values.get(wu.id, "N/A"), ) console = Console() @@ -79,10 +79,9 @@ def list_not_available_proteomics_workunits(date_cutoff: datetime) -> None: workunits_by_status = {} for status in ["Pending", "Processing", "Failed"]: - workunits_by_status[status] = client.read( - endpoint="workunit", - obj={"status": status, "createdafter": date_cutoff.isoformat()}, - ).to_list_dict() + workunits_by_status[status] = Workunit.find_by( + {"status": status, "createdafter": date_cutoff.isoformat()}, client=client + ).values() render_output(workunits_by_status, client=client) diff --git a/docs/changelog.md b/docs/changelog.md index 704f0908..068579a4 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -20,7 +20,7 @@ Versioning currently follows `X.Y.Z` where - More types and relationships - Relationships defer imports to descriptor call, i.e. circular relationships are possible now. - `HasOne` and `HasMany` allow defining `optional=True` to indicate fields which can be missing under some circumstances. -- Add `nodelist` to `bfabric_list_not_available_proteomics_workunits.py` output. +- Add `nodelist` and `name` columns to `bfabric_list_not_available_proteomics_workunits.py` output. ### Changed