use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-core-public by MangoAutomation.
the class ValueChangeCounterQuantizerTest method testNoStartValueManyValuesPerPeriod.
@Test
public void testNoStartValueManyValuesPerPeriod() throws QueryCancelledException {
// Generate data at 12 noon for every day in the period
NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
NextTimePeriodAdjuster hourlyAdjuster = new NextTimePeriodAdjuster(TimePeriods.HOURS, 1);
time = ZonedDateTime.of(2017, 01, 01, 12, 00, 00, 0, zoneId);
List<IdPointValueTime> data = new ArrayList<>();
while (time.toInstant().isBefore(to.toInstant())) {
// Insert 10 values per day
int value = 1;
ZonedDateTime daily = ZonedDateTime.ofInstant(time.toInstant(), zoneId);
for (int i = 0; i < 10; i++) {
data.add(new IdPointValueTime(1, new MultistateValue(value), daily.toInstant().toEpochMilli()));
daily = (ZonedDateTime) hourlyAdjuster.adjustInto(daily);
value = value + 1;
}
time = (ZonedDateTime) adjuster.adjustInto(time);
}
// Reset time to track periods
time = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
MutableInt counter = new MutableInt(0);
BucketCalculator bc = new TimePeriodBucketCalculator(from, to, TimePeriods.DAYS, 1);
ValueChangeCounterQuantizer quantizer = new ValueChangeCounterQuantizer(bc, new StatisticsGeneratorQuantizerCallback<ValueChangeCounter>() {
@Override
public void quantizedStatistics(ValueChangeCounter statisticsGenerator) throws QueryCancelledException {
counter.increment();
ValueChangeCounter stats = statisticsGenerator;
// Test periodStart
Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
// Test periiodEnd
Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
// Test first
Assert.assertEquals(1, stats.getFirstValue().getIntegerValue());
Assert.assertEquals(time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getFirstTime());
// Test last
Assert.assertEquals(10, stats.getLastValue().getIntegerValue());
Assert.assertEquals(time.plusHours(12).plusHours(9).toInstant().toEpochMilli(), (long) stats.getLastTime());
// Test start (the first start value will be null
if (counter.getValue() == 1)
Assert.assertEquals(null, stats.getStartValue());
else
Assert.assertEquals(10, stats.getStartValue().getIntegerValue());
// Test count
Assert.assertEquals(10, stats.getCount());
// Ensure data
// Test Changes
Assert.assertEquals(10, stats.getChanges());
// Move to next period time
time = (ZonedDateTime) adjuster.adjustInto(time);
}
});
quantizer.firstValue(null, true);
for (int count = 0; count < data.size(); count++) quantizer.accept(data.get(count));
quantizer.lastValue(data.get(data.size() - 1), true);
quantizer.done();
Assert.assertEquals(Integer.valueOf(31), counter.getValue());
}
use of com.infiniteautomation.mango.db.query.QueryCancelledException 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.db.query.QueryCancelledException in project ma-modules-public by infiniteautomation.
the class PointValueTimeStreamJsonSerializer method serialize.
@Override
public void serialize(PointValueTimeStream<T, INFO> value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
PointValueTimeWriter writer = new PointValueTimeJsonWriter(value.getQueryInfo(), jgen);
value.setContentType(StreamContentType.JSON);
try {
value.start(writer);
value.streamData(writer);
value.finish(writer);
jgen.flush();
} catch (QueryCancelledException e) {
LOG.info("Query cancelled.", e);
}
}
use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-modules-public by infiniteautomation.
the class MultiDataPointStatisticsQuantizerStream method firstValue.
@Override
public void firstValue(IdPointValueTime value, boolean bookend) {
try {
DataPointStatisticsQuantizer<?> quantizer = this.quantizerMap.get(value.getSeriesId());
if (!info.isSingleArray())
writer.writeStartArray(quantizer.vo.getXid());
updateQuantizers(value);
quantizer.firstValue(value, bookend);
} catch (IOException e) {
throw new QueryCancelledException(e);
}
}
use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-modules-public by infiniteautomation.
the class MultiDataPointDefaultRollupStatisticsQuantizerStream method firstValue.
@Override
public void firstValue(IdPointValueTime value, boolean bookend) {
try {
if (!useSimplify) {
super.firstValue(value, bookend);
return;
}
DataPointStatisticsQuantizer<?> quantizer = this.quantizerMap.get(value.getSeriesId());
updateQuantizers(value);
quantizer.firstValue(value, bookend);
} catch (IOException e) {
throw new QueryCancelledException(e);
}
}
Aggregations