#!/usr/bin/env bash
# This test checks that Qdrant exposes per-collection metrics when requested via query parameter.

set -e

QDRANT_HOST=${QDRANT_HOST:-'localhost:6333'}
COLLECTION_NAME="test_collection_metrics"

echo "Using Qdrant host: $QDRANT_HOST"

# 1. Cleanup & Create collection
echo "Creating collection $COLLECTION_NAME..."
# Attempt to delete collection if it exists, ignore output/errors
curl -X DELETE "http://$QDRANT_HOST/collections/$COLLECTION_NAME" -s > /dev/null || true

curl -X PUT "http://$QDRANT_HOST/collections/$COLLECTION_NAME" \
  -H 'Content-Type: application/json' \
  --fail -s \
  --data-raw '{
      "vectors": {
        "size": 4,
        "distance": "Dot"
      }
    }'


# 2. Insert points
echo "Inserting points..."
curl -X PUT "http://$QDRANT_HOST/collections/$COLLECTION_NAME/points?wait=true" \
  -H 'Content-Type: application/json' \
  --fail -s \
  --data-raw '{
      "points": [
        {"id": 1, "vector": [0.05, 0.61, 0.76, 0.74]},
        {"id": 2, "vector": [0.19, 0.81, 0.75, 0.11]}
      ]
    }'


# 3. Search points (to generate read metrics)
echo "Searching points..."
curl -X POST "http://$QDRANT_HOST/collections/$COLLECTION_NAME/points/search" \
  -H 'Content-Type: application/json' \
  --fail -s \
  --data-raw '{
        "vector": [0.2,0.1,0.9,0.7],
        "top": 3
    }'


# 4. Fetch metrics with per_collection=true and verify
echo "Fetching per-collection metrics..."
METRICS=$(curl -s --fail "http://$QDRANT_HOST/metrics?per_collection=true")

echo "Verifying per-collection metrics..."

# Check for existence of collection label in rest responses
if echo "$METRICS" | grep -q "rest_responses_total{.*collection=\"$COLLECTION_NAME\".*}"; then
  echo "Found per-collection rest_responses_total"
else
  echo "Failed to find per-collection rest_responses_total"
  exit 1
fi

# 5. Fetch default metrics (no per_collection) and verify global mode
echo "Fetching default metrics..."
DEFAULT_METRICS=$(curl -s --fail "http://$QDRANT_HOST/metrics")

# In default mode, rest_responses should NOT have collection label
if echo "$DEFAULT_METRICS" | grep -q 'rest_responses_total{.*collection='; then
  echo "ERROR: Found per-collection label in default mode"
  exit 1
else
  echo "Default mode correctly shows global metrics (no collection label)"
fi

echo "All per-collection metrics verification passed!"

# Cleanup
echo "Cleaning up..."
curl -X DELETE "http://$QDRANT_HOST/collections/$COLLECTION_NAME" -s
