|
5 | 5 | # - Define and lock analytical grains for consistent aggregation and reporting |
6 | 6 | # - Enforce referential integrity between fact and dimension tables |
7 | 7 |
|
| 8 | +import pandas as pd |
| 9 | +from typing import Dict, List |
| 10 | +from data_pipeline.shared.run_context import RunContext |
| 11 | +from data_pipeline.shared.raw_loader_exporter import load_logical_table, export_file |
| 12 | + |
| 13 | + |
| 14 | +# ------------------------------------------------------------ |
| 15 | +# SEMANTIC REPORT & LOGS |
| 16 | +# ------------------------------------------------------------ |
| 17 | + |
| 18 | + |
| 19 | +def init_report(): |
| 20 | + return {"status": "success", "errors": [], "info": []} |
| 21 | + |
| 22 | + |
| 23 | +def log_info(message: str, report: Dict[str, List[str]]) -> None: |
| 24 | + print(f"[INFO] {message}") |
| 25 | + report["info"].append(message) |
| 26 | + |
| 27 | + |
| 28 | +def log_error(message: str, report: Dict[str, List[str]]) -> None: |
| 29 | + print(f"[ERROR] {message}") |
| 30 | + report["errors"].append(message) |
| 31 | + |
| 32 | + |
| 33 | +# ------------------------------------------------------------ |
| 34 | +# SEMANTIC LAYERING AND SCHEMA ENFORCEMENT |
| 35 | +# ------------------------------------------------------------ |
| 36 | + |
| 37 | + |
| 38 | +# ------------------------------------------------------------ |
| 39 | +# BUILD BI SEMANTIC |
| 40 | +# ------------------------------------------------------------ |
| 41 | + |
| 42 | + |
| 43 | +def build_semantic_layer(run_context: RunContext) -> Dict: |
| 44 | + |
| 45 | + report = init_report() |
| 46 | + |
| 47 | + def info(msg): |
| 48 | + log_info(msg, report) |
| 49 | + |
| 50 | + def error(msg): |
| 51 | + log_error(msg, report) |
| 52 | + |
| 53 | + assembled_path = run_context.assembled_path |
| 54 | + |
| 55 | + df = load_logical_table( |
| 56 | + assembled_path, |
| 57 | + "assembled_events", |
| 58 | + log_info=info, |
| 59 | + log_error=error, |
| 60 | + ) |
| 61 | + |
| 62 | + if df is None or df.empty: |
| 63 | + error("assembled events is empty") |
| 64 | + report["status"] = "failed" |
| 65 | + |
| 66 | + return report |
| 67 | + |
| 68 | + fact_seller = pd.DataFrame() |
| 69 | + dim_seller = pd.DataFrame() |
| 70 | + |
| 71 | + fulfillment_tables = { |
| 72 | + "seller_week_fulfillment_fact.parquet": fact_seller, |
| 73 | + "dim_seller.parquet": dim_seller, |
| 74 | + } |
| 75 | + |
| 76 | + for table_name, table in fulfillment_tables.items(): |
| 77 | + |
| 78 | + output_path = run_context.semantic_path / table_name |
| 79 | + |
| 80 | + if not export_file(table, output_path): |
| 81 | + error(f"{table_name}: Export failed") |
| 82 | + report["status"] = "failed" |
| 83 | + break |
| 84 | + |
| 85 | + info(f"Export success: {table_name} ({len(table)} rows)") |
| 86 | + |
| 87 | + return report |
| 88 | + |
8 | 89 |
|
9 | 90 | # ============================================================================= |
10 | 91 | # END OF SCRIPT |
|
0 commit comments