Search in sources :

Example 1 with DataPointVOPointValueTimeBookend

use of com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend in project ma-modules-public by infiniteautomation.

the class MultiPointLatestDatabaseStream method simplify.

/**
 * Simplify according to our requirements
 *
 * TODO This currently only works for Numeric Points
 *
 * @param list
 * @return
 */
protected List<DataPointVOPointValueTimeBookend> simplify(List<DataPointVOPointValueTimeBookend> list) {
    LogStopWatch logStopWatch = new LogStopWatch();
    if (info.simplifyTolerance != null) {
        // TODO improve Simplify code to return a list
        Simplify<DataPointVOPointValueTimeBookend> simplify = new Simplify<DataPointVOPointValueTimeBookend>(new DataPointVOPointValueTimeBookend[0], SimplifyPointValueExtractor.extractor);
        DataPointVOPointValueTimeBookend[] simplified = simplify.simplify(list.toArray(new DataPointVOPointValueTimeBookend[list.size()]), info.simplifyTolerance, info.simplifyHighQuality);
        logStopWatch.stop("Finished Simplify, tolerance: " + info.simplifyTolerance);
        return Arrays.asList(simplified);
    } else {
        if (list.size() < info.simplifyTarget)
            return list;
        // Compute target bounds as 10% of target
        int lowerTarget = info.simplifyTarget - (int) (info.simplifyTarget * 0.1);
        int upperTarget = info.simplifyTarget + (int) (info.simplifyTarget * 0.1);
        // Compute tolerance bounds and initial tolerance
        Double max = Double.MIN_VALUE;
        Double min = Double.MAX_VALUE;
        for (DataPointVOPointValueTimeBookend value : list) {
            if (value.getPvt().getDoubleValue() > max)
                max = value.getPvt().getDoubleValue();
            if (value.getPvt().getDoubleValue() < min)
                min = value.getPvt().getDoubleValue();
        }
        double difference = max - min;
        double tolerance = difference / 20d;
        double topBound = difference;
        double bottomBound = 0;
        // Determine max iterations we can allow
        int maxIterations = 100;
        int iteration = 1;
        Simplify<DataPointVOPointValueTimeBookend> simplify = new Simplify<DataPointVOPointValueTimeBookend>(new DataPointVOPointValueTimeBookend[0], SimplifyPointValueExtractor.extractor);
        DataPointVOPointValueTimeBookend[] simplified = simplify.simplify(list.toArray(new DataPointVOPointValueTimeBookend[list.size()]), tolerance, info.simplifyHighQuality);
        DataPointVOPointValueTimeBookend[] best = simplified;
        while (simplified.length < lowerTarget || simplified.length > upperTarget) {
            if (simplified.length > info.simplifyTarget) {
                bottomBound = tolerance;
            } else {
                topBound = tolerance;
            }
            // Adjust tolerance
            tolerance = bottomBound + (topBound - bottomBound) / 2.0d;
            simplify = new Simplify<DataPointVOPointValueTimeBookend>(new DataPointVOPointValueTimeBookend[0], SimplifyPointValueExtractor.extractor);
            simplified = simplify.simplify(list.toArray(new DataPointVOPointValueTimeBookend[list.size()]), tolerance, info.simplifyHighQuality);
            // Keep our best effort
            if (Math.abs(info.simplifyTarget - simplified.length) < Math.abs(info.simplifyTarget - best.length))
                best = simplified;
            if (iteration > maxIterations) {
                simplified = best;
                break;
            }
            iteration++;
        }
        logStopWatch.stop("Finished Simplify, target: " + info.simplifyTarget + " actual " + simplified.length);
        return Arrays.asList(simplified);
    }
}
Also used : Simplify(com.goebl.simplify.Simplify) DataPointVOPointValueTimeBookend(com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend) LogStopWatch(com.serotonin.log.LogStopWatch)

Example 2 with DataPointVOPointValueTimeBookend

