package Client;

import Docker.*;
import Server.GenerateServerConfigRecord;
import Server.InterfaceAndConfigUploadTest;
import de.qaware.chronix.client.benchmark.benchmarkrunner.BenchmarkRunner;
import de.qaware.chronix.client.benchmark.benchmarkrunner.util.TimeSeriesCounter;
import de.qaware.chronix.client.benchmark.configurator.Configurator;
import de.qaware.chronix.client.benchmark.util.JsonTimeSeriesHandler;
import de.qaware.chronix.database.BenchmarkDataSource.QueryFunction;
import de.qaware.chronix.database.TimeSeriesMetaData;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mcqueen666 on 31.08.16.
 */
public class SimpleTestClient {

    public static void main(String[] args){

        Configurator configurator = Configurator.getInstance();
        JsonTimeSeriesHandler jsonTimeSeriesHandler = JsonTimeSeriesHandler.getInstance();

        long startMillis;
        long endMillis;
        String server = "localhost";

        try {
            if (configurator.isServerUp(server)) {
                System.out.println("Server is up");
            }
        } catch (Exception e){
            System.out.println("Server not responding. Error: " + e.getLocalizedMessage());
            return;
        }


        GenerateServerConfigRecord.main(new String[]{server});
        UploadDockerFiles.main(new String[]{server});
        InterfaceAndConfigUploadTest.main(new String[]{server});
        //BuildDockerContainer.main(new String[]{server,"chronix","influxdb","kairosdb", "opentsdb", "graphite"});
        StartDockerContainer.main(new String[]{server,"chronix","influxdb","kairosdb", "opentsdb", "graphite"});
        //RunningTestDockerContainer.main(new String[]{server,"chronix","influxdb","kairosdb", "opentsdb", "graphite"});

        //StopDockerContainer.main(new String[]{server,"chronix","influxdb","kairosdb", "opentsdb", "graphite"});



        // import test

        List<File> directories = new ArrayList<>();
        directories.add(new File("/Users/mcqueen666/chronixBenchmark/timeseries_records/air-lasttest_small"));
        //directories.add(new File("/Users/mcqueen666/chronixBenchmark/timeseries_records/air-lasttest"));
        //directories.add(new File("/Users/mcqueen666/chronixBenchmark/timeseries_records/shd"));
        //directories.add(new File("/Users/mcqueen666/chronixBenchmark/timeseries_records/promt"));
        // as if was not imported previously
        for(File directory : directories){
            jsonTimeSeriesHandler.deleteTimeSeriesMetaDataJsonFile(directory.getName());
        }
        startMillis = System.currentTimeMillis();
        ImportTest.importTimeSeriesFromDirectory(server, directories);
        endMillis = System.currentTimeMillis();
        System.out.println("\nImport test total time: " + (endMillis - startMillis) + "ms");



        // query test
        TimeSeriesCounter timeSeriesCounter = TimeSeriesCounter.getInstance();
        List<TimeSeriesMetaData> randomTimeSeries = timeSeriesCounter.getRandomTimeSeriesMetaData(10);
        startMillis = System.currentTimeMillis();
        QueryTest.queryCount(server, randomTimeSeries, QueryFunction.COUNT);
        endMillis = System.currentTimeMillis();
        System.out.println("\nQuery test total time: " + (endMillis - startMillis) + "ms");


        //get benchmark query record test
        BenchmarkRunner benchmarkRunner = BenchmarkRunner.getInstance();
        System.out.println("Downloading benchmark records from server successful: " +  benchmarkRunner.getBenchmarkRecordsFromServer(server));

    }
}

@@ -184,24 +184,110 @@
 markRunner;%0A
+import de.qaware.chronix.client.benchmark.benchmarkrunner.util.BenchmarkRunnerHelper;%0A
 import de.qa
@@ -2871,18 +2871,16 @@
 rintln(%22
-%5Cn
 Import t
@@ -2926,24 +2926,26 @@
 illis) + %22ms
+%5Cn
 %22);%0A%0A%0A%0A     
@@ -3143,16 +3143,229 @@
 ta(10);%0A
+        BenchmarkRunnerHelper benchmarkRunnerHelper = BenchmarkRunnerHelper.getInstance();%0A        QueryFunction function = QueryFunction.COUNT;%0A        function = benchmarkRunnerHelper.getRandomQueryFunction();%0A%0A
         
@@ -3461,35 +3461,24 @@
 Series, 
-QueryF
+f
 unction
-.COUNT
 );%0A     
@@ -3548,18 +3548,16 @@
 rintln(%22
-%5Cn
 Query te
@@ -3606,16 +3606,18 @@
 s) + %22ms
+%5Cn
 %22);%0A%0A%0A  

test random query function. NOTE: graphite stddev is wrong(server error from graphite)