#!/bin/bash

CLI_ORIGIN=$1
#CLI_TYPE=$2
CLI_ID=$3
CLI_METRIC=$4


function _mk_dgmod_id() {
  local answer=$(($1 * 10 + $2))
  echo "$answer"
}

edge_id=$(_mk_dgmod_id "${CLI_ORIGIN}" "${CLI_ID}")

case ${CLI_METRIC} in
  #
  # performance
  #

  "cpu")
    dgmod_pid=$(pgrep -of "$ARG_TYPE --id.${edge_id}[ \n]")
    ps --pid="${dgmod_pid}" -o cputime | awk -F'[-: ]+' '/:/ && NF==4 { t=($4+60*($3+60*($2+24*$1))); print t} /:/ && NF==3 {t=($3+60*($2+60*$1)); print t}'
    ;;
  "rss")
    dgmod_pid=$(pgrep -of "$ARG_TYPE --id.${edge_id}[ \n]")
    ps --pid="${dgmod_pid}" -o rss=
    ;;
  "pid")
    pgrep -of "$ARG_TYPE --id.${edge_id}[ \n]"
    ;;

  "cache.abs-time")
    /opt/edge/venv/bin/python3.11 /opt/varusr/bin/edge-metric-cache cache.abs-time-sec "$CLI_ID" "$CLI_ORIGIN"
    ;;
  "cache.count")
    /opt/edge/venv/bin/python3.11 /opt/varusr/bin/edge-metric-cache cache.count "$CLI_ID" "$CLI_ORIGIN"
    ;;
  "arch-session-count")
    /opt/edge/venv/bin/python3.11 /opt/varusr/bin/edge-metric-cache arch-session-count "$CLI_ID" "$CLI_ORIGIN"
    ;;
  "write.done-count")
    /opt/edge/venv/bin/python3.11 /opt/varusr/bin/edge-metric-cache write.done-count "$CLI_ID" "$CLI_ORIGIN"
    ;;
  "write.fail-count")
    /opt/edge/venv/bin/python3.11 /opt/varusr/bin/edge-metric-cache write.fail-count "$CLI_ID" "$CLI_ORIGIN"
    ;;
  "write.blob-count")
    /opt/edge/venv/bin/python3.11 /opt/varusr/bin/edge-metric-cache write.blob-count "$CLI_ID" "$CLI_ORIGIN"
    ;;
  "cache-session-count")
    /opt/edge/venv/bin/python3.11 /opt/varusr/bin/edge-metric-cache cache-session-count "$CLI_ID" "$CLI_ORIGIN"
    ;;

  #
  # tracker
  #

  "event.track-count")
    sed -n 's/event.track-count: //p' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "event.track.lr-track-count")
    sed -n 's/event.track.lr-track-count: //p' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "event.track.lr-track-dublicate-count")
    sed -n 's/event.track.lr-track-dup-count: //p' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "event.track.lr-track-time")
    sed -n 's/event.track.lr-track-time: //p' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "event.track.ra-track-count")
    sed -n 's/event.track.ra-track-count: //p' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "event.track.ra-track-time")
    sed -n 's/event.track.ra-track-time: //p' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "event.track.lrra-track-count")
    sed -n 's/event.track.lrra-track-count: //p' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "event.track.lrra-track-time")
    sed -n 's/event.track.lrra-track-time: //p' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "react.scene.ra-scene-item-count")
    sed -n 's/react.scene.ra-scene-item-count: //p' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "react.scene.lr-scene-item-count")
    sed -n 's/react.scene.lr-scene-item-count: //p' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "react.scene.ra-scene-count")
    sed -n 's/react.scene.ra-scene-count: //p' "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "react.scene.lr-scene-count")
    sed -n 's/react.scene.lr-scene-count: //p' "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "react.scene-count")
    sed -n 's/react.scene-count: //p' "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;
  "react.track-count")
    sed -n 's/react.track-count: //p' "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/tr${CLI_ID}" 2>/dev/null | grep .
    ;;

  #
  # speedcontrol
  #

  "addtrack")
    jq '.units // [] | map(select(.type | contains("tr-react")))[0].stat.count.total // 0' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/sc${CLI_ID}" 2>/dev/null
    ;;
  "addtrack-lr")
    jq '.units // [] | map(select(.type | contains("tr-react")))[0].stat.count.lr // 0' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/sc${CLI_ID}" 2>/dev/null
    ;;
  "addtrack-ra")
    jq '.units // [] | map(select(.type | contains("tr-react")))[0].stat.count.ra // 0' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/sc${CLI_ID}" 2>/dev/null
    ;;
  "addtrack-lrra")
    jq '.units // [] | map(select(.type | contains("tr-react")))[0].stat.count.lrra // 0' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/sc${CLI_ID}" 2>/dev/null
    ;;

  "fact")
    jq .'event // 0 |.count' "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/sc0" 2>/dev/null | grep .
    ;;

  #
  # lpr
  #

  "react.total-count")
    jq '.units // [] | map(select(.type | contains("anpr")))[0].stat.queue[0] // 0' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/lr${CLI_ID}" 2>/dev/null
    ;;
  "react.skip-count")
    jq '.units // [] | map(select(.type | contains("anpr")))[0].stat.queue[1] // 0' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/lr${CLI_ID}" 2>/dev/null
    ;;
  "vast2.total-count")
    jq '.units // [] | map(select(.type | contains("vast2~recipient")))[0].stat["emit-event"].count[0] // 0' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/lr${CLI_ID}" 2>/dev/null
    ;;
  "vast2.fail-count")
    jq '.units // [] | map(select(.type | contains("vast2~recipient")))[0].stat["emit-event"].count[2] // 0' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/lr${CLI_ID}" 2>/dev/null
    ;;
  "camera.temperature")
    jq '.units // [] | map(select(.type | contains("mediagrabber~device")))[0].stat["temp"] // -321' \
      "/var/spool/edge/pipeline-${CLI_ORIGIN}/zabbix/vs${CLI_ID}" 2>/dev/null
    ;;
  *)
    echo "Parameter not supported" 1>&2
    exit 1
    ;;
  esac
