use of org.influxdb.InfluxDB in project tutorials by eugenp.
the class InfluxDBConnectionLiveTest method whenBatchWrittenBatchExists.
@Test
public void whenBatchWrittenBatchExists() {
InfluxDB connection = connectDatabase();
String dbName = "baeldung";
connection.createDatabase(dbName);
// Need a retention policy before we can proceed
// Since we are doing batches, we need not set it
connection.createRetentionPolicy("defaultPolicy", "baeldung", "30d", 1, true);
BatchPoints batchPoints = BatchPoints.database(dbName).retentionPolicy("defaultPolicy").build();
Point point1 = Point.measurement("memory").time(System.currentTimeMillis(), TimeUnit.MILLISECONDS).addField("free", 4743656L).addField("used", 1015096L).addField("buffer", 1010467L).build();
Point point2 = Point.measurement("memory").time(System.currentTimeMillis() - 100, TimeUnit.MILLISECONDS).addField("free", 4743696L).addField("used", 1016096L).addField("buffer", 1008467L).build();
batchPoints.point(point1);
batchPoints.point(point2);
connection.write(batchPoints);
List<MemoryPoint> memoryPointList = getPoints(connection, "Select * from memory", "baeldung");
assertEquals(2, memoryPointList.size());
assertTrue(4743696L == memoryPointList.get(0).getFree());
memoryPointList = getPoints(connection, "Select * from memory order by time desc", "baeldung");
assertEquals(2, memoryPointList.size());
assertTrue(4743656L == memoryPointList.get(0).getFree());
// Clean up database
connection.deleteDatabase("baeldung");
connection.close();
}
use of org.influxdb.InfluxDB in project tutorials by eugenp.
the class InfluxDBConnectionLiveTest method whenPointsWrittenPointsExists.
@Test
public void whenPointsWrittenPointsExists() throws Exception {
InfluxDB connection = connectDatabase();
String dbName = "baeldung";
connection.createDatabase(dbName);
// Need a retention policy before we can proceed
connection.createRetentionPolicy("defaultPolicy", "baeldung", "30d", 1, true);
// Since we are doing a batch thread, we need to set this as a default
connection.setRetentionPolicy("defaultPolicy");
// Enable batch mode
connection.enableBatch(10, 10, TimeUnit.MILLISECONDS);
for (int i = 0; i < 10; i++) {
Point point = Point.measurement("memory").time(System.currentTimeMillis(), TimeUnit.MILLISECONDS).addField("name", "server1").addField("free", 4743656L).addField("used", 1015096L).addField("buffer", 1010467L).build();
connection.write(dbName, "defaultPolicy", point);
Thread.sleep(2);
}
// Unfortunately, the sleep inside the loop doesn't always add enough time to insure
// that Influx's batch thread flushes all of the writes and this sometimes fails without
// another brief pause.
Thread.sleep(10);
List<com.baeldung.influxdb.MemoryPoint> memoryPointList = getPoints(connection, "Select * from memory", "baeldung");
assertEquals(10, memoryPointList.size());
// Turn off batch and clean up
connection.disableBatch();
connection.deleteDatabase("baeldung");
connection.close();
}
use of org.influxdb.InfluxDB in project openems by OpenEMS.
the class Influx method writeDataToOldMiniMonitoring.
/**
* Writes data to old database for old Mini monitoring
*
* XXX remove after full migration
*
* @param device
* @param data
* @throws OpenemsException
*/
private void writeDataToOldMiniMonitoring(Edge edge, int influxId, TreeBasedTable<Long, String, Object> data) throws OpenemsException {
InfluxDB influxDB = getInfluxDbConnection();
BatchPoints batchPoints = //
BatchPoints.database(database).tag("fems", //
String.valueOf(influxId)).build();
for (Entry<Long, Map<String, Object>> entry : data.rowMap().entrySet()) {
Long timestamp = entry.getKey();
Builder builder = Point.measurement(TMP_MINI_MEASUREMENT).time(timestamp, TimeUnit.MILLISECONDS);
Map<String, Object> fields = new HashMap<>();
for (Entry<String, Object> valueEntry : entry.getValue().entrySet()) {
String channel = valueEntry.getKey();
Object valueObj = valueEntry.getValue();
if (valueObj instanceof Number) {
Long value = ((Number) valueObj).longValue();
// convert channel ids to old identifiers
if (channel.equals("ess0/Soc")) {
fields.put("Stack_SOC", value);
edge.setSoc(value.intValue());
} else if (channel.equals("meter0/ActivePower")) {
fields.put("PCS_Grid_Power_Total", value * -1);
} else if (channel.equals("meter1/ActivePower")) {
fields.put("PCS_PV_Power_Total", value);
} else if (channel.equals("meter2/ActivePower")) {
fields.put("PCS_Load_Power_Total", value);
}
// from here value needs to be divided by 10 for backwards compatibility
value = value / 10;
if (channel.equals("meter2/Energy")) {
fields.put("PCS_Summary_Consumption_Accumulative_cor", value);
fields.put("PCS_Summary_Consumption_Accumulative", value);
} else if (channel.equals("meter0/BuyFromGridEnergy")) {
fields.put("PCS_Summary_Grid_Buy_Accumulative_cor", value);
fields.put("PCS_Summary_Grid_Buy_Accumulative", value);
} else if (channel.equals("meter0/SellToGridEnergy")) {
fields.put("PCS_Summary_Grid_Sell_Accumulative_cor", value);
fields.put("PCS_Summary_Grid_Sell_Accumulative", value);
} else if (channel.equals("meter1/EnergyL1")) {
fields.put("PCS_Summary_PV_Accumulative_cor", value);
fields.put("PCS_Summary_PV_Accumulative", value);
}
}
}
if (fields.size() > 0) {
builder.fields(fields);
batchPoints.point(builder.build());
}
}
// write to DB
influxDB.write(batchPoints);
}
use of org.influxdb.InfluxDB in project openems by OpenEMS.
the class Influx method getInfluxDbConnection.
private synchronized InfluxDB getInfluxDbConnection() throws OpenemsException {
if (!this.influxDbOpt.isPresent()) {
InfluxDB influxDB = InfluxDBFactory.connect("http://" + url + ":" + port, username, password);
try {
influxDB.ping();
this.influxDbOpt = Optional.of(influxDB);
} catch (RuntimeException e) {
throw new OpenemsException("Unable to connect to InfluxDB: " + e.getMessage());
}
}
return this.influxDbOpt.get();
}
use of org.influxdb.InfluxDB in project openems by OpenEMS.
the class Influx method queryHistoricData.
@Override
public JsonArray queryHistoricData(Optional<Integer> edgeIdOpt, ZonedDateTime fromDate, ZonedDateTime toDate, JsonObject channels, int resolution) throws OpenemsException {
// if given, query only this id. If not given, do not apply
Optional<Integer> influxIdOpt = Optional.empty();
// filter
if (edgeIdOpt.isPresent()) {
Edge edge = this.metadataService.getEdge(edgeIdOpt.get());
influxIdOpt = Optional.of(InfluxdbUtils.parseNumberFromName(edge.getName()));
}
InfluxDB influxDB = getInfluxDbConnection();
return InfluxdbUtils.queryHistoricData(influxDB, this.database, influxIdOpt, fromDate, toDate, channels, resolution);
}
Aggregations