Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Enhance metric names in logging and writing stages
Browse files Browse the repository at this point in the history
Modified mqttclient's method to separate full metric's name into actual metric name and path, improving logging clarity and metric registration in the writing stage. The changes were made while validating the metric's type and recording them - the name and path are now correctly logged and registered, enhancing the tracking and categoricity of the metrics.
  • Loading branch information
AlexGodbehere committed Aug 1, 2023
1 parent b436359 commit 908b143
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/mqttclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,19 @@ export default class MQTTClient {

if (value === null) return;

// Get the value after the last /
let metricName = birth.name.split('/').pop();

// Get the path as everything behind the last /
let path = birth.name.substring(0, birth.name.lastIndexOf("/"));

writeApi.useDefaultTags({
instance: birth.instance,
schema: birth.schema,
group: topic.address.group,
node: topic.address.node,
device: topic.address.device
device: topic.address.device,
path: path,
});


Expand All @@ -309,10 +316,10 @@ export default class MQTTClient {
// Validate
numVal = Number(value);
if (!Number.isInteger(numVal)) {
logger.warn(`${topic.address}/${birth.name} should be a ${birth.type} but received ${numVal}. Not recording.`);
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${numVal}. Not recording.`);
return;
}
writeApi.writePoint(new Point(birth.name).intField('value', numVal));
writeApi.writePoint(new Point(`${metricName}_i`).intField('value', numVal));
break;
case "UInt8":
case "UInt16":
Expand All @@ -321,37 +328,37 @@ export default class MQTTClient {
// Validate
numVal = Number(value);
if (!Number.isInteger(numVal)) {
logger.warn(`${topic.address}/${birth.name} should be a ${birth.type} but received ${numVal}. Not recording.`);
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${numVal}. Not recording.`);
return;
}
writeApi.writePoint(new Point(birth.name).uintField('value', numVal));
writeApi.writePoint(new Point(`${metricName}_u`).uintField('value', numVal));
break;
case "Float":
case "Double":
// Validate
numVal = Number(value);
if (isNaN(parseFloat(numVal))) {
logger.warn(`${topic.address}/${birth.name} should be a ${birth.type} but received ${numVal}. Not recording.`);
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${numVal}. Not recording.`);
return;
}
writeApi.writePoint(new Point(birth.name).floatField('value', numVal));
writeApi.writePoint(new Point(`${metricName}_d`).floatField('value', numVal));
break;
case "Boolean":
if (typeof value != "boolean") {
logger.warn(`${topic.address}/${birth.name} should be a ${birth.type} but received ${value}. Not recording.`);
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${value}. Not recording.`);
return;
}
writeApi.writePoint(new Point(birth.name).booleanField('value', value));
writeApi.writePoint(new Point(`${metricName}_b`).booleanField('value', value));
break;
default:
writeApi.writePoint(new Point(birth.name).stringField('value', value));
writeApi.writePoint(new Point(`${metricName}_s`).stringField('value', value));
break;

}

i++;

logger.debug(`Added to write buffer (${i}/${batchSize}): [${birth.type}] ${topic.address}/${birth.name} = ${value}`);
logger.debug(`Added to write buffer (${i}/${batchSize}): [${birth.type}] ${topic.address}/${path}/${metricName} = ${value}`);

if (i >= batchSize) {
this.flushBuffer(`${batchSize} point BATCH`);
Expand Down

0 comments on commit 908b143

Please sign in to comment.