#!/usr/bin/env bash

set -eu
set -o pipefail


source "${BASH_SOURCE[0]%/*}"/lib/testing.sh


cid_es="$(container_id elasticsearch)"
cid_fl="$(container_id fleet-server)"
cid_apm="$(container_id apm-server)"

ip_es="$(service_ip elasticsearch)"
ip_fl="$(service_ip fleet-server)"
ip_apm="$(service_ip apm-server)"

grouplog 'Wait for readiness of Elasticsearch'
poll_ready "$cid_es" 'http://elasticsearch:9200/' --resolve "elasticsearch:9200:${ip_es}" -u 'elastic:testpasswd'
endgroup

grouplog 'Wait for readiness of Fleet Server'
poll_ready "$cid_fl" 'http://fleet-server:8220/api/status' --resolve "fleet-server:8220:${ip_fl}"
endgroup

grouplog 'Wait for readiness of APM Server'
poll_ready "$cid_apm" 'http://apm-server:8200/' --resolve "apm-server:8200:${ip_apm}"
endgroup

log 'Searching a system document generated by Fleet Server'

query=$( (IFS= read -r -d '' data || echo "$data" | jq -c) <<EOD
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "agent.name": "fleet-server"
          }
        },
        {
          "term": {
            "agent.type": "metricbeat"
          }
        },
        {
          "term": {
            "event.module": "system"
          }
        },
        {
          "term": {
            "event.dataset": "system.cpu"
          }
        },
        {
          "term": {
            "metricset.name": "cpu"
          }
        }
      ]
    }
  }
}
EOD
)

declare -a search_args=( '-s' '-u' 'elastic:testpasswd'
	'http://elasticsearch:9200/metrics-system.cpu-default/_search?size=1&pretty'
	'--resolve' "elasticsearch:9200:${ip_es}"
	'-H' 'Content-Type: application/json'
	'-d' "${query}"
)
declare response
declare -i count

echo "curl arguments: ${search_args[*]}"

declare -i was_retried=0

# retry for max 60s (30*2s)
for _ in $(seq 1 30); do
	response="$(curl "${search_args[@]}")"

	set +u  # prevent "unbound variable" if assigned value is not an integer
	count="$(jq -rn --argjson data "${response}" '$data.hits.total.value')"
	set -u

	if (( count > 0 )); then
		break
	fi

	was_retried=1
	echo -n 'x' >&2
	sleep 2
done
if ((was_retried)); then
	# flush stderr, important in non-interactive environments (CI)
	echo >&2
fi

echo "$response"
# Elastic Agent buffers metrics until Elasticsearch becomes ready, so we
# tolerate multiple results
if (( count == 0 )); then
	echo 'Expected at least 1 document'
	exit 1
fi

log 'Searching a container document generated by Fleet Server'

query=$( (IFS= read -r -d '' data || echo "$data" | jq -c) <<EOD
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "agent.name": "fleet-server"
          }
        },
        {
          "term": {
            "agent.type": "filebeat"
          }
        },
        {
          "term": {
            "container.name": "docker-elk-elasticsearch-1"
          }
        }
      ]
    }
  }
}
EOD
)

search_args=( '-s' '-u' 'elastic:testpasswd'
	'http://elasticsearch:9200/logs-docker.container_logs-default/_search?size=1&pretty'
	'--resolve' "elasticsearch:9200:${ip_es}"
	'-H' 'Content-Type: application/json'
	'-d' "${query}"
)
response=
count=0

echo "curl arguments: ${search_args[*]}"

was_retried=0

# retry for max 60s (30*2s)
for _ in $(seq 1 30); do
	response="$(curl "${search_args[@]}")"

	set +u  # prevent "unbound variable" if assigned value is not an integer
	count="$(jq -rn --argjson data "${response}" '$data.hits.total.value')"
	set -u

	if (( count > 0 )); then
		break
	fi

	was_retried=1
	echo -n 'x' >&2
	sleep 2
done
if ((was_retried)); then
	# flush stderr, important in non-interactive environments (CI)
	echo >&2
fi

echo "$response"
if (( count == 0 )); then
	echo 'Expected at least 1 document'
	exit 1
fi
