Skip to content

Instantly share code, notes, and snippets.

@raed667
Created December 28, 2016 12:21
Show Gist options
  • Save raed667/a881c1e3d585e987d9ee5b387594fd73 to your computer and use it in GitHub Desktop.
Save raed667/a881c1e3d585e987d9ee5b387594fd73 to your computer and use it in GitHub Desktop.
Display error rate when inserting MQTT data into ES
const mqtt = require('mqtt');
const ES = require('esta');
const INDEX = 'mqtt',
TYPE = 'eclipse';
let counter = 0,
errors = 0;
ES.CONNECT(INDEX, () => {
const client = mqtt.connect('mqtt://iot.eclipse.org');
client.on('connect', () => {
client.subscribe('#');
});
client.on('message', (topic, message) => {
if (counter > 5000) {
client.end(true);
console.log(`Error rate : ${(errors / counter) * 100}%`);
console.log(`Error count : ${errors}`);
process.exit();
}
addARecord(message.toString(), topic);
});
});
function addARecord(message, topic) {
const record = {
index: INDEX,
type: TYPE,
date: new Date().toISOString(),
message: message,
topic: topic
};
ES.CREATE(record, function (response) {
counter++;
// Check if data has been inserted properly
if (response.created === undefined) {
errors++;
}
});
}
process.on('uncaughtException', function (err) {
console.log('ERROR: ' + err);
});
@raed667
Copy link
Author

raed667 commented Dec 28, 2016

We try to insert 5000 messages and check the error rate of the insertion, most of the time it is NOT zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment