#!/usr/bin/env bash
URI=${1:-chum://20201101T001637-vh6a_02}
TOPIC=${2:-/emu_gateway/zci_faults}
echo "URI: ${URI}"
echo "TOPIC: ${TOPIC}"

time bazel run //bin:show_message -- --chum_uri "${URI}" --topic "${TOPIC}"  --json-stream --stdout 2>/dev/null \
  | jq 'select(has("freeze_frame"))
        | select(has(.freeze_frame))
        | .[.freeze_frame] + {freeze_frame:.freeze_frame}
        | .topic = (.topic // .error_message[(.error_message | rindex(" ") + 1):])
        | {freeze_frame: .freeze_frame, topic: .topic}' \
  | jq --slurp 'reduce .[] as $fault ({};.[$fault.freeze_frame][$fault.topic] += 1)'
