hmci/src/main/java/biz/nellemann/hmci/InfluxClient.java

142 lines
4.6 KiB
Java
Raw Normal View History

/*
2020-08-18 11:49:48 +00:00
* Copyright 2020 Mark Nellemann <mark.nellemann@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package biz.nellemann.hmci;
import biz.nellemann.hmci.dto.toml.InfluxConfiguration;
2022-03-04 17:51:29 +00:00
import org.influxdb.BatchOptions;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;
2022-03-04 17:51:29 +00:00
public final class InfluxClient {
2020-08-10 13:44:14 +00:00
private final static Logger log = LoggerFactory.getLogger(InfluxClient.class);
final private String url;
final private String username;
final private String password;
final private String database;
private InfluxDB influxDB;
InfluxClient(InfluxConfiguration config) {
this.url = config.url;
this.username = config.username;
this.password = config.password;
this.database = config.database;
}
2020-09-02 07:27:38 +00:00
2021-01-14 12:51:18 +00:00
synchronized void login() throws RuntimeException, InterruptedException {
if(influxDB != null) {
return;
}
boolean connected = false;
int loginErrors = 0;
do {
try {
log.debug("Connecting to InfluxDB - {}", url);
influxDB = InfluxDBFactory.connect(url, username, password).setDatabase(database);
2022-02-10 16:39:53 +00:00
influxDB.version(); // This ensures that we actually try to connect to the db
2022-03-04 17:51:29 +00:00
influxDB.enableBatch(
BatchOptions.DEFAULTS
.threadFactory(runnable -> {
Thread thread = new Thread(runnable);
thread.setDaemon(true);
return thread;
})
);
2022-03-04 17:51:29 +00:00
Runtime.getRuntime().addShutdownHook(new Thread(influxDB::close));
connected = true;
} catch(Exception e) {
2021-01-14 12:51:18 +00:00
sleep(15 * 1000);
if(loginErrors++ > 3) {
log.error("login() - error, giving up: {}", e.getMessage());
2021-01-14 12:51:18 +00:00
throw new RuntimeException(e);
} else {
log.warn("login() - error, retrying: {}", e.getMessage());
}
}
} while(!connected);
2020-08-14 07:34:44 +00:00
}
2020-09-02 07:27:38 +00:00
synchronized void logoff() {
if(influxDB != null) {
influxDB.close();
}
influxDB = null;
}
2022-12-05 14:18:42 +00:00
public void write(List<Measurement> measurements, Instant timestamp, String name) {
log.debug("write() - measurement: {} {}", name, measurements.size());
processMeasurementMap(measurements, timestamp, name).forEach( (point) -> { influxDB.write(point); });
}
2022-12-05 14:18:42 +00:00
public void write(List<Measurement> measurements, String name) {
log.debug("write() - measurement: {} {}", name, measurements.size());
processMeasurementMap(measurements, name).forEach( (point) -> { influxDB.write(point); });
}
private List<Point> processMeasurementMap(List<Measurement> measurements, Instant timestamp, String name) {
List<Point> listOfPoints = new ArrayList<>();
measurements.forEach( (m) -> {
2022-12-05 14:18:42 +00:00
Point.Builder builder = Point.measurement(name)
.time(timestamp.toEpochMilli(), TimeUnit.MILLISECONDS)
.tag(m.tags)
.fields(m.fields);
2021-03-25 15:46:04 +00:00
listOfPoints.add(builder.build());
});
return listOfPoints;
}
2022-12-05 14:18:42 +00:00
private List<Point> processMeasurementMap(List<Measurement> measurements, String name) {
List<Point> listOfPoints = new ArrayList<>();
measurements.forEach( (m) -> {
log.trace("processMeasurementMap() - timestamp: {}, tags: {}, fields: {}", m.timestamp, m.tags, m.fields);
Point.Builder builder = Point.measurement(name)
.time(m.timestamp.toEpochMilli(), TimeUnit.MILLISECONDS)
.tag(m.tags)
.fields(m.fields);
listOfPoints.add(builder.build());
});
return listOfPoints;
}
2020-08-10 13:44:14 +00:00
}