use of com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend in project ma-modules-public by infiniteautomation.

the class MultiPointSimplifyTimeRangeDatabaseStream method finish.

/* (non-Javadoc)
     * @see com.infiniteautomation.mango.rest.v2.model.pointValue.query.MultiPointLatestDatabaseStream#finish(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter)
     */
@Override
public void finish(PointValueTimeWriter writer) throws IOException {
    // Write out the values after simplifying
    Iterator<Integer> it = valuesMap.keySet().iterator();
    if (info.isSingleArray() && voMap.size() > 1) {
        List<DataPointVOPointValueTimeBookend> sorted = new ArrayList<>();
        while (it.hasNext()) {
            Integer id = it.next();
            BookendPair pair = bookendMap.get(id);
            if (pair != null && pair.startBookend != null)
                sorted.add(pair.startBookend);
            sorted.addAll(simplify(valuesMap.get(id)));
            if (// Can be null bookend if limit is hit
            pair != null && pair.endBookend != null)
                sorted.add(pair.endBookend);
        }
        // Sort the Sorted List
        Collections.sort(sorted, new Comparator<DataPointVOPointValueTimeBookend>() {

            @Override
            public int compare(DataPointVOPointValueTimeBookend o1, DataPointVOPointValueTimeBookend o2) {
                return o1.getPvt().compareTo(o2.getPvt());
            }
        });
        for (DataPointVOPointValueTimeBookend value : sorted) super.writeValue(value);
    } else {
        while (it.hasNext()) {
            Integer id = it.next();
            List<DataPointVOPointValueTimeBookend> values = simplify(valuesMap.get(id));
            BookendPair pair = bookendMap.get(id);
            if (pair != null && pair.startBookend != null)
                super.writeValue(pair.startBookend);
            for (DataPointVOPointValueTimeBookend value : values) super.writeValue(value);
            if (// Can be null bookend if limit is hit
            pair != null && pair.endBookend != null)
                super.writeValue(pair.endBookend);
        }
    }
    super.finish(writer);
}
Also used : ArrayList(java.util.ArrayList) DataPointVOPointValueTimeBookend(com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend)

Example 3 with DataPointVOPointValueTimeBookend

use of com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend in project ma-modules-public by infiniteautomation.

the class MultiPointSimplifyLatestDatabaseStream method finish.

/* (non-Javadoc)
     * @see com.infiniteautomation.mango.rest.v2.model.pointValue.query.MultiPointLatestDatabaseStream#finish(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter)
     */
@Override
public void finish(PointValueTimeWriter writer) throws IOException {
    // Write out the values after simplifying
    Iterator<Integer> it = valuesMap.keySet().iterator();
    if (info.isSingleArray() && voMap.size() > 1) {
        List<DataPointVOPointValueTimeBookend> sorted = new ArrayList<>();
        while (it.hasNext()) sorted.addAll(simplify(valuesMap.get(it.next())));
        // Sort the Sorted List
        Collections.sort(sorted, new Comparator<DataPointVOPointValueTimeBookend>() {

            @Override
            public int compare(DataPointVOPointValueTimeBookend o1, DataPointVOPointValueTimeBookend o2) {
                return o1.getPvt().compareTo(o2.getPvt());
            }
        });
        for (DataPointVOPointValueTimeBookend value : sorted) super.writeValue(value);
    } else {
        while (it.hasNext()) {
            List<DataPointVOPointValueTimeBookend> values = simplify(valuesMap.get(it.next()));
            for (DataPointVOPointValueTimeBookend value : values) super.writeValue(value);
        }
    }
    super.finish(writer);
}
Also used : ArrayList(java.util.ArrayList) DataPointVOPointValueTimeBookend(com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend)

Aggregations

DataPointVOPointValueTimeBookend (com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend)3 ArrayList (java.util.ArrayList)2 Simplify (com.goebl.simplify.Simplify)1 LogStopWatch (com.serotonin.log.LogStopWatch)1