Skip to content

Commit

Permalink
make use of entities api to make the script nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Aug 12, 2024
1 parent 9deac5f commit 9d3153b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
31 changes: 15 additions & 16 deletions bfabric/scripts/bfabric_list_not_available_proteomics_workunits.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,34 @@

from argparse import ArgumentParser
from datetime import datetime, timedelta
from typing import Any

from loguru import logger
from rich.console import Console
from rich.table import Column, Table

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),
Column("Name", no_wrap=False),
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"]]
Expand All @@ -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()
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9d3153b

Please sign in to comment.