import { check, sleep } from "k6"
import exec from "k6/execution"
import { SharedArray } from "k6/data"
import http from "k6/http"
import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js';

import { scrapeDataMod } from "./scrapeDataModule.js";

export const options = {
        scenarios:{},
        teardownTimeout: "120s",
        setupTimeout: "120s"
}

let scenarios = {
        base:{
                executor: "per-vu-iterations",
                vus: 1,
                iterations: 1,
                maxDuration: "5m"
        }
}

const baseURL = "http://localhost:8001";
let scrapeData = scrapeDataMod; 

let ENV = {};
if(__ENV.scenario){
        options.scenarios[__ENV.scenario] = scenarios[__ENV.scenario];
        ENV = options.scenarios[__ENV.scenario].env;
}else{
        options.scenarios = scenarios;
}

export function setup(){
        let baseData = {};
}

export default function(baseData){
    scrapeData.forEach((query) => {
        let body = {
            "instanceCount": query.instanceCount,
            "prefix": query.prefix,
            "idName": query.idName
        }
        const url = new URL(`${baseURL}/getPrice`);
        url.searchParams.append("instanceCount", query.instanceCount);
        url.searchParams.append("prefix", query.prefix);
        url.searchParams.append("idName", query.idName);
        url.searchParams.append("url", query.url);

        const res = http.get(url.toString());
        console.log(res.body);
    });

}

export function teardown(baseData){

}

function log(msg){
        if(__ENV.debug == "true"){
                console.log(msg);
        }
}
