From 47e83a2577777c5e5b115cd8be73065d128b986f Mon Sep 17 00:00:00 2001 From: Matthew VanTassel Date: Tue, 14 Feb 2017 18:54:08 -0400 Subject: [PATCH 1/2] Cleanup promises chaining and standardize logging --- nzb2influx.js => nzbget2influx.js | 18 +++++++++++------- package.json | 9 +++++---- 2 files changed, 16 insertions(+), 11 deletions(-) rename nzb2influx.js => nzbget2influx.js (81%) diff --git a/nzb2influx.js b/nzbget2influx.js similarity index 81% rename from nzb2influx.js rename to nzbget2influx.js index 548149b..e6a9c15 100644 --- a/nzb2influx.js +++ b/nzbget2influx.js @@ -2,6 +2,7 @@ const Influx = require('influx'); const nzbget = require('node-nzbget'); +const chalk = require('chalk'); const checkInterval = process.env.UPDATE_INTERVAL_MS || 1000 * 30; @@ -20,8 +21,13 @@ const nzbgetConfig = { password: process.env.NZBGET_PASSWORD || '' }; +function log(message, color) { + color = color || 'black'; + console.log(chalk[color](message)); +} + function writeToInflux(seriesName, values, tags) { - var payload = { + let payload = { fields: values }; @@ -52,22 +58,20 @@ function onGetNZBData(data) { }; writeToInflux('nzb', value, tags).then(function() { - console.dir(`wrote ${nzb.NZBName} nzb data to influx: ${new Date()}`); + log(`wrote ${nzb.NZBName} nzb data to influx: ${new Date()}`, 'blue'); }); }); writeToInflux('nzbs', { count: nzbs.length }, null).then(function() { - console.dir(`wrote ${nzbs.length} nzbs data to influx: ${new Date()}`); + log(`wrote ${nzbs.length} nzb data to influx: ${new Date()}`, 'blue'); restart(); }); } -function restart(err) { - if (err) { - console.log(err); - } +function restart() { + log(`${new Date()}: fetching nzbget metrics`, 'green'); // Every {checkInterval} seconds setTimeout(getAllTheMetrics, checkInterval); diff --git a/package.json b/package.json index 54dab5a..be6c498 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,13 @@ { "name": "nzb2influx", - "version": "0.1.2", + "version": "0.1.3", "private": true, "scripts": { - "start": "node nzb2influx.js" + "start": "node nzbget2influx.js" }, - "devDependencies": { + "dependencies": { "influx": "~5.0", - "node-nzbget": "0.0.4" + "node-nzbget": "0.0.4", + "chalk": "^1.1.3" } } From 196e922969de0c70f5042b6ac753c6da92bd07f9 Mon Sep 17 00:00:00 2001 From: Matthew VanTassel Date: Fri, 28 Jul 2017 15:45:08 -0300 Subject: [PATCH 2/2] Remove Chalk, Add Error Handling and Better Logging --- nzbget2influx.js | 25 ++++++++++++++++--------- package.json | 3 +-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/nzbget2influx.js b/nzbget2influx.js index e6a9c15..522379e 100644 --- a/nzbget2influx.js +++ b/nzbget2influx.js @@ -2,7 +2,6 @@ const Influx = require('influx'); const nzbget = require('node-nzbget'); -const chalk = require('chalk'); const checkInterval = process.env.UPDATE_INTERVAL_MS || 1000 * 30; @@ -21,9 +20,8 @@ const nzbgetConfig = { password: process.env.NZBGET_PASSWORD || '' }; -function log(message, color) { - color = color || 'black'; - console.log(chalk[color](message)); +function log(message) { + console.log(message); } function writeToInflux(seriesName, values, tags) { @@ -45,6 +43,8 @@ const ng = new nzbget({ }); function onGetNZBData(data) { + log(`${new Date()}: Parsing NZB Data`); + let nzbs = data.result; nzbs.forEach(function(nzb) { @@ -58,27 +58,34 @@ function onGetNZBData(data) { }; writeToInflux('nzb', value, tags).then(function() { - log(`wrote ${nzb.NZBName} nzb data to influx: ${new Date()}`, 'blue'); + log(`wrote ${nzb.NZBName} nzb data to influx: ${new Date()}`); }); }); writeToInflux('nzbs', { count: nzbs.length }, null).then(function() { - log(`wrote ${nzbs.length} nzb data to influx: ${new Date()}`, 'blue'); + log(`wrote ${nzbs.length} nzb data to influx: ${new Date()}`); restart(); }); } -function restart() { - log(`${new Date()}: fetching nzbget metrics`, 'green'); +function handleError(err) { + log(`${new Date()}: Error`); + log(err); +} +function restart() { // Every {checkInterval} seconds setTimeout(getAllTheMetrics, checkInterval); } function getAllTheMetrics() { - ng.listgroups().then(onGetNZBData).catch(restart); + ng.listgroups().then(onGetNZBData).catch(err => { + handleError(err); + restart(); + }); } +log(`${new Date()}: Initialize NZB2Influx`); getAllTheMetrics(); diff --git a/package.json b/package.json index be6c498..635d828 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,6 @@ }, "dependencies": { "influx": "~5.0", - "node-nzbget": "0.0.4", - "chalk": "^1.1.3" + "node-nzbget": "0.0.4" } }