#!/bin/bash

DATABASE=$1 # "medium" or "big"
SESSION_ID=$2
MEASURES_DIR=./time_measures

NB_MEASURES=10
STEPS_MEDIUM_DB='{"steps": [{"lat": 48.78872170000001,"lng": -0.7280873000000021,"path_id": 7061},{"lat": 48.73181390000002,"lng": -0.7179939000000024,"path_id": 4139},{"lat": 48.676642,"lng": -0.6817526000000051,"path_id": 109},{"lat": 48.67861300000001,"lng": -0.6439691999999918,"path_id": 4177},{"lat": 48.7009625,"lng": -0.6448388000000005,"path_id": 4183},{"lat": 48.73732735527879,"lng": -0.6272379744023348,"path_id": 7047},{"lat": 48.766032100000004,"lng": -0.6849768999999983,"path_id": 110},{"lat": 48.81694350000001,"lng": -0.6613134000000054,"path_id": 4178},{"lat": 48.805070000000015,"lng": -0.6267835000000033,"path_id": 7021},{"lat": 48.78119819999999,"lng": -0.6334089999999981,"path_id": 4211},{"lat": 48.7650436,"lng": -0.6056722999999975,"path_id": 5611},{"lat": 48.71871339999999,"lng": -0.5552605000000099,"path_id": 637},{"lat": 48.7487884,"lng": -0.5378913000000063,"path_id": 698},{"lat": 48.78614750000001,"lng": -0.5632506999999909,"path_id": 4270},{"lat": 48.78451229999999,"lng": -0.5297335000000069,"path_id": 737},{"lat": 48.77718510751703,"lng": -0.5195043605179128,"path_id": 4300},{"lat": 48.80810350000001,"lng": -0.5094418000000012,"path_id": 6903},{"lat": 48.8110711,"lng": -0.4875585000000004,"path_id": 6862},{"lat": 48.80337130000001,"lng": -0.4788153000000062,"path_id": 6863},{"lat": 48.7722458,"lng": -0.4598700999999994,"path_id": 5522},{"lat": 48.7500188,"lng": -0.4239464000000015,"path_id": 6767},{"lat": 48.79490820408397,"lng": -0.3516193881266494,"path_id": 1439},{"lat": 48.774549100000016,"lng": -0.3201351000000052,"path_id": 6715},{"lat": 48.808130000000006,"lng": -0.29126549999999307,"path_id": 6738},{"lat": 48.745965,"lng": -0.24042960000000724,"path_id": 1501},{"lat": 48.7750731,"lng": -0.2063589000000099,"path_id": 4496},{"lat": 48.78273430000001,"lng": -0.16739840000000503,"path_id": 8093},{"lat": 48.7545855,"lng": -0.148252499999999,"path_id": 1567},{"lat": 48.7399976273847,"lng": -0.10886289938103699,"path_id": 6587},{"lat": 48.685151645123064,"lng": -0.14202597929864336,"path_id": 8088},{"lat": 48.7331811,"lng": -0.040682100000004606,"path_id": 7456},{"lat": 48.730169499999995,"lng": -0.006157399999993096,"path_id": 7466},{"lat": 48.691925299999994,"lng": -0.010618899999994547,"path_id": 6318},{"lat": 48.69748177728096,"lng": 0.09194033562911928,"path_id": 4665},{"lat": 48.692691,"lng": 0.12254079999998167,"path_id": 4689},{"lat": 48.72093480000001,"lng": 0.1583081999999969,"path_id": 4712},{"lat": 48.644576125589296,"lng": 0.20426585602449254,"path_id": 4720},{"lat": 48.70390922334383,"lng": 0.2592412032409186,"path_id": 4790},{"lat": 48.661601097217876,"lng": 0.25658600841475193,"path_id": 6376},{"lat": 48.651978400000004,"lng": 0.2847110999999858,"path_id": 6345},{"lat": 48.6566794174864,"lng": 0.32453158568303664,"path_id": 2640},{"lat": 48.659802768793455,"lng": 0.3474000596792193,"path_id": 2650},{"lat": 48.644699200000005,"lng": 0.35545089999998497,"path_id": 4841},{"lat": 48.61567840000001,"lng": 0.3517553000000051,"path_id": 4849},{"lat": 48.564724800000015,"lng": 0.2624846000000058,"path_id": 6367},{"lat": 48.5398916,"lng": 0.3795004000000013,"path_id": 7165},{"lat": 48.52095420260181,"lng": 0.46465391313148524,"path_id": 7787},{"lat": 48.464230900000004,"lng": 0.45510510000000615,"path_id": 6069},{"lat": 48.53586557886738,"lng": 0.5931378100232408,"path_id": 4995},{"lat": 48.52928500000001,"lng": 0.6260873000000133,"path_id": 5006},{"lat": 48.533391247525124,"lng": 0.6481910378033451,"path_id": 5042},{"lat": 48.577232900000006,"lng": 0.67391269999999,"path_id": 3581},{"lat": 48.5795185,"lng": 0.7066258000000092,"path_id": 3739},{"lat": 48.53995800000002,"lng": 0.6869060000000005,"path_id": 3623},{"lat": 48.5359437488372,"lng": 0.6912809757068716,"path_id": 3623},{"lat": 48.511737499999995,"lng": 0.6729164000000099,"path_id": 3593},{"lat": 48.49124379999997,"lng": 0.6834466999999876,"path_id": 3630},{"lat": 48.48738920000002,"lng": 0.6502814000000035,"path_id": 3441},{"lat": 48.5047461,"lng": 0.6321343999999929,"path_id": 3364},{"lat": 48.43746869999999,"lng": 0.5907351000000194,"path_id": 7766},{"lat": 48.43067998875949,"lng": 0.7381241776589542,"path_id": 5353},{"lat": 48.3774613,"lng": 0.620931399999991,"path_id": 6051},{"lat": 48.30171857552506,"lng": 0.6833065121897075,"path_id": 6204},{"lat": 48.3058772,"lng": 0.5374620000000174,"path_id": 6174},{"lat": 48.39975019999999,"lng": 0.30353279999998595,"path_id": 4777},{"lat": 48.61152178064436,"lng": 0.02678793256564393,"path_id": 8528},{"lat": 48.58126070000001,"lng": -0.030045000000002986,"path_id": 1732},{"lat": 48.57413842317843,"lng": -0.12063000313336845,"path_id": 1562},{"lat": 48.4704601,"lng": -0.15201150000000885,"path_id": 5394},{"lat": 48.5211142,"lng": -0.3140358,"path_id": 5629},{"lat": 48.5306078,"lng": -0.4423807000000024,"path_id": 4335},{"lat": 48.5469623,"lng": -0.48764910000000267,"path_id": 878},{"lat": 48.58734660000001,"lng": -0.5976487999999991,"path_id": 4223},{"lat": 48.61087130000001,"lng": -0.6010315000000088,"path_id": 326},{"lat": 48.61396018034764,"lng": -0.570809390607494,"path_id": 494},{"lat": 48.612043899999996,"lng": -0.5199207000000028,"path_id": 715},{"lat": 48.63959500000001,"lng": -0.4897316999999957,"path_id": 886},{"lat": 48.64058295423239,"lng": -0.45624500678267177,"path_id": 1014},{"lat": 48.616869718771966,"lng": -0.4685391699291319,"path_id": 983},{"lat": 48.57784138065519,"lng": -0.4363485819627533,"path_id": 1050},{"lat": 48.59565210000001,"lng": -0.38701859999999755,"path_id": 5961},{"lat": 48.609623400000004,"lng": -0.20698610000000617,"path_id": 5940},{"lat": 48.63182200000001,"lng": -0.21355209999999403,"path_id": 8055},{"lat": 48.6129627,"lng": -0.09591579999999711,"path_id": 5920},{"lat": 48.64295144732567,"lng": -0.040852126035595404,"path_id": 1685},{"lat": 48.68075370000001,"lng": -0.046584000000000625,"path_id": 8583},{"lat": 48.675437099999996,"lng": -0.07790740000000351,"path_id": 8564},{"lat": 48.7812038,"lng": -0.055628700000001086,"path_id": 8099},{"lat": 48.83095460000001,"lng": 0.011730200000017454,"path_id": 1867},{"lat": 48.85684071765811,"lng": 0.05253954573986696,"path_id": 1995},{"lat": 48.88185430000001,"lng": 0.12435850000001913,"path_id": 2252},{"lat": 48.930391076452246,"lng": 0.1626837010355686,"path_id": 2295},{"lat": 48.94935437081008,"lng": 0.2541650682723562,"path_id": 5761},{"lat": 48.883915499999986,"lng": 0.2652445999999919,"path_id": 6532},{"lat": 48.859749300000004,"lng": 0.3261208999999887,"path_id": 6495},{"lat": 48.835374800000004,"lng": 0.3320875999999906,"path_id": 6491},{"lat": 48.774814200000016,"lng": 0.3728876999999864,"path_id": 4863},{"lat": 48.801146400000015,"lng": 0.4462774000000147,"path_id": 6522},{"lat": 48.74335909999999,"lng": 0.5005958000000144,"path_id": 5860},{"lat": 48.748907900000006,"lng": 0.6154992000000092,"path_id": 3314},{"lat": 48.775356900000006,"lng": 0.6953322999999934,"path_id": 6442},{"lat": 48.80584295122468,"lng": 0.622037502408217,"path_id": 5024}]}'
STEPS_BIG_DB='{"steps": [{"lat": 44.40018778200495,"lng": 0.9883771908915584,"path_id": 169899},{"lat": 44.23831930000001,"lng": 1.2774443000000035,"path_id": 125826},{"lat": 44.337832899999995,"lng": 1.4496121999999945,"path_id": 51128},{"lat": 44.24025119999999,"lng": 1.6089541999999968,"path_id": 161645},{"lat": 44.369933499999995,"lng": 1.803867500000016,"path_id": 64765},{"lat": 44.46619299999999,"lng": 1.4093345999999851,"path_id": 192840},{"lat": 44.40395120000001,"lng": 1.1285949000000173,"path_id": 66258},{"lat": 44.5469963,"lng": 1.080862999999983,"path_id": 147987},{"lat": 44.65210690000001,"lng": 1.1733569000000044,"path_id": 143244},{"lat": 44.57784079999999,"lng": 1.3804053000000092,"path_id": 251},{"lat": 44.66788439999999,"lng": 1.4108565000000084,"path_id": 100640},{"lat": 44.6401763,"lng": 1.507850999999989,"path_id": 33213},{"lat": 44.521939499999974,"lng": 1.5459739999999833,"path_id": 62926},{"lat": 44.596103400000004,"lng": 1.6275356000000185,"path_id": 128221},{"lat": 44.46257740000001,"lng": 1.7491142000000037,"path_id": 152665},{"lat": 44.48786577758629,"lng": 1.9581521494801324,"path_id": 11392},{"lat": 44.589200899999994,"lng": 1.7456906999999955,"path_id": 62058},{"lat": 44.7475971,"lng": 1.6675977999999914,"path_id": 103494},{"lat": 44.74754839999998,"lng": 1.449830099999998,"path_id": 84807},{"lat": 44.891118599999984,"lng": 1.4784941000000051,"path_id": 135865},{"lat": 45.020028100000005,"lng": 1.6124456999999959,"path_id": 131624},{"lat": 44.8388804,"lng": 1.7647118999999953,"path_id": 189792},{"lat": 44.9560056,"lng": 1.9026717000000115,"path_id": 72000},{"lat": 44.80577899999999,"lng": 1.9953387000000067,"path_id": 102924},{"lat": 44.659736399999986,"lng": 1.9534054000000145,"path_id": 104388},{"lat": 44.5835307,"lng": 2.0760624000000183,"path_id": 146309},{"lat": 44.86836009999998,"lng": 2.175293200000019,"path_id": 82783}]}'

