package macrobase.runtime.resources;

import macrobase.MacroBase;
import macrobase.analysis.pipeline.BasicBatchedPipeline;
import macrobase.conf.MacroBaseConf;
import macrobase.analysis.result.AnalysisResult;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Path("/analyze")
@Produces(MediaType.APPLICATION_JSON)
public class AnalyzeResource extends BaseResource {
    private static final Logger log = LoggerFactory.getLogger(SchemaResource.class);

    static class AnalysisRequest {
        public String pgUrl;
        public String baseQuery;
        public List<String> attributes;
        public List<String> highMetrics;
        public List<String> lowMetrics;
    }

    static class AnalysisResponse {
        public List<AnalysisResult> results;
        public String errorMessage;
    }

    public AnalyzeResource(MacroBaseConf conf) {
        super(conf);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public AnalysisResponse getAnalysis(AnalysisRequest request) {
        AnalysisResponse response = new AnalysisResponse();

        try {
            List<String> allMetrics = new ArrayList<>();
            allMetrics.addAll(request.highMetrics);
            allMetrics.addAll(request.lowMetrics);

            conf.set(MacroBaseConf.DB_URL, request.pgUrl);
            conf.set(MacroBaseConf.BASE_QUERY, request.baseQuery);
            conf.set(MacroBaseConf.ATTRIBUTES, request.attributes);
            conf.set(MacroBaseConf.METRICS, allMetrics);
            conf.set(MacroBaseConf.LOW_METRIC_TRANSFORM, request.lowMetrics);
            conf.set(MacroBaseConf.USE_PERCENTILE, true);

            // temp hack to enable CSV loading
            if(request.baseQuery.contains("csv://")) {
                conf.set(MacroBaseConf.CSV_INPUT_FILE, request.baseQuery.replace("csv://", ""));
                conf.set(MacroBaseConf.DATA_LOADER_TYPE, MacroBaseConf.DataIngesterType.CSV_LOADER);
            }

            List<AnalysisResult> results = new BasicBatchedPipeline().initialize(conf).run();

            for (AnalysisResult result : results) {
                if (result.getItemSets().size() > 1000) {
                    log.warn("Very large result set! {}; truncating to 1000", result.getItemSets().size());
                    result.setItemSets(result.getItemSets().subList(0, 1000));
                }
            }

            response.results = results;

            MacroBase.reporter.report();
        } catch (Exception e) {
            log.error("An error occurred while processing a request: {}", e);
            response.errorMessage = ExceptionUtils.getStackTrace(e);
        }

        return response;
    }
}

@@ -137,26 +137,34 @@
 ase.
-conf.MacroBaseConf
+analysis.pipeline.Pipeline
 ;%0Aim
@@ -210,16 +210,53 @@
 Result;%0A
+import macrobase.conf.MacroBaseConf;%0A
 import o
@@ -386,9 +386,95 @@
 .rs.
-*
+Consumes;%0Aimport javax.ws.rs.POST;%0Aimport javax.ws.rs.Path;%0Aimport javax.ws.rs.Produces
 ;%0Aim
@@ -510,29 +510,8 @@
 pe;%0A
-import java.io.File;%0A
 impo
@@ -1174,24 +1174,122 @@
 nf);%0A    %7D%0A%0A
+    private static Pipeline defaultPipeline() %7B%0A        return new BasicBatchedPipeline();%0A    %7D%0A%0A
     @POST%0A  
@@ -2080,16 +2080,17 @@
       if
+ 
 (request
@@ -2351,65 +2351,618 @@
     
-List%3CAnalysisResult%3E results
+Pipeline pipeline = defaultPipeline();%0A%0A            if (conf.isSet(MacroBaseConf.PIPELINE_NAME)) %7B%0A                Class c = Class.forName(conf.getString(MacroBaseConf.PIPELINE_NAME));%0A                Object ao
  = 
+c.
 new
- BasicBatchedPipeline()
+Instance();%0A%0A                if (!(ao instanceof Pipeline)) %7B%0A                    log.error(%22%7B%7D is not an instance of Pipeline! Exiting...%22, ao);%0A                    response.errorMessage = %22Found a not existing Pipeline name!%22;%0A                    return response;%0A                %7D%0A                pipeline = (Pipeline) ao;%0A            %7D%0A%0A            List%3CAnalysisResult%3E results = pipeline
 .ini

Allow configuring any pipeline to work with web GUI