"""Computes the metrics of each dataset in an Ossie model, on a DuckDB database.""" import sys import duckdb from ossie import OssieDocument model = OssieDocument.from_ossie_yaml(open(sys.argv[1]).read()) db = duckdb.connect(sys.argv[2]) def sql(expression): # `sql` is the portable form and wins. Without it, we read the OSSIE_SQL_2026 entry of dialects. if expression.sql: return expression.sql return next(d.expression for d in expression.dialects if d.dialect == "OSSIE_SQL_2026") for dataset in model.datasets: # Fields are the dataset's interface: every metric reads them. # This script doesn't handle source.column, which the spec also allows in a dataset-level metric. fields = ", ".join(f"{sql(field.expression)} AS {field.name}" for field in dataset.fields) for metric in dataset.metrics or []: query = f"SELECT {sql(metric.expression)} FROM (SELECT {fields} FROM {dataset.source})" print(f"{dataset.name}.{metric.name} = {db.sql(query).fetchone()[0]}")