use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField in project ma-modules-public by infiniteautomation.
the class MultiPointStatisticsStreamTest method testSingleNumericPointOnlyCacheChangeCachedValueAtFrom.
@Test
public void testSingleNumericPointOnlyCacheChangeCachedValueAtFrom() throws IOException, QueryCancelledException {
// Setup the data to run once daily for 30 days
ZonedDateTime from = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
ZonedDateTime to = ZonedDateTime.of(2017, 02, 01, 00, 00, 00, 0, zoneId);
NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
int cacheSize = 10;
MockDataSourceVO ds = createDataSource();
DataPointVO dp = createDataPoint(ds.getId(), DataType.NUMERIC, cacheSize);
DataPointWrapper<AnalogStatistics> point = new DataPointWrapper<AnalogStatistics>(ds, dp, new PointValueTime(1.0, 0), (value) -> {
return new NumericValue(value.getDoubleValue() + 1.0);
}, (info, w) -> {
return new AnalogStatistics(info.getFromMillis(), info.getToMillis(), w.values.get(0), w.values);
}, new AnalogStatisticsVerifier());
// Insert the data skipping first day so we get the initial value
ZonedDateTime time = (ZonedDateTime) adjuster.adjustInto(from);
timer.setStartTime(time.toInstant().toEpochMilli());
while (time.toInstant().isBefore(to.toInstant())) {
point.updatePointValue(new PointValueTime(point.getNextValue(), time.toInstant().toEpochMilli()));
time = (ZonedDateTime) adjuster.adjustInto(time);
timer.fastForwardTo(time.toInstant().toEpochMilli());
}
// Insert some values directly into the cache
point.values.clear();
for (int i = 0; i < cacheSize; i++) {
time = (ZonedDateTime) adjuster.adjustInto(time);
timer.fastForwardTo(time.toInstant().toEpochMilli());
point.saveOnlyToCache(new AnnotatedPointValueTime(point.getNextValue(), timer.currentTimeMillis(), new TranslatableMessage("common.default", "testing")));
}
// Ensure we get all the data
from = ZonedDateTime.ofInstant(Instant.ofEpochMilli(point.values.get(0).getTime()), zoneId);
Instant now = Instant.ofEpochMilli(timer.currentTimeMillis() + 1);
to = ZonedDateTime.ofInstant(now, zoneId);
// Perform the query
String dateTimeFormat = null;
String timezone = zoneId.getId();
PointValueTimeCacheControl cache = PointValueTimeCacheControl.CACHE_ONLY;
PointValueField[] fields = getFields();
ZonedDateTimeStatisticsQueryInfo info = new ZonedDateTimeStatisticsQueryInfo(from, to, dateTimeFormat, timezone, cache, fields);
test(info, point);
}
use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField in project ma-modules-public by infiniteautomation.
the class MultiPointStatisticsStreamTest method testMultiplePointsOnlyCacheChange.
@Test
public void testMultiplePointsOnlyCacheChange() throws IOException, QueryCancelledException {
// Setup the data to run once daily for 30 days
ZonedDateTime from = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
ZonedDateTime to = ZonedDateTime.of(2017, 02, 01, 00, 00, 00, 0, zoneId);
NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
int cacheSize = 10;
MockDataSourceVO ds = createDataSource();
DataPointVO numericDp = createDataPoint(ds.getId(), DataType.NUMERIC, cacheSize);
DataPointWrapper<AnalogStatistics> numericPoint = new DataPointWrapper<AnalogStatistics>(ds, numericDp, new PointValueTime(1.0, 0), (value) -> {
return new NumericValue(value.getDoubleValue() + 1.0);
}, (info, w) -> {
return new AnalogStatistics(info.getFromMillis(), info.getToMillis(), null, w.values);
}, (w, gen, root) -> {
JsonNode stats = root.get(w.vo.getXid());
if (stats == null)
fail("Missing stats for point " + w.vo.getXid());
JsonNode stat = stats.get(PointValueTimeWriter.START);
if (stat == null)
fail("Missing " + PointValueTimeWriter.START + " entry");
assertNull(gen.getStartValue());
assertTrue(stat.isNull());
stat = stats.get(PointValueTimeWriter.FIRST);
if (stat == null)
fail("Missing " + PointValueTimeWriter.FIRST + " entry");
PointValueTime value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
assertEquals(gen.getFirstValue(), value.getValue().getDoubleValue(), 0.00001);
assertEquals((long) gen.getFirstTime(), value.getTime());
stat = stats.get(PointValueTimeWriter.LAST);
if (stat == null)
fail("Missing " + PointValueTimeWriter.LAST + " entry");
value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
assertEquals(gen.getLastValue(), value.getValue().getDoubleValue(), 0.00001);
assertEquals((long) gen.getLastTime(), value.getTime());
stat = stats.get(PointValueTimeWriter.COUNT);
if (stat == null)
fail("Missing " + PointValueTimeWriter.COUNT + " entry");
assertEquals(gen.getCount(), stat.asInt());
stat = stats.get(PointValueTimeWriter.ACCUMULATOR);
if (stat == null)
fail("Missing " + PointValueTimeWriter.ACCUMULATOR + " entry");
value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
Double accumulatorValue = gen.getLastValue();
if (accumulatorValue == null)
accumulatorValue = gen.getMaximumValue();
assertEquals(accumulatorValue, value.getDoubleValue(), 0.00001);
stat = stats.get(PointValueTimeWriter.AVERAGE);
if (stat == null)
fail("Missing " + PointValueTimeWriter.AVERAGE + " entry");
value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
assertEquals(gen.getAverage(), value.getDoubleValue(), 0.00001);
stat = stats.get(PointValueTimeWriter.DELTA);
if (stat == null)
fail("Missing " + PointValueTimeWriter.DELTA + " entry");
value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
assertEquals(gen.getDelta(), value.getDoubleValue(), 0.00001);
stat = stats.get(PointValueTimeWriter.MINIMUM);
if (stat == null)
fail("Missing " + PointValueTimeWriter.MINIMUM + " entry");
value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
assertEquals(gen.getMinimumValue(), value.getValue().getDoubleValue(), 0.00001);
assertEquals((long) gen.getMinimumTime(), value.getTime());
stat = stats.get(PointValueTimeWriter.MAXIMUM);
if (stat == null)
fail("Missing " + PointValueTimeWriter.MAXIMUM + " entry");
value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
assertEquals(gen.getMaximumValue(), value.getValue().getDoubleValue(), 0.00001);
assertEquals((long) gen.getMaximumTime(), value.getTime());
stat = stats.get(PointValueTimeWriter.SUM);
if (stat == null)
fail("Missing " + PointValueTimeWriter.SUM + " entry");
value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
assertEquals(gen.getSum(), value.getDoubleValue(), 0.00001);
stat = stats.get(PointValueTimeWriter.INTEGRAL);
if (stat == null)
fail("Missing " + PointValueTimeWriter.INTEGRAL + " entry");
value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
assertEquals(gen.getIntegral(), value.getDoubleValue(), 0.00001);
});
DataPointVO multistateDp = createDataPoint(ds.getId(), DataType.MULTISTATE, cacheSize);
DataPointWrapper<StartsAndRuntimeList> multistatePoint = new DataPointWrapper<StartsAndRuntimeList>(ds, multistateDp, new PointValueTime(1, 0), (value) -> {
return new MultistateValue(value.getIntegerValue() + 1);
}, (info, w) -> {
return new StartsAndRuntimeList(info.getFromMillis(), info.getToMillis(), null, w.values);
}, (w, gen, root) -> {
JsonNode stats = root.get(w.vo.getXid());
if (stats == null)
fail("Missing stats for point " + w.vo.getXid());
JsonNode stat = stats.get(PointValueTimeWriter.START);
if (stat == null)
fail("Missing " + PointValueTimeWriter.START + " entry");
assertNull(gen.getStartValue());
assertTrue(stat.isNull());
stat = stats.get(PointValueTimeWriter.FIRST);
if (stat == null)
fail("Missing " + PointValueTimeWriter.FIRST + " entry");
PointValueTime value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
assertEquals(gen.getFirstValue(), value.getValue());
assertEquals((long) gen.getFirstTime(), value.getTime());
stat = stats.get(PointValueTimeWriter.LAST);
if (stat == null)
fail("Missing " + PointValueTimeWriter.LAST + " entry");
value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
assertEquals(gen.getLastValue(), value.getValue());
assertEquals((long) gen.getLastTime(), value.getTime());
stat = stats.get(PointValueTimeWriter.COUNT);
if (stat == null)
fail("Missing " + PointValueTimeWriter.COUNT + " entry");
assertEquals(gen.getCount(), stat.asInt());
// Test data
stat = stats.get(PointValueTimeWriter.STARTS_AND_RUNTIMES);
if (stat == null)
fail("Missing data entry");
for (int i = 0; i < gen.getData().size(); i++) {
StartsAndRuntime expected = gen.getData().get(i);
JsonNode actual = stat.get(i);
assertEquals((int) expected.getValue(), actual.get(PointValueTimeWriter.VALUE).intValue());
assertEquals(expected.getStarts(), actual.get(PointValueTimeWriter.STARTS).intValue());
assertEquals(expected.getRuntime(), actual.get(PointValueTimeWriter.RUNTIME).asLong());
assertEquals(expected.getProportion(), actual.get(PointValueTimeWriter.PROPORTION).doubleValue(), 0.000001);
}
});
// Insert the data skipping first day so we get the initial value
ZonedDateTime time = (ZonedDateTime) adjuster.adjustInto(from);
timer.setStartTime(time.toInstant().toEpochMilli());
while (time.toInstant().isBefore(to.toInstant())) {
numericPoint.updatePointValue(new PointValueTime(numericPoint.getNextValue(), time.toInstant().toEpochMilli()));
multistatePoint.updatePointValue(new PointValueTime(multistatePoint.getNextValue(), time.toInstant().toEpochMilli()));
time = (ZonedDateTime) adjuster.adjustInto(time);
timer.fastForwardTo(time.toInstant().toEpochMilli());
}
// Insert some values directly into the cache
numericPoint.values.clear();
multistatePoint.values.clear();
for (int i = 0; i < cacheSize; i++) {
time = (ZonedDateTime) adjuster.adjustInto(time);
timer.fastForwardTo(time.toInstant().toEpochMilli());
numericPoint.saveOnlyToCache(new PointValueTime(numericPoint.getNextValue(), timer.currentTimeMillis()));
multistatePoint.saveOnlyToCache(new PointValueTime(multistatePoint.getNextValue(), timer.currentTimeMillis()));
}
// Ensure we get all the data
Instant now = Instant.ofEpochMilli(timer.currentTimeMillis() + 1);
to = ZonedDateTime.ofInstant(now, zoneId);
// Perform the query
String dateTimeFormat = null;
String timezone = zoneId.getId();
PointValueTimeCacheControl cache = PointValueTimeCacheControl.CACHE_ONLY;
PointValueField[] fields = getFields();
ZonedDateTimeStatisticsQueryInfo info = new ZonedDateTimeStatisticsQueryInfo(from, to, dateTimeFormat, timezone, cache, fields);
test(info, numericPoint, multistatePoint);
}
use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField in project ma-modules-public by infiniteautomation.
the class MultiPointStatisticsStream method processRow.
@Override
protected void processRow(IdPointValueTime value, boolean firstBookend, boolean lastBookend, boolean cached) throws QueryCancelledException {
try {
final DataPointVO vo = voMap.get(value.getSeriesId());
if (info.isUseCache() != PointValueTimeCacheControl.NONE && !cached)
if (!processValueThroughCache(value, firstBookend, lastBookend))
return;
StatisticsGenerator generator = statsMap.compute(value.getSeriesId(), (k, v) -> {
if (v == null) {
switch(vo.getPointLocator().getDataType()) {
case BINARY:
case MULTISTATE:
v = new StartsAndRuntimeList(info.getFromMillis(), info.getToMillis(), value);
break;
case ALPHANUMERIC:
v = new ValueChangeCounter(info.getFromMillis(), info.getToMillis(), value);
break;
case NUMERIC:
v = new AnalogStatistics(info.getFromMillis(), info.getToMillis(), value);
break;
default:
throw new ShouldNeverHappenException("Invalid Data Type: " + voMap.get(value.getSeriesId()).getPointLocator().getDataType());
}
}
if (!lastBookend && !firstBookend) {
v.addValueTime(value);
}
return v;
});
if (lastBookend) {
generator.done();
this.writer.writeStartObject(vo.getXid());
DataPointStatisticsGenerator gen = new DataPointStatisticsGenerator(vo, generator);
// Pre-process the fields
boolean rendered = false;
Set<PointValueField> fields = new HashSet<>();
for (PointValueField field : this.writer.getInfo().getFields()) {
if (field == PointValueField.RENDERED) {
rendered = true;
} else if (field == PointValueField.ANNOTATION) {
continue;
} else {
fields.add(field);
}
}
// Remove the Value field we will write it after
fields.remove(PointValueField.VALUE);
for (PointValueField field : fields) {
field.writeValue(gen, info, Common.getTranslations(), false, writer);
}
this.writer.writeAllStatistics(generator, vo, rendered, fields.contains(PointValueField.RAW));
this.writer.writeEndObject();
}
} catch (IOException e) {
throw new QueryCancelledException(e);
}
}
use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField in project ma-modules-public by infiniteautomation.
the class MultiPointStatisticsStreamTest method testSingleNumericPointNoCacheNoChangeInitialValue.
@Test
public void testSingleNumericPointNoCacheNoChangeInitialValue() {
// Setup the data to run once daily for 30 days
ZonedDateTime from = ZonedDateTime.of(2017, 1, 1, 0, 0, 0, 0, zoneId);
ZonedDateTime to = ZonedDateTime.of(2017, 2, 1, 0, 0, 0, 0, zoneId);
Duration adjuster = Duration.ofDays(1);
MockDataSourceVO ds = createDataSource();
DataPointVO dp = createDataPoint(ds.getId(), DataType.NUMERIC, 1);
DataPointWrapper<AnalogStatistics> wrapper = new DataPointWrapper<>(ds, dp, new PointValueTime(1.0, 0), // no change
Function.identity(), (w) -> new AnalogStatistics(from.toInstant().toEpochMilli(), to.toInstant().toEpochMilli(), w.initialValue, w.values), new AnalogStatisticsVerifier());
// Insert the data skipping first day so we get the initial value
ZonedDateTime time = from.plus(adjuster);
timer.setStartTime(time.toInstant().toEpochMilli());
while (time.toInstant().isBefore(to.toInstant())) {
wrapper.updatePointValue(new PointValueTime(wrapper.getNextValue(), time.toInstant().toEpochMilli()));
time = time.plus(adjuster);
timer.fastForwardTo(time.toInstant().toEpochMilli());
}
// Perform the query
String timezone = zoneId.getId();
PointValueField[] fields = getFields();
var result = getStatistics(List.of(dp), from, to, timezone, fields);
test(result, wrapper);
}
use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField in project ma-modules-public by infiniteautomation.
the class MultiPointStatisticsStreamTest method testMultiplePointsNoCacheChangeInitialValue.
@Test
public void testMultiplePointsNoCacheChangeInitialValue() {
// Setup the data to run once daily for 30 days
ZonedDateTime from = ZonedDateTime.of(2017, 1, 1, 0, 0, 0, 0, zoneId);
ZonedDateTime to = ZonedDateTime.of(2017, 2, 1, 0, 0, 0, 0, zoneId);
Duration adjuster = Duration.ofDays(1);
MockDataSourceVO ds = createDataSource();
DataPointVO numericDp = createDataPoint(ds.getId(), DataType.NUMERIC, 1);
DataPointWrapper<AnalogStatistics> numericWrapper = new DataPointWrapper<>(ds, numericDp, new PointValueTime(1.0, 0), (value) -> new NumericValue(value.getDoubleValue() + 1.0), (w) -> new AnalogStatistics(from.toInstant().toEpochMilli(), to.toInstant().toEpochMilli(), w.initialValue, w.values), new AnalogStatisticsVerifier());
DataPointVO multistateDp = createDataPoint(ds.getId(), DataType.MULTISTATE, 1);
DataPointWrapper<StartsAndRuntimeList> multistateWrapper = new DataPointWrapper<>(ds, multistateDp, new PointValueTime(1, 0), (value) -> new MultistateValue(value.getIntegerValue() + 1), (w) -> new StartsAndRuntimeList(from.toInstant().toEpochMilli(), to.toInstant().toEpochMilli(), w.initialValue, w.values), new StartsAndRuntimeListVerifier());
// Insert the data skipping first day so we get the initial value
ZonedDateTime time = from.plus(adjuster);
timer.setStartTime(time.toInstant().toEpochMilli());
while (time.toInstant().isBefore(to.toInstant())) {
numericWrapper.updatePointValue(new PointValueTime(numericWrapper.getNextValue(), time.toInstant().toEpochMilli()));
multistateWrapper.updatePointValue(new PointValueTime(multistateWrapper.getNextValue(), time.toInstant().toEpochMilli()));
time = time.plus(adjuster);
timer.fastForwardTo(time.toInstant().toEpochMilli());
}
// Perform the query
String timezone = zoneId.getId();
PointValueField[] fields = getFields();
var result = getStatistics(List.of(numericDp, multistateDp), from, to, timezone, fields);
test(result, numericWrapper, multistateWrapper);
}
Aggregations