#!/usr/bin/env bash report_date_value() { printf '%s\n' "${1:-$(date +%Y-%m-%d)}" } report_output_dir() { printf '%s\n' "reports/daily" } report_html_dir() { printf '%s\n' "$(report_output_dir)/html" } report_markdown_path() { local report_date report_date="$(report_date_value "${1:-}")" printf '%s\n' "$(report_output_dir)/daily_report_${report_date}.md" } report_html_path() { local report_date report_date="$(report_date_value "${1:-}")" printf '%s\n' "$(report_html_dir)/daily_report_${report_date}.html" } report_archive_dir() { local report_date report_date="$(report_date_value "${1:-}")" printf '%s\n' "$(report_output_dir)/${report_date:0:4}/${report_date:5:2}" } report_archive_markdown_path() { local report_date report_date="$(report_date_value "${1:-}")" printf '%s\n' "$(report_archive_dir "$report_date")/daily_report_${report_date}.md" } report_archive_html_path() { local report_date report_date="$(report_date_value "${1:-}")" printf '%s\n' "$(report_archive_dir "$report_date")/daily_report_${report_date}.html" } archive_report_artifacts() { local report_date markdown_path html_path archive_dir report_date="$(report_date_value "${1:-}")" markdown_path="$(report_markdown_path "$report_date")" html_path="$(report_html_path "$report_date")" archive_dir="$(report_archive_dir "$report_date")" mkdir -p "$archive_dir" cp "$markdown_path" "$(report_archive_markdown_path "$report_date")" cp "$html_path" "$(report_archive_html_path "$report_date")" } track_report_state() { local db_url report_date status model_count summary_md output_path error_message db_url="$1" report_date="$2" status="$3" model_count="${4:-}" summary_md="${5:-}" output_path="${6:-}" error_message="${7:-}" psql "$db_url" \ -v ON_ERROR_STOP=1 \ --set=report_date="$report_date" \ --set=status="$status" \ --set=model_count="$model_count" \ --set=summary_md="$summary_md" \ --set=output_path="$output_path" \ --set=error_message="$error_message" <<'SQL' INSERT INTO daily_report ( report_date, status, model_count, summary_md, output_path, error_message, created_at, updated_at ) VALUES ( :'report_date', :'status', NULLIF(:'model_count', '')::INTEGER, NULLIF(:'summary_md', ''), NULLIF(:'output_path', ''), NULLIF(:'error_message', ''), NOW(), NOW() ) ON CONFLICT (report_date) DO UPDATE SET status = EXCLUDED.status, model_count = COALESCE(EXCLUDED.model_count, daily_report.model_count), summary_md = COALESCE(EXCLUDED.summary_md, daily_report.summary_md), output_path = COALESCE(EXCLUDED.output_path, daily_report.output_path), error_message = EXCLUDED.error_message, updated_at = NOW(); INSERT INTO report_runs ( source, report_date, status, summary_md, output_path, error_message ) VALUES ( 'pipeline', :'report_date', :'status', NULLIF(:'summary_md', ''), NULLIF(:'output_path', ''), NULLIF(:'error_message', '') ); SQL }