#!/usr/bin/env bash

set -euo pipefail

outfile=/tmp/topics.txt
tmp_file=/tmp/topics_tmp.txt

function echoerr() {
  printf "%s\n" "$*" >&2;
}

function get_start_time() {
  echo $(date --iso-8601=minutes -d "$1")
}

function get_datacatalog_query() {
  echo "https://catalog-api-prod.zooxlabs.com/api/runs?window_start=$1&types=mpi_testing&autonomous_mode=AUTONOMOUS"
}

function main() {
  if [ $# -lt 1 ]
  then
    echoerr "No relative start_time supplied (e.g. ./aggregate_topics_since.sh \"7 days ago\")"
    exit 1
  fi
  hr_start_time_relative=${1}

  start_time=$(get_start_time $hr_start_time_relative)
  query=$(get_datacatalog_query $start_time)
  runs_json=$(wget -O - "$query")

  runs_list=$(echo "$runs_json" | python -c "import sys, json; print '\n'.join([r['meta_id'] for r in json.load(sys.stdin)['runs']])")
  runs_count=$(echo "$runs_list" | wc -l)
  echoerr "Found $runs_count runs since $hr_start_time_relative"

  echo '' > $outfile
  while read -r run; do
    echoerr "Analyzing run: $run"
    new_topics=$(chum_ls --uri $run | tail -n +2) # strip topic count
    echoerr "  `echo "$new_topics" | wc -l` topics"
    (cat $outfile; echo "$new_topics") | sort | uniq > $tmp_file
    mv $tmp_file $outfile
  done <<< "$runs_list"

  echoerr "Found `cat $outfile | wc -l` unique topics across $runs_count runs"
  cat $outfile
}

main "$@"
