Skip to content

Commit

Permalink
Influxdb2 addition (#19)
Browse files Browse the repository at this point in the history
* InfluxDB2 addition with errors

* Ready to test out influxdb2

* InfluxDB 2.0 working

* Removed bucket and measurement change option. InfluxDB 2.0 seems stable
  • Loading branch information
retrodaredevil authored Feb 1, 2021
1 parent 85dc5fd commit f0ea388
Show file tree
Hide file tree
Showing 38 changed files with 627 additions and 130 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ The [input and output](other/docs/input_and_outputs.md) README is documentation
[CouchDB setup](other/docs/couchdb_setup.md)<br/>
*Used for the android and web application*

[InfluxDB setup](other/docs/influxdb_setup.md)<br/>
[InfluxDB 2.0 setup](other/docs/influxdb2_setup.md)<br/>
*Used for Grafana*

#### [Developer Use](other/docs/developer_use.md)
Expand Down
4 changes: 2 additions & 2 deletions client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ project.ext.mainClassName = "me.retrodaredevil.solarthing.program.SolarMain"

dependencies {
implementation "com.github.retrodaredevil.io-lib:jSerialComm:$ioLibVersion"
implementation group: 'org.influxdb', name: 'influxdb-java', version: '2.19'
// implementation "com.influxdb:influxdb-client-java:1.13.0"
implementation group: 'org.influxdb', name: 'influxdb-java', version: '2.19' // for InfluxDB 1.X
implementation "com.influxdb:influxdb-client-java:1.15.0" // for InfluxDB 2.0 // https://github.com/influxdata/influxdb-client-java/releases

implementation "org.apache.logging.log4j:log4j-jcl:$log4jVersion" // commons logging bridge.
implementation "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion"
Expand Down
19 changes: 19 additions & 0 deletions client/src/main/java/me/retrodaredevil/okhttp3/OkHttpUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.retrodaredevil.okhttp3;

import okhttp3.OkHttpClient;

import java.util.concurrent.TimeUnit;

public final class OkHttpUtil {
private OkHttpUtil() { throw new UnsupportedOperationException(); }

public static OkHttpClient.Builder createBuilder(OkHttpProperties okHttpProperties) {
return new OkHttpClient.Builder()
.retryOnConnectionFailure(okHttpProperties.isRetryOnConnectionFailure())
.callTimeout(okHttpProperties.getCallTimeoutMillis(), TimeUnit.MILLISECONDS)
.connectTimeout(okHttpProperties.getConnectTimeoutMillis(), TimeUnit.MILLISECONDS)
.readTimeout(okHttpProperties.getReadTimeoutMillis(), TimeUnit.MILLISECONDS)
.writeTimeout(okHttpProperties.getWriteTimeoutMillis(), TimeUnit.MILLISECONDS)
.pingInterval(okHttpProperties.getPingIntervalMillis(), TimeUnit.MILLISECONDS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package me.retrodaredevil.solarthing.config.databases.implementations;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import me.retrodaredevil.influxdb.influxdb2.InfluxDb2Properties;
import me.retrodaredevil.okhttp3.OkHttpProperties;
import me.retrodaredevil.solarthing.annotations.Nullable;
import me.retrodaredevil.solarthing.config.databases.DatabaseSettings;
import me.retrodaredevil.solarthing.config.databases.DatabaseType;
import me.retrodaredevil.solarthing.config.databases.SimpleDatabaseType;

import static java.util.Objects.requireNonNull;

@JsonDeserialize(builder = InfluxDb2DatabaseSettings.Builder.class)
@JsonTypeName("influxdb2")
public class InfluxDb2DatabaseSettings implements DatabaseSettings {
public static final DatabaseType TYPE = new SimpleDatabaseType("influxdb2");

private final InfluxDb2Properties influxDbProperties;
private final OkHttpProperties okHttpProperties;

public InfluxDb2DatabaseSettings(InfluxDb2Properties influxDbProperties, OkHttpProperties okHttpProperties) {
requireNonNull(this.influxDbProperties = influxDbProperties);
requireNonNull(this.okHttpProperties = okHttpProperties);
}

@Override
public DatabaseType getDatabaseType() {
return TYPE;
}

public InfluxDb2Properties getInfluxDbProperties() {
return influxDbProperties;
}
public OkHttpProperties getOkHttpProperties() {
return okHttpProperties;
}

@JsonPOJOBuilder
public static class Builder {
private InfluxDb2Properties influxDbProperties;
private OkHttpProperties okHttpProperties;

public InfluxDb2DatabaseSettings build() {
return new InfluxDb2DatabaseSettings(influxDbProperties, okHttpProperties);
}

@JsonUnwrapped
public Builder setInfluxDbProperties(InfluxDb2Properties influxDbProperties) {
this.influxDbProperties = influxDbProperties;
return this;
}

@JsonUnwrapped
public Builder setOkHttpProperties(OkHttpProperties okHttpProperties) {
this.okHttpProperties = okHttpProperties;
return this;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import me.retrodaredevil.influxdb.InfluxProperties;
import me.retrodaredevil.influxdb.influxdb1.InfluxProperties;
import me.retrodaredevil.okhttp3.OkHttpProperties;
import me.retrodaredevil.solarthing.config.databases.DatabaseSettings;
import me.retrodaredevil.solarthing.config.databases.DatabaseType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public IOBundle createIOBundle() throws Exception {
return new JSerialIOBundle(
serialPort,
serialConfig,
new JSerialIOBundle.Config(50, 65536, 65536)
new JSerialIOBundle.Config(200, 65536, 65536)
);
}
@JsonExplicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import me.retrodaredevil.solarthing.packets.collection.InstancePacketGroup;

public class ConstantDatabaseNameGetter implements DatabaseNameGetter {
public class ConstantNameGetter implements NameGetter {
private final String databaseName;

public ConstantDatabaseNameGetter(String databaseName) {
public ConstantNameGetter(String databaseName) {
this.databaseName = databaseName;
}

@Override
public String getDatabaseName(InstancePacketGroup instancePacketGroup) {
public String getName(InstancePacketGroup instancePacketGroup) {
return databaseName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import me.retrodaredevil.solarthing.packets.collection.InstancePacketGroup;

public interface DatabaseNameGetter {
String getDatabaseName(InstancePacketGroup instancePacketGroup);
public interface NameGetter {
String getName(InstancePacketGroup instancePacketGroup);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package me.retrodaredevil.solarthing.influxdb;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ValueNode;
import me.retrodaredevil.solarthing.annotations.TagKeys;
import me.retrodaredevil.solarthing.packets.DocumentedPacket;
import me.retrodaredevil.solarthing.packets.DocumentedPacketType;
import me.retrodaredevil.solarthing.packets.Packet;
import me.retrodaredevil.solarthing.packets.identification.Identifiable;
import me.retrodaredevil.solarthing.packets.identification.Identifier;
import me.retrodaredevil.solarthing.packets.identification.SupplementaryIdentifier;

import java.util.*;

public final class PointUtil {
private PointUtil() { throw new UnsupportedOperationException(); }

public static Map<String, String> getTags(Packet packet) {
Map<String, String> r = new HashMap<>();
if(packet instanceof Identifiable){
Identifier identifier = ((Identifiable) packet).getIdentifier();
r.put("identifier", identifier.getRepresentation());
if(identifier instanceof SupplementaryIdentifier){
SupplementaryIdentifier supplementaryIdentifier = (SupplementaryIdentifier) identifier;
r.put("identifier_supplementaryTo", supplementaryIdentifier.getSupplementaryTo().getRepresentation());
}
}
if(packet instanceof DocumentedPacket){
DocumentedPacket documentedPacket = (DocumentedPacket) packet;
DocumentedPacketType type = documentedPacket.getPacketType();
r.put("packetType", type.toString());
}
return r;
}
public static Collection<String> getTagKeys(Class<?> clazz){
/*
Why we have to do this: https://stackoverflow.com/questions/26910620/class-getannotations-getdeclaredannotations-returns-empty-array-for-subcla#26911089
*/
Collection<String> tagKeys = new HashSet<>();
for(Class<?> interfaceClass : clazz.getInterfaces()){
tagKeys.addAll(getTagKeys(interfaceClass));
}
TagKeys[] tagKeysAnnotations = clazz.getAnnotationsByType(TagKeys.class); // since Java 8, but that's fine
for(TagKeys tagKeysAnnotation : tagKeysAnnotations){
tagKeys.addAll(Arrays.asList(tagKeysAnnotation.value()));
}
return tagKeys;
}
public static Set<Map.Entry<String, ValueNode>> flattenJsonObject(ObjectNode object) {
Map<String, ValueNode> r = new LinkedHashMap<>();
for (Iterator<Map.Entry<String, JsonNode>> it = object.fields(); it.hasNext(); ) {
Map.Entry<String, JsonNode> entry = it.next();
String key = entry.getKey();
JsonNode element = entry.getValue();
if (element.isValueNode() && !element.isNull()) {
r.put(key, (ValueNode) element);
} else if(element.isObject()){
Set<Map.Entry<String, ValueNode>> flat = flattenJsonObject((ObjectNode) element);
for(Map.Entry<String, ValueNode> subEntry : flat){
r.put(key + "." + subEntry.getKey(), subEntry.getValue());
}
}
// ignore nulls and arrays
}
return r.entrySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import me.retrodaredevil.solarthing.packets.collection.InstancePacketGroup;

public enum SourceIdDatabaseNameGetter implements DatabaseNameGetter {
public enum SourceIdNameGetter implements NameGetter {
INSTANCE;
@Override
public String getDatabaseName(InstancePacketGroup instancePacketGroup) {
public String getName(InstancePacketGroup instancePacketGroup) {
return instancePacketGroup.getSourceId();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.retrodaredevil.solarthing.influxdb;
package me.retrodaredevil.solarthing.influxdb.influxdb1;

import me.retrodaredevil.solarthing.influxdb.PointUtil;
import me.retrodaredevil.solarthing.packets.DocumentedPacket;
import me.retrodaredevil.solarthing.packets.DocumentedPacketType;
import me.retrodaredevil.solarthing.packets.Packet;
Expand All @@ -8,6 +9,8 @@
import me.retrodaredevil.solarthing.packets.identification.SupplementaryIdentifier;
import org.influxdb.dto.Point;

import java.util.Map;

public class ConstantMeasurementPacketPointCreator implements PacketPointCreator {
private final String measurement;

Expand All @@ -18,18 +21,8 @@ public ConstantMeasurementPacketPointCreator(String measurement) {
@Override
public Point.Builder createBuilder(Packet packet) {
Point.Builder r = Point.measurement(measurement);
if(packet instanceof Identifiable){
Identifier identifier = ((Identifiable) packet).getIdentifier();
r.tag("identifier", identifier.getRepresentation());
if(identifier instanceof SupplementaryIdentifier){
SupplementaryIdentifier supplementaryIdentifier = (SupplementaryIdentifier) identifier;
r.tag("identifier_supplementaryTo", supplementaryIdentifier.getSupplementaryTo().getRepresentation());
}
}
if(packet instanceof DocumentedPacket){
DocumentedPacket documentedPacket = (DocumentedPacket) packet;
DocumentedPacketType type = documentedPacket.getPacketType();
r.tag("packetType", type.toString());
for (Map.Entry<String, String> entry : PointUtil.getTags(packet).entrySet()) {
r.tag(entry.getKey(), entry.getValue());
}
return r;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package me.retrodaredevil.solarthing.influxdb;
package me.retrodaredevil.solarthing.influxdb.influxdb1;

import me.retrodaredevil.solarthing.influxdb.PointUtil;
import me.retrodaredevil.solarthing.packets.DocumentedPacket;
import me.retrodaredevil.solarthing.packets.DocumentedPacketType;
import me.retrodaredevil.solarthing.packets.Packet;
import me.retrodaredevil.solarthing.packets.identification.Identifiable;
import me.retrodaredevil.solarthing.packets.identification.Identifier;
import org.influxdb.dto.Point;

import java.util.Map;

public enum DocumentedMeasurementPacketPointCreator implements PacketPointCreator {
INSTANCE;
@Override
public Point.Builder createBuilder(Packet packet) {
if(packet instanceof DocumentedPacket){
DocumentedPacket documentedPacket = (DocumentedPacket) packet;
DocumentedPacketType type = documentedPacket.getPacketType();
if(packet instanceof Identifiable){
Identifier identifier = ((Identifiable) packet).getIdentifier();
return Point.measurement(type.toString()).tag("identifier", identifier.getRepresentation());
}
return Point.measurement(type.toString());
return apply(Point.measurement(type.toString()), packet);
}
return apply(Point.measurement(packet.getClass().getSimpleName()), packet);
}
private static Point.Builder apply(Point.Builder point, Packet packet) {
for (Map.Entry<String, String> entry : PointUtil.getTags(packet).entrySet()) {
point.tag(entry.getKey(), entry.getValue());
}
return Point.measurement(packet.getClass().getSimpleName());
return point;
}
}
Loading

0 comments on commit f0ea388

Please sign in to comment.