Search in sources :

Example 16 with InfluxDB

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();
}
Also used : InfluxDB(org.influxdb.InfluxDB) Test(org.junit.Test)

Example 17 with InfluxDB

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();
}
Also used : InfluxDB(org.influxdb.InfluxDB) Test(org.junit.Test)

Example 18 with InfluxDB

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);
}
Also used : BatchPoints(org.influxdb.dto.BatchPoints) HashMap(java.util.HashMap) InfluxDB(org.influxdb.InfluxDB) Builder(org.influxdb.dto.Point.Builder) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 19 with InfluxDB

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();
}
Also used : InfluxDB(org.influxdb.InfluxDB) OpenemsException(io.openems.common.exceptions.OpenemsException)

Example 20 with InfluxDB

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);
}
Also used : InfluxDB(org.influxdb.InfluxDB) Edge(io.openems.backend.metadata.api.Edge)

Aggregations

InfluxDB (org.influxdb.InfluxDB)26 Test (org.junit.Test)7 Query (org.influxdb.dto.Query)5 BatchPoints (org.influxdb.dto.BatchPoints)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Builder (org.influxdb.dto.Point.Builder)3 Test (org.junit.jupiter.api.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 JsonObject (com.google.gson.JsonObject)2 List (java.util.List)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 PipelineResult (org.apache.beam.sdk.PipelineResult)2 QueryResult (org.influxdb.dto.QueryResult)2 Series (org.influxdb.dto.QueryResult.Series)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Health (org.springframework.boot.actuate.health.Health)2 Bean (org.springframework.context.annotation.Bean)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1