sitewhere / sitewhere

SiteWhere is an industrial strength open-source application enablement platform for the Internet of Things (IoT). It provides a multi-tenant microservice-based infrastructure that includes device/asset management, data ingestion, big-data storage, and integration through a modern, scalable architecture. SiteWhere provides REST APIs for all system functionality. SiteWhere provides SDKs for many common device platforms including Android, iOS, Arduino, and any Java-capable platform such as Raspberry Pi rapidly accelerating the speed of innovation.
https://sitewhere.io
Other
1.01k stars 383 forks source link

[1.14.0] InfluxDB tags not added #646

Open nilscrls opened 6 years ago

nilscrls commented 6 years ago

Hi!

Using Sitewhere v1.14.0 and a Mongo/Influx datastore, if I got it correctly when you send a device measurement with "INFLUX_TAG_foo" as metadata, the "foo" tag should be added to Influx but it creates the field "INFLUX_TAG_foo" instead.

In: https://github.com/sitewhere/sitewhere/blob/1fa2fe84b93d14da22fe41b71a0a0452a008835f/sitewhere-influx/src/main/java/com/sitewhere/influx/InfluxDbDeviceEventManagement.java#L236-L240

Is this a bug or have I misunderstood the feature?

derekadams commented 6 years ago

It looks like the addUserDefinedTag() method is adding a tag and not a field:

https://github.com/sitewhere/sitewhere/blob/1fa2fe84b93d14da22fe41b71a0a0452a008835f/sitewhere-influx/src/main/java/com/sitewhere/influx/device/InfluxDbDeviceEvent.java#L545

However, it looks like the standard metadata save logic would also add this as a field:

https://github.com/sitewhere/sitewhere/blob/1fa2fe84b93d14da22fe41b71a0a0452a008835f/sitewhere-influx/src/main/java/com/sitewhere/influx/device/InfluxDbDeviceEvent.java#L534

It seems like we should choose one or the other, but I'm not sure if existing users are depending on the field being there.

nilscrls commented 6 years ago

Well after testing it appears the functionality is broken. Using InfluxDB without tags doesn't seem pretty usefull. May I suggest you to move this check to here : https://github.com/sitewhere/sitewhere/blob/1fa2fe84b93d14da22fe41b71a0a0452a008835f/sitewhere-influx/src/main/java/com/sitewhere/influx/device/InfluxDbDeviceEvent.java#L533-L535

Example working solution :

for (String key : event.getMetadata().keySet()) {
+    if (key.startsWith("INFLUX_TAG_")) {
+        builder.tag(key, event.getMetadata(key));
+    }
+    else {
        builder.addField(EVENT_METADATA_PREFIX + key, event.getMetadata(key));
+    }
}