From 0dbab0c916e8ce11765039c00589439eead3ea97 Mon Sep 17 00:00:00 2001 From: Adriean Khisbe Date: Tue, 5 Mar 2019 08:55:08 +0100 Subject: [PATCH] Make linting green again :white_check_mark: - disable some rules (put as warning) - refactor some stuff - fix some bugs introduced in 404e362 :bug: - fix some original bugs (paramater, argument) --- .eslintrc | 21 +-- lib/api/comment.js | 11 +- lib/api/downtime.js | 5 +- lib/api/embed.js | 5 +- lib/api/event.js | 7 +- lib/api/graph.js | 5 +- lib/api/host.js | 2 +- lib/api/index.js | 2 +- lib/api/metric.js | 128 +++++++++--------- lib/api/monitor.js | 20 ++- lib/api/screenboard.js | 5 +- lib/api/search.js | 9 +- lib/api/{serviceCheck.js => service-check.js} | 11 +- lib/api/tag.js | 4 +- lib/api/timeboard.js | 19 ++- lib/client.js | 17 +-- lib/index.js | 12 +- 17 files changed, 143 insertions(+), 140 deletions(-) rename lib/api/{serviceCheck.js => service-check.js} (89%) diff --git a/.eslintrc b/.eslintrc index 0522bf4..7c6905e 100644 --- a/.eslintrc +++ b/.eslintrc @@ -20,20 +20,11 @@ ], "rules": { "promise/no-native": "off", - "strict": "off" + "strict": "off", + "fp/no-arguments": "warn", + "no-param-reassign": "warn", + "prefer-rest-params": "warn", + "fp/no-loops": "warn" }, - "overrides": [ - { - "files": [ "scripts/migrate*.js", "lib/*.js" ], - "env": { - "mongo": true - } - }, - { - "files": ["**/test/**"], - "rules": { - "unicorn/filename-case": "off" - } - } - ] + "overrides": [] } diff --git a/lib/api/comment.js b/lib/api/comment.js index 21e7879..d46ea55 100644 --- a/lib/api/comment.js +++ b/lib/api/comment.js @@ -75,12 +75,10 @@ module.exports = function(client) { const params = { body: { - message + message, + handle: handle || undefined } }; - if (handle) { - params.body.handle = properties.handle; - } client.request('PUT', util.format('/comments/%s', commentId), params, callback); } @@ -150,7 +148,10 @@ module.exports = function(client) { const commentId = args._[4]; remove(commentId, callback); } else { - callback('unknown subcommand or arguments try `dogapi comment --help` for help', false); + return callback( + 'unknown subcommand or arguments try `dogapi comment --help` for help', + false + ); } } }; diff --git a/lib/api/downtime.js b/lib/api/downtime.js index 805f1d4..8549a1f 100644 --- a/lib/api/downtime.js +++ b/lib/api/downtime.js @@ -235,7 +235,10 @@ module.exports = function(client) { } update(downtimeId, properties, callback); } else { - callback('unknown subcommand or arguments try `dogapi downtime --help` for help', false); + return callback( + 'unknown subcommand or arguments try `dogapi downtime --help` for help', + false + ); } } }; diff --git a/lib/api/embed.js b/lib/api/embed.js index 1252efd..696aa76 100644 --- a/lib/api/embed.js +++ b/lib/api/embed.js @@ -162,7 +162,10 @@ module.exports = function(client) { } else if (subcommand === 'getall') { getAll(callback); } else { - callback('unknown subcommand or arguments try `dogapi embed --help` for help', false); + return callback( + 'unknown subcommand or arguments try `dogapi embed --help` for help', + false + ); } } }; diff --git a/lib/api/event.js b/lib/api/event.js index f92fb47..164d4c4 100644 --- a/lib/api/event.js +++ b/lib/api/event.js @@ -118,7 +118,7 @@ module.exports = function(client) { * ``` */ function query(start, end, parameters, callback) { - if (arguments.length < 4 && typeof argument[2] === 'function') { + if (arguments.length < 4 && typeof arguments[2] === 'function') { callback = parameters; parameters = {}; } @@ -203,7 +203,10 @@ module.exports = function(client) { } create(title, text, properties, callback); } else { - callback('unknown subcommand or arguments try `dogapi event --help` for help', false); + return callback( + 'unknown subcommand or arguments try `dogapi event --help` for help', + false + ); } } }; diff --git a/lib/api/graph.js b/lib/api/graph.js index 2390f47..9e451fc 100644 --- a/lib/api/graph.js +++ b/lib/api/graph.js @@ -69,7 +69,10 @@ module.exports = function(client) { const eventQuery = args.events; snapshot(query, from, to, eventQuery, callback); } else { - callback('unknown subcommand or arguments try `dogapi graph --help` for help', false); + return callback( + 'unknown subcommand or arguments try `dogapi graph --help` for help', + false + ); } } }; diff --git a/lib/api/host.js b/lib/api/host.js index 3b639e7..b216300 100644 --- a/lib/api/host.js +++ b/lib/api/host.js @@ -102,7 +102,7 @@ module.exports = function(client) { const hostname = args._[4]; unmute(hostname, callback); } else { - callback('unknown subcommand or arguments try `dogapi host --help` for help', false); + return callback('unknown subcommand or arguments try `dogapi host --help` for help', false); } } }; diff --git a/lib/api/index.js b/lib/api/index.js index 5d9b0f1..2a5de0b 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -10,7 +10,7 @@ module.exports = { monitor: require('./monitor'), screenboard: require('./screenboard'), search: require('./search'), - serviceCheck: require('./serviceCheck'), + serviceCheck: require('./service-check'), tag: require('./tag'), timeboard: require('./timeboard'), user: require('./user') diff --git a/lib/api/metric.js b/lib/api/metric.js index 7c47c9f..dd7baac 100644 --- a/lib/api/metric.js +++ b/lib/api/metric.js @@ -1,62 +1,4 @@ module.exports = function(client) { - /* section: metric - *comment: | - * submit a new metric - *params: - * metric: the metric name - * points: | - * a single data point (e.g. `50`), an array of data points (e.g. `[50, 100]`) - * or an array of `[timestamp, value]` elements (e.g. `[[now, 50], [now, 100]]`) - * extra: | - * optional, object which can contain the following keys - * * host: the host source of the metric - * * tags: array of "tag:value"'s to use for the metric - * * metric_type|type: which metric type to use ("gauge" or "count") [default: gauge] - * callback: | - * function(err, res) - *example: | - * ```javascript - * const dogapi = require("dogapi"); - * const options = { - * api_key: "api_key", - * app_key: "app_key" - * }; - * dogapi.initialize(options); - * dogapi.metric.send("my.metric", 1000, function(err, results){ - * console.dir(results); - * }); - * dogapi.metric.send("my.metric", [500, 1000], function(err, results){ - * console.dir(results); - * }); - * const now = parseInt(new Date().getTime() / 1000); - * dogapi.metric.send("my.metric", [[now, 1000]], function(err, results){ - * console.dir(results); - * }); - * dogapi.metric.send("my.counter", 5, {type: "count"}, function(err, results){ - * console.dir(results); - * }); - * ``` - */ - function send(metric, points, extra, callback) { - if (arguments.length < 4 && typeof arguments[2] === 'function') { - callback = extra; - extra = {}; - } - extra = extra || {}; - const series = [ - { - metric, - points, - host: extra.host, - tags: extra.tags, - // DEV: For backwards compatibility, allow `metric_type` - type: extra.type || extra.metric_type - } - ]; - - send_all(series, callback); - } - /* section: metric *comment: | * send a list of metrics @@ -117,7 +59,6 @@ module.exports = function(client) { // 500 => [, 500] // [, 500] => unchanged if (!Array.isArray(point)) { - const now = parseInt(new Date().getTime() / 1000); point = [now, point]; } return point; @@ -140,13 +81,71 @@ module.exports = function(client) { client.request('POST', '/series', params, callback); } + /* section: metric + *comment: | + * submit a new metric + *params: + * metric: the metric name + * points: | + * a single data point (e.g. `50`), an array of data points (e.g. `[50, 100]`) + * or an array of `[timestamp, value]` elements (e.g. `[[now, 50], [now, 100]]`) + * extra: | + * optional, object which can contain the following keys + * * host: the host source of the metric + * * tags: array of "tag:value"'s to use for the metric + * * metric_type|type: which metric type to use ("gauge" or "count") [default: gauge] + * callback: | + * function(err, res) + *example: | + * ```javascript + * const dogapi = require("dogapi"); + * const options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.metric.send("my.metric", 1000, function(err, results){ + * console.dir(results); + * }); + * dogapi.metric.send("my.metric", [500, 1000], function(err, results){ + * console.dir(results); + * }); + * const now = parseInt(new Date().getTime() / 1000); + * dogapi.metric.send("my.metric", [[now, 1000]], function(err, results){ + * console.dir(results); + * }); + * dogapi.metric.send("my.counter", 5, {type: "count"}, function(err, results){ + * console.dir(results); + * }); + * ``` + */ + function send(metric, points, extra, callback) { + if (arguments.length < 4 && typeof arguments[2] === 'function') { + callback = extra; + extra = {}; + } + extra = extra || {}; + const series = [ + { + metric, + points, + host: extra.host, + tags: extra.tags, + // DEV: For backwards compatibility, allow `metric_type` + type: extra.type || extra.metric_type + } + ]; + + send_all(series, callback); + } + /* section: metric *comment: | * make a metric query *params: * from: POSIX timestamp for start of query * to: POSIX timestamp for end of query - * query: the string query to perform (e.g. "system.cpu.idle{*}by{host}") + * q: the string query to perform (e.g. "system.cpu.idle{*}by{host}") * callback: function(err, res) *example: | * ```javascript @@ -164,12 +163,12 @@ module.exports = function(client) { * }); * ``` */ - function query(from, to, query, callback) { + function query(from, to, q, callback) { const params = { query: { from, to, - query + query: q } }; client.request('GET', '/query', params, callback); @@ -213,7 +212,10 @@ module.exports = function(client) { const q = args._[6]; query(from, to, q, callback); } else { - callback('unknown subcommand or arguments try `dogapi metric --help` for help', false); + return callback( + 'unknown subcommand or arguments try `dogapi metric --help` for help', + false + ); } } }; diff --git a/lib/api/monitor.js b/lib/api/monitor.js index 7d31351..cb3b5bd 100644 --- a/lib/api/monitor.js +++ b/lib/api/monitor.js @@ -379,15 +379,8 @@ module.exports = function(client) { ]; }, handleCli(subcommand, args, callback) { - const states = []; - if (args.states) { - states = args.states.split(','); - } - - const tags = []; - if (args.tags) { - tags = args.tags.split(','); - } + const states = args.states ? args.states.split(',') : []; + const tags = args.tags ? args.tags.split(',') : []; const name = args.name; const message = args.message; @@ -397,10 +390,10 @@ module.exports = function(client) { get(monitorId, states, callback); } else if (subcommand === 'getall') { const options = {}; - if (states.length) { + if (states.length > 0) { options.group_states = states; } - if (tags.length) { + if (tags.length > 0) { options.tags = tags; } getAll(options, callback); @@ -448,7 +441,10 @@ module.exports = function(client) { } update(monitorId, query, properties, callback); } else { - callback('unknown subcommand or arguments try `dogapi monitor --help` for help', false); + return callback( + 'unknown subcommand or arguments try `dogapi monitor --help` for help', + false + ); } } }; diff --git a/lib/api/screenboard.js b/lib/api/screenboard.js index 57de1ed..8c2086e 100644 --- a/lib/api/screenboard.js +++ b/lib/api/screenboard.js @@ -343,7 +343,10 @@ module.exports = function(client) { create(boardTitle, widgets, options, callback); } else { - callback('unknown subcommand or arguments try `dogapi screenboard --help` for help', false); + return callback( + 'unknown subcommand or arguments try `dogapi screenboard --help` for help', + false + ); } } }; diff --git a/lib/api/search.js b/lib/api/search.js index 0500fed..a63a434 100644 --- a/lib/api/search.js +++ b/lib/api/search.js @@ -19,10 +19,10 @@ module.exports = function(client) { * }); * ``` */ - function query(query, callback) { + function query(q, callback) { const params = { query: { - q: query + q } }; client.request('GET', '/search', params, callback); @@ -44,7 +44,10 @@ module.exports = function(client) { if (subcommand === 'query' && args._.length > 4) { query(args._[4], callback); } else { - callback('unknown subcommand or arguments try `dogapi search --help` for help', false); + return callback( + 'unknown subcommand or arguments try `dogapi search --help` for help', + false + ); } } }; diff --git a/lib/api/serviceCheck.js b/lib/api/service-check.js similarity index 89% rename from lib/api/serviceCheck.js rename to lib/api/service-check.js index 0d39c8d..1bff849 100644 --- a/lib/api/serviceCheck.js +++ b/lib/api/service-check.js @@ -28,7 +28,7 @@ module.exports = function(client) { * }); * ``` */ - function check(check, hostName, status, parameters, callback) { + function check(checkName, hostName, status, parameters, callback) { if (arguments.length < 5 && typeof arguments[3] === 'function') { callback = parameters; parameters = {}; @@ -38,8 +38,9 @@ module.exports = function(client) { parameters = {}; } - parameters.check = check; - (parameters.host_name = hostName), (parameters.status = status); + parameters.check = checkName; + parameters.host_name = hostName; + parameters.status = status; const params = { body: parameters @@ -73,14 +74,14 @@ module.exports = function(client) { parameters.time = parseInt(args.time); } if (args.message) { - paramaters.message = args.message; + parameters.message = args.message; } if (args.tags) { parameters.tags = args.tags.split(','); } check(args._[4], args._[5], parseInt(args._[6]), parameters, callback); } else { - callback('not enough arguments try `dogapi servicecheck --help` for help', false); + return callback('not enough arguments try `dogapi servicecheck --help` for help', false); } } }; diff --git a/lib/api/tag.js b/lib/api/tag.js index 1e967e0..1962234 100644 --- a/lib/api/tag.js +++ b/lib/api/tag.js @@ -1,5 +1,3 @@ -const util = require('util'); - module.exports = function(client) { /* section: tag *comment: | @@ -254,7 +252,7 @@ module.exports = function(client) { } else if (subcommand === 'delete') { remove(host, source, callback); } else { - callback('unknown subcommand or arguments try `dogapi tag --help` for help', false); + return callback('unknown subcommand or arguments try `dogapi tag --help` for help', false); } } }; diff --git a/lib/api/timeboard.js b/lib/api/timeboard.js index 160bc94..cfc787e 100644 --- a/lib/api/timeboard.js +++ b/lib/api/timeboard.js @@ -67,7 +67,7 @@ module.exports = function(client) { graphs } }; - if (Array.isArray(templateVariables) && templateVariables.length) { + if (Array.isArray(templateVariables) && templateVariables.length > 0) { params.body.template_variables = templateVariables; } @@ -140,7 +140,7 @@ module.exports = function(client) { graphs } }; - if (Array.isArray(templateVariables) && templateVariables.length) { + if (Array.isArray(templateVariables) && templateVariables.length > 0) { params.body.template_variables = templateVariables; } @@ -251,10 +251,7 @@ module.exports = function(client) { const title = args._[4]; const description = args._[5]; const graphs = json.parse(args._[6]); - const templateVariables = []; - if (args.tmpvars) { - templateVariables = json.parse(args.tmpvars); - } + const templateVariables = args.tmpvars ? json.parse(args.tmpvars) : []; create(title, description, graphs, templateVariables, callback); } else if (subcommand === 'update') { @@ -262,14 +259,14 @@ module.exports = function(client) { const title = args._[5]; const description = args._[6]; const graphs = json.parse(args._[7]); - const templateVariables = []; - if (args.tmpvars) { - templateVariables = json.parse(args.tmpvars); - } + const templateVariables = args.tmpvars ? json.parse(args.tmpvars) : []; update(dashId, title, description, graphs, templateVariables, callback); } else { - callback('unknown subcommand or arguments try `dogapi timeboard --help` for help', false); + return callback( + 'unknown subcommand or arguments try `dogapi timeboard --help` for help', + false + ); } } }; diff --git a/lib/client.js b/lib/client.js index fdc69e8..b77aa33 100644 --- a/lib/client.js +++ b/lib/client.js @@ -2,7 +2,7 @@ const https = require('https'); const url = require('url'); const util = require('util'); const extend = require('extend'); -const _ = require('lodash'); +const _ = require('lodash/fp'); const json = require('./json'); /* section: client @@ -67,15 +67,12 @@ DatadogMetricClient.prototype.request = function(method, path, params, callback) query }); - const http_options = _.assign( - { - hostname: this.api_host, - port: 443, - method: method.toUpperCase(), - path - }, - this.http_options - ); + const http_options = _.assign(this.http_options, { + hostname: this.api_host, + port: 443, + method: method.toUpperCase(), + path + }); if (this.proxy_agent) { http_options.agent = this.proxy_agent; diff --git a/lib/index.js b/lib/index.js index a2c5133..33d1630 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,9 @@ -const _ = require('lodash'); +const _ = require('lodash/fp'); const api = require('./api'); const Client = require('./client'); +const forEach = _.forEach.convert({cap: false}); + const initialClient = new Client({}); /* section: dogapi *comment: configure the dogapi client with your app/api keys @@ -50,15 +52,15 @@ function initialize(options) { function DogApi(options) { const client = new Client(options || {}); - _.forEach(api, (value, key) => { + forEach((value, key) => { this[key] = value(client); - }); + }, api); } DogApi.initialize = initialize; -_.forEach(api, (value, key) => { +forEach((value, key) => { DogApi[key] = value(initialClient); -}); +}, api); /* section: dogapi *comment: get the current POSIX timestamp