#!/usr/bin/env bash report_date_value() { printf '%s\n' "${1:-$(date +%Y-%m-%d)}" } report_output_dir() { printf '%s\n' "$(report_output_root)" } report_html_dir() { printf '%s\n' "$(report_html_root)" } 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" } report_output_root() { printf '%s\n' "${REPORT_OUTPUT_DIR:-reports/daily}" } report_html_root() { printf '%s\n' "$(report_output_root)/html" } report_ad_hoc_dir() { local report_date run_kind trigger_source report_date="$(report_date_value "${1:-}")" run_kind="${2:-manual}" trigger_source="${3:-cli}" printf '%s\n' "reports/ad_hoc/${report_date}/${run_kind}/${trigger_source}" } report_ad_hoc_markdown_path() { local report_date run_kind trigger_source report_date="$(report_date_value "${1:-}")" run_kind="${2:-manual}" trigger_source="${3:-cli}" printf '%s\n' "$(report_ad_hoc_dir "$report_date" "$run_kind" "$trigger_source")/daily_report_${report_date}.md" } report_ad_hoc_html_path() { local report_date run_kind trigger_source report_date="$(report_date_value "${1:-}")" run_kind="${2:-manual}" trigger_source="${3:-cli}" printf '%s\n' "$(report_ad_hoc_dir "$report_date" "$run_kind" "$trigger_source")/html/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 run_kind trigger_source is_official_daily db_url="$1" report_date="$2" status="$3" model_count="${4:-}" summary_md="${5:-}" output_path="${6:-}" error_message="${7:-}" run_kind="${8:-manual}" trigger_source="${9:-cli}" is_official_daily="${10:-false}" if [[ "$is_official_daily" == "true" ]]; then 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" \ --set=run_kind="$run_kind" \ --set=trigger_source="$trigger_source" \ --set=is_official_daily="$is_official_daily" <<'SQL' INSERT INTO daily_report ( report_date, status, model_count, summary_md, output_path, error_message, run_kind, trigger_source, is_official_daily, created_at, updated_at ) VALUES ( :'report_date', :'status', NULLIF(:'model_count', '')::INTEGER, NULLIF(:'summary_md', ''), NULLIF(:'output_path', ''), NULLIF(:'error_message', ''), NULLIF(:'run_kind', ''), NULLIF(:'trigger_source', ''), NULLIF(:'is_official_daily', '')::BOOLEAN, 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, run_kind = EXCLUDED.run_kind, trigger_source = EXCLUDED.trigger_source, is_official_daily = TRUE, updated_at = NOW(); SQL fi psql "$db_url" \ -v ON_ERROR_STOP=1 \ --set=report_date="$report_date" \ --set=status="$status" \ --set=summary_md="$summary_md" \ --set=output_path="$output_path" \ --set=error_message="$error_message" \ --set=run_kind="$run_kind" \ --set=trigger_source="$trigger_source" \ --set=is_official_daily="$is_official_daily" <<'SQL' INSERT INTO report_runs ( source, report_date, status, summary_md, output_path, error_message, run_kind, trigger_source, is_official_daily ) VALUES ( 'pipeline', :'report_date', :'status', NULLIF(:'summary_md', ''), NULLIF(:'output_path', ''), NULLIF(:'error_message', ''), NULLIF(:'run_kind', ''), NULLIF(:'trigger_source', ''), NULLIF(:'is_official_daily', '')::BOOLEAN ); SQL }