All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.intelie.liverig.plugin.widgets.AutoAnalysisExpressions Maven / Gradle / Ivy

The newest version!
package net.intelie.liverig.plugin.widgets;

import net.intelie.live.Query;
import net.intelie.live.QueryEvent;
import net.intelie.live.util.SyncQuery;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AutoAnalysisExpressions {
    private String adjustTimestamp;
    private String eventType;
    private String qualifier;
    private Long begin;
    private Long end;
    private List computeFields;
    private Map> lookupValues;

    public AutoAnalysisExpressions(String eventType, String qualifier, Long begin, Long end, List computeFields, Map> lookupValues, String adjustTimestamp) {
        this.eventType = eventType;
        this.qualifier = qualifier;
        this.begin = begin;
        this.end = end;
        this.computeFields = computeFields;
        this.lookupValues = lookupValues;
        this.adjustTimestamp = adjustTimestamp;
    }

    private String filter() {
        if (eventType != null) return "'" + eventType + "'";

        String lookupId = lookupValues.keySet().stream().findFirst().get();

        return PipesExpressions.lookupImpl(lookupId) + PipesExpressions.LOOKUP_VALUE_MACRO;
    }

    private String getPipesTimeSpan() {
        return "from ts " + begin + " to ts " + end;
    }

    private String getGeneralFieldsExpression() {

        StringBuilder expr = new StringBuilder(filter() + " " + adjustTimestamp + " mnemonic!: '" + qualifier + "' => timestamp");

        for (String field : computeFields) {
            switch (field) {
                case "min":
                    expr.append(", min(value#) as min");
                    break;
                case "max":
                    expr.append(", max(value#) as max");
                    break;
                case "avg":
                    expr.append(", avg(value#) as avg");
                    break;
                case "stdev":
                    expr.append(", stdev(value#) as stdev");
                    break;
                case "linreg":
                    expr.append(", (mregression(value#) as coefficients");
                    expr.append(", expand regression(value#)) as linearRegression");
                    break;
            }
        }

        expr.append(", index_uom as indexUnit");

        expr.append(" at the end");

        return expr.toString();
    }

    private String getLinearRegressionExpression(Double coefa, Double coefb) {

        String expr = filter() + " " + adjustTimestamp + " mnemonic!: '" + qualifier + "' =>";
        expr += " mregression.apply((" + coefa + ", " + coefb + "), timestamp()) as y, timestamp# as x every item";

        return expr;
    }

    private String getOriginalCurveExpression() {
        return filter() + " " + adjustTimestamp + " mnemonic!: '" + qualifier + "' => value# as y, timestamp# as x";
    }

    public Query generalAnalysis(AutoAnalysisAnnotation annotation) {
        String expr = getGeneralFieldsExpression();
        String span = getPipesTimeSpan();

        GeneralAnalysis generalAnalysis = new GeneralAnalysis();
        final AutoAnalysisAnnotationExtra extra = annotation.getExtra();
        extra.setGeneralAnalysis(generalAnalysis);

        return new Query(expr)
                .span(span)
                .lookupValues(lookupValues)
                .reduceHistory("@compress 640, 6400, (itermap(*) |> value#) by *[@@0]:object:compile.map(explode @@group)")
                .description("auto analysis - general fields")
                .listenWith(new SyncQuery.Listener() {
                    @Override
                    public void onEvent(QueryEvent event, boolean history) {
                        for (Map data : event) {
                            if (data.get("min") instanceof Double)
                                generalAnalysis.setMin((Double) data.get("min"));

                            if (data.get("indexUnit") instanceof String)
                                extra.setIndexUnit((String) data.get("indexUnit"));

                            if (data.get("max") instanceof Double)
                                generalAnalysis.setMax((Double) data.get("max"));

                            if (data.get("avg") instanceof Double)
                                generalAnalysis.setAvg((Double) data.get("avg"));

                            if (data.get("stdev") instanceof Double)
                                generalAnalysis.setStdev((Double) data.get("stdev"));

                            if (data.get("linearRegression") instanceof Map) {
                                Map linreg = (Map) data.get("linearRegression");
                                extra.setLinearRegression(new LinearRegression());

                                if (linreg.get("coefficients") instanceof List) {
                                    List coeff = (List) linreg.get("coefficients");
                                    if (coeff.size() > 1) {
                                        Double c1 = coeff.get(0);
                                        Double c2 = coeff.get(1);
                                        extra.getLinearRegression().setCoefficients(new Double[]{c1, c2});
                                    }
                                }

                                if (linreg.get("slope") instanceof Double)
                                    extra.getLinearRegression().setSlope((Double) linreg.get("slope"));

                                if (linreg.get("intercept") instanceof Double)
                                    extra.getLinearRegression().setIntercept((Double) linreg.get("intercept"));

                                if (linreg.get("correlation") instanceof Double)
                                    extra.getLinearRegression().setCorrelation((Double) linreg.get("correlation"));
                            }
                        }
                    }
                });
    }

    public Query originalCurve(AutoAnalysisAnnotation annotation) {
        String expr = getOriginalCurveExpression();
        String span = getPipesTimeSpan();

        List points = new ArrayList<>();
        annotation.getExtra().getGeneralAnalysis().setOriginalCurvePoints(points);

        return new Query(expr)
                .span(span)
                .lookupValues(lookupValues)
                .reduceHistory("@compress 640, 6400, (itermap(*) |> value#) by *[@@0]:object:compile.map(explode @@group)")
                .description("auto analysis - original points")
                .listenWith(new SyncQuery.Listener() {
                    @Override
                    public void onEvent(QueryEvent event, boolean history) {
                        for (Map data : event) {
                            Double timestamp = (Double) data.get("timestamp");
                            Double x = (Double) data.get("x");
                            Double y = (Double) data.get("y");

                            points.add(new CurvePoint(timestamp, x, y));
                        }
                    }
                });
    }

    public Query linearRegression(AutoAnalysisAnnotation annotation) {

        if (annotation.getExtra().getLinearRegression() == null) return null;

        Double[] coefficients = annotation.getExtra().getLinearRegression().getCoefficients();

        if (coefficients == null || coefficients.length < 2) return null;

        String expr = getLinearRegressionExpression(coefficients[0], coefficients[1]);
        String span = getPipesTimeSpan();

        List points = new ArrayList<>();
        annotation.getExtra().getLinearRegression().setPoints(points);

        return new Query(expr)
                .span(span)
                .lookupValues(lookupValues)
                .reduceHistory("@compress 640, 6400, (itermap(*) |> value#) by *[@@0]:object:compile.map(explode @@group)")
                .description("auto analysis - linear regression points")
                .listenWith(new SyncQuery.Listener() {
                    @Override
                    public void onEvent(QueryEvent event, boolean history) {
                        for (Map data : event) {
                            Double timestamp = (Double) data.get("timestamp");
                            Double x = (Double) data.get("x");
                            Double y = (Double) data.get("y");

                            points.add(new CurvePoint(timestamp, x, y));
                        }
                    }
                });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy