3
0
Fork 0
mirror of https://github.com/YosysHQ/sby.git synced 2025-08-20 11:50:26 +00:00

Add jsonl status format

Replace `--statuscsv` and `--livecsv` with `--statusfmt <fmt>` and `--live <fmt` respectively.
Currently supports both csv and jsonl.
In the case of `--live`, updates can be printed in multiple formats, while `--statusfmt` only supports one at a time.
This commit is contained in:
Krystine Sherwin 2025-07-29 10:00:52 +12:00
parent ac120cee92
commit 3bf5be0637
No known key found for this signature in database
5 changed files with 36 additions and 33 deletions

View file

@ -106,10 +106,10 @@ class FileInUseError(Exception):
class SbyStatusDb:
def __init__(self, path: Path, task, timeout: float = 5.0, live_csv = False):
def __init__(self, path: Path, task, timeout: float = 5.0, live_formats = []):
self.debug = False
self.task = task
self.live_csv = live_csv
self.live_formats = live_formats
self.con = sqlite3.connect(path, isolation_level=None, timeout=timeout)
self.db = self.con.cursor()
@ -250,10 +250,11 @@ class SbyStatusDb:
),
)
if self.live_csv:
if self.live_formats:
row = self.get_status_data_joined(self.db.lastrowid)
csvline = format_status_data_csvline(row)
self.task.log(f"{click.style('csv', fg='yellow')}: {csvline}")
for fmt in self.live_formats:
fmtline = format_status_data_fmtline(row, fmt)
self.task.log(f"{click.style(fmt, fg='yellow')}: {fmtline}")
@transaction
def add_task_trace(
@ -440,14 +441,14 @@ class SbyStatusDb:
return {row["id"]: parse_status_data_row(row) for row in rows}
def print_status_summary_csv(self, tasknames: list[str], latest: bool):
def print_status_summary_fmt(self, tasknames: list[str], status_format: str, latest: bool):
# get all statuses
all_properties = self.all_status_data_joined()
latest_task_ids = filter_latest_task_ids(self.all_tasks())
# print csv header
csvheader = format_status_data_csvline(None)
print(csvheader)
# print header
header = format_status_data_fmtline(None, status_format)
print(header)
# find summary for each task/property combo
prop_map: dict[(str, str), dict[str, (int, int)]] = {}
@ -488,9 +489,8 @@ class SbyStatusDb:
del prop["UNKNOWN"]
for _, row in prop.values():
csvline = format_status_data_csvline(all_properties[row])
print(csvline)
line = format_status_data_fmtline(all_properties[row], status_format)
print(line)
def combine_statuses(statuses):
statuses = set(statuses)
@ -506,9 +506,9 @@ def parse_status_data_row(raw: sqlite3.Row):
row_dict["data"] = json.loads(row_dict.get("data") or "{}")
return row_dict
def format_status_data_csvline(row: dict|None) -> str:
def format_status_data_fmtline(row: dict|None, fmt: str = "csv") -> str:
if row is None:
csv_header = [
data = [
"time",
"task_name",
"mode",
@ -520,7 +520,6 @@ def format_status_data_csvline(row: dict|None) -> str:
"trace",
"depth",
]
return ','.join(csv_header)
else:
engine = row['data'].get('engine', row['data'].get('source'))
try:
@ -546,7 +545,11 @@ def format_status_data_csvline(row: dict|None) -> str:
trace_path,
depth,
]
return ','.join("" if v is None else str(v) for v in csv_line)
data = ["" if v is None else str(v) for v in csv_line]
if fmt == "csv":
return ','.join(data)
elif fmt == "jsonl":
return json.dumps(data)
def filter_latest_task_ids(all_tasks: dict[int, dict[str]]):
latest: dict[str, int] = {}