#!/usr/bin/env bash
# scripts/manual/soak-report.sh - summarize server stability/quality counters for a time window
# Manual: operator soak evidence collector; not part of CI.
set -euo pipefail

SERVER_HOST=${LESAVKA_SERVER_HOST:-theia}
SINCE=${1:-"30 minutes ago"}
UNTIL=${2:-"now"}

tmp=$(mktemp)
trap 'rm -f "$tmp"' EXIT

ssh "$SERVER_HOST" \
  "journalctl -u lesavka-server --since '$SINCE' --until '$UNTIL' --no-pager -o short-iso" \
  >"$tmp"

count() {
  local pattern=$1
  grep -E -c "$pattern" "$tmp" || true
}

echo "Lesavka Soak Report"
echo "  host:  $SERVER_HOST"
echo "  since: $SINCE"
echo "  until: $UNTIL"
echo
echo "Counters"
echo "  stream_camera selections:  $(count 'stream_camera output selected')"
echo "  camera relay recreated:    $(count 'camera relay (re)created')"
echo "  camera relay reused:       $(count 'camera relay reused')"
echo "  camera superseded events:  $(count 'session superseded')"
echo "  webcam frames logged:      $(count 'srv  webcam frame')"
echo "  monitor channel drops:     $(count 'channel full - dropping frames')"
echo "  adaptive fps down events:  $(count 'adaptive eye fps ↓')"
echo "  adaptive fps up events:    $(count 'adaptive eye fps ↑')"
echo "  HID EAGAIN drops:          $(count 'Resource temporarily unavailable (os error 11)')"
echo "  webcam appsrc push fail:   $(count 'appsrc push failed')"
echo "  HDMI appsrc push fail:     $(count 'HDMI appsrc push failed')"
echo "  server abort/crash signs:  $(count 'Bail out!|status=6/ABRT|Main process exited|core-dump')"

echo
echo "Recent critical lines"
grep -E 'Bail out!|status=6/ABRT|Main process exited|core-dump|stream_camera output selected|adaptive eye fps|channel full - dropping frames' "$tmp" | tail -n 60 || true
