use of com.influxdb.client.domain.Organization in project solarthing by wildmountainfarms.
the class InfluxDb2PacketSaver method handle.
@Override
public void handle(PacketCollection packetCollection) throws PacketHandleException {
final InstancePacketGroup packetGroup = PacketGroups.parseToInstancePacketGroup(packetCollection, DefaultInstanceOptions.REQUIRE_NO_DEFAULTS);
DefaultInstanceOptions.requireNoDefaults(packetGroup);
Organization organization = findOrCreateOrg();
Bucket bucket = findOrCreateBucket(bucketNameGetter.getName(packetGroup), organization);
final long time = packetCollection.getDateMillis();
List<Point> points = new ArrayList<>();
for (Packet packet : packetGroup.getPackets()) {
Point point = pointCreator.createBuilder(packet).time(time, WritePrecision.MS);
Collection<String> tagKeys = PointUtil.getTagKeys(packet.getClass());
ObjectNode json = OBJECT_MAPPER.valueToTree(packet);
for (Map.Entry<String, ValueNode> entry : PointUtil.flattenJsonObject(json)) {
String key = entry.getKey();
ValueNode prim = entry.getValue();
if (tagKeys.contains(key)) {
point.addTag(key, prim.asText());
}
if (prim.isNumber()) {
// always store as float datatype because you can never change the type from int to float easily
final Number value;
if (prim.isBigDecimal()) {
DecimalNode decimal = (DecimalNode) prim;
value = decimal.decimalValue();
} else {
value = prim.asDouble();
}
point.addField(key, value);
} else if (prim.isTextual() || prim.isBinary()) {
point.addField(key, prim.asText());
} else if (prim.isBoolean()) {
point.addField(key, prim.asBoolean());
} else
throw new AssertionError("This primitive isn't a number, string/binary or boolean! It's: " + prim + " class: " + prim.getClass() + " text=" + prim.asText());
}
points.add(point);
}
try {
client.getWriteApiBlocking().writePoints(bucket.getName(), bucket.getOrgID(), points);
} catch (InfluxException exception) {
throw new PacketHandleException("Could not write points", exception);
}
}
Aggregations