use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueTimeWriter in project ma-modules-public by infiniteautomation.
the class MultiPointStatisticsStreamTest method generateOutput.
/**
* Generate a JsonNode from the query
*/
protected JsonNode generateOutput(ZonedDateTimeStatisticsQueryInfo info, Map<Integer, DataPointVO> voMap) throws QueryCancelledException, IOException {
MultiPointStatisticsStream stream = new MultiPointStatisticsStream(info, voMap, Common.getBean(PointValueDao.class));
JsonFactory factory = new JsonFactory();
ByteArrayOutputStream output = new ByteArrayOutputStream();
JsonGenerator jgen = factory.createGenerator(output);
PointValueTimeWriter writer = new PointValueTimeJsonWriter(stream.getQueryInfo(), jgen);
stream.setContentType(StreamContentType.JSON);
stream.start(writer);
stream.streamData(writer);
stream.finish(writer);
jgen.flush();
ObjectMapper mapper = new ObjectMapper();
return mapper.readTree(output.toString(StandardCharsets.UTF_8.name()));
}
use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueTimeWriter 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.rest.latest.model.pointValue.PointValueTimeWriter 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);
}
}
Aggregations