Search in sources :

Example 21 with InfluxDB

use of org.influxdb.InfluxDB in project openems by OpenEMS.

the class InfluxdbPersistence method getInfluxDB.

private Optional<InfluxDB> getInfluxDB() {
    if (!this.ip.valueOptional().isPresent() || !this.fems.valueOptional().isPresent() || !this.username.valueOptional().isPresent() || !this.password.valueOptional().isPresent()) {
        return Optional.empty();
    }
    if (_influxdb.isPresent()) {
        return this._influxdb;
    }
    String ip = this.ip.valueOptional().get().getHostAddress();
    String username = this.username.valueOptional().get();
    String password = this.password.valueOptional().get();
    InfluxDB influxdb = InfluxDBFactory.connect("http://" + ip + ":8086", username, password);
    try {
        influxdb.createDatabase(database.valueOptional().orElse("db"));
    } catch (RuntimeException e) {
        log.error("Unable to connect to InfluxDB: " + e.getCause());
        return Optional.empty();
    }
    this._influxdb = Optional.of(influxdb);
    return this._influxdb;
}
Also used : InfluxDB(org.influxdb.InfluxDB)

Example 22 with InfluxDB

use of org.influxdb.InfluxDB in project openems by OpenEMS.

the class InfluxdbPersistence method forever.

@Override
protected void forever() {
    // Prepare DB connection
    Optional<InfluxDB> _influxdb = getInfluxDB();
    if (!_influxdb.isPresent()) {
        synchronized (queue) {
            // Clear queue if we don't have a valid influxdb connection. This is necessary to avoid filling the
            // memory in case of no available DB connection
            queue.clear();
        }
    }
    InfluxDB influxDB = _influxdb.get();
    /*
		 * Convert FieldVales in queue to Points
		 */
    BatchPoints batchPoints = // 
    BatchPoints.database(database.valueOptional().orElse("db")).tag("fems", // 
    String.valueOf(fems.valueOptional().get())).build();
    synchronized (queue) {
        queue.asMap().forEach((timestamp, fieldValues) -> {
            Builder builder = // 
            Point.measurement("data").time(timestamp, TimeUnit.MILLISECONDS);
            fieldValues.forEach(fieldValue -> {
                if (fieldValue instanceof NumberFieldValue) {
                    builder.addField(fieldValue.field, ((NumberFieldValue) fieldValue).value);
                } else if (fieldValue instanceof StringFieldValue) {
                    builder.addField(fieldValue.field, ((StringFieldValue) fieldValue).value);
                }
            });
            batchPoints.point(builder.build());
        });
        queue.clear();
    }
    // write to DB
    try {
        influxDB.write(batchPoints);
        log.debug("Wrote [" + batchPoints.getPoints().size() + "] points to InfluxDB");
    } catch (RuntimeException e) {
        log.error("Error writing to InfluxDB: " + e);
    }
}
Also used : BatchPoints(org.influxdb.dto.BatchPoints) InfluxDB(org.influxdb.InfluxDB) Builder(org.influxdb.dto.Point.Builder)

Example 23 with InfluxDB

use of org.influxdb.InfluxDB in project cloudstack by apache.

the class StatsCollector method createInfluxDbConnection.

/**
 * Creates connection to InfluxDB. If the database does not exist, it throws a CloudRuntimeException. </br>
 * @note the user can configure the database name on parameter 'stats.output.influxdb.database.name'; such database must be yet created and configured by the user.
 * The Default name for the database is 'cloudstack_stats'.
 */
protected InfluxDB createInfluxDbConnection() {
    String influxDbQueryUrl = String.format("http://%s:%s/", externalStatsHost, externalStatsPort);
    InfluxDB influxDbConnection = InfluxDBFactory.connect(influxDbQueryUrl);
    if (!influxDbConnection.databaseExists(databaseName)) {
        throw new CloudRuntimeException(String.format("Database with name %s does not exist in influxdb host %s:%s", databaseName, externalStatsHost, externalStatsPort));
    }
    return influxDbConnection;
}
Also used : InfluxDB(org.influxdb.InfluxDB) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 24 with InfluxDB

use of org.influxdb.InfluxDB in project beam by apache.

the class InfluxDbIO method checkDatabase.

private static boolean checkDatabase(String dbName, DataSourceConfiguration configuration, boolean disableCertificateValidation) {
    try (InfluxDB connection = getConnection(configuration, disableCertificateValidation)) {
        connection.setDatabase(dbName);
        QueryResult result = connection.query(new Query("SHOW DATABASES"));
        List<Series> results = result.getResults().get(0).getSeries();
        for (Series series : results) {
            List<List<Object>> values = series.getValues();
            for (List<Object> listObj : values) {
                for (Object dataObj : listObj) {
                    if (dataObj.toString().equals(dbName)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : Series(org.influxdb.dto.QueryResult.Series) QueryResult(org.influxdb.dto.QueryResult) Query(org.influxdb.dto.Query) InfluxDB(org.influxdb.InfluxDB) ArrayList(java.util.ArrayList) List(java.util.List)

Example 25 with InfluxDB

use of org.influxdb.InfluxDB in project beam by apache.

the class InfluxDbIOTest method validateWriteTest.

@Test
public void validateWriteTest() {
    InfluxDB influxDb = Mockito.mock(InfluxDB.class);
    PowerMockito.when(InfluxDBFactory.connect(anyString(), anyString(), anyString(), any(OkHttpClient.Builder.class))).thenReturn(influxDb);
    PowerMockito.when(InfluxDBFactory.connect(anyString(), anyString(), anyString())).thenReturn(influxDb);
    String influxHost = "http://localhost";
    String userName = "admin";
    String password = "admin";
    String influxDatabaseName = "testDataBase";
    AtomicInteger countInvocation = new AtomicInteger();
    Mockito.doAnswer(invocation -> countInvocation.getAndIncrement()).when(influxDb).write(any(List.class));
    doReturn(getDatabase(influxDatabaseName)).when(influxDb).query(new Query("SHOW DATABASES"));
    final int numOfElementsToWrite = 1000;
    pipeline.apply("Generate data", Create.of(GenerateData.getMetric("test_m", numOfElementsToWrite))).apply("Write data to InfluxDB", InfluxDbIO.write().withDataSourceConfiguration(DataSourceConfiguration.create(StaticValueProvider.of(influxHost), StaticValueProvider.of(userName), StaticValueProvider.of(password))).withDatabase(influxDatabaseName));
    PipelineResult result = pipeline.run();
    Assert.assertEquals(State.DONE, result.waitUntilFinish());
    Assert.assertTrue(countInvocation.get() > 0);
}
Also used : Query(org.influxdb.dto.Query) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InfluxDB(org.influxdb.InfluxDB) PipelineResult(org.apache.beam.sdk.PipelineResult) ArrayList(java.util.ArrayList) List(java.util.List) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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