#!/bin/bash

sfc="fms-sfc"
sfci="fms-sfc-1"
service_function="fms-storage"
endpoint="fms-storage-endpoint"
measurement="tomcat_connector"
response_time_field="(max(processing_time) - min(processing_time)) / ((count(processing_time) - 1)*1000)"
request_size_field="(max(bytes_received) - min(bytes_received)) / (count(bytes_received) - 1)"
response_size_field="(max(bytes_sent) - min(bytes_sent)) / (count(bytes_sent) - 1)"
ue="ue22"
rtt_measurement="graph_measurement"
query_period=30


while true
do
    echo "Building temporal graph..."

    end=$(date +%s)
    start=$((${end}-${query_period}))

    echo "Start - ${start}, End - ${end}"
    JSON_STRING=$( jq -n \
                  --arg sfc "${sfc}" \
                  --arg sfci "${sfci}" \
                  --arg sf "${service_function}" \
                  --arg measurement "${measurement}"\
                  --arg response_time "${response_time_field}"\
                  --arg request_size "${request_size_field}"\
                  --arg response_size "${response_size_field}"\
                  --argjson from ${start}\
                  --argjson to ${end}\
                  '{from: $from, to: $to, service_function_chain: $sfc, service_function_chain_instance: $sfci, service_functions: {($sf): {measurement_name: $measurement, response_time_field: $response_time, request_size_field: $request_size, response_size_field: $response_size}}}' )
    echo "Sending build request to CLMC"
    response=$(curl -s -X POST -d "${JSON_STRING}" http://172.40.231.51/clmc-service/graph/temporal)
    timestamp=$(echo ${response} | jq -r '.graph.time_range.to')
    graph_uuid=$(echo ${response} | jq -r '.graph.uuid')
    echo "Received request uuid ${graph_uuid}"
    echo "Timestamp to use for measurement ${timestamp}"

    echo "Querying for round-trip time..."
    response=$(curl -s -X GET "http://172.40.231.51/clmc-service/graph/temporal/${graph_uuid}/round-trip-time?startpoint=${ue}&endpoint=${endpoint}")

    global_tags=$(echo ${response} | jq -r '.global_tags | to_entries | map("\(.key)=\(.value|tostring)") | join(",")')
    echo "Global tags: ${global_tags}"

    local_tags=$(echo ${response} | jq -r '.local_tags | to_entries | map("\(.key)=\(.value|tostring)") | join (",")')
    echo "Local tags: ${local_tags}"

    rtt=$(echo ${response} | jq -r '.round_trip_time')
    echo "Round-trip-time: ${rtt}"

    service_delay=$(echo ${response} | jq -r '.response_time')
    echo "Service delay: ${service_delay}"

    network_delay=$(echo ${response} | jq -r '.total_forward_latency')
    echo "Network latency ${network_delay}"

    measurement_line="${rtt_measurement},${global_tags},${local_tags} round_trip_time=${rtt},service_delay=${service_delay},network_delay=${network_delay} ${timestamp}"
    echo "Measurement line: ${measurement_line}"
    response=$(curl -si -XPOST "http://172.40.231.51/influxdb/write?db=${sfc}" --data-binary "${measurement_line}")
    echo "InfluxDB response: ${response}"

    echo "Deleting temporal graph..."
    response=$(curl -s -X DELETE "http://172.40.231.51/clmc-service/graph/temporal/${graph_uuid}")
    echo ${response}

    echo "Sleeping ${query_period} seconds"
    sleep ${query_period}

done