use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-modules-public by infiniteautomation.
the class MultiDataPointStatisticsQuantizerStream method lastValue.
@Override
public void lastValue(IdPointValueTime value, boolean bookend) {
try {
DataPointStatisticsQuantizer<?> quantizer = this.quantizerMap.get(value.getSeriesId());
IdPointValueTimeRow row = this.currentValueTimeMap.remove(value.getSeriesId());
if (row != null) {
quantizer.accept(row.value);
}
quantizer.lastValue(value, bookend);
// This will definitely be the last time we see this point
if (!info.isSingleArray()) {
quantizer.done();
writer.writeEndArray();
}
} catch (IOException e) {
throw new QueryCancelledException(e);
}
}
use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-modules-public by infiniteautomation.
the class PointValueTimeStreamCsvMessageConverter method writeInternal.
@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
MediaType contentType = outputMessage.getHeaders().getContentType();
JsonEncoding encoding = getJsonEncoding(contentType);
try {
PointValueTimeStream<?, ?> stream = (PointValueTimeStream<?, ?>) object;
stream.setContentType(StreamContentType.CSV);
JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
// Set the schema
CsvSchema.Builder builder = CsvSchema.builder();
builder.setUseHeader(true);
// Setup our rendering parameters
LatestQueryInfo info = stream.getQueryInfo();
if (stream instanceof MultiPointTimeRangeDatabaseStream || stream instanceof MultiPointLatestDatabaseStream) {
if (info.isSingleArray()) {
if (info.isMultiplePointsPerArray()) {
Map<Integer, DataPointVO> voMap = stream.getVoMap();
Iterator<Integer> it = voMap.keySet().iterator();
boolean firstTimestamp = true;
while (it.hasNext()) {
String xid = voMap.get(it.next()).getXid();
for (PointValueField field : info.getFields()) {
if (field == PointValueField.TIMESTAMP) {
if (firstTimestamp)
field.createColumn(builder, xid);
firstTimestamp = false;
} else
field.createColumn(builder, xid);
}
}
} else {
for (PointValueField field : info.getFields()) field.createColumn(builder, null);
}
} else {
for (PointValueField field : info.getFields()) field.createColumn(builder, null);
}
} else if (stream instanceof MultiDataPointStatisticsQuantizerStream || stream instanceof MultiDataPointDefaultRollupStatisticsQuantizerStream) {
if (stream.getQueryInfo().isSingleArray()) {
if (stream.getQueryInfo().isMultiplePointsPerArray()) {
Map<Integer, DataPointVO> voMap = stream.getVoMap();
Iterator<Integer> it = voMap.keySet().iterator();
boolean firstTimestamp = true;
while (it.hasNext()) {
String xid = voMap.get(it.next()).getXid();
for (PointValueField field : info.getFields()) {
if (field == PointValueField.TIMESTAMP) {
if (firstTimestamp)
field.createColumn(builder, xid);
firstTimestamp = false;
} else if (field == PointValueField.VALUE) {
if (info.getRollup() == RollupEnum.ALL) {
for (RollupEnum rollup : getAllRollups()) {
builder.addColumn(xid + PointValueTimeWriter.DOT + rollup.name(), ColumnType.NUMBER_OR_STRING);
}
} else {
field.createColumn(builder, xid);
}
} else {
field.createColumn(builder, xid);
}
}
}
} else {
// Single array
if (info.getRollup() == RollupEnum.ALL) {
for (RollupEnum rollup : getAllRollups()) {
builder.addColumn(rollup.name(), ColumnType.NUMBER_OR_STRING);
}
for (PointValueField field : info.getFields()) {
if (field == PointValueField.VALUE)
continue;
field.createColumn(builder, null);
}
} else {
for (PointValueField field : info.getFields()) field.createColumn(builder, null);
}
}
} else {
if (info.getRollup() == RollupEnum.ALL) {
for (RollupEnum rollup : getAllRollups()) {
builder.addColumn(rollup.name(), ColumnType.NUMBER_OR_STRING);
}
for (PointValueField field : info.getFields()) {
if (field == PointValueField.VALUE)
continue;
field.createColumn(builder, null);
}
} else {
for (PointValueField field : info.getFields()) field.createColumn(builder, null);
}
}
}
generator.setSchema(builder.build());
PointValueTimeWriter writer = new PointValueTimeCsvWriter(stream.getQueryInfo(), stream.getVoMap().size(), generator);
try {
stream.start(writer);
stream.streamData(writer);
stream.finish(writer);
generator.flush();
} catch (QueryCancelledException e) {
throw new HttpMessageNotWritableException("Query Cancelled");
}
} catch (JsonProcessingException ex) {
throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
}
}
use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-core-public by infiniteautomation.
the class AnalogStatisticsQuantizerTest method testNoStartValueManyValuesPerPeriod.
// TODO Test with End Value on edge of period end i.e. not a bookend (this won't happen via a query)
//
// Many Values Per Period Tests
//
@Test
public void testNoStartValueManyValuesPerPeriod() throws QueryCancelledException {
// Generate data at 12 noon for every day in the period
NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(ChronoUnit.DAYS, 1);
NextTimePeriodAdjuster hourlyAdjuster = new NextTimePeriodAdjuster(ChronoUnit.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
double value = 1.0;
ZonedDateTime daily = ZonedDateTime.ofInstant(time.toInstant(), zoneId);
for (int i = 0; i < 10; i++) {
data.add(new IdPointValueTime(1, new NumericValue(value), daily.toInstant().toEpochMilli()));
daily = (ZonedDateTime) hourlyAdjuster.adjustInto(daily);
value = value + 1.0d;
}
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 TemporalAmountBucketCalculator(from, to, Period.ofDays(1));
AnalogStatisticsQuantizer quantizer = new AnalogStatisticsQuantizer(bc, new StatisticsGeneratorQuantizerCallback<AnalogStatistics>() {
@Override
public void quantizedStatistics(AnalogStatistics statisticsGenerator) throws QueryCancelledException {
counter.increment();
AnalogStatistics stats = statisticsGenerator;
// Test periodStart
Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
// Test periiodEnd
Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
if (counter.getValue() == 1) {
// Test Minimum
Assert.assertEquals(1.0, stats.getMinimumValue(), 0.0001);
Assert.assertEquals(time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getMinimumTime());
// Test Maximum
Assert.assertEquals(10.0, stats.getMaximumValue(), 0.0001);
Assert.assertEquals(time.plusHours(12).plusHours(9).toInstant().toEpochMilli(), (long) stats.getMaximumTime());
} else {
// Have start value
// Test Minimum
Assert.assertEquals(1.0, stats.getMinimumValue(), 0.0001);
Assert.assertEquals(time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getMinimumTime());
// Test Maximum
Assert.assertEquals(10.0, stats.getMaximumValue(), 0.0001);
Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMaximumTime());
}
// 1-9 for 1hr each, 10 for 12hrs at the start and 2hrs at the end
if (counter.getValue() == 1) {
double integral = 1d * 60d * 60d + 2d * 60 * 60 + 3d * 60 * 60 + 4d * 60 * 60 + 5d * 60 * 60 + 6d * 60 * 60 + 7d * 60 * 60 + 8d * 60 * 60 + 9d * 60 * 60;
integral = integral + 10d * 3d * 60 * 60;
// first 12hrs didn't have a value
double average = integral / (12d * 60d * 60d);
Assert.assertEquals(average, stats.getAverage(), 0.0001);
// Test Integral
Assert.assertEquals(integral, stats.getIntegral(), 0.0001);
} else {
double integral = 1d * 60d * 60d + 2d * 60 * 60 + 3d * 60 * 60 + 4d * 60 * 60 + 5d * 60 * 60 + 6d * 60 * 60 + 7d * 60 * 60 + 8d * 60 * 60 + 9d * 60 * 60;
integral = integral + 10d * 15d * 60 * 60;
double average = integral / (24d * 60d * 60d);
Assert.assertEquals(average, stats.getAverage(), 0.0001);
// Test Integral
Assert.assertEquals(integral, stats.getIntegral(), 0.0001);
}
// Test sum
Assert.assertEquals(55d, stats.getSum(), 0.0001);
// Test first
Assert.assertEquals(1.0d, stats.getFirstValue().getDoubleValue(), 0.0001);
Assert.assertEquals(time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getFirstTime());
// Test last
Assert.assertEquals(10.0d, stats.getLastValue().getDoubleValue(), 0.0001);
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.assertNull(stats.getStartValue());
else
Assert.assertEquals(10.0, stats.getStartValue().getDoubleValue(), 0.0001);
// Test count
Assert.assertEquals(10, stats.getCount());
// Test delta
if (counter.getValue() == 1) {
// 1 to 10
Assert.assertEquals(9.0, stats.getDelta(), 0.0001);
} else {
Assert.assertEquals(0.0, stats.getDelta(), 0.0001);
}
// 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-core-public by infiniteautomation.
the class AnalogStatisticsQuantizerTest method testStartValueNoPeriodValues.
@Test
public void testStartValueNoPeriodValues() throws QueryCancelledException {
// Generate data at 12 noon for every day in the period
NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(ChronoUnit.DAYS, 1);
// Reset time to track periods
time = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
MutableInt counter = new MutableInt(0);
BucketCalculator bc = new TemporalAmountBucketCalculator(from, to, Period.ofDays(1));
AnalogStatisticsQuantizer quantizer = new AnalogStatisticsQuantizer(bc, new StatisticsGeneratorQuantizerCallback<AnalogStatistics>() {
@Override
public void quantizedStatistics(AnalogStatistics statisticsGenerator) throws QueryCancelledException {
counter.increment();
AnalogStatistics stats = statisticsGenerator;
// Test periodStart
Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
// Test periodEnd
Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
// Test Minimum
Assert.assertEquals(1.0, stats.getMinimumValue(), 0.0001);
Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMinimumTime());
// Test Maximum
Assert.assertEquals(1.0, stats.getMaximumValue(), 0.0001);
Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMaximumTime());
// Test Average
Assert.assertEquals(1.0, stats.getAverage(), 0.0001);
// Test Integral
double integral = 1.0 * 24 * 60 * 60;
Assert.assertEquals(integral, stats.getIntegral(), 0.0001);
// Test sum
Assert.assertEquals(0.0d, stats.getSum(), 0.0001);
// Test first
Assert.assertNull(stats.getFirstValue());
Assert.assertNull(stats.getFirstTime());
// Test last
Assert.assertNull(stats.getLastValue());
Assert.assertNull(stats.getLastTime());
// Test start
Assert.assertEquals(1.0, stats.getStartValue().getDoubleValue(), 0.0001);
// Test count
Assert.assertEquals(0, stats.getCount());
// Test delta
Assert.assertEquals(0.0d, stats.getDelta(), 0.0001);
// Move to next period time
time = (ZonedDateTime) adjuster.adjustInto(time);
}
});
quantizer.firstValue(new IdPointValueTime(1, new NumericValue(1.0), time.minusHours(3).toInstant().toEpochMilli()), true);
quantizer.done();
Assert.assertEquals(Integer.valueOf(31), counter.getValue());
}
use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-core-public by infiniteautomation.
the class AnalogStatisticsQuantizerTest method testStartValueManyValuesPerPeriod.
@Test
public void testStartValueManyValuesPerPeriod() throws QueryCancelledException {
// Generate data at 12 noon for every day in the period
NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(ChronoUnit.DAYS, 1);
NextTimePeriodAdjuster hourlyAdjuster = new NextTimePeriodAdjuster(ChronoUnit.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
double value = 1.0;
ZonedDateTime daily = ZonedDateTime.ofInstant(time.toInstant(), zoneId);
for (int i = 0; i < 10; i++) {
data.add(new IdPointValueTime(1, new NumericValue(value), daily.toInstant().toEpochMilli()));
daily = (ZonedDateTime) hourlyAdjuster.adjustInto(daily);
value = value + 1.0d;
}
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 TemporalAmountBucketCalculator(from, to, Period.ofDays(1));
AnalogStatisticsQuantizer quantizer = new AnalogStatisticsQuantizer(bc, new StatisticsGeneratorQuantizerCallback<AnalogStatistics>() {
@Override
public void quantizedStatistics(AnalogStatistics statisticsGenerator) throws QueryCancelledException {
counter.increment();
AnalogStatistics stats = statisticsGenerator;
// Test periodStart
Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
// Test periiodEnd
Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
if (counter.getValue() == 1) {
// Test Minimum
Assert.assertEquals(1.0, stats.getMinimumValue(), 0.0001);
Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMinimumTime());
// Test Maximum
Assert.assertEquals(10.0, stats.getMaximumValue(), 0.0001);
Assert.assertEquals(time.plusHours(12).plusHours(9).toInstant().toEpochMilli(), (long) stats.getMaximumTime());
} else {
// Test Minimum
Assert.assertEquals(1.0, stats.getMinimumValue(), 0.0001);
Assert.assertEquals(time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getMinimumTime());
// Test Maximum
Assert.assertEquals(10.0, stats.getMaximumValue(), 0.0001);
Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMaximumTime());
}
// 1-9 for 1hr each, 10 for 12hrs at the start and 2hrs at the end
if (counter.getValue() == 1) {
double integral = 1d * 13 * 60d * 60d + 2d * 60 * 60 + 3d * 60 * 60 + 4d * 60 * 60 + 5d * 60 * 60 + 6d * 60 * 60 + 7d * 60 * 60 + 8d * 60 * 60 + 9d * 60 * 60;
integral = integral + 10d * 3d * 60 * 60;
double average = integral / (24d * 60d * 60d);
Assert.assertEquals(average, stats.getAverage(), 0.0001);
// Test Integral
Assert.assertEquals(integral, stats.getIntegral(), 0.0001);
} else {
double integral = 1d * 60d * 60d + 2d * 60 * 60 + 3d * 60 * 60 + 4d * 60 * 60 + 5d * 60 * 60 + 6d * 60 * 60 + 7d * 60 * 60 + 8d * 60 * 60 + 9d * 60 * 60;
integral = integral + 10d * 15d * 60 * 60;
double average = integral / (24d * 60d * 60d);
Assert.assertEquals(average, stats.getAverage(), 0.0001);
// Test Integral
Assert.assertEquals(integral, stats.getIntegral(), 0.0001);
}
// Test sum
Assert.assertEquals(55d, stats.getSum(), 0.0001);
// Test first
Assert.assertEquals(1.0d, stats.getFirstValue().getDoubleValue(), 0.0001);
Assert.assertEquals(time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getFirstTime());
// Test last
Assert.assertEquals(10.0d, stats.getLastValue().getDoubleValue(), 0.0001);
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(1.0, stats.getStartValue().getDoubleValue(), 0.0001);
else
Assert.assertEquals(10.0, stats.getStartValue().getDoubleValue(), 0.0001);
// Test count
Assert.assertEquals(10, stats.getCount());
// Test delta
if (counter.getValue() == 1) {
// 1 to 10
Assert.assertEquals(9.0, stats.getDelta(), 0.0001);
} else {
Assert.assertEquals(0.0, stats.getDelta(), 0.0001);
}
// Move to next period time
time = (ZonedDateTime) adjuster.adjustInto(time);
}
});
quantizer.firstValue(new IdPointValueTime(1, new NumericValue(1.0), time.minusHours(3).toInstant().toEpochMilli()), 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());
}
Aggregations