#!/bin/bash

# Example script to replay dataplane metrics collection for last month
# This script demonstrates how to use the replay API to re-collect metrics data

# Configuration
METERING_API_URL="${METERING_API_URL:-http://metering-server.observability:8081}"

# Calculate last month's timestamps
# Get first day of last month at 00:00:00 UTC
LAST_MONTH_START=1759276800

# Get first day of current month at 00:00:00 UTC
CURRENT_MONTH_START=1761955200

echo "Replaying dataplane metrics for last month"
echo "Start timestamp: $LAST_MONTH_START ($(date -u -r $LAST_MONTH_START 2>/dev/null || date -u -d @$LAST_MONTH_START))"
echo "End timestamp: $CURRENT_MONTH_START ($(date -u -r $CURRENT_MONTH_START 2>/dev/null || date -u -d @$CURRENT_MONTH_START))"

# Replay only dataplane metrics collection
curl -X POST "${METERING_API_URL}/api/v1/metering/statistic/tasks/replay" \
  -H "Content-Type: application/json" \
  -d "{
    \"startTimestamp\": ${LAST_MONTH_START},
    \"endTimestamp\": ${CURRENT_MONTH_START},
    \"statisticClass\": \"metrics_collection\",
    \"meteringGroup\": \"dedicated_dataplane\"
  }" | jq .

curl -X POST "${METERING_API_URL}/api/v1/metering/statistic/tasks/replay" \
  -H "Content-Type: application/json" \
  -d "{
    \"startTimestamp\": ${LAST_MONTH_START},
    \"endTimestamp\": ${CURRENT_MONTH_START},
    \"statisticClass\": \"logs_collection_loki\",
    \"meteringGroup\": \"dedicated_dataplane\"
  }" | jq .

curl -X POST "${METERING_API_URL}/api/v1/metering/statistic/tasks/replay" \
  -H "Content-Type: application/json" \
  -d "{
    \"startTimestamp\": ${LAST_MONTH_START},
    \"endTimestamp\": ${CURRENT_MONTH_START},
    \"statisticClass\": \"logs_storage_s3\",
    \"meteringGroup\": \"dedicated_dataplane\"
  }" | jq .

echo ""
echo "Replay request submitted. Check the response for task IDs and periods replayed."