launch_scenario() {
# $1 (boolean): if true, keep the backend cache before each spec run

    # Reset the time measure files
    if [ -d "$MEASURES_DIR" ]; then
        rm -f "$MEASURES_DIR"/time_measures_*
    else
        mkdir "$MEASURES_DIR"
    fi

    for i in $(seq 1 $NB_MEASURES)
    do
        if ! $1; then
            # Empty the pgRouting network topology
            docker compose run --rm web ./manage.py dbshell -- -c "update core_path set source=null, target=null;"
        fi

        if [[ "$DATABASE" == "medium" ]]; then
            STEPS="$STEPS_MEDIUM_DB"
        else
            STEPS="$STEPS_BIG_DB"
        fi
        # Send a request to take the time measures
        curl 'http://geotrek.local:8000/api/path/drf/paths/route-geometry' -X POST \
             -H 'X-CSRFToken: DKUegVuaV01Lsb2s4ORLTx6dqG7CWiFP' \
             -H 'Content-Type: application/json; charset=UTF-8' \
             -H "Cookie: csrftoken=DKUegVuaV01Lsb2s4ORLTx6dqG7CWiFP; sessionid=$SESSION_ID" \
             --data-raw "$STEPS" \
             -s -o /dev/null
    done

    # Compute and display the average times
    echo "Branch:" $(git rev-parse --abbrev-ref HEAD) >> "$MEASURES_DIR"/time_averages.txt
    echo "Database:" $DATABASE >> "$MEASURES_DIR"/time_averages.txt
    echo "pgr network topology:" $1 >> "$MEASURES_DIR"/time_averages.txt
    echo "Number of runs:" $NB_MEASURES >> "$MEASURES_DIR"/time_averages.txt
    python3 ./time_averages.py >> "$MEASURES_DIR"/time_averages.txt
    echo "" >> "$MEASURES_DIR"/time_averages.txt
}

# Reset the time averages files
if [ -d "$MEASURES_DIR" ]; then
        rm -f "$MEASURES_DIR"/time_averages.txt
    else
        mkdir "$MEASURES_DIR"
    fi
launch_scenario false
launch_scenario true