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

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

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

import com.google.common.base.Strings;
import net.intelie.live.*;
import net.intelie.live.plugins.annotations.api.AnnotationData;
import net.intelie.live.plugins.annotations.api.AnnotationService;
import net.intelie.live.util.SyncQuery;
import net.intelie.liverig.plugin.assets.Asset;
import net.intelie.liverig.plugin.assets.AssetService;
import net.intelie.liverig.plugin.assets.AssetTypeService;
import net.intelie.liverig.plugin.normalizer.NormalizerConfig;
import net.intelie.live.UseProxy;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.intelie.live.Permission.ADMIN;
import static net.intelie.live.plugins.annotations.Permission.EDIT_ANNOTATIONS;
import static net.intelie.liverig.plugin.assets.AssetNormalizerService.Helper.*;

@UseProxy
@Path("/")
@Consumes("application/json")
@Produces("application/json")
public class AutoAnalysisService {

    private final Live.Engine engine;
    private final AnnotationService annotationService;
    private final Live live;
    private final AssetService assetService;
    private final String indexProperty;


    public AutoAnalysisService(Live live, AnnotationService annotationService, AssetService assetService, String indexProperty) {
        this.live = live;
        this.engine = live.engine();
        this.annotationService = annotationService;
        this.assetService = assetService;
        this.indexProperty = indexProperty;
    }

    @GET
    @Path("/analyse")
    @NeedsAuthority(Permission.DASHBOARDS)
    public AutoAnalysisAnnotation analyse(@QueryParam("assetId") String assetId, @QueryParam("channel") String channel,
                                          @QueryParam("qualifier") String qualifier, @QueryParam("begin") Long begin,
                                          @QueryParam("end") Long end, @QueryParam("lookupId") String lookupId,
                                          @QueryParam("lookupValues") List lookupValues,
                                          @QueryParam("computeFields") final List computeFields) throws Exception {

        if (Strings.isNullOrEmpty(assetId) ||
                Strings.isNullOrEmpty(channel) ||
                Strings.isNullOrEmpty(qualifier) ||
                begin == null || begin < 0 ||
                end == null || end < 0 ||
                end < begin ||
                computeFields == null || computeFields.isEmpty())
            throw new IllegalArgumentException("Insufficient parameters provided");

        String filter = null, assetName = null;
        Map> lookup = null;

        if (lookupId == null || lookupValues == null) {
            String[] assetIdParts = assetId.split("/", 2);
            String assetType = assetIdParts[0];
            String extensionId = assetIdParts[1];

            AssetTypeService assetTypeService = assetService.get(assetType);

            if (assetTypeService == null)
                throw new IllegalArgumentException("Could not retrieve service for type " + assetType);

            Asset asset = assetTypeService.get(extensionId);
            NormalizerConfig normalizer = getNormalizer(assetTypeService, extensionId);

            if (normalizer == null)
                throw new IllegalArgumentException("Could not find asset " + assetId);

            if (asset != null)
                assetName = asset.getName();

            filter = normalizer.event_type();
        } else {
            lookup = new HashMap<>();
            lookup.put(lookupId, lookupValues);

            assetName = lookupValues.isEmpty() ? "*" : lookupValues.get(0);
        }

        AutoAnalysisExpressions autoAnalysisExpressions = new AutoAnalysisExpressions(filter, qualifier, begin, end, computeFields, lookup, indexProperty);

        AutoAnalysisAnnotation autoAnalysisAnnotation = new AutoAnalysisAnnotation();
        autoAnalysisAnnotation.setExtra(new AutoAnalysisAnnotationExtra());
        autoAnalysisAnnotation.setBegin(begin);
        autoAnalysisAnnotation.setEnd(end);

        Query generalAnalysis = autoAnalysisExpressions.generalAnalysis(autoAnalysisAnnotation);
        Query originalPoints = autoAnalysisExpressions.originalCurve(autoAnalysisAnnotation);
        SyncQuery.runSyncQueries(engine, generalAnalysis, originalPoints);

        autoAnalysisAnnotation.getExtra().setQualifier(qualifier);
        autoAnalysisAnnotation.getExtra().setAsset(assetName);
        autoAnalysisAnnotation.getExtra().setAssetId(assetId);
        autoAnalysisAnnotation.getExtra().setChannel(channel);

        if (autoAnalysisAnnotation.getExtra().getLinearRegression() != null) {
            Query linearRegression = autoAnalysisExpressions.linearRegression(autoAnalysisAnnotation);
            if (linearRegression != null)
                SyncQuery.runSyncQueries(engine, linearRegression);
        }

        if (computeFields.contains("derivatives")) {
            autoAnalysisAnnotation.getExtra().setDerivatives(new Derivatives());
            autoAnalysisAnnotation.getExtra().getDerivatives().calculate(autoAnalysisAnnotation.getExtra().getGeneralAnalysis().getOriginalCurvePoints(), 1000D);
        }

        return autoAnalysisAnnotation;
    }

    @POST
    @Path("/save-annotation")
    @NeedsAuthority(Permission.DASHBOARDS)
    public void saveAnnotation(AutoAnalysisAnnotation autoAnalysisAnnotation) throws Exception {
        LoggedUser loggedUser = live.auth().getLoggedUser();

        if (loggedUser == null || !canEditAnnotation(loggedUser))
            throw new WebApplicationException(Response.Status.FORBIDDEN);

        if (autoAnalysisAnnotation.hasInsufficientFields())
            throw new IllegalArgumentException("Insufficient parameters provided");

        annotationService.createOrUpdate(autoAnalysisAnnotation.toAnnotationData(), false, false);
    }

    @POST
    @Path("/edit-annotation")
    @NeedsAuthority(Permission.DASHBOARDS)
    public void editAnnotation(AnnotationData annotationData) throws Exception {

        if (annotationData.getAuthorId() == null ||
                annotationData.getAuthor() == null || annotationData.getAuthor().isEmpty() ||
                annotationData.getBegin() < 0 ||
                annotationData.getEnd() == null || annotationData.getEnd() < 0 ||
                annotationData.getEnd() < annotationData.getBegin() ||
                annotationData.getDashboard() == null || annotationData.getDashboard().isEmpty() ||
                annotationData.getDashboardId() == null ||
                annotationData.getColor() == null || annotationData.getColor().isEmpty() ||
                annotationData.getCreatedAt() < 0)
            throw new IllegalArgumentException("Insufficient parameters provided");

        if (canEditAnnotation(annotationData)) annotationService.createOrUpdate(annotationData, true, false);
        else throw new WebApplicationException(Response.Status.FORBIDDEN);
    }

    private boolean canEditAnnotation(AnnotationData annotationData) throws Exception {
        LoggedUser loggedUser = live.auth().getLoggedUser();

        if (loggedUser == null)
            return false;

        boolean sameUser = loggedUser.getUser().getId().equals(annotationData.getAuthorId());

        return canEditAnnotation(loggedUser) || sameUser;
    }

    private boolean canEditAnnotation(LoggedUser loggedUser) throws Exception {
        boolean isAdmin = loggedUser.getUser().isSuperuser() || loggedUser.hasAnyOf(ADMIN);
        boolean canEditAnnotation = loggedUser.hasAnyOf(EDIT_ANNOTATIONS);

        return isAdmin || canEditAnnotation;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy