<commit_before>package de.qaware.chronix.client.benchmark.queryhandler;

import de.qaware.chronix.client.benchmark.configurator.Configurator;
import de.qaware.chronix.shared.QueryUtil.BenchmarkRecord;
import de.qaware.chronix.shared.QueryUtil.CleanCommand;
import de.qaware.chronix.shared.QueryUtil.ImportRecord;
import de.qaware.chronix.shared.QueryUtil.QueryRecord;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mcqueen666 on 20.06.16.
 */
public class QueryHandler {

    private static QueryHandler instance;
    private Configurator configurator;
    private Map<String, Long> queryLatency;

    private QueryHandler(){
        configurator = Configurator.getInstance();
        queryLatency = new HashMap<>();
    }

    public static synchronized QueryHandler getInstance(){
        if(instance == null){
            instance = new QueryHandler();
        }
        return instance;
    }

    /**
     * Returns the latency of a query with given query id.
     *
     * @param queryID the query id
     * @return the latency in milliseconds or null if no record of given query id exists (query failed for some reason)
     */
    public Long getLatencyForQueryID(String queryID){
        return queryLatency.get(queryID);
    }


    /**
     * Transmits the given queryRecord to given server on which the query should be performed.
     *
     * @param serverAddress the server address or ip WITHOUT http://
     * @param queryRecord the queryRecord
     * @return String starting with server status code and either the query result string or the server error message string
     *         "[StatusCode] : [QueryResult] or [error message]"
     */
    public String[] doQueryOnServer(String serverAddress, QueryRecord queryRecord) {
        final Client client = ClientBuilder.newBuilder().build();
        final WebTarget target = client.target("http://"
                + serverAddress
                + ":"
                + configurator.getApplicationPort()
                + "/queryrunner/performQuery");
        long startMillis = System.currentTimeMillis();
        final Response response = target.request().post(Entity.json(queryRecord));
        long endMillis = System.currentTimeMillis();

        int statusCode = response.getStatus();
        String[] queryResults = response.readEntity(String[].class);
        client.close();

        if(statusCode == 200){
            queryLatency.put(queryRecord.getQueryID(), (endMillis - startMillis));
            return queryResults;
        }

        return new String[]{"Server status code: " + statusCode};


    }

    public String[] doImportOnServer(String serverAddress, ImportRecord importRecord) {
        final Client client = ClientBuilder.newBuilder().build();
        final WebTarget target = client.target("http://"
                + serverAddress
                + ":"
                + configurator.getApplicationPort()
                + "/queryrunner/performImport");
        long startMillis = System.currentTimeMillis();
        final Response response = target.request().post(Entity.json(importRecord));
        long endMillis = System.currentTimeMillis();

        int statusCode = response.getStatus();
        String[] queryResults = response.readEntity(String[].class);
        client.close();

        if(statusCode == 200){
            queryLatency.put(importRecord.getQueryID(), (endMillis - startMillis));
            return queryResults;<commit_msg>latency is now saved in correspondence to pair of queryID and tsdbName to be unique per measurement import<commit_after>package de.qaware.chronix.client.benchmark.queryhandler;

import de.qaware.chronix.client.benchmark.configurator.Configurator;
import de.qaware.chronix.shared.DataModels.Pair;
import de.qaware.chronix.shared.QueryUtil.BenchmarkRecord;
import de.qaware.chronix.shared.QueryUtil.CleanCommand;
import de.qaware.chronix.shared.QueryUtil.ImportRecord;
import de.qaware.chronix.shared.QueryUtil.QueryRecord;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mcqueen666 on 20.06.16.
 */
public class QueryHandler {

    private static QueryHandler instance;
    private Configurator configurator;
    private Map<Pair<String, String>, Long> queryLatency;

    private QueryHandler(){
        configurator = Configurator.getInstance();
        queryLatency = new HashMap<>();
    }

    public static synchronized QueryHandler getInstance(){
        if(instance == null){
            instance = new QueryHandler();
        }
        return instance;
    }

    /**
     * Returns the latency of a query with given query id, tsdbName Pair.
     *
     * @param queryID_tsdb_Pair the query id, tsdbName pair
     * @return the latency in milliseconds or null if no record of given query id exists (query failed for some reason)
     */
    public Long getLatencyForQueryID(Pair<String, String> queryID_tsdb_Pair){
        return queryLatency.get(queryID_tsdb_Pair);
    }


    /**
     * Transmits the given queryRecord to given server on which the query should be performed.
     *
     * @param serverAddress the server address or ip WITHOUT http://
     * @param queryRecord the queryRecord
     * @return String starting with server status code and either the query result string or the server error message string
     *         "[StatusCode] : [QueryResult] or [error message]"
     */
    public String[] doQueryOnServer(String serverAddress, QueryRecord queryRecord) {
        final Client client = ClientBuilder.newBuilder().build();
        final WebTarget target = client.target("http://"
                + serverAddress
                + ":"
                + configurator.getApplicationPort()
                + "/queryrunner/performQuery");
        long startMillis = System.currentTimeMillis();
        final Response response = target.request().post(Entity.json(queryRecord));
        long endMillis = System.currentTimeMillis();

        int statusCode = response.getStatus();
        String[] queryResults = response.readEntity(String[].class);
        client.close();

        if(statusCode == 200){
            queryLatency.put(Pair.of(queryRecord.getQueryID(), queryRecord.getTsdbName()), (endMillis - startMillis));
            return queryResults;
        }

        return new String[]{"Server status code: " + statusCode};


    }

    public String[] doImportOnServer(String serverAddress, ImportRecord importRecord) {
        final Client client = ClientBuilder.newBuilder().build();
        final WebTarget target = client.target("http://"
                + serverAddress
                + ":"
                + configurator.getApplicationPort()
                + "/queryrunner/performImport");
        long startMillis = System.currentTimeMillis();
        final Response response = target.request().post(Entity.json(importRecord));
        long endMillis = System.currentTimeMillis();

        int statusCode = response.getStatus();
        String[] queryResults = response.readEntity(String[].class);
        client.close();

        if(statusCode == 200){
            queryLatency.put(Pair.of(importRecord.getQueryID(), importRecord.getTsdbName()), (endMillis - startMillis));
            return queryResults;
{'fraction_non_alphanumeric': 0.1005104043973302, 'fraction_numerical': 0.0039261876717707105, 'mean_word_length': 2.9169656586365966, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5650342', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1975, 'n_words': 518}