Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Incorrect metric timestamp on Influx Ingester #289

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions influxdb-sparkplug-ingester/lib/mqttclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,29 @@ export default class MQTTClient {
logger.error(`Metric ${metric.alias} is unknown for ${topic.address.group}/${topic.address.node}/${topic.address.device}`);
}

let metricTimestamp: Date
if (metric.timestamp) {
metricTimestamp = new Date(metric.timestamp);
} else if (payload.timestamp) {
// Metrics might not have a timestamp so use the packet timestamp if we have it.
metricTimestamp = new Date(payload.timestamp);
} else {
// No timestamp can be found on the metric or the payload, just use the current time instead.
metricTimestamp = new Date();
}
// Send each metric to InfluxDB
this.writeToInfluxDB(birth, topic, metric.value)

this.writeToInfluxDB(birth, topic, metric.value, metricTimestamp)
});
}

writeToInfluxDB(birth, topic: Topic, value) {

/**
* Writes metric values to InfluxDB using the metric timestamp.
* @param birth Birth certificate for device.
* @param topic Topic the metric was published on.
* @param value Metric value to write to InfluxDB.
* @param timestamp Timestamp from the metric to write to influx.
*/
writeToInfluxDB(birth, topic: Topic, value, timestamp: Date) {
if (value === null) return;
if (birth.transient) {
logger.debug(`Metric ${birth.name} is transient, not writing to InfluxDB`);
Expand Down Expand Up @@ -444,7 +459,11 @@ export default class MQTTClient {
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${numVal}. Not recording.`);
return;
}
writeApi.writePoint(new Point(`${metricName}:i`).intField('value', numVal));
writeApi.writePoint(
new Point(`${metricName}:i`)
.intField('value', numVal)
.timestamp(timestamp)
);
break;
case "UInt8":
case "UInt16":
Expand All @@ -456,7 +475,11 @@ export default class MQTTClient {
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${numVal}. Not recording.`);
return;
}
writeApi.writePoint(new Point(`${metricName}:u`).uintField('value', numVal));
writeApi.writePoint(
new Point(`${metricName}:u`)
.uintField('value', numVal)
.timestamp(timestamp)
);
break;
case "Float":
case "Double":
Expand All @@ -466,17 +489,27 @@ export default class MQTTClient {
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${numVal}. Not recording.`);
return;
}
writeApi.writePoint(new Point(`${metricName}:d`).floatField('value', numVal));
writeApi.writePoint(
new Point(`${metricName}:d`)
.floatField('value', numVal)
.timestamp(timestamp)
);
break;
case "Boolean":
if (typeof value != "boolean") {
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${value}. Not recording.`);
return;
}
writeApi.writePoint(new Point(`${metricName}:b`).booleanField('value', value));
writeApi.writePoint(
new Point(`${metricName}:b`)
.booleanField('value', value)
.timestamp(timestamp));
break;
default:
writeApi.writePoint(new Point(`${metricName}:s`).stringField('value', value));
writeApi.writePoint(
new Point(`${metricName}:s`)
.stringField('value', value)
.timestamp(timestamp));
break;

}
Expand Down
Loading