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;
}
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);
}
}
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;
}
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;
}
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);
}
Aggregations