#!/bin/bash

current_date=$(date +%Y-%m-%d)
start_date=$(date -d "$current_date - 7 days" +%Y-%m-%d)

report_file_name="primary_unfinished_${start_date}_${current_date}.html"
tsv_file_name="primary_unfinished_${start_date}_${current_date}.tsv"

path_to_report="/mnt/sun-pcs01/ingest/ingest_reports/${report_file_name}"
path_to_tsv="/mnt/sun-pcs01/ingest/ingest_reports/${tsv_file_name}"
path_to_report_zweb="s3://zoox-web/ingest_reports/${report_file_name}"

link_for_browser="http://ingest_reports.web.zooxlabs.com/${report_file_name}"

bazel run //data/ingest/completeness_check:check_primary -- --start $start_date --end $current_date > "$path_to_tsv"

chmod 777 "$path_to_tsv"

cat "$path_to_tsv" | python ./data/ingest/completeness_check/make_report.py "$path_to_tsv" "$start_date" "$current_date"> "$path_to_report"

chmod 777 "$path_to_report"

# Uploading report to Zweb
aws s3 cp ${path_to_report} ${path_to_report_zweb}

echo ${link_for_browser}

notification_message="Report is ready. Unfinished *primary* ingests in the previous week:"

curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"${notification_message}\",\"attachments\" : [{\"title\": \"${link_for_browser}\",\"title_link\": \"${link_for_browser}\"}]}" https://hooks.slack.com/services/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
