diff --git a/bin/dogapi b/bin/dogapi index 69f3a1a..e71588c 100755 --- a/bin/dogapi +++ b/bin/dogapi @@ -1,81 +1,90 @@ #!/usr/bin/env node -var dogapi = require("../"); -var json = require("../lib/json"); -var minimist = require("minimist"); -var rc = require("rc"); -var EOL = require("os").EOL; +const dogapi = require('..'); +const json = require('../lib/json'); +const minimist = require('minimist'); +const rc = require('rc'); +const EOL = require('os').EOL; -var config = rc("dogapi", { - api_key: null, - app_key: null +const config = rc('dogapi', { + api_key: null, + app_key: null }); dogapi.initialize(config); -var usage = [ - "Usage:", - " dogapi --help", - " dogapi --help", - " dogapi --version", - " dogapi now", - " dogapi past ", - " dogapi future " +let usage = [ + 'Usage:', + ' dogapi --help', + ' dogapi --help', + ' dogapi --version', + ' dogapi now', + ' dogapi past ', + ' dogapi future ' ]; -var help = []; -for(var key in dogapi){ - if(!dogapi.hasOwnProperty(key)){ - continue; - } else if(!dogapi[key].hasOwnProperty("getUsage") || typeof dogapi[key].getUsage !== "function"){ - continue; - } else if(!dogapi[key].hasOwnProperty("handleCli") || typeof dogapi[key].handleCli !== "function"){ - continue; - } - usage = usage.concat(dogapi[key].getUsage()); +let help = []; +for (const key in dogapi) { + if (!dogapi.hasOwnProperty(key)) { + continue; + } else if ( + !dogapi[key].hasOwnProperty('getUsage') || + typeof dogapi[key].getUsage !== 'function' + ) { + continue; + } else if ( + !dogapi[key].hasOwnProperty('handleCli') || + typeof dogapi[key].handleCli !== 'function' + ) { + continue; + } + usage = usage.concat(dogapi[key].getUsage()); - if(dogapi[key].hasOwnProperty("getHelp") && typeof dogapi[key].getHelp === "function"){ - help = help.concat([""], dogapi[key].getHelp()); - } + if (dogapi[key].hasOwnProperty('getHelp') && typeof dogapi[key].getHelp === 'function') { + help = help.concat([''], dogapi[key].getHelp()); + } } usage = usage.concat(help); usage = usage.join(EOL); -var args = minimist(process.argv); +const args = minimist(process.argv); -var command = args._[2]; -var subcommand = args._[3]; +let command = args._[2]; +const subcommand = args._[3]; // this is the one unusual case -if(command === "servicecheck"){ - command = "serviceCheck"; +if (command === 'servicecheck') { + command = 'serviceCheck'; } -if(command === "now"){ - console.log(dogapi.now()); -} else if(command === "past" && args._.length > 3){ - console.log(dogapi.now() - parseInt(args._[args._.length - 1])); -} else if(command === "future" && args._.length > 3){ - console.log(dogapi.now() + parseInt(args._[args._.length - 1])); -} else if(dogapi.hasOwnProperty(command)){ - if(subcommand){ - dogapi[command].handleCli(subcommand, args, function(err, res){ - if(err){ - console.error(json.stringify(err, null, ' ')); - process.exit(1); - } else { - if(res === ""){ - res = "success"; - } - console.log(json.stringify(res, null, ' ')); - } - }); - } else { - var commandUsage = ["Usage:"].concat(dogapi[command].getUsage()); - if(dogapi[command].hasOwnProperty("getHelp") && typeof dogapi[command].getHelp === "function"){ - commandUsage = commandUsage.concat([EOL], dogapi[command].getHelp()); +if (command === 'now') { + console.log(dogapi.now()); +} else if (command === 'past' && args._.length > 3) { + console.log(dogapi.now() - parseInt(args._[args._.length - 1])); +} else if (command === 'future' && args._.length > 3) { + console.log(dogapi.now() + parseInt(args._[args._.length - 1])); +} else if (dogapi.hasOwnProperty(command)) { + if (subcommand) { + dogapi[command].handleCli(subcommand, args, function(err, res) { + if (err) { + console.error(json.stringify(err, null, ' ')); + process.exit(1); + } else { + if (res === '') { + res = 'success'; } - console.log(commandUsage.join(EOL).replace(/\$\{command\}/g, " dogapi")); + console.log(json.stringify(res, null, ' ')); + } + }); + } else { + let commandUsage = ['Usage:'].concat(dogapi[command].getUsage()); + if ( + dogapi[command].hasOwnProperty('getHelp') && + typeof dogapi[command].getHelp === 'function' + ) { + commandUsage = commandUsage.concat([EOL], dogapi[command].getHelp()); } -} else if(args.version){ - console.log(require("../package.json").version); + console.log(commandUsage.join(EOL).replace(/\$\{command\}/g, ' dogapi')); + } +} else if (args.version) { + console.log(require('../package.json').version); } else { - console.log(usage); + console.log(usage); } diff --git a/examples/new-interface.js b/examples/new-interface.js index 7fd6cb6..ea37b5e 100644 --- a/examples/new-interface.js +++ b/examples/new-interface.js @@ -1,10 +1,10 @@ -var Dogapi = require("../lib"); +const Dogapi = require('../lib'); -var options = { - api_key: "YOUR_KEY_HERE", - app_key: "YOUR_KEY_HERE", +const options = { + api_key: 'YOUR_KEY_HERE', + app_key: 'YOUR_KEY_HERE' }; const dogapi = new Dogapi(options); -dogapi.metric.send('test', 1) \ No newline at end of file +dogapi.metric.send('test', 1); diff --git a/examples/original-interface.js b/examples/original-interface.js index c1e9e8c..161f1a0 100644 --- a/examples/original-interface.js +++ b/examples/original-interface.js @@ -1,10 +1,10 @@ -var dogapi = require("../lib"); +const dogapi = require('../lib'); -var options = { - api_key: "YOUR_KEY_HERE", - app_key: "YOUR_KEY_HERE", +const options = { + api_key: 'YOUR_KEY_HERE', + app_key: 'YOUR_KEY_HERE' }; dogapi.initialize(options); -dogapi.metric.send('test', 1) \ No newline at end of file +dogapi.metric.send('test', 1); diff --git a/lib/api/comment.js b/lib/api/comment.js index df5b233..21e7879 100644 --- a/lib/api/comment.js +++ b/lib/api/comment.js @@ -1,7 +1,7 @@ -const util = require("util"); +const util = require('util'); -module.exports = function (client) { - /*section: comment +module.exports = function(client) { + /* section: comment *comment: create a new comment *params: * message: the message of the comment @@ -23,31 +23,31 @@ module.exports = function (client) { * }); * ``` */ - function create(message, properties, callback) { - if (arguments.length < 3 && typeof arguments[1] === "function") { - callback = properties; - properties = {}; - } - - const params = { - body: { - message: message - } - }; + function create(message, properties, callback) { + if (arguments.length < 3 && typeof arguments[1] === 'function') { + callback = properties; + properties = {}; + } - if (typeof properties === "object") { - if (properties.handle) { - params.body.handle = properties.handle; - } - if (properties.related_event_id) { - params.body.related_event_id = properties.related_event_id; - } - } + const params = { + body: { + message + } + }; - client.request("POST", "/comments", params, callback); + if (typeof properties === 'object') { + if (properties.handle) { + params.body.handle = properties.handle; + } + if (properties.related_event_id) { + params.body.related_event_id = properties.related_event_id; + } } - /*section: comment + client.request('POST', '/comments', params, callback); + } + + /* section: comment *comment: update an existing comment *params: * commentId: the id of the comment to update @@ -67,25 +67,25 @@ module.exports = function (client) { * }); * ``` */ - function update(commentId, message, handle, callback) { - if (arguments.length < 4 && typeof arguments[2] === "function") { - callback = handle; - handle = undefined; - } - - const params = { - body: { - message: message - } - }; - if (handle) { - params.body.handle = properties.handle; - } + function update(commentId, message, handle, callback) { + if (arguments.length < 4 && typeof arguments[2] === 'function') { + callback = handle; + handle = undefined; + } - client.request("PUT", util.format("/comments/%s", commentId), params, callback); + const params = { + body: { + message + } + }; + if (handle) { + params.body.handle = properties.handle; } - /*section: comment + client.request('PUT', util.format('/comments/%s', commentId), params, callback); + } + + /* section: comment *comment: remove a comment *params: * commentId: the id of the comment to remove @@ -103,56 +103,55 @@ module.exports = function (client) { * }); * ``` */ - function remove(commentId, callback) { - client.request("DELETE", util.format("/comments/%s", commentId), callback); - } + function remove(commentId, callback) { + client.request('DELETE', util.format('/comments/%s', commentId), callback); + } - - return { - create: create, - update: update, - remove: remove, - getUsage: function () { - return [ - " dogapi comment create [--handle ] [--event ]", - " dogapi comment update [--handle ]", - " dogapi comment remove " - ]; - }, - getHelp: function () { - return [ - "Comment:", - " Subcommands:", - " create add a new comment", - " update update an existing comment", - " remove delete a comment", - "", - " Options:", - " --handle the handle to associate with the comment (e.g. \"user@domain.com\")", - " --event related event id to associate the comment with" - ]; - }, - handleCli: function (subcommand, args, callback) { - if (subcommand === "create") { - const message = args._[4]; - const properties = {}; - if (args["handle"]) { - properties.handle = args["handle"]; - } - if (args["event"]) { - properties.related_event_id = parseInt(args["event"]); - } - create(message, properties, callback); - } else if (subcommand === "update") { - const commentId = args._[4]; - const message = args._[5]; - update(commentId, message, args["handle"], callback); - } else if (subcommand === "remove") { - const commentId = args._[4]; - remove(commentId, callback); - } else { - callback("unknown subcommand or arguments try `dogapi comment --help` for help", false); - } + return { + create, + update, + remove, + getUsage() { + return [ + ' dogapi comment create [--handle ] [--event ]', + ' dogapi comment update [--handle ]', + ' dogapi comment remove ' + ]; + }, + getHelp() { + return [ + 'Comment:', + ' Subcommands:', + ' create add a new comment', + ' update update an existing comment', + ' remove delete a comment', + '', + ' Options:', + ' --handle the handle to associate with the comment (e.g. "user@domain.com")', + ' --event related event id to associate the comment with' + ]; + }, + handleCli(subcommand, args, callback) { + if (subcommand === 'create') { + const message = args._[4]; + const properties = {}; + if (args.handle) { + properties.handle = args.handle; } - }; + if (args.event) { + properties.related_event_id = parseInt(args.event); + } + create(message, properties, callback); + } else if (subcommand === 'update') { + const commentId = args._[4]; + const message = args._[5]; + update(commentId, message, args.handle, callback); + } else if (subcommand === 'remove') { + const commentId = args._[4]; + remove(commentId, callback); + } else { + 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 05eb600..805f1d4 100644 --- a/lib/api/downtime.js +++ b/lib/api/downtime.js @@ -1,7 +1,7 @@ -const util = require("util"); +const util = require('util'); -module.exports = function (client) { - /*section: downtime +module.exports = function(client) { + /* section: downtime *comment: schedule a new downtime *params: * scope: string scope that the downtime should apply to (e.g. "env:staging") @@ -24,32 +24,32 @@ module.exports = function (client) { * }); * ``` */ - function create(scope, properties, callback) { - if (arguments.length < 3 && typeof arguments[1] === "function") { - callback = properties; - properties = {}; - } + function create(scope, properties, callback) { + if (arguments.length < 3 && typeof arguments[1] === 'function') { + callback = properties; + properties = {}; + } - const params = { - body: { - scope: scope - } - }; - if (typeof properties === "object") { - if (properties.start) { - params.body.start = parseInt(properties.start); - } - if (properties.end) { - params.body.end = parseInt(properties.end); - } - if (properties.message) { - params.body.message = properties.message; - } - } - client.request("POST", "/downtime", params, callback); + const params = { + body: { + scope + } + }; + if (typeof properties === 'object') { + if (properties.start) { + params.body.start = parseInt(properties.start); + } + if (properties.end) { + params.body.end = parseInt(properties.end); + } + if (properties.message) { + params.body.message = properties.message; + } } + client.request('POST', '/downtime', params, callback); + } - /*section: downtime + /* section: downtime *comment: update an existing downtime *params: * downtimeId: the id the downtie to update @@ -76,32 +76,32 @@ module.exports = function (client) { * }); * ``` */ - function update(downtimeId, properties, callback) { - if (arguments.length < 3 && typeof arguments[1] === "function") { - callback = properties; - properties = {}; - } - const params = { - body: {} - }; - if (typeof properties === "object") { - if (properties.scope) { - params.body.scope = properties.scope; - } - if (properties.start) { - params.body.start = parseInt(properties.start); - } - if (properties.end) { - params.body.end = parseInt(properties.end); - } - if (properties.message) { - params.body.message = properties.message; - } - } - client.request("PUT", util.format("/downtime/%s", downtimeId), params, callback); + function update(downtimeId, properties, callback) { + if (arguments.length < 3 && typeof arguments[1] === 'function') { + callback = properties; + properties = {}; + } + const params = { + body: {} + }; + if (typeof properties === 'object') { + if (properties.scope) { + params.body.scope = properties.scope; + } + if (properties.start) { + params.body.start = parseInt(properties.start); + } + if (properties.end) { + params.body.end = parseInt(properties.end); + } + if (properties.message) { + params.body.message = properties.message; + } } + client.request('PUT', util.format('/downtime/%s', downtimeId), params, callback); + } - /*section: downtime + /* section: downtime *comment: delete a scheduled downtime *params: * downtimeId: the id of the downtime @@ -119,11 +119,11 @@ module.exports = function (client) { * }); * ``` */ - function remove(downtimeId, callback) { - client.request("DELETE", util.format("/downtime/%s", downtimeId), callback); - } + function remove(downtimeId, callback) { + client.request('DELETE', util.format('/downtime/%s', downtimeId), callback); + } - /*section: downtime + /* section: downtime *comment: get a scheduled downtimes details *params: * downtimeId: the id of the downtime @@ -141,11 +141,11 @@ module.exports = function (client) { * }); * ``` */ - function get(downtimeId, callback) { - client.request("GET", util.format("/downtime/%s", downtimeId), callback); - } + function get(downtimeId, callback) { + client.request('GET', util.format('/downtime/%s', downtimeId), callback); + } - /*section: downtime + /* section: downtime *comment: get all downtimes details *params: * callback: function(err, res) @@ -162,82 +162,81 @@ module.exports = function (client) { * }); * ``` */ - function getAll(callback) { - client.request("GET", "/downtime", callback); - } - + function getAll(callback) { + client.request('GET', '/downtime', callback); + } - return { - create: create, - update: update, - remove: remove, - get: get, - getAll: getAll, - getUsage: function () { - return [ - " dogapi downtime create [--start ] [--end ] [--message ]", - " dogapi downtime update [--scope ] [--start ] [--end ] [--message ]", - " dogapi downtime remove ", - " dogapi downtime get ", - " dogapi downtime getall", - ]; - }, - getHelp: function () { - return [ - "Downtime:", - " Subcommands:", - " create create a new downtime with the provided scope (e.g. \"env:staging\")", - " update update an existing downtime with the provided id", - " remove remove the downtime with the provided id", - " get get the details of the downtime with the provided id", - " getall get the details of all downtimes", - "", - " Options:", - " --start POSIX timestamp for when the downtime should start", - " --end POSIX timestamp for when the downtime should end", - " --message a string message to accompany the downtime", - " --scope the scope of the downtime (e.g. \"env:staging\")" - ]; - }, - handleCli: function (subcommand, args, callback) { - if (subcommand === "get") { - get(args._[4], callback); - } else if (subcommand === "getall") { - getAll(callback); - } else if (subcommand === "remove") { - remove(args._[4], callback); - } else if (subcommand === "create") { - const scope = args._[4]; - const properties = {}; - if (args["start"]) { - properties.start = parseInt(args["start"]); - } - if (args["end"]) { - properties.end = parseInt(args["end"]); - } - if (args["message"]) { - properties.message = args["message"]; - } - create(scope, properties, callback); - } else if (subcommand === "update") { - const downtimeId = args._[4]; - const properties = {}; - if (args["scope"]) { - properties.scope = args["scope"]; - } - if (args["start"]) { - properties.start = parseInt(args["start"]); - } - if (args["end"]) { - properties.end = parseInt(args["end"]); - } - if (args["message"]) { - properties.message = args["message"]; - } - update(downtimeId, properties, callback); - } else { - callback("unknown subcommand or arguments try `dogapi downtime --help` for help", false); - } + return { + create, + update, + remove, + get, + getAll, + getUsage() { + return [ + ' dogapi downtime create [--start ] [--end ] [--message ]', + ' dogapi downtime update [--scope ] [--start ] [--end ] [--message ]', + ' dogapi downtime remove ', + ' dogapi downtime get ', + ' dogapi downtime getall' + ]; + }, + getHelp() { + return [ + 'Downtime:', + ' Subcommands:', + ' create create a new downtime with the provided scope (e.g. "env:staging")', + ' update update an existing downtime with the provided id', + ' remove remove the downtime with the provided id', + ' get get the details of the downtime with the provided id', + ' getall get the details of all downtimes', + '', + ' Options:', + ' --start POSIX timestamp for when the downtime should start', + ' --end POSIX timestamp for when the downtime should end', + ' --message a string message to accompany the downtime', + ' --scope the scope of the downtime (e.g. "env:staging")' + ]; + }, + handleCli(subcommand, args, callback) { + if (subcommand === 'get') { + get(args._[4], callback); + } else if (subcommand === 'getall') { + getAll(callback); + } else if (subcommand === 'remove') { + remove(args._[4], callback); + } else if (subcommand === 'create') { + const scope = args._[4]; + const properties = {}; + if (args.start) { + properties.start = parseInt(args.start); } - }; + if (args.end) { + properties.end = parseInt(args.end); + } + if (args.message) { + properties.message = args.message; + } + create(scope, properties, callback); + } else if (subcommand === 'update') { + const downtimeId = args._[4]; + const properties = {}; + if (args.scope) { + properties.scope = args.scope; + } + if (args.start) { + properties.start = parseInt(args.start); + } + if (args.end) { + properties.end = parseInt(args.end); + } + if (args.message) { + properties.message = args.message; + } + update(downtimeId, properties, callback); + } else { + 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 3921ed1..1252efd 100644 --- a/lib/api/embed.js +++ b/lib/api/embed.js @@ -1,10 +1,9 @@ -const extend = require("extend"); -const json = require("../json"); // TODO inline lib -const querystring = require("querystring"); +const querystring = require('querystring'); +const extend = require('extend'); +const json = require('../json'); // TODO inline lib -module.exports = function (client) { - - /*section: embed +module.exports = function(client) { + /* section: embed *comment: create an embed graph of a metric query *params: * graph_json: The request array to pass create in the embed @@ -45,28 +44,28 @@ module.exports = function (client) { * }); * ``` */ - function create(graphJSON, options, callback) { - if (callback === undefined && typeof options === "function") { - callback = options; - options = {}; - } - const body = { - graph_json: JSON.stringify(graphJSON) - }; - // Use `extend` to merge `options` into `body` - // DEV: `extend` will ignore any properties whose value is `undefined` - extend(body, options || {}); + function create(graphJSON, options, callback) { + if (callback === undefined && typeof options === 'function') { + callback = options; + options = {}; + } + const body = { + graph_json: JSON.stringify(graphJSON) + }; + // Use `extend` to merge `options` into `body` + // DEV: `extend` will ignore any properties whose value is `undefined` + extend(body, options || {}); - // Create the request - const params = { - body: querystring.stringify(body), - contentType: "application/x-www-form-urlencoded" - }; + // Create the request + const params = { + body: querystring.stringify(body), + contentType: 'application/x-www-form-urlencoded' + }; - client.request("POST", "/graph/embed", params, callback); - } + client.request('POST', '/graph/embed', params, callback); + } - /*section: embed + /* section: embed *comment: delete an embed with a specific id *params: * embedId: the id of the embed to delete @@ -79,12 +78,11 @@ module.exports = function (client) { * }); * ``` */ - function revoke(embedId, callback) { - client.request("GET", "/graph/embed/" + embedId + "/revoke", callback); - } + function revoke(embedId, callback) { + client.request('GET', `/graph/embed/${embedId}/revoke`, callback); + } - - /*section: embed + /* section: embed *comment: get all embeds from datadog *params: * callback: function(err, res) @@ -95,11 +93,11 @@ module.exports = function (client) { * }); * ``` */ - function getAll(callback) { - client.request("GET", "/graph/embed", callback); - } + function getAll(callback) { + client.request('GET', '/graph/embed', callback); + } - /*section: embed + /* section: embed *comment: get a single embed *params: * embedId: the id of the embed to get @@ -112,61 +110,60 @@ module.exports = function (client) { * }); * ``` */ - function get(embedId, callback) { - client.request("GET", "/graph/embed/" + embedId, callback); - } - - return { - create: create, - revoke: revoke, - getAll: getAll, - get: get, - getUsage: function () { - return [ - " dogapi embed create [--timeframe ] [--size ] [--legend ] [--title ]", - " dogapi embed revoke <embed_id>", - " dogapi embed get <embed_id>", - " dogapi embed getall" - ]; - }, - getHelp: function () { - return [ - "Embed:", - " Subcommands:", - " create <embed_json> --timeframe <timeframe> --size <size> --legend <legend> --title <title> | create a new graph embed", - " revoke <embed_id> revoke/delete an embed", - " get <embed_id> gets a single embed object", - " getall gets all embed objects", - " Options:", - " --events <event-query> a query for event bands to add to the snapshot", - " --timeframe <timeframe> The timeframe for the embed (1_hour, 4_hours, 1_day, 2_days, and 1_week)", - " --size <size> The size of the embed to create (small, medium, large, xlarge)", - " --legend <legend> Whether or not to have a legend (yes, no)", - " --title <title> The title of the embed to create" + function get(embedId, callback) { + client.request('GET', `/graph/embed/${embedId}`, callback); + } - ]; - }, - handleCli: function (subcommand, args, callback) { - if (args._.length > 4 && subcommand === "create") { - const graph_json = json.parse(args._[4]); - const options = { - timeframe: args["timeframe"], - size: args["size"], - legend: args["legend"], - title: args["title"] - }; - create(graph_json, options, callback); - } else if (args._.length > 4 && subcommand === "revoke") { - const embedId = args._[4]; - revoke(embedId, callback); - } else if (args._.length > 4 && subcommand === "get") { - const embedId = args._[4]; - get(embedId, callback); - } else if (subcommand === "getall") { - getAll(callback); - } else { - callback("unknown subcommand or arguments try `dogapi embed --help` for help", false); - } - } - }; + return { + create, + revoke, + getAll, + get, + getUsage() { + return [ + ' dogapi embed create <embed_json> [--timeframe <timeframe>] [--size <size>] [--legend <legend>] [--title <title>]', + ' dogapi embed revoke <embed_id>', + ' dogapi embed get <embed_id>', + ' dogapi embed getall' + ]; + }, + getHelp() { + return [ + 'Embed:', + ' Subcommands:', + ' create <embed_json> --timeframe <timeframe> --size <size> --legend <legend> --title <title> | create a new graph embed', + ' revoke <embed_id> revoke/delete an embed', + ' get <embed_id> gets a single embed object', + ' getall gets all embed objects', + ' Options:', + ' --events <event-query> a query for event bands to add to the snapshot', + ' --timeframe <timeframe> The timeframe for the embed (1_hour, 4_hours, 1_day, 2_days, and 1_week)', + ' --size <size> The size of the embed to create (small, medium, large, xlarge)', + ' --legend <legend> Whether or not to have a legend (yes, no)', + ' --title <title> The title of the embed to create' + ]; + }, + handleCli(subcommand, args, callback) { + if (args._.length > 4 && subcommand === 'create') { + const graph_json = json.parse(args._[4]); + const options = { + timeframe: args.timeframe, + size: args.size, + legend: args.legend, + title: args.title + }; + create(graph_json, options, callback); + } else if (args._.length > 4 && subcommand === 'revoke') { + const embedId = args._[4]; + revoke(embedId, callback); + } else if (args._.length > 4 && subcommand === 'get') { + const embedId = args._[4]; + get(embedId, callback); + } else if (subcommand === 'getall') { + getAll(callback); + } else { + 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 3a31912..f92fb47 100644 --- a/lib/api/event.js +++ b/lib/api/event.js @@ -1,7 +1,7 @@ -const util = require("util"); +const util = require('util'); -module.exports = function (client) { - /*section: event +module.exports = function(client) { + /* section: event *comment: | * create a new event *params: @@ -42,25 +42,25 @@ module.exports = function (client) { * }); * ``` */ - function create(title, text, properties, callback) { - if (arguments.length < 4 && typeof arguments[2] === "function") { - callback = properties; - properties = {}; - } - if (typeof properties !== "object") { - properties = {}; - } + function create(title, text, properties, callback) { + if (arguments.length < 4 && typeof arguments[2] === 'function') { + callback = properties; + properties = {}; + } + if (typeof properties !== 'object') { + properties = {}; + } - properties.title = title; - properties.text = text; + properties.title = title; + properties.text = text; - const params = { - body: properties - }; - client.request("POST", "/events", params, callback); - } + const params = { + body: properties + }; + client.request('POST', '/events', params, callback); + } - /*section: event + /* section: event *comment: | * get event details from the provided event id *params: @@ -81,11 +81,11 @@ module.exports = function (client) { * }); * ``` */ - function get(eventId, callback) { - client.request("GET", util.format("/events/%s", eventId), callback); - } + function get(eventId, callback) { + client.request('GET', util.format('/events/%s', eventId), callback); + } - /*section: event + /* section: event *comment: | * query the event stream *params: @@ -117,94 +117,94 @@ module.exports = function (client) { * }); * ``` */ - function query(start, end, parameters, callback) { - if (arguments.length < 4 && typeof argument[2] === "function") { - callback = parameters; - parameters = {}; - } + function query(start, end, parameters, callback) { + if (arguments.length < 4 && typeof argument[2] === 'function') { + callback = parameters; + parameters = {}; + } - if (typeof parameters !== "object") { - parameters = {} - } - parameters.start = start; - parameters.end = end; + if (typeof parameters !== 'object') { + parameters = {}; + } + parameters.start = start; + parameters.end = end; - const params = { - query: parameters - }; + const params = { + query: parameters + }; - client.request("GET", "/events", params, callback); - } + client.request('GET', '/events', params, callback); + } - return { - create: create, - get: get, - query: query, - getUsage: function () { - return [ - " dogapi event get <event-id>", - " dogapi event query <from> <to> [--priority <priority>] [--sources <sources>] [--tags <tags>]", - " dogapi event create <title> <text> [--time <timestamp>] [--priority <priority>] [--host <host>] [--tags <tags>] [--type <type>] [--agg-key <agg-key>] [--source <source>]" - ]; - }, - getHelp: function () { - return [ - "Event:", - " Subcommands:", - " get <event-id> get the event with the provided <event-id>", - " query <from> <to> query the event stream between <from> and <to> POSIX timestamps", - " create <title> <text> create a new event with <title> and <text>", - " Options:", - " --priority <priority> the priority of the event \"normal\" or \"low\"", - " --sources <sources> a comma separated list of sources (e.g. \"users,jenkins,chef\")", - " --tags <tags> a comma separated list of \"tag:value\"'s", - " --time <time> a POSIX timestamp for when the event happened", - " --host <host> the host to associate the event to", - " --type <type> the event type \"error\", \"warning\", \"info\" or \"success\"", - " --agg-key <agg-key> an aggregation key to use to associate like events", - " --source <source> the source to associate with this event (e.g. \"users\", \"jenkins\", etc)" - ]; - }, - handleCli: function (subcommand, args, callback) { - if (subcommand === "get" && args._.length > 4) { - get(parseInt(args._[4]), callback); - } else if (subcommand === "query" && args._.length > 5) { - const from = parseInt(args._[4]); - const to = parseInt(args._[5]); - const parameters = {}; - if (args["sources"]) { - parameters.sources = args["sources"]; - } - if (args["tags"]) { - parameters.tags = args["tags"]; - } - query(from, to, parameters, callback); - } else if (subcommand === "create" && args._.length > 5) { - const title = args._[4]; - const text = args._[5]; - const properties = {}; - if (args["priority"]) { - properties.priority = args["priority"]; - } - if (args["host"]) { - properties.host = args["host"]; - } - if (args["time"]) { - properties.date_happened = parseInt(args["time"]); - } - if (args["tags"]) { - properties.tags = args["tags"].split(","); - } - if (args["agg-key"]) { - properties.aggregation_key = args["agg-key"]; - } - if (args["source"]) { - properties.source_type_name = args["source"]; - } - create(title, text, properties, callback); - } else { - callback("unknown subcommand or arguments try `dogapi event --help` for help", false); - } + return { + create, + get, + query, + getUsage() { + return [ + ' dogapi event get <event-id>', + ' dogapi event query <from> <to> [--priority <priority>] [--sources <sources>] [--tags <tags>]', + ' dogapi event create <title> <text> [--time <timestamp>] [--priority <priority>] [--host <host>] [--tags <tags>] [--type <type>] [--agg-key <agg-key>] [--source <source>]' + ]; + }, + getHelp() { + return [ + 'Event:', + ' Subcommands:', + ' get <event-id> get the event with the provided <event-id>', + ' query <from> <to> query the event stream between <from> and <to> POSIX timestamps', + ' create <title> <text> create a new event with <title> and <text>', + ' Options:', + ' --priority <priority> the priority of the event "normal" or "low"', + ' --sources <sources> a comma separated list of sources (e.g. "users,jenkins,chef")', + ' --tags <tags> a comma separated list of "tag:value"\'s', + ' --time <time> a POSIX timestamp for when the event happened', + ' --host <host> the host to associate the event to', + ' --type <type> the event type "error", "warning", "info" or "success"', + ' --agg-key <agg-key> an aggregation key to use to associate like events', + ' --source <source> the source to associate with this event (e.g. "users", "jenkins", etc)' + ]; + }, + handleCli(subcommand, args, callback) { + if (subcommand === 'get' && args._.length > 4) { + get(parseInt(args._[4]), callback); + } else if (subcommand === 'query' && args._.length > 5) { + const from = parseInt(args._[4]); + const to = parseInt(args._[5]); + const parameters = {}; + if (args.sources) { + parameters.sources = args.sources; } - }; + if (args.tags) { + parameters.tags = args.tags; + } + query(from, to, parameters, callback); + } else if (subcommand === 'create' && args._.length > 5) { + const title = args._[4]; + const text = args._[5]; + const properties = {}; + if (args.priority) { + properties.priority = args.priority; + } + if (args.host) { + properties.host = args.host; + } + if (args.time) { + properties.date_happened = parseInt(args.time); + } + if (args.tags) { + properties.tags = args.tags.split(','); + } + if (args['agg-key']) { + properties.aggregation_key = args['agg-key']; + } + if (args.source) { + properties.source_type_name = args.source; + } + create(title, text, properties, callback); + } else { + 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 fd2906e..2390f47 100644 --- a/lib/api/graph.js +++ b/lib/api/graph.js @@ -1,9 +1,9 @@ -const Embed = require("./embed"); +const Embed = require('./embed'); -module.exports = function (client) { - const embed = Embed(client); +module.exports = function(client) { + const embed = Embed(client); - /*section: graph + /* section: graph *comment: take a snapshot of a metric query *params: * query: the metric query to use for the snapshot @@ -27,52 +27,50 @@ module.exports = function (client) { * }); * ``` */ - function snapshot(query, from, to, eventQuery, callback) { - if (arguments.length < 5 && typeof arguments[3] === "function") { - callback = eventQuery; - eventQuery = undefined; - } - const params = { - query: { - metric_query: query, - start: parseInt(from), - end: parseInt(to) - } - }; - if (eventQuery) { - params.query.event_query = eventQuery; - } - - client.request("GET", "/graph/snapshot", params, callback); + function snapshot(query, from, to, eventQuery, callback) { + if (arguments.length < 5 && typeof arguments[3] === 'function') { + callback = eventQuery; + eventQuery = undefined; } - - return { - snapshot: snapshot, - createEmbed: embed.create, - getUsage: function () { - return [ - " dogapi graph snapshot <query> <from> <to> [--events <event-query>]" - ]; - }, - getHelp: function () { - return [ - "Graph:", - " Subcommands:", - " snapshot <query> <from> <to> --events <event-query> | take a snapshot of a graph", - " Options:", - " --events <event-query> a query for event bands to add to the snapshot" - ]; - }, - handleCli: function (subcommand, args, callback) { - if (args._.length > 5 && subcommand === "snapshot") { - const query = args._[4]; - const from = parseInt(args._[5]); - const to = parseInt(args._[6]); - const eventQuery = args["events"]; - snapshot(query, from, to, eventQuery, callback); - } else { - callback("unknown subcommand or arguments try `dogapi graph --help` for help", false); - } - } + const params = { + query: { + metric_query: query, + start: parseInt(from), + end: parseInt(to) + } }; + if (eventQuery) { + params.query.event_query = eventQuery; + } + + client.request('GET', '/graph/snapshot', params, callback); + } + + return { + snapshot, + createEmbed: embed.create, + getUsage() { + return [' dogapi graph snapshot <query> <from> <to> [--events <event-query>]']; + }, + getHelp() { + return [ + 'Graph:', + ' Subcommands:', + ' snapshot <query> <from> <to> --events <event-query> | take a snapshot of a graph', + ' Options:', + ' --events <event-query> a query for event bands to add to the snapshot' + ]; + }, + handleCli(subcommand, args, callback) { + if (args._.length > 5 && subcommand === 'snapshot') { + const query = args._[4]; + const from = parseInt(args._[5]); + const to = parseInt(args._[6]); + const eventQuery = args.events; + snapshot(query, from, to, eventQuery, callback); + } else { + 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 d6a597d..3b639e7 100644 --- a/lib/api/host.js +++ b/lib/api/host.js @@ -1,7 +1,7 @@ -const util = require("util"); +const util = require('util'); -module.exports = function (client) { - /*section: host +module.exports = function(client) { + /* section: host *comment: mute the given host, if it is not already muted *params: * hostname: the hostname of the host to mute @@ -23,27 +23,27 @@ module.exports = function (client) { * }); * ``` */ - function mute(hostname, options, callback) { - if (arguments.length < 3 && typeof arguments[1] === "function") { - callback = options; - options = {}; - } - const params = {}; - if (typeof options === "object") { - params.body = {}; // create body property - if (options.end) { - params.body.end = parseInt(options.end); - } - if (options.override) { - params.body.override = options.override; - } - } else { - params.body = ""; // create empty body - } - client.request("POST", util.format("/host/%s/mute", hostname), params, callback); + function mute(hostname, options, callback) { + if (arguments.length < 3 && typeof arguments[1] === 'function') { + callback = options; + options = {}; + } + const params = {}; + if (typeof options === 'object') { + params.body = {}; // create body property + if (options.end) { + params.body.end = parseInt(options.end); + } + if (options.override) { + params.body.override = options.override; + } + } else { + params.body = ''; // create empty body } + client.request('POST', util.format('/host/%s/mute', hostname), params, callback); + } - /*section: host + /* section: host *comment: unmute the given host, if it is not already unmuted *params: * hostname: the hostname of the host to unmute @@ -61,49 +61,49 @@ module.exports = function (client) { * }); * ``` */ - function unmute(hostname, callback) { - const params = { body: "" }; // create empty body - client.request("POST", util.format("/host/%s/unmute", hostname), params, callback); - } + function unmute(hostname, callback) { + const params = {body: ''}; // create empty body + client.request('POST', util.format('/host/%s/unmute', hostname), params, callback); + } - return { - mute: mute, - unmute: unmute, - getUsage: function () { - return [ - " dogapi host mute <host> [--end <end>] [--override]", - " dogapi host unmute <host>" - ]; - }, - getHelp: function () { - return [ - "Host:", - " Subcommands:", - " mute <host> mute the host with the provided hostname", - " unmute <host> unmute the host with the provided hostname", - "", - " Options:", - " --end <end> POSIX timestamp for when the mute should end", - " --override override an existing \"end\" for a mute on a host" - ]; - }, - handleCli: function (subcommand, args, callback) { - if (subcommand === "mute") { - const hostname = args._[4]; - const options = {}; - if (args["end"]) { - options.end = parseInt(args["end"]); - } - if (args["override"]) { - options.override = args["override"]; - } - mute(hostname, options, callback); - } else if (subcommand === "unmute") { - const hostname = args._[4]; - unmute(hostname, callback); - } else { - callback("unknown subcommand or arguments try `dogapi host --help` for help", false); - } + return { + mute, + unmute, + getUsage() { + return [ + ' dogapi host mute <host> [--end <end>] [--override]', + ' dogapi host unmute <host>' + ]; + }, + getHelp() { + return [ + 'Host:', + ' Subcommands:', + ' mute <host> mute the host with the provided hostname', + ' unmute <host> unmute the host with the provided hostname', + '', + ' Options:', + ' --end <end> POSIX timestamp for when the mute should end', + ' --override override an existing "end" for a mute on a host' + ]; + }, + handleCli(subcommand, args, callback) { + if (subcommand === 'mute') { + const hostname = args._[4]; + const options = {}; + if (args.end) { + options.end = parseInt(args.end); + } + if (args.override) { + options.override = args.override; } - }; + mute(hostname, options, callback); + } else if (subcommand === 'unmute') { + const hostname = args._[4]; + unmute(hostname, callback); + } else { + 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 cc81266..5d9b0f1 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,17 +1,17 @@ module.exports = { - comment: require("./comment"), - downtime: require("./downtime"), - embed: require("./embed"), - event: require("./event"), - graph: require("./graph"), - host: require("./host"), - infrastructure: require("./infrastructure"), - metric: require("./metric"), - monitor: require("./monitor"), - screenboard: require("./screenboard"), - search: require("./search"), - serviceCheck: require("./serviceCheck"), - tag: require("./tag"), - timeboard: require("./timeboard"), - user: require("./user"), + comment: require('./comment'), + downtime: require('./downtime'), + embed: require('./embed'), + event: require('./event'), + graph: require('./graph'), + host: require('./host'), + infrastructure: require('./infrastructure'), + metric: require('./metric'), + monitor: require('./monitor'), + screenboard: require('./screenboard'), + search: require('./search'), + serviceCheck: require('./serviceCheck'), + tag: require('./tag'), + timeboard: require('./timeboard'), + user: require('./user') }; diff --git a/lib/api/infrastructure.js b/lib/api/infrastructure.js index 25caa6b..89ad706 100644 --- a/lib/api/infrastructure.js +++ b/lib/api/infrastructure.js @@ -1,6 +1,5 @@ -module.exports = function (client) { - - /*section: infrastructure +module.exports = function(client) { + /* section: infrastructure *comment: | * search for metrics or hosts *params: @@ -21,32 +20,30 @@ module.exports = function (client) { * }); * ``` */ - function search(query, callback) { - const params = { - query: { - q: query - } - }; + function search(query, callback) { + const params = { + query: { + q: query + } + }; - client.request("GET", "/search", params, callback); + client.request('GET', '/search', params, callback); + } + return { + search, + getUsage() { + return [' dogapi infrastructure search <query>']; + }, + getHelp() { + return [ + 'Infrastructure:', + ' Subcommands:', + ' search <query> query for hosts or metrics with <query> (see http://docs.datadoghq.com/api/#search)' + ]; + }, + handleCli(subcommand, args, callback) { + const query = args._[4]; + search(query, callback); } - return { - search: search, - getUsage: function () { - return [ - " dogapi infrastructure search <query>" - ] - }, - getHelp: function () { - return [ - "Infrastructure:", - " Subcommands:", - " search <query> query for hosts or metrics with <query> (see http://docs.datadoghq.com/api/#search)", - ]; - }, - handleCli: function (subcommand, args, callback) { - const query = args._[4]; - search(query, callback); - } - }; + }; }; diff --git a/lib/api/metric.js b/lib/api/metric.js index c8150f5..7c47c9f 100644 --- a/lib/api/metric.js +++ b/lib/api/metric.js @@ -1,6 +1,5 @@ -module.exports = function (client) { - - /*section: metric +module.exports = function(client) { + /* section: metric *comment: | * submit a new metric *params: @@ -38,27 +37,27 @@ module.exports = function (client) { * }); * ``` */ - function send(metric, points, extra, callback) { - if (arguments.length < 4 && typeof arguments[2] === "function") { - callback = extra; - extra = {}; - } - extra = extra || {}; - const series = [ - { - metric: metric, - points: points, - host: extra.host, - tags: extra.tags, - // DEV: For backwards compatibility, allow `metric_type` - type: extra.type || extra.metric_type - } - ]; - - send_all(series, callback); + 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 + /* section: metric *comment: | * send a list of metrics *params: @@ -100,48 +99,48 @@ module.exports = function (client) { * }); * ``` */ - function send_all(metrics, callback) { - const now = parseInt(new Date().getTime() / 1000); - for (let i = 0; i < metrics.length; ++i) { - // Try to normalize `points` - // DEV: We need `points` to be an array of arrays regardless of what they give us - // Always wrap points in an array, this way we will get: - // 500 => [500] - // [500, 100] => [[<timestamp>, 500], [<timestamp>, 1000]] - // [[<timestamp>, 500]] => [[<timestamp>, 500]] - let points = metrics[i].points; - if (!Array.isArray(metrics[i].points)) { - points = [points]; - } - points = points.map(function (point) { - // Make sure each point is an array, if not make array with current timestamp - // 500 => [<timestamp>, 500] - // [<timestamp>, 500] => unchanged - if (!Array.isArray(point)) { - const now = parseInt(new Date().getTime() / 1000); - point = [now, point]; - } - return point; - }); - - metrics[i].points = points; - - // DEV: Change `metric_type` to `type` for backwards compatibility - metrics[i].type = metrics[i].type || metrics[i].metric_type; - // Remove `metric_type` if it was set - // DEV: This will not cause an error if `metric_type` does not exist - delete metrics[i].metric_type; + function send_all(metrics, callback) { + const now = parseInt(new Date().getTime() / 1000); + for (let i = 0; i < metrics.length; ++i) { + // Try to normalize `points` + // DEV: We need `points` to be an array of arrays regardless of what they give us + // Always wrap points in an array, this way we will get: + // 500 => [500] + // [500, 100] => [[<timestamp>, 500], [<timestamp>, 1000]] + // [[<timestamp>, 500]] => [[<timestamp>, 500]] + let points = metrics[i].points; + if (!Array.isArray(metrics[i].points)) { + points = [points]; + } + points = points.map(function(point) { + // Make sure each point is an array, if not make array with current timestamp + // 500 => [<timestamp>, 500] + // [<timestamp>, 500] => unchanged + if (!Array.isArray(point)) { + const now = parseInt(new Date().getTime() / 1000); + point = [now, point]; } + return point; + }); + + metrics[i].points = points; - const params = { - body: { - series: metrics - } - }; - client.request("POST", "/series", params, callback); + // DEV: Change `metric_type` to `type` for backwards compatibility + metrics[i].type = metrics[i].type || metrics[i].metric_type; + // Remove `metric_type` if it was set + // DEV: This will not cause an error if `metric_type` does not exist + delete metrics[i].metric_type; } - /*section: metric + const params = { + body: { + series: metrics + } + }; + client.request('POST', '/series', params, callback); + } + + /* section: metric *comment: | * make a metric query *params: @@ -165,57 +164,57 @@ module.exports = function (client) { * }); * ``` */ - function query(from, to, query, callback) { - const params = { - query: { - from: from, - to: to, - query: query - } - }; - client.request("GET", "/query", params, callback); - } + function query(from, to, query, callback) { + const params = { + query: { + from, + to, + query + } + }; + client.request('GET', '/query', params, callback); + } - return { - send: send, - send_all: send_all, - query: query, - getUsage: function () { - return [ - " dogapi metric send <metric> <point> [--tags <tags>] [--host <host>] [--type <type>]", - " dogapi metric query <from> <to> <query>" - ] - }, - getHelp: function () { - return [ - "Metric:", - " Subcommands:", - " send <metric> <point> add a new datapoint for <metric> for right now", - " query <from> <to> <query> query for <query> between <from> and <to> POSIX timestamps", - "", - " Options:", - " --tags <tags> a comma separated list of \"tag:value\"'s", - " --host <host> the hostname that should be associated with this metric", - " --type <type> the type of metric \"gauge\" or \"count\"" - ] - }, - handleCli: function (subcommand, args, callback) { - if (args._.length > 5 && subcommand === "send") { - const extra = {}; - if (args.tags) { - extra.tags = args.tags.split(","); - } - extra.host = args.host; - extra.type = args.type; - send(args._[4], args._[5], extra, callback); - } else if (subcommand === "query" && args._.length > 6) { - const from = parseInt(args._[4]); - const to = parseInt(args._[5]); - const q = args._[6]; - query(from, to, q, callback); - } else { - callback("unknown subcommand or arguments try `dogapi metric --help` for help", false); - } + return { + send, + send_all, + query, + getUsage() { + return [ + ' dogapi metric send <metric> <point> [--tags <tags>] [--host <host>] [--type <type>]', + ' dogapi metric query <from> <to> <query>' + ]; + }, + getHelp() { + return [ + 'Metric:', + ' Subcommands:', + ' send <metric> <point> add a new datapoint for <metric> for right now', + ' query <from> <to> <query> query for <query> between <from> and <to> POSIX timestamps', + '', + ' Options:', + ' --tags <tags> a comma separated list of "tag:value"\'s', + ' --host <host> the hostname that should be associated with this metric', + ' --type <type> the type of metric "gauge" or "count"' + ]; + }, + handleCli(subcommand, args, callback) { + if (args._.length > 5 && subcommand === 'send') { + const extra = {}; + if (args.tags) { + extra.tags = args.tags.split(','); } - }; + extra.host = args.host; + extra.type = args.type; + send(args._[4], args._[5], extra, callback); + } else if (subcommand === 'query' && args._.length > 6) { + const from = parseInt(args._[4]); + const to = parseInt(args._[5]); + const q = args._[6]; + query(from, to, q, callback); + } else { + 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 9f36def..7d31351 100644 --- a/lib/api/monitor.js +++ b/lib/api/monitor.js @@ -1,8 +1,7 @@ -const util = require("util"); +const util = require('util'); -module.exports = function(client){ - - /*section: monitor +module.exports = function(client) { + /* section: monitor *comment: create a new monitor *params: * type: one of "metric alert" or "service check" @@ -29,37 +28,37 @@ module.exports = function(client){ * }); * ``` */ - function create(type, query, properties, callback){ - if(arguments.length < 4 && typeof arguments[2] === "function"){ - callback = properties; - properties = {}; - } - - const params = { - body: { - type: type, - query: query - } - }; - - if(typeof properties === "object"){ - if(properties.name){ - params.body.name = properties.name; - } - if(properties.message){ - params.body.message = properties.message; - } - if(properties.tags){ - params.body.tags = properties.tags; - } - if(typeof properties.options === "object"){ - params.body.options = properties.options; - } - } - client.request("POST", "/monitor", params, callback); + function create(type, query, properties, callback) { + if (arguments.length < 4 && typeof arguments[2] === 'function') { + callback = properties; + properties = {}; + } + + const params = { + body: { + type, + query + } + }; + + if (typeof properties === 'object') { + if (properties.name) { + params.body.name = properties.name; + } + if (properties.message) { + params.body.message = properties.message; + } + if (properties.tags) { + params.body.tags = properties.tags; + } + if (typeof properties.options === 'object') { + params.body.options = properties.options; + } } - - /*section: monitor + client.request('POST', '/monitor', params, callback); + } + + /* section: monitor *comment: get an existing monitor's details *params: * monitorId: the id of the monitor @@ -78,23 +77,23 @@ module.exports = function(client){ * }); * ``` */ - function get(monitorId, groupStates, callback){ - if(arguments.length < 3 && typeof arguments[1] === "function"){ - callback = groupStates; - groupStates = undefined; - } - - const params = {}; - if(groupStates){ - params.query = { - group_states: groupStates.join(",") - }; - } - - client.request("GET", util.format("/monitor/%s", monitorId), params, callback); + function get(monitorId, groupStates, callback) { + if (arguments.length < 3 && typeof arguments[1] === 'function') { + callback = groupStates; + groupStates = undefined; } - - /*section: monitor + + const params = {}; + if (groupStates) { + params.query = { + group_states: groupStates.join(',') + }; + } + + client.request('GET', util.format('/monitor/%s', monitorId), params, callback); + } + + /* section: monitor *comment: get all monitors *params: * options: | @@ -116,28 +115,28 @@ module.exports = function(client){ * }); * ``` */ - function getAll(options, callback){ - if(arguments.length < 2 && typeof arguments[0] === "function"){ - callback = options; - options = {}; - } - const params = {}; - if(typeof options === "object"){ - params.query = {}; - if(options.group_states){ - params.query.group_states = options.group_states.join(","); - } - if(options.tags){ - params.query.tags = options.tags.join(","); - } - if(options.monitor_tags){ - params.query.monitor_tags = options.monitor_tags.join(","); - } - } - client.request("GET", "/monitor", params, callback); + function getAll(options, callback) { + if (arguments.length < 2 && typeof arguments[0] === 'function') { + callback = options; + options = {}; + } + const params = {}; + if (typeof options === 'object') { + params.query = {}; + if (options.group_states) { + params.query.group_states = options.group_states.join(','); + } + if (options.tags) { + params.query.tags = options.tags.join(','); + } + if (options.monitor_tags) { + params.query.monitor_tags = options.monitor_tags.join(','); + } } - - /*section: monitor + client.request('GET', '/monitor', params, callback); + } + + /* section: monitor *comment: update a monitor's details *params: * monitorId: the id of the monitor to edit @@ -163,36 +162,36 @@ module.exports = function(client){ * }); * ``` */ - function update(monitorId, query, properties, callback){ - if(arguments.length < 4 && typeof arguments[2] === "function"){ - callback = properties; - properties = {}; - } - - const params = { - body: { - query: query - } - }; - - if(typeof properties === "object"){ - if(properties.name){ - params.body.name = properties.name; - } - if(properties.message){ - params.body.message = properties.message; - } - if(properties.tags){ - params.body.tags = properties.tags; - } - if(typeof properties.options === "object"){ - params.body.options = properties.options; - } - } - client.request("PUT", util.format("/monitor/%s", monitorId), params, callback); + function update(monitorId, query, properties, callback) { + if (arguments.length < 4 && typeof arguments[2] === 'function') { + callback = properties; + properties = {}; } - - /*section: monitor + + const params = { + body: { + query + } + }; + + if (typeof properties === 'object') { + if (properties.name) { + params.body.name = properties.name; + } + if (properties.message) { + params.body.message = properties.message; + } + if (properties.tags) { + params.body.tags = properties.tags; + } + if (typeof properties.options === 'object') { + params.body.options = properties.options; + } + } + client.request('PUT', util.format('/monitor/%s', monitorId), params, callback); + } + + /* section: monitor *comment: delete an existing monitor *params: * monitorId: the id of the monitor to remove @@ -210,11 +209,11 @@ module.exports = function(client){ * }); * ``` */ - function remove(monitorId, callback){ - client.request("DELETE", util.format("/monitor/%s", monitorId), callback); - } - - /*section: monitor + function remove(monitorId, callback) { + client.request('DELETE', util.format('/monitor/%s', monitorId), callback); + } + + /* section: monitor *comment: mute an existing monitor *params: * monitorId: the id of the monitor to mute @@ -236,27 +235,27 @@ module.exports = function(client){ * }); * ``` */ - function mute(monitorId, options, callback){ - if(arguments.length < 3 && typeof arguments[1] === "function"){ - callback = options; - options = {}; - } - const params = {}; - if(typeof options === "object"){ - params.body = {}; - if(options.scope){ - params.body.scope = options.scope; - } - if(options.end){ - params.body.end = parseInt(options.end); - } - } else { - params.body = ""; // create empty body - } - client.request("POST", util.format("/monitor/%s/mute", monitorId), params, callback); + function mute(monitorId, options, callback) { + if (arguments.length < 3 && typeof arguments[1] === 'function') { + callback = options; + options = {}; + } + const params = {}; + if (typeof options === 'object') { + params.body = {}; + if (options.scope) { + params.body.scope = options.scope; + } + if (options.end) { + params.body.end = parseInt(options.end); + } + } else { + params.body = ''; // create empty body } - - /*section: monitor + client.request('POST', util.format('/monitor/%s/mute', monitorId), params, callback); + } + + /* section: monitor *comment: mute all monitors *params: * callback: function(err, res) @@ -273,11 +272,11 @@ module.exports = function(client){ * }); * ``` */ - function muteAll(callback){ - client.request("POST", "/monitor/mute_all", callback); - } - - /*section: monitor + function muteAll(callback) { + client.request('POST', '/monitor/mute_all', callback); + } + + /* section: monitor *comment: unmute an existing monitor *params: * monitorId: the id of the monitor to unmute @@ -296,23 +295,23 @@ module.exports = function(client){ * }); * ``` */ - function unmute(monitorId, scope, callback){ - if(arguments.length < 3 && typeof arguments[1] === "function"){ - callback = scope; - scope = undefined; - } - const params = {}; - if(scope){ - params.body = { - scope: scope - }; - } else { - params.body = ""; // create empty body - } - client.request("POST", util.format("/monitor/%s/unmute", monitorId), params, callback); + function unmute(monitorId, scope, callback) { + if (arguments.length < 3 && typeof arguments[1] === 'function') { + callback = scope; + scope = undefined; + } + const params = {}; + if (scope) { + params.body = { + scope + }; + } else { + params.body = ''; // create empty body } - - /*section: monitor + client.request('POST', util.format('/monitor/%s/unmute', monitorId), params, callback); + } + + /* section: monitor *comment: unmute all monitors *params: * callback: function(err, res) @@ -329,128 +328,128 @@ module.exports = function(client){ * }); * ``` */ - function unmuteAll(callback){ - client.request("POST", "/monitor/unmute_all", callback); - } - - return { - create: create, - get: get, - update: update, - remove: remove, - getAll: getAll, - mute: mute, - muteAll: muteAll, - unmute: unmute, - unmuteAll: unmuteAll, - getUsage: function(){ - return [ - " dogapi monitor create <type> <query> [--name <name>] [--message <message>]", - " dogapi monitor get <monitor-id> [--states <states>]", - " dogapi monitor getall [--states <states>] [--tags <tags>]", - " dogapi monitor mute <monitor-id> [--scope <scope>] [--end <end>]", - " dogapi monitor muteall", - " dogapi monitor remove <monitor-id>", - " dogapi monitor unmute <monitor-id> [--scope <scope>]", - " dogapi monitor unmuteall", - " dogapi monitor update <monitor-id> <query> [--name <name>] [--message <message>]" - ]; - }, - getHelp: function(){ - return [ - "Monitor:", - " Subcommands:", - " create <type> <query> create a new monitor", - " get <monitor-id> get a monitors details", - " getall get a list of all monitors", - " mute <monitor-id> mute the monitor with the id <monitor-id>", - " muteall mute all monitors", - " remove <monitor-id> delete the monitor with the id <monitor-id>", - " unmute <monitor-id> unmute the monitor with the id <monitor-id>", - " unmuteall unmute all monitors", - " update <monitor-id> <query> update an existing monitor", - "", - " Options:", - " --states <states> a comma separated list containing any of \"all\", \"alert\", \"warn\", or \"no data\"", - " --tags <tags> a comma separated list of \"tag:value\"'s", - " --scope <scope> the scope of the monitor to mute (e.g. \"role:db\")", - " --end <end> POSIX timestamp for when the mute should end", - " --name <name> the name for the monitor", - " --message <message> the message for the monitor" - ]; - }, - handleCli: function(subcommand, args, callback){ - const states = []; - if(args["states"]){ - states = args["states"].split(","); - } - - const tags = []; - if(args["tags"]){ - tags = args["tags"].split(","); - } - - const name = args["name"]; - const message = args["message"]; - - if(subcommand === "get"){ - const monitorId = args._[4]; - get(monitorId, states, callback); - } else if(subcommand === "getall"){ - const options = {}; - if(states.length){ - options.group_states = states; - } - if(tags.length){ - options.tags = tags; - } - getAll(options, callback); - } else if(subcommand === "mute"){ - const monitorId = args._[4]; - const options = {}; - if(args["scope"]){ - options.scope = args["scope"]; - } - if(args["end"]){ - options.end = args["end"]; - } - mute(monitorId, options, callback); - } else if(subcommand === "unmute"){ - const monitorId = args._[4]; - const scope = args["scope"]; - unmute(monitorId, scope, callback); - } else if(subcommand === "unmuteall"){ - unmuteAll(callback); - } else if(subcommand === "muteall"){ - muteAll(callback); - } else if(subcommand === "remove"){ - const monitorId = args._[4]; - remove(monitorId, callback); - } else if(subcommand === "create" && args._.length > 5){ - const type = args._[4]; - const query = args._[5]; - const properties = {}; - if(name){ - properties.name = name; - } - if(message){ - properties.message = message; - } - create(type, query, properties, callback); - } else if(subcommand === "update" && args._.length > 5){ - const monitorId = args._[4]; - const query = args._[5]; - const properties = {}; - if(name){ - properties.name = name; - } - if(message){ - properties.message = message; - } - update(monitorId, query, properties, callback); - } else { - callback("unknown subcommand or arguments try `dogapi monitor --help` for help", false); - } + function unmuteAll(callback) { + client.request('POST', '/monitor/unmute_all', callback); + } + + return { + create, + get, + update, + remove, + getAll, + mute, + muteAll, + unmute, + unmuteAll, + getUsage() { + return [ + ' dogapi monitor create <type> <query> [--name <name>] [--message <message>]', + ' dogapi monitor get <monitor-id> [--states <states>]', + ' dogapi monitor getall [--states <states>] [--tags <tags>]', + ' dogapi monitor mute <monitor-id> [--scope <scope>] [--end <end>]', + ' dogapi monitor muteall', + ' dogapi monitor remove <monitor-id>', + ' dogapi monitor unmute <monitor-id> [--scope <scope>]', + ' dogapi monitor unmuteall', + ' dogapi monitor update <monitor-id> <query> [--name <name>] [--message <message>]' + ]; + }, + getHelp() { + return [ + 'Monitor:', + ' Subcommands:', + ' create <type> <query> create a new monitor', + ' get <monitor-id> get a monitors details', + ' getall get a list of all monitors', + ' mute <monitor-id> mute the monitor with the id <monitor-id>', + ' muteall mute all monitors', + ' remove <monitor-id> delete the monitor with the id <monitor-id>', + ' unmute <monitor-id> unmute the monitor with the id <monitor-id>', + ' unmuteall unmute all monitors', + ' update <monitor-id> <query> update an existing monitor', + '', + ' Options:', + ' --states <states> a comma separated list containing any of "all", "alert", "warn", or "no data"', + ' --tags <tags> a comma separated list of "tag:value"\'s', + ' --scope <scope> the scope of the monitor to mute (e.g. "role:db")', + ' --end <end> POSIX timestamp for when the mute should end', + ' --name <name> the name for the monitor', + ' --message <message> the message for the monitor' + ]; + }, + handleCli(subcommand, args, callback) { + const states = []; + if (args.states) { + states = args.states.split(','); + } + + const tags = []; + if (args.tags) { + tags = args.tags.split(','); + } + + const name = args.name; + const message = args.message; + + if (subcommand === 'get') { + const monitorId = args._[4]; + get(monitorId, states, callback); + } else if (subcommand === 'getall') { + const options = {}; + if (states.length) { + options.group_states = states; } - }; + if (tags.length) { + options.tags = tags; + } + getAll(options, callback); + } else if (subcommand === 'mute') { + const monitorId = args._[4]; + const options = {}; + if (args.scope) { + options.scope = args.scope; + } + if (args.end) { + options.end = args.end; + } + mute(monitorId, options, callback); + } else if (subcommand === 'unmute') { + const monitorId = args._[4]; + const scope = args.scope; + unmute(monitorId, scope, callback); + } else if (subcommand === 'unmuteall') { + unmuteAll(callback); + } else if (subcommand === 'muteall') { + muteAll(callback); + } else if (subcommand === 'remove') { + const monitorId = args._[4]; + remove(monitorId, callback); + } else if (subcommand === 'create' && args._.length > 5) { + const type = args._[4]; + const query = args._[5]; + const properties = {}; + if (name) { + properties.name = name; + } + if (message) { + properties.message = message; + } + create(type, query, properties, callback); + } else if (subcommand === 'update' && args._.length > 5) { + const monitorId = args._[4]; + const query = args._[5]; + const properties = {}; + if (name) { + properties.name = name; + } + if (message) { + properties.message = message; + } + update(monitorId, query, properties, callback); + } else { + 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 b4253b7..57de1ed 100644 --- a/lib/api/screenboard.js +++ b/lib/api/screenboard.js @@ -1,8 +1,8 @@ -const json = require("../json"); -const util = require("util"); +const util = require('util'); +const json = require('../json'); -module.exports = function (client) { - /*section: screenboard +module.exports = function(client) { + /* section: screenboard *comment: create a new screenboard *params: * boardTitle: the name of the screenboard @@ -56,42 +56,42 @@ module.exports = function (client) { * ); * ``` */ - function create(boardTitle, widgets, options, callback) { - if (arguments.length < 4 && typeof arguments[2] === "function") { - callback = options; - options = {}; - } - if (typeof options !== "object") { - options = {}; - } - - const params = { - body: { - board_title: boardTitle, - widgets: widgets - } - }; + function create(boardTitle, widgets, options, callback) { + if (arguments.length < 4 && typeof arguments[2] === 'function') { + callback = options; + options = {}; + } + if (typeof options !== 'object') { + options = {}; + } - if (options.description) { - params.body.description = options.description; - } - if (options.templateVariables) { - params.body.template_variables = options.templateVariables; - } - if (options.width) { - params.body.width = options.width; - } - if (options.height) { - params.body.height = options.height; - } - if (options.readOnly) { - params.body.read_only = options.readOnly; - } + const params = { + body: { + board_title: boardTitle, + widgets + } + }; - client.request("POST", "/screen", params, callback); + if (options.description) { + params.body.description = options.description; + } + if (options.templateVariables) { + params.body.template_variables = options.templateVariables; + } + if (options.width) { + params.body.width = options.width; + } + if (options.height) { + params.body.height = options.height; + } + if (options.readOnly) { + params.body.read_only = options.readOnly; } - /*section: screenboard + client.request('POST', '/screen', params, callback); + } + + /* section: screenboard *comment: update an existing screenboard *params: * boardId: the id of the screenboard to update @@ -139,42 +139,42 @@ module.exports = function (client) { * ); * ``` */ - function update(boardId, boardTitle, widgets, options, callback) { - if (arguments.length < 5 && typeof arguments[3] === "function") { - callback = options; - options = {}; - } - if (typeof options !== "object") { - options = {}; - } - - const params = { - body: { - board_title: boardTitle, - widgets: widgets - } - }; + function update(boardId, boardTitle, widgets, options, callback) { + if (arguments.length < 5 && typeof arguments[3] === 'function') { + callback = options; + options = {}; + } + if (typeof options !== 'object') { + options = {}; + } - if (options.description) { - params.body.description = options.description; - } - if (options.templateVariables) { - params.body.template_variables = options.templateVariables; - } - if (options.width) { - params.body.width = options.width; - } - if (options.height) { - params.body.height = options.height; - } - if (options.readOnly) { - params.body.read_only = options.readOnly; - } + const params = { + body: { + board_title: boardTitle, + widgets + } + }; - client.request("PUT", util.format("/screen/%s", boardId), params, callback); + if (options.description) { + params.body.description = options.description; + } + if (options.templateVariables) { + params.body.template_variables = options.templateVariables; + } + if (options.width) { + params.body.width = options.width; + } + if (options.height) { + params.body.height = options.height; } + if (options.readOnly) { + params.body.read_only = options.readOnly; + } + + client.request('PUT', util.format('/screen/%s', boardId), params, callback); + } - /*section: screenboard + /* section: screenboard *comment: delete an existing screenboard *params: * boardId: the id of the screenboard to delete @@ -192,11 +192,11 @@ module.exports = function (client) { * }); * ``` */ - function remove(boardId, callback) { - client.request("DELETE", util.format("/screen/%s", boardId), callback); - } + function remove(boardId, callback) { + client.request('DELETE', util.format('/screen/%s', boardId), callback); + } - /*section: screenboard + /* section: screenboard *comment: get the info of a single existing screenboard *params: * boardId: the id of the screenboard to fetch @@ -214,11 +214,11 @@ module.exports = function (client) { * }); * ``` */ - function get(boardId, callback) { - client.request("GET", util.format("/screen/%s", boardId), callback); - } + function get(boardId, callback) { + client.request('GET', util.format('/screen/%s', boardId), callback); + } - /*section: screenboard + /* section: screenboard *comment: get all existing screenboards *params: * callback: function(err, res) @@ -235,11 +235,11 @@ module.exports = function (client) { * }); * ``` */ - function getAll(callback) { - client.request("GET", "/screen", callback); - } + function getAll(callback) { + client.request('GET', '/screen', callback); + } - /*section: screenboard + /* section: screenboard *comment: share an existing screenboard *params: * boardId: the id of the screenboard to share @@ -257,95 +257,94 @@ module.exports = function (client) { * }); * ``` */ - function share(boardId, callback) { - client.request("POST", util.format("/screen/share/%s", boardId), callback); - } + function share(boardId, callback) { + client.request('POST', util.format('/screen/share/%s', boardId), callback); + } + return { + create, + remove, + update, + get, + getAll, + share, + getUsage() { + return [ + ' dogapi screenboard create <boardTitle> <widgets> [--description <description>] [--tmpvars <templateVariables>] [--width <width>] [--height <height>]', + ' dogapi screenboard remove <boardId>', + ' dogapi screenboard get <boardId>', + ' dogapi screenboard getall', + ' dogapi screenboard share <boardId>' + ]; + }, + getHelp() { + return [ + 'Screenboard:', + ' Subcommands:', + ' create <boardTitle> <widgets> create a new screenboard, <widgets> is a json of the graph definition', + ' update <boardId> <boardTitle> <widgets> update a screenboard', + ' remove <boardId> remove an existing screenboard', + ' get <boardId> get an existing screenboard', + ' getall get all screenboards', + ' share <boardId> get share info for an existing screenboard', + ' Options:', + " --description <description> a description of the screenboard's content", + ' --tmpvars <templateVariables> json representation of the template variable definition', + ' --width <width> width of the screenboard in pixels', + ' --height <height> height of the screenboard in pixels' + ]; + }, + handleCli(subcommand, args, callback) { + if (subcommand === 'get') { + get(args._[4], callback); + } else if (subcommand === 'getall') { + getAll(callback); + } else if (subcommand === 'remove') { + remove(args._[4], callback); + } else if (subcommand === 'share') { + share(args._[4], callback); + } else if (subcommand === 'update') { + const boardId = args._[4]; + const boardTitle = args._[5]; + const widgets = json.parse(args._[6]); - return { - create: create, - remove: remove, - update: update, - get: get, - getAll: getAll, - share: share, - getUsage: function () { - return [ - " dogapi screenboard create <boardTitle> <widgets> [--description <description>] [--tmpvars <templateVariables>] [--width <width>] [--height <height>]", - " dogapi screenboard remove <boardId>", - " dogapi screenboard get <boardId>", - " dogapi screenboard getall", - " dogapi screenboard share <boardId>" - ]; - }, - getHelp: function () { - return [ - "Screenboard:", - " Subcommands:", - " create <boardTitle> <widgets> create a new screenboard, <widgets> is a json of the graph definition", - " update <boardId> <boardTitle> <widgets> update a screenboard", - " remove <boardId> remove an existing screenboard", - " get <boardId> get an existing screenboard", - " getall get all screenboards", - " share <boardId> get share info for an existing screenboard", - " Options:", - " --description <description> a description of the screenboard's content", - " --tmpvars <templateVariables> json representation of the template variable definition", - " --width <width> width of the screenboard in pixels", - " --height <height> height of the screenboard in pixels", - ]; - }, - handleCli: function (subcommand, args, callback) { - if (subcommand === "get") { - get(args._[4], callback); - } else if (subcommand === "getall") { - getAll(callback); - } else if (subcommand === "remove") { - remove(args._[4], callback); - } else if (subcommand === "share") { - share(args._[4], callback); - } else if (subcommand === "update") { - const boardId = args._[4]; - const boardTitle = args._[5]; - const widgets = json.parse(args._[6]); - - const options = {}; - if (args["description"]) { - options.description = args["description"]; - } - if (args["tmpvars"]) { - options.templateVariables = json.parse(args["tmpvars"]); - } - if (args["width"]) { - options.width = parseInt(args["width"]); - } - if (args["height"]) { - options.height = parseInt(args["height"]); - } - - update(boardId, boardTitle, widgets, options, callback); - } else if (subcommand === "create") { - const boardTitle = args._[4]; - const widgets = json.parse(args._[5]); + const options = {}; + if (args.description) { + options.description = args.description; + } + if (args.tmpvars) { + options.templateVariables = json.parse(args.tmpvars); + } + if (args.width) { + options.width = parseInt(args.width); + } + if (args.height) { + options.height = parseInt(args.height); + } - const options = {}; - if (args["description"]) { - options.description = args["description"]; - } - if (args["tmpvars"]) { - options.templateVariables = json.parse(args["tmpvars"]); - } - if (args["width"]) { - options.width = parseInt(args["width"]); - } - if (args["height"]) { - options.height = parseInt(args["height"]); - } + update(boardId, boardTitle, widgets, options, callback); + } else if (subcommand === 'create') { + const boardTitle = args._[4]; + const widgets = json.parse(args._[5]); - create(boardTitle, widgets, options, callback); - } else { - callback("unknown subcommand or arguments try `dogapi screenboard --help` for help", false); - } + const options = {}; + if (args.description) { + options.description = args.description; } - }; + if (args.tmpvars) { + options.templateVariables = json.parse(args.tmpvars); + } + if (args.width) { + options.width = parseInt(args.width); + } + if (args.height) { + options.height = parseInt(args.height); + } + + create(boardTitle, widgets, options, callback); + } else { + 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 0c57b03..0500fed 100644 --- a/lib/api/search.js +++ b/lib/api/search.js @@ -1,5 +1,5 @@ -module.exports = function (client) { - /*section: search +module.exports = function(client) { + /* section: search *comment: | * search for metrics and hosts from the past 24 hours *params: @@ -19,35 +19,33 @@ module.exports = function (client) { * }); * ``` */ - function query(query, callback) { - const params = { - query: { - q: query - } - }; - client.request("GET", "/search", params, callback); - } - - return { - query: query, - getUsage: function () { - return [ - " dogapi search query <query>" - ]; - }, - getHelp: function () { - return [ - "Search:", - " Subcommands:", - " query <query> search for hosts and metrics from the last 24 hours" - ]; - }, - handleCli: function (subcommand, args, callback) { - if (subcommand === "query" && args._.length > 4) { - query(args._[4], callback); - } else { - callback("unknown subcommand or arguments try `dogapi search --help` for help", false); - } - } + function query(query, callback) { + const params = { + query: { + q: query + } }; + client.request('GET', '/search', params, callback); + } + + return { + query, + getUsage() { + return [' dogapi search query <query>']; + }, + getHelp() { + return [ + 'Search:', + ' Subcommands:', + ' query <query> search for hosts and metrics from the last 24 hours' + ]; + }, + handleCli(subcommand, args, callback) { + if (subcommand === 'query' && args._.length > 4) { + query(args._[4], callback); + } else { + callback('unknown subcommand or arguments try `dogapi search --help` for help', false); + } + } + }; }; diff --git a/lib/api/serviceCheck.js b/lib/api/serviceCheck.js index b93d1f8..0d39c8d 100644 --- a/lib/api/serviceCheck.js +++ b/lib/api/serviceCheck.js @@ -1,6 +1,5 @@ -module.exports = function (client) { - - /*section: serviceCheck +module.exports = function(client) { + /* section: serviceCheck *comment: | * post an update to a service check *params: @@ -29,62 +28,60 @@ module.exports = function (client) { * }); * ``` */ - function check(check, hostName, status, parameters, callback) { - if (arguments.length < 5 && typeof arguments[3] === "function") { - callback = parameters; - parameters = {}; - } + function check(check, hostName, status, parameters, callback) { + if (arguments.length < 5 && typeof arguments[3] === 'function') { + callback = parameters; + parameters = {}; + } - if (typeof parameters !== "object") { - parameters = {}; - } + if (typeof parameters !== 'object') { + parameters = {}; + } - parameters.check = check; - parameters.host_name = hostName, - parameters.status = status; + parameters.check = check; + (parameters.host_name = hostName), (parameters.status = status); - const params = { - body: parameters - }; - client.request("POST", "/check_run", params, callback); + const params = { + body: parameters }; + client.request('POST', '/check_run', params, callback); + } - - return { - check: check, - getUsage: function () { - return [ - " dogapi servicecheck check <check> <host> <status> [--time <timestamp>] [--message <message>] [--tags <tags>]" - ]; - }, - getHelp: function () { - return [ - "Service Check:", - " Subcommands:", - " check <check> <host> <status> add a new service check for <check> and <host> at level <status> (0=OK, 1=WARNING, 2=CRITICAL, 3=UNKNOWN)", - "", - " Options:", - " --time <timestamp> the POSIX timestamp to use for the check", - " --message <message> an optional message to accompany the check", - " --tags <tags> a comma separated list of \"tag:value\"'s for the check" - ]; - }, - handleCli: function (subcommand, args, callback) { - if (args._.length > 6) { - const parameters = {}; - if (args["time"]) { - parameters.time = parseInt(args["time"]); - } - if (args["message"]) { - paramaters.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 { + check, + getUsage() { + return [ + ' dogapi servicecheck check <check> <host> <status> [--time <timestamp>] [--message <message>] [--tags <tags>]' + ]; + }, + getHelp() { + return [ + 'Service Check:', + ' Subcommands:', + ' check <check> <host> <status> add a new service check for <check> and <host> at level <status> (0=OK, 1=WARNING, 2=CRITICAL, 3=UNKNOWN)', + '', + ' Options:', + ' --time <timestamp> the POSIX timestamp to use for the check', + ' --message <message> an optional message to accompany the check', + ' --tags <tags> a comma separated list of "tag:value"\'s for the check' + ]; + }, + handleCli(subcommand, args, callback) { + if (args._.length > 6) { + const parameters = {}; + if (args.time) { + parameters.time = parseInt(args.time); } - }; + if (args.message) { + paramaters.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); + } + } + }; }; diff --git a/lib/api/tag.js b/lib/api/tag.js index 210b3a3..1e967e0 100644 --- a/lib/api/tag.js +++ b/lib/api/tag.js @@ -1,8 +1,7 @@ const util = require('util'); -module.exports = function (client) { - - /*section: tag +module.exports = function(client) { + /* section: tag *comment: | * get all host tags *params: @@ -23,21 +22,21 @@ module.exports = function (client) { * }); * ``` */ - function getAll(source, callback) { - if (arguments.length < 2 && typeof arguments[0] === "function") { - callback = source; - source = undefined; - } - - const params = { - query: { - source: source - } - }; - client.request("GET", "/tags/hosts", params, callback); + function getAll(source, callback) { + if (arguments.length < 2 && typeof arguments[0] === 'function') { + callback = source; + source = undefined; } - /*section: tag + const params = { + query: { + source + } + }; + client.request('GET', '/tags/hosts', params, callback); + } + + /* section: tag *comment: | * get the host tags for a provided host name or host id *params: @@ -63,28 +62,26 @@ module.exports = function (client) { * }); * ``` */ - function get(hostname, options, callback) { - if (arguments.length < 3 && typeof arguments[1] === "function") { - callback = options; - options = {}; - } - options = options || {}; + function get(hostname, options, callback) { + if (arguments.length < 3 && typeof arguments[1] === 'function') { + callback = options; + options = {}; + } + options = options || {}; - const params = { - query: { - } - }; - if (options.source) { - params.query.source = options.source; - } - if (options.by_source) { - params.query.by_source = options.by_source; - } - client.request("GET", "/tags/hosts/" + hostname, params, callback); + const params = { + query: {} }; + if (options.source) { + params.query.source = options.source; + } + if (options.by_source) { + params.query.by_source = options.by_source; + } + client.request('GET', `/tags/hosts/${hostname}`, params, callback); + } - - /*section: tag + /* section: tag *comment: | * assign new host tags to the provided host name or host id *params: @@ -109,24 +106,23 @@ module.exports = function (client) { * }); * ``` */ - function create(hostname, tags, source, callback) { - if (arguments.length < 4 && typeof arguments[2] === "function") { - callback = source; - source = undefined; - } - - const params = { - body: { - tags: tags, - source: source - }, - }; + function create(hostname, tags, source, callback) { + if (arguments.length < 4 && typeof arguments[2] === 'function') { + callback = source; + source = undefined; + } - client.request("POST", "/tags/hosts/" + hostname, params, callback); + const params = { + body: { + tags, + source + } }; + client.request('POST', `/tags/hosts/${hostname}`, params, callback); + } - /*section: tag + /* section: tag *comment: | * update the host tags for the provided host name or host id *params: @@ -151,22 +147,22 @@ module.exports = function (client) { * }); * ``` */ - function update(hostname, tags, source, callback) { - if (arguments.length < 4 && typeof arguments[2] === "function") { - callback = source; - source = undefined; - } + function update(hostname, tags, source, callback) { + if (arguments.length < 4 && typeof arguments[2] === 'function') { + callback = source; + source = undefined; + } - const params = { - body: { - tags: tags, - source: source - }, - }; - client.request("PUT", "/tags/hosts/" + hostname, params, callback); + const params = { + body: { + tags, + source + } }; + client.request('PUT', `/tags/hosts/${hostname}`, params, callback); + } - /*section: tag + /* section: tag *comment: | * delete the host tags for the provided host name or host id *params: @@ -189,77 +185,77 @@ module.exports = function (client) { * }); * ``` */ - function remove(hostname, source, callback) { - if (arguments.length < 3 && typeof arguments[1] === "function") { - callback = source; - source = undefined; - } + function remove(hostname, source, callback) { + if (arguments.length < 3 && typeof arguments[1] === 'function') { + callback = source; + source = undefined; + } - const params = { - query: { - source: source - } - }; - client.request("DELETE", "/tags/hosts/" + hostname, params, callback); + const params = { + query: { + source + } }; + client.request('DELETE', `/tags/hosts/${hostname}`, params, callback); + } - return { - _client: client, - getAll: getAll, - get: get, - create: create, - update: update, - remove: remove, - getUsage: function () { - return [ - " dogapi tag getall [--source <source>]", - " dogapi tag get <host> [--source <source>] [--by-source]", - " dogapi tag remove <host> [--source <source>]", - " dogapi tag create <host> <tags> [--source <source>]", - " dogapi tag update <host> <tags> [--source <source>]" - ]; - }, - getHelp: function () { - return [ - "Tag:", - " Subcommands:", - " getall get all tags", - " get <host> get all tags for a given host", - " remove <host> delete tags for a given host", - " create <host> <tags> add the comma separates \"tag:value\"'s from <tag> to <host>", - " update <host> <tags> update the comma separates \"tag:value\"'s from <tag> to <host>", - "", - " Options:", - " --source <source> the source of the tags (e.g. \"chef\", \"user\", \"jenkins\", etc)", - " --by-source whether the results should be grouped by source" - ]; - }, - handleCli: function (subcommand, args, callback) { - const source = args["source"]; - const host = args._[4]; + return { + _client: client, + getAll, + get, + create, + update, + remove, + getUsage() { + return [ + ' dogapi tag getall [--source <source>]', + ' dogapi tag get <host> [--source <source>] [--by-source]', + ' dogapi tag remove <host> [--source <source>]', + ' dogapi tag create <host> <tags> [--source <source>]', + ' dogapi tag update <host> <tags> [--source <source>]' + ]; + }, + getHelp() { + return [ + 'Tag:', + ' Subcommands:', + ' getall get all tags', + ' get <host> get all tags for a given host', + ' remove <host> delete tags for a given host', + ' create <host> <tags> add the comma separates "tag:value"\'s from <tag> to <host>', + ' update <host> <tags> update the comma separates "tag:value"\'s from <tag> to <host>', + '', + ' Options:', + ' --source <source> the source of the tags (e.g. "chef", "user", "jenkins", etc)', + ' --by-source whether the results should be grouped by source' + ]; + }, + handleCli(subcommand, args, callback) { + const source = args.source; + const host = args._[4]; - if (subcommand === "getall") { - getAll(source, callback); - } else if (subcommand === "get") { - const options = {}; - if (source) { - options.source = source; - } - if (args["by-source"]) { - options.by_source = true; - } - get(host, options, callback); - } else if (subcommand === "create") { - const tags = args._[5].split(","); - create(host, tags, source, callback); - } else if (subcommand === "update") { - const tags = args._[5].split(","); - update(host, tags, source, callback); - } else if (subcommand === "delete") { - remove(host, source, callback); - } else { - callback("unknown subcommand or arguments try `dogapi tag --help` for help", false); - } + if (subcommand === 'getall') { + getAll(source, callback); + } else if (subcommand === 'get') { + const options = {}; + if (source) { + options.source = source; } - }; + if (args['by-source']) { + options.by_source = true; + } + get(host, options, callback); + } else if (subcommand === 'create') { + const tags = args._[5].split(','); + create(host, tags, source, callback); + } else if (subcommand === 'update') { + const tags = args._[5].split(','); + update(host, tags, source, callback); + } else if (subcommand === 'delete') { + remove(host, source, callback); + } else { + 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 90ffee4..160bc94 100644 --- a/lib/api/timeboard.js +++ b/lib/api/timeboard.js @@ -1,8 +1,8 @@ -const json = require("../json"); -const util = require("util"); +const util = require('util'); +const json = require('../json'); -module.exports = function (client) { - /*section: timeboard +module.exports = function(client) { + /* section: timeboard *comment: add a new timeboard *params: * title: the title of the timeboard @@ -54,27 +54,27 @@ module.exports = function (client) { * ); * ``` */ - function create(title, description, graphs, templateVariables, callback) { - if (arguments.length < 5 && typeof arguments[3] === "function") { - callback = templateVariables; - templateVariables = []; - } - - const params = { - body: { - title: title, - description: description, - graphs: graphs - } - }; - if (Array.isArray(templateVariables) && templateVariables.length) { - params.body.template_variables = templateVariables; - } + function create(title, description, graphs, templateVariables, callback) { + if (arguments.length < 5 && typeof arguments[3] === 'function') { + callback = templateVariables; + templateVariables = []; + } - client.request("POST", "/dash", params, callback); + const params = { + body: { + title, + description, + graphs + } + }; + if (Array.isArray(templateVariables) && templateVariables.length) { + params.body.template_variables = templateVariables; } - /*section: timeboard + client.request('POST', '/dash', params, callback); + } + + /* section: timeboard *comment: update an existing timeboard *params: * dashId: the id of the timeboard to update @@ -127,27 +127,27 @@ module.exports = function (client) { * ); * ``` */ - function update(dashId, title, description, graphs, templateVariables, callback) { - if (arguments.length < 6 && typeof arguments[4] === "function") { - callback = templateVariables; - templateVariables = []; - } - - const params = { - body: { - title: title, - description: description, - graphs: graphs - } - }; - if (Array.isArray(templateVariables) && templateVariables.length) { - params.body.template_variables = templateVariables; - } + function update(dashId, title, description, graphs, templateVariables, callback) { + if (arguments.length < 6 && typeof arguments[4] === 'function') { + callback = templateVariables; + templateVariables = []; + } - client.request("PUT", util.format("/dash/%s", dashId), params, callback); + const params = { + body: { + title, + description, + graphs + } + }; + if (Array.isArray(templateVariables) && templateVariables.length) { + params.body.template_variables = templateVariables; } - /*section: timeboard + client.request('PUT', util.format('/dash/%s', dashId), params, callback); + } + + /* section: timeboard *comment: remove an existing timeboard *params: * dashId: the id of the timeboard to remove @@ -165,11 +165,11 @@ module.exports = function (client) { * }); * ``` */ - function remove(dashId, callback) { - client.request("DELETE", util.format("/dash/%s", dashId), {}, callback); - } + function remove(dashId, callback) { + client.request('DELETE', util.format('/dash/%s', dashId), {}, callback); + } - /*section: timeboard + /* section: timeboard *comment: get all existing timeboards *params: * callback: function(err, res) @@ -186,11 +186,11 @@ module.exports = function (client) { * }); * ``` */ - function getAll(callback) { - client.request("GET", "/dash", {}, callback); - } + function getAll(callback) { + client.request('GET', '/dash', {}, callback); + } - /*section: timeboard + /* section: timeboard *comment: get an existing timeboard *params: * dashId: the id of the timeboard to get @@ -208,69 +208,69 @@ module.exports = function (client) { * }); * ``` */ - function get(dashId, callback) { - client.request("GET", util.format("/dash/%s", dashId), {}, callback); - } + function get(dashId, callback) { + client.request('GET', util.format('/dash/%s', dashId), {}, callback); + } - return { - create: create, - update: update, - remove: remove, - getAll: getAll, - get: get, - getUsage: function () { - return [ - " dogapi timeboard get <dash-id>", - " dogapi timeboard getall", - " dogapi timeboard remove <dash-id>", - " dogapi timeboard create <title> <description> <graphs> [--tmpvars <templateVariables>]", - " dogapi timeboard update <dash-id> <title> <description> <graphs> [--tmpvars <templateVariables>]", - ]; - }, - getHelp: function () { - return [ - "Timeboard:", - " Subcommands:", - " get <dash-id> get an existing timeboard", - " getall get all existing timeboards", - " remove <dash-id> remove an existing timeboard", - " create <title> <description> <graphs> create a new timeboard, <graphs> is a json of the graphs definition", - " update <dash-id> <title> <description> <graphs> update an existing timeboard, <graphs> is a json of the graphs definition", - " Options:", - " --tmpvars <templateVariables> a json representation of the template variables definition" - ]; - }, - handleCli: function (subcommand, args, callback) { - if (subcommand === "get") { - get(args._[4], callback); - } else if (subcommand === "getall") { - getAll(callback); - } else if (subcommand === "remove") { - remove(args._[4], callback); - } else if (subcommand === "create") { - const title = args._[4]; - const description = args._[5]; - const graphs = json.parse(args._[6]); - const templateVariables = []; - if (args["tmpvars"]) { - templateVariables = json.parse(args["tmpvars"]); - } - - create(title, description, graphs, templateVariables, callback); - } else if (subcommand === "update") { - const dashId = parseInt(args._[4]); - const title = args._[5]; - const description = args._[6]; - const graphs = json.parse(args._[7]); - const templateVariables = []; - if (args["tmpvars"]) { - templateVariables = json.parse(args["tmpvars"]); - } + return { + create, + update, + remove, + getAll, + get, + getUsage() { + return [ + ' dogapi timeboard get <dash-id>', + ' dogapi timeboard getall', + ' dogapi timeboard remove <dash-id>', + ' dogapi timeboard create <title> <description> <graphs> [--tmpvars <templateVariables>]', + ' dogapi timeboard update <dash-id> <title> <description> <graphs> [--tmpvars <templateVariables>]' + ]; + }, + getHelp() { + return [ + 'Timeboard:', + ' Subcommands:', + ' get <dash-id> get an existing timeboard', + ' getall get all existing timeboards', + ' remove <dash-id> remove an existing timeboard', + ' create <title> <description> <graphs> create a new timeboard, <graphs> is a json of the graphs definition', + ' update <dash-id> <title> <description> <graphs> update an existing timeboard, <graphs> is a json of the graphs definition', + ' Options:', + ' --tmpvars <templateVariables> a json representation of the template variables definition' + ]; + }, + handleCli(subcommand, args, callback) { + if (subcommand === 'get') { + get(args._[4], callback); + } else if (subcommand === 'getall') { + getAll(callback); + } else if (subcommand === 'remove') { + remove(args._[4], callback); + } else if (subcommand === 'create') { + const title = args._[4]; + const description = args._[5]; + const graphs = json.parse(args._[6]); + const templateVariables = []; + if (args.tmpvars) { + templateVariables = json.parse(args.tmpvars); + } - update(dashId, title, description, graphs, templateVariables, callback); - } else { - callback("unknown subcommand or arguments try `dogapi timeboard --help` for help", false); - } + create(title, description, graphs, templateVariables, callback); + } else if (subcommand === 'update') { + const dashId = parseInt(args._[4]); + const title = args._[5]; + const description = args._[6]; + const graphs = json.parse(args._[7]); + const templateVariables = []; + if (args.tmpvars) { + templateVariables = json.parse(args.tmpvars); } - }; + + update(dashId, title, description, graphs, templateVariables, callback); + } else { + callback('unknown subcommand or arguments try `dogapi timeboard --help` for help', false); + } + } + }; }; diff --git a/lib/api/user.js b/lib/api/user.js index 6e426d8..190ae5b 100644 --- a/lib/api/user.js +++ b/lib/api/user.js @@ -1,6 +1,5 @@ -module.exports = function (client) { - - /*section: user +module.exports = function(client) { + /* section: user *comment: invite users via e-mail *params: * emails: an array of email addresses to send invites to @@ -19,31 +18,29 @@ module.exports = function (client) { * }); * ``` */ - function invite(emails, callback) { - const params = { - body: { - emails: emails - } - }; - client.request("POST", "/invite_users", params, callback); + function invite(emails, callback) { + const params = { + body: { + emails + } }; + client.request('POST', '/invite_users', params, callback); + } - return { - invite: invite, - getUsage: function () { - return [ - " dogapi user invite <address>..." - ]; - }, - getHelp: function () { - return [ - "User:", - " Subcommands:", - " invite <address>... invite the given list of e-mail addresses to your datadog org" - ]; - }, - handleCli: function (subcommand, args, callback) { - invite(args._.slice(4), callback) - } - }; + return { + invite, + getUsage() { + return [' dogapi user invite <address>...']; + }, + getHelp() { + return [ + 'User:', + ' Subcommands:', + ' invite <address>... invite the given list of e-mail addresses to your datadog org' + ]; + }, + handleCli(subcommand, args, callback) { + invite(args._.slice(4), callback); + } + }; }; diff --git a/lib/client.js b/lib/client.js index 79aa338..fdc69e8 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1,27 +1,27 @@ -const https = require("https"); -const url = require("url"); -const util = require("util"); -const extend = require("extend"); +const https = require('https'); +const url = require('url'); +const util = require('util'); +const extend = require('extend'); const _ = require('lodash'); -const json = require("./json"); +const json = require('./json'); -/*section: client +/* section: client *comment: | * the constructor for _client_ object *params: *example: | * See [client.request](#client-request) */ -const DatadogMetricClient = function(options){ - this.api_key = options.api_key || null; - this.app_key = options.app_key || null; - this.proxy_agent = options.proxy_agent || null; - this.http_options = options.http_options || null; - this.api_version = options.api_version || "v1"; - this.api_host = options.api_host || "app.datadoghq.com"; +const DatadogMetricClient = function(options) { + this.api_key = options.api_key || null; + this.app_key = options.app_key || null; + this.proxy_agent = options.proxy_agent || null; + this.http_options = options.http_options || null; + this.api_version = options.api_version || 'v1'; + this.api_host = options.api_host || 'app.datadoghq.com'; }; -/*section: client +/* section: client *comment: | * used to make a raw request to the datadog api *params: @@ -46,87 +46,92 @@ const DatadogMetricClient = function(options){ * }); * ``` */ -DatadogMetricClient.prototype.request = function(method, path, params, callback){ - if(arguments.length === 3 && typeof arguments[2] === "function"){ - callback = arguments[2]; - params = {body: ''}; // create params with empty body property - } - - const body = (typeof params["body"] === "object") ? json.stringify(params["body"]) : params["body"]; - const query = { - "api_key": this.api_key, - "application_key": this.app_key, +DatadogMetricClient.prototype.request = function(method, path, params, callback) { + if (arguments.length === 3 && typeof arguments[2] === 'function') { + callback = arguments[2]; + params = {body: ''}; // create params with empty body property + } + + const body = typeof params.body === 'object' ? json.stringify(params.body) : params.body; + const query = { + api_key: this.api_key, + application_key: this.app_key + }; + + if (typeof params.query === 'object') { + extend(query, params.query); + } + + path = url.format({ + pathname: util.format('/api/%s%s', this.api_version, path), + query + }); + + const http_options = _.assign( + { + hostname: this.api_host, + port: 443, + method: method.toUpperCase(), + path + }, + this.http_options + ); + + if (this.proxy_agent) { + http_options.agent = this.proxy_agent; + } + + if (['POST', 'PUT'].indexOf(http_options.method) >= 0) { + http_options.headers = { + 'Content-Type': params.contentType ? params.contentType : 'application/json', + 'Content-Length': Buffer.byteLength(body) }; + } - if(typeof params["query"] === "object"){ - extend(query, params["query"]); - } - - path = url.format({ - "pathname": util.format("/api/%s%s", this.api_version, path), - "query": query, + const req = https.request(http_options, function(res) { + res.on('error', function(err) { + if (typeof callback === 'function') { + callback(err, null, res.statusCode); + } }); - const http_options = _.assign({ - hostname: this.api_host, - port: 443, - method: method.toUpperCase(), - path: path - }, this.http_options); - - if(this.proxy_agent){ - http_options["agent"] = this.proxy_agent; - } - - if(["POST", "PUT"].indexOf(http_options["method"]) >= 0){ - http_options["headers"] = { - "Content-Type": params["contentType"] ? params["contentType"] : "application/json", - "Content-Length": Buffer.byteLength(body), - }; - } - - const req = https.request(http_options, function(res){ - res.on("error", function(err){ - if(typeof callback == "function"){ - callback(err, null, res.statusCode); - } - }); - - let data = ""; - res.on("data", function(chunk){ - data += chunk; - }); - - res.on("end", function(){ - let error = null; - try{ data = json.parse(data); }catch(e){} - if(data["errors"]){ - error = data["errors"]; - data = null; - } - - if(typeof callback === "function"){ - callback(error, data, res.statusCode); - } - }); + let data = ''; + res.on('data', function(chunk) { + data += chunk; }); - req.setTimeout(30000, function(){ - req.abort(); + res.on('end', function() { + let error = null; + try { + data = json.parse(data); + } catch (e) {} + if (data.errors) { + error = data.errors; + data = null; + } + + if (typeof callback === 'function') { + callback(error, data, res.statusCode); + } }); + }); - // This should only occur for errors such as a socket hang up prior to any - // data being received, or SSL-related issues. - req.on("error", function(err){ - if(typeof callback === "function"){ - callback(err, null, 0); - } - }); + req.setTimeout(30000, function() { + req.abort(); + }); - if(["POST", "PUT"].indexOf(http_options["method"]) >= 0){ - req.write(body); + // This should only occur for errors such as a socket hang up prior to any + // data being received, or SSL-related issues. + req.on('error', function(err) { + if (typeof callback === 'function') { + callback(err, null, 0); } - req.end() + }); + + if (['POST', 'PUT'].indexOf(http_options.method) >= 0) { + req.write(body); + } + req.end(); }; module.exports = DatadogMetricClient; diff --git a/lib/constants.js b/lib/constants.js index 59c145a..701f3f7 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -1,13 +1,13 @@ module.exports.STATUSES = { - OK: 0, - WARNING: 1, - CRITICAL: 2, - UNKNOWN: 3, + OK: 0, + WARNING: 1, + CRITICAL: 2, + UNKNOWN: 3 }; module.exports.ALL_STATUSES = [ - module.exports.STATUSES.OK, - module.exports.STATUSES.WARNING, - module.exports.STATUSES.CRITICAL, - module.exports.STATUSES.UNKNOWN, + module.exports.STATUSES.OK, + module.exports.STATUSES.WARNING, + module.exports.STATUSES.CRITICAL, + module.exports.STATUSES.UNKNOWN ]; diff --git a/lib/index.js b/lib/index.js index 8e1266b..a2c5133 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,9 +1,9 @@ const _ = require('lodash'); -const api = require("./api"); -const Client = require("./client"); +const api = require('./api'); +const Client = require('./client'); -const initialClient = new Client({}) -/*section: dogapi +const initialClient = new Client({}); +/* section: dogapi *comment: configure the dogapi client with your app/api keys *params: * options: @@ -39,28 +39,28 @@ const initialClient = new Client({}) * dogapi.event.create(...); * ``` */ -function initialize(options){ - options = options || {}; - for(var key in options){ - if(initialClient.hasOwnProperty(key)){ - initialClient[key] = options[key]; - } +function initialize(options) { + options = options || {}; + for (const key in options) { + if (initialClient.hasOwnProperty(key)) { + initialClient[key] = options[key]; } -}; + } +} function DogApi(options) { - const client = new Client(options || {}); - _.forEach(api, (value, key) => { - this[key] = value(client); - }); + const client = new Client(options || {}); + _.forEach(api, (value, key) => { + this[key] = value(client); + }); } DogApi.initialize = initialize; _.forEach(api, (value, key) => { - DogApi[key] = value(initialClient); + DogApi[key] = value(initialClient); }); -/*section: dogapi +/* section: dogapi *comment: get the current POSIX timestamp *example: | * ```javascript @@ -70,9 +70,9 @@ _.forEach(api, (value, key) => { * parseInt(new Date().getTime() / 1000); * ``` */ -function now(){ - return parseInt(new Date().getTime() / 1000); -}; +function now() { + return parseInt(new Date().getTime() / 1000); +} module.exports = DogApi; module.exports.now = now; module.exports.OK = 0; diff --git a/lib/json.js b/lib/json.js index 1036179..fab1de2 100644 --- a/lib/json.js +++ b/lib/json.js @@ -1,7 +1,6 @@ -var JSONBig = require("json-bigint"); - +const JSONBig = require('json-bigint'); module.exports = { - stringify: JSONBig.stringify, - parse: JSONBig.parse + stringify: JSONBig.stringify, + parse: JSONBig.parse }; diff --git a/test/api/embed.js b/test/api/embed.js index 6cf9bff..a59fc18 100644 --- a/test/api/embed.js +++ b/test/api/embed.js @@ -1,86 +1,86 @@ -const assert = require("assert"); -const Client = require("../../lib/client"); -const extend = require("extend"); -const Embed = require("../../lib/api/embed"); -const sinon = require("sinon"); -const querystring = require("querystring"); +const assert = require('assert'); +const querystring = require('querystring'); +const extend = require('extend'); +const sinon = require('sinon'); +const Client = require('../../lib/client'); +const Embed = require('../../lib/api/embed'); -describe("api/embed", function(){ - const client = new Client({}); - const embed = Embed(client); - let stub_request; - beforeEach(function(){ - // Setup `client.request` as a stub - stub_request = sinon.stub(client, "request"); - }); - afterEach(function(){ - // Reset the original `client.request` - stub_request.restore(); - stub_request = null; - }); - describe("#create", function(){ - it("should make a valid api call", function(){ - const graphJSON = { - viz: "timeseries", - requests: [ - { - q: "system.cpu.idle{*}" - } - ] - }; - const options = { - timeframe: "1_hour", - size: "large", - legend: "yes", - title: "test graph embed" - }; +describe('api/embed', function() { + const client = new Client({}); + const embed = Embed(client); + let stub_request; + beforeEach(function() { + // Setup `client.request` as a stub + stub_request = sinon.stub(client, 'request'); + }); + afterEach(function() { + // Reset the original `client.request` + stub_request.restore(); + stub_request = null; + }); + describe('#create', function() { + it('should make a valid api call', function() { + const graphJSON = { + viz: 'timeseries', + requests: [ + { + q: 'system.cpu.idle{*}' + } + ] + }; + const options = { + timeframe: '1_hour', + size: 'large', + legend: 'yes', + title: 'test graph embed' + }; - // Make our api call - embed.create(graphJSON, options); + // Make our api call + embed.create(graphJSON, options); - // Assert we properly called `client.request` - assert(stub_request.calledOnce); - const call_args = stub_request.getCall(0).args; - // Method and endpoint are correct - assert.equal(call_args[0], "POST"); - assert.equal(call_args[1], "/graph/embed"); + // Assert we properly called `client.request` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; + // Method and endpoint are correct + assert.equal(call_args[0], 'POST'); + assert.equal(call_args[1], '/graph/embed'); - // Properly formatted body and content-type - const params = call_args[2]; - const expectedBody = { - graph_json: JSON.stringify(graphJSON), - timeframe: "1_hour", - size: "large", - legend: "yes", - title: "test graph embed" - }; - assert.deepEqual(querystring.parse(params.body), expectedBody); - assert(params.contentType, "application/x-form-urlencoded"); - }); + // Properly formatted body and content-type + const params = call_args[2]; + const expectedBody = { + graph_json: JSON.stringify(graphJSON), + timeframe: '1_hour', + size: 'large', + legend: 'yes', + title: 'test graph embed' + }; + assert.deepEqual(querystring.parse(params.body), expectedBody); + assert(params.contentType, 'application/x-form-urlencoded'); + }); - it("should only require graph_json", function(){ - const graphJSON = { - viz: "timeseries", - requests: [ - { - q: "system.cpu.idle{*}" - } - ] - }; + it('should only require graph_json', function() { + const graphJSON = { + viz: 'timeseries', + requests: [ + { + q: 'system.cpu.idle{*}' + } + ] + }; - // Make our api call - embed.create(graphJSON); + // Make our api call + embed.create(graphJSON); - // Assert we properly called `client.request` - assert(stub_request.calledOnce); - const call_args = stub_request.getCall(0).args; + // Assert we properly called `client.request` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; - // Properly formatted body - const params = call_args[2]; - const expectedBody = { - graph_json: JSON.stringify(graphJSON) - }; - assert.deepEqual(querystring.parse(params.body), expectedBody); - }); + // Properly formatted body + const params = call_args[2]; + const expectedBody = { + graph_json: JSON.stringify(graphJSON) + }; + assert.deepEqual(querystring.parse(params.body), expectedBody); }); + }); }); diff --git a/test/api/graph.js b/test/api/graph.js index 927cbbc..757ace5 100644 --- a/test/api/graph.js +++ b/test/api/graph.js @@ -1,10 +1,8 @@ -var assert = require("assert"); -var client = require("../../lib/client"); -var extend = require("extend"); -var graph = require("../../lib/api/graph"); -var sinon = require("sinon"); -var querystring = require("querystring"); +const assert = require('assert'); +const querystring = require('querystring'); +const extend = require('extend'); +const sinon = require('sinon'); +const client = require('../../lib/client'); +const graph = require('../../lib/api/graph'); -describe("api/graph", function(){ - -}); +describe('api/graph', function() {}); diff --git a/test/api/metric.js b/test/api/metric.js index 0bbc432..c7dc868 100644 --- a/test/api/metric.js +++ b/test/api/metric.js @@ -1,357 +1,356 @@ -const assert = require("assert"); -const Client = require("../../lib/client"); -const extend = require("extend"); -const Metric = require("../../lib/api/metric"); -const sinon = require("sinon"); - -describe("api/metrics", function(){ - const client = new Client({}); - const metric = Metric(client); - var stub_request; - beforeEach(function(){ - // Setup `client.request` as a stub - stub_request = sinon.stub(client, "request"); +const assert = require('assert'); +const extend = require('extend'); +const sinon = require('sinon'); +const Client = require('../../lib/client'); +const Metric = require('../../lib/api/metric'); + +describe('api/metrics', function() { + const client = new Client({}); + const metric = Metric(client); + let stub_request; + beforeEach(function() { + // Setup `client.request` as a stub + stub_request = sinon.stub(client, 'request'); + }); + afterEach(function() { + // Reset the original `client.request` + stub_request.restore(); + stub_request = null; + }); + describe('#send', function() { + it('should make a valid api call', function() { + // Make our api call + const now = parseInt(new Date().getTime() / 1000); + metric.send('metric.send', [[now, 500]]); + + // Assert we properly called `client.request` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; + // Method and endpoint are correct + assert.equal(call_args[0], 'POST'); + assert.equal(call_args[1], '/series'); + + // Properly formatted body + // { body: series: [ {metric: "metric.send", host: undefined, tags: undefined, type: undefined} ] } + // DEV: host/tags/type are optional and should be undefined for this case + const data = call_args[2]; + assert(data.hasOwnProperty('body')); + assert(data.body.hasOwnProperty('series')); + + // Assert we have only 1 series + // series = [ {metric: "", ...}, ... ] + const series = data.body.series; + assert(Array.isArray(series)); + assert.equal(series.length, 1); + + // Assert the first series is properly formatted + // first_series = {metric: "", points: [], ...} + const first_series = series[0]; + assert.equal(first_series.metric, 'metric.send'); + assert(Array.isArray(first_series.points)); + assert.deepEqual(first_series.points, [[now, 500]]); + + // These keys are optional and should be set, but undefined + assert(first_series.hasOwnProperty('host')); + assert.equal(first_series.host, undefined); + assert(first_series.hasOwnProperty('tags')); + assert.equal(first_series.tags, undefined); + assert(first_series.hasOwnProperty('type')); + assert.equal(first_series.type, undefined); }); - afterEach(function(){ - // Reset the original `client.request` - stub_request.restore(); - stub_request = null; + + it('should properly normalize values to points', function() { + // Make our api call + metric.send('metrics.send.normalize', 500); + + // Assert we called `client.request` with the correct `points` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; + // { body: series: [ {points: [], }, ] } + const body = call_args[2].body; + assert.equal(body.series.length, 1); + + // points = [ [<timestamp>, 500] ] + const points = body.series[0].points; + assert(Array.isArray(points)); + assert.equal(points.length, 1); + + // point = [<timestamp>, 500] + const point = points[0]; + assert(Array.isArray(point)); + assert.equal(point.length, 2); + assert.equal(point[1], 500); + }); + + it('should properly normalize array of values to points', function() { + // Make our api call + metric.send('metrics.send.normalize', [500, 1000]); + + // Assert we called `client.request` with the correct `points` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; + // { body: series: [ {points: [], }, ] } + const body = call_args[2].body; + assert.equal(body.series.length, 1); + + // points = [ [<timestamp>, 500], [<timestamp>, 1000] ] + const points = body.series[0].points; + assert(Array.isArray(points)); + assert.equal(points.length, 2); + + // point = [<timestamp>, 500] + let point = points[0]; + assert(Array.isArray(point)); + assert.equal(point.length, 2); + assert.equal(point[1], 500); + + // point = [<timestamp>, 1000] + point = points[1]; + assert(Array.isArray(point)); + assert.equal(point.length, 2); + assert.equal(point[1], 1000); + }); + + it('should not normalize correctly formatted points', function() { + // Make our api call + const now = parseInt(new Date().getTime() / 1000); + metric.send('metrics.send.normalize', [[now, 1000]]); + + // Assert we called `client.request` with the correct `points` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; + // { body: series: [ {points: [], }, ] } + const body = call_args[2].body; + assert.equal(body.series.length, 1); + + // points = [ [<timestamp>, 1000], ] + const points = body.series[0].points; + assert(Array.isArray(points)); + assert.equal(points.length, 1); + + // point = [<timestamp>, 500] + const point = points[0]; + assert(Array.isArray(point)); + assert.deepEqual(point, [now, 1000]); + }); + + it('should properly set metric type', function() { + // Make our api call + metric.send('metrics.send.counter', 5, {type: 'count'}); + + // Assert we called `client.request` with the correct `points` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; + // { body: series: [ {points: [], }, ] } + const body = call_args[2].body; + assert.equal(body.series.length, 1); + + // Assert we have only 1 series + // series = [ {metric: "", ...}, ... ] + const series = body.series; + assert(Array.isArray(series)); + assert.equal(series.length, 1); + + // Assert the first series is properly formatted + // first_series = {metric: "", type: "count", points: [], ...} + const first_series = series[0]; + assert.equal(first_series.metric, 'metrics.send.counter'); + assert(first_series.hasOwnProperty('type')); + assert.equal(first_series.type, 'count'); }); - describe("#send", function(){ - it("should make a valid api call", function(){ - // Make our api call - var now = parseInt(new Date().getTime() / 1000); - metric.send("metric.send", [[now, 500]]); - - // Assert we properly called `client.request` - assert(stub_request.calledOnce); - var call_args = stub_request.getCall(0).args; - // Method and endpoint are correct - assert.equal(call_args[0], "POST"); - assert.equal(call_args[1], "/series"); - - // Properly formatted body - // { body: series: [ {metric: "metric.send", host: undefined, tags: undefined, type: undefined} ] } - // DEV: host/tags/type are optional and should be undefined for this case - var data = call_args[2]; - assert(data.hasOwnProperty("body")); - assert(data.body.hasOwnProperty("series")); - - // Assert we have only 1 series - // series = [ {metric: "", ...}, ... ] - var series = data.body.series; - assert(Array.isArray(series)); - assert.equal(series.length, 1); - - // Assert the first series is properly formatted - // first_series = {metric: "", points: [], ...} - var first_series = series[0] - assert.equal(first_series.metric, "metric.send"); - assert(Array.isArray(first_series.points)); - assert.deepEqual(first_series.points, [[now, 500]]); - - // These keys are optional and should be set, but undefined - assert(first_series.hasOwnProperty("host")); - assert.equal(first_series.host, undefined); - assert(first_series.hasOwnProperty("tags")); - assert.equal(first_series.tags, undefined); - assert(first_series.hasOwnProperty("type")); - assert.equal(first_series.type, undefined); - }); - - it("should properly normalize values to points", function(){ - // Make our api call - metric.send("metrics.send.normalize", 500); - - // Assert we called `client.request` with the correct `points` - assert(stub_request.calledOnce); - var call_args = stub_request.getCall(0).args; - // { body: series: [ {points: [], }, ] } - var body = call_args[2].body; - assert.equal(body.series.length, 1); - - // points = [ [<timestamp>, 500] ] - var points = body.series[0].points; - assert(Array.isArray(points)); - assert.equal(points.length, 1); - - // point = [<timestamp>, 500] - var point = points[0]; - assert(Array.isArray(point)); - assert.equal(point.length, 2); - assert.equal(point[1], 500); - }); - - it("should properly normalize array of values to points", function(){ - // Make our api call - metric.send("metrics.send.normalize", [500, 1000]); - - // Assert we called `client.request` with the correct `points` - assert(stub_request.calledOnce); - var call_args = stub_request.getCall(0).args; - // { body: series: [ {points: [], }, ] } - var body = call_args[2].body; - assert.equal(body.series.length, 1); - - // points = [ [<timestamp>, 500], [<timestamp>, 1000] ] - var points = body.series[0].points; - assert(Array.isArray(points)); - assert.equal(points.length, 2); - - // point = [<timestamp>, 500] - var point = points[0]; - assert(Array.isArray(point)); - assert.equal(point.length, 2); - assert.equal(point[1], 500); - - // point = [<timestamp>, 1000] - point = points[1]; - assert(Array.isArray(point)); - assert.equal(point.length, 2); - assert.equal(point[1], 1000); - }); - - it("should not normalize correctly formatted points", function(){ - // Make our api call - var now = parseInt(new Date().getTime() / 1000); - metric.send("metrics.send.normalize", [[now, 1000]]); - - // Assert we called `client.request` with the correct `points` - assert(stub_request.calledOnce); - var call_args = stub_request.getCall(0).args; - // { body: series: [ {points: [], }, ] } - var body = call_args[2].body; - assert.equal(body.series.length, 1); - - // points = [ [<timestamp>, 1000], ] - var points = body.series[0].points; - assert(Array.isArray(points)); - assert.equal(points.length, 1); - - // point = [<timestamp>, 500] - var point = points[0]; - assert(Array.isArray(point)); - assert.deepEqual(point, [now, 1000]); - }); - - it("should properly set metric type", function(){ - // Make our api call - metric.send("metrics.send.counter", 5, {type: "count"}); - - // Assert we called `client.request` with the correct `points` - assert(stub_request.calledOnce); - var call_args = stub_request.getCall(0).args; - // { body: series: [ {points: [], }, ] } - var body = call_args[2].body; - assert.equal(body.series.length, 1); - - // Assert we have only 1 series - // series = [ {metric: "", ...}, ... ] - var series = body.series; - assert(Array.isArray(series)); - assert.equal(series.length, 1); - - // Assert the first series is properly formatted - // first_series = {metric: "", type: "count", points: [], ...} - var first_series = series[0] - assert.equal(first_series.metric, "metrics.send.counter"); - assert(first_series.hasOwnProperty("type")); - assert.equal(first_series.type, "count"); - }); - - it("should properly set convert metric_type to type", function(){ - // Make our api call - metric.send("metrics.send.counter", 5, {metric_type: "count"}); - - // Assert we called `client.request` with the correct `points` - assert(stub_request.calledOnce); - var call_args = stub_request.getCall(0).args; - // { body: series: [ {points: [], }, ] } - var body = call_args[2].body; - assert.equal(body.series.length, 1); - - - // Assert we have only 1 series - // series = [ {metric: "", ...}, ... ] - var series = body.series; - assert(Array.isArray(series)); - assert.equal(series.length, 1); - - // Assert the first series is properly formatted - // first_series = {metric: "", type: "count", points: [], ...} - var first_series = series[0] - assert.equal(first_series.metric, "metrics.send.counter"); - assert(first_series.hasOwnProperty("type")); - assert.equal(first_series.type, "count"); - }); + + it('should properly set convert metric_type to type', function() { + // Make our api call + metric.send('metrics.send.counter', 5, {metric_type: 'count'}); + + // Assert we called `client.request` with the correct `points` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; + // { body: series: [ {points: [], }, ] } + const body = call_args[2].body; + assert.equal(body.series.length, 1); + + // Assert we have only 1 series + // series = [ {metric: "", ...}, ... ] + const series = body.series; + assert(Array.isArray(series)); + assert.equal(series.length, 1); + + // Assert the first series is properly formatted + // first_series = {metric: "", type: "count", points: [], ...} + const first_series = series[0]; + assert.equal(first_series.metric, 'metrics.send.counter'); + assert(first_series.hasOwnProperty('type')); + assert.equal(first_series.type, 'count'); + }); + }); + + describe('#send_all', function() { + it('should make a valid api call', function() { + // Make our api call + const now = parseInt(new Date().getTime() / 1000); + const metrics = [ + { + metric: 'metric.send_all', + points: [[now, 500]] + } + ]; + metric.send_all(metrics); + + // Assert we properly called `client.request` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; + // Method and endpoint are correct + assert.equal(call_args[0], 'POST'); + assert.equal(call_args[1], '/series'); + + // Properly formatted body + // { body: series: [ {metric: "metric.send_all", host: undefined, tags: undefined, type: undefined} ] } + // DEV: host/tags/type are optional and should be undefined for this case + const data = call_args[2]; + assert(data.hasOwnProperty('body')); + assert(data.body.hasOwnProperty('series')); + + // Assert we have only 1 series + // series = [ {metric: "", ...}, ... ] + const series = data.body.series; + assert(Array.isArray(series)); + assert.equal(series.length, 1); + + // Assert the first series is properly formatted + // first_series = {metric: "", points: [], ...} + const first_series = series[0]; + assert.equal(first_series.metric, 'metric.send_all'); + assert(Array.isArray(first_series.points)); + assert.deepEqual(first_series.points, [[now, 500]]); + }); + + it('should properly normalize metric points', function() { + // Make our api call + const now = parseInt(new Date().getTime() / 1000); + const metrics = [ + { + metric: 'metric.send_all.normalize', + points: [[now, 500]] + }, + { + metric: 'metric.send_all.normalize', + points: [500, 1000] + }, + { + metric: 'metric.send_all.normalize', + points: 1000 + } + ]; + metric.send_all(metrics); + + // Assert we called `client.request` with the correct `points` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; + // { body: series: [ {points: [], }, ] } + const body = call_args[2].body; + assert.equal(body.series.length, 3); + + // points = [ [<timestamp>, 500] ] + let points = body.series[0].points; + assert(Array.isArray(points)); + assert.equal(points.length, 1); + assert.equal(points[0].length, 2); + assert.equal(points[0][1], 500); + + // points = [ [<timestamp>, 500], [<timestamp>, 1000] ] + points = body.series[1].points; + assert(Array.isArray(points)); + assert.equal(points.length, 2); + assert.equal(points[0].length, 2); + assert.equal(points[0][1], 500); + assert.equal(points[1].length, 2); + assert.equal(points[1][1], 1000); + + // points = [ [<timestamp>, 1000] ] + points = body.series[2].points; + assert(Array.isArray(points)); + assert.equal(points.length, 1); + assert.equal(points[0].length, 2); + assert.equal(points[0][1], 1000); + }); + + it('should properly send metric type', function() { + // Make our api call + const metrics = [ + { + metric: 'metric.send.counter', + points: 5, + type: 'count' + } + ]; + metric.send_all(metrics); + + // Assert we properly called `client.request` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; + // Method and endpoint are correct + assert.equal(call_args[0], 'POST'); + assert.equal(call_args[1], '/series'); + + // Properly formatted body + // { body: series: [ {metric: "metric.send.counter", host: undefined, tags: undefined, type: "count"} ] } + // DEV: host/tags are optional and should be undefined for this case + const data = call_args[2]; + assert(data.hasOwnProperty('body')); + assert(data.body.hasOwnProperty('series')); + + // Assert we have only 1 series + // series = [ {metric: "", ...}, ... ] + const series = data.body.series; + assert(Array.isArray(series)); + assert.equal(series.length, 1); + + // Assert the first series is properly formatted + // first_series = {metric: "", type: "count", points: [], ...} + const first_series = series[0]; + assert.equal(first_series.metric, 'metric.send.counter'); + assert(Array.isArray(first_series.points)); + assert.deepEqual(first_series.type, 'count'); }); - describe("#send_all", function(){ - it("should make a valid api call", function(){ - // Make our api call - var now = parseInt(new Date().getTime() / 1000); - var metrics = [ - { - metric: "metric.send_all", - points: [[now, 500]] - } - ]; - metric.send_all(metrics); - - // Assert we properly called `client.request` - assert(stub_request.calledOnce); - var call_args = stub_request.getCall(0).args; - // Method and endpoint are correct - assert.equal(call_args[0], "POST"); - assert.equal(call_args[1], "/series"); - - // Properly formatted body - // { body: series: [ {metric: "metric.send_all", host: undefined, tags: undefined, type: undefined} ] } - // DEV: host/tags/type are optional and should be undefined for this case - var data = call_args[2]; - assert(data.hasOwnProperty("body")); - assert(data.body.hasOwnProperty("series")); - - // Assert we have only 1 series - // series = [ {metric: "", ...}, ... ] - var series = data.body.series; - assert(Array.isArray(series)); - assert.equal(series.length, 1); - - // Assert the first series is properly formatted - // first_series = {metric: "", points: [], ...} - var first_series = series[0] - assert.equal(first_series.metric, "metric.send_all"); - assert(Array.isArray(first_series.points)); - assert.deepEqual(first_series.points, [[now, 500]]); - }); - - it("should properly normalize metric points", function(){ - // Make our api call - var now = parseInt(new Date().getTime() / 1000); - var metrics = [ - { - metric: "metric.send_all.normalize", - points: [[now, 500]] - }, - { - metric: "metric.send_all.normalize", - points: [500, 1000] - }, - { - metric: "metric.send_all.normalize", - points: 1000 - } - ]; - metric.send_all(metrics); - - // Assert we called `client.request` with the correct `points` - assert(stub_request.calledOnce); - var call_args = stub_request.getCall(0).args; - // { body: series: [ {points: [], }, ] } - var body = call_args[2].body; - assert.equal(body.series.length, 3); - - // points = [ [<timestamp>, 500] ] - var points = body.series[0].points; - assert(Array.isArray(points)); - assert.equal(points.length, 1); - assert.equal(points[0].length, 2); - assert.equal(points[0][1], 500); - - // points = [ [<timestamp>, 500], [<timestamp>, 1000] ] - points = body.series[1].points; - assert(Array.isArray(points)); - assert.equal(points.length, 2); - assert.equal(points[0].length, 2); - assert.equal(points[0][1], 500); - assert.equal(points[1].length, 2); - assert.equal(points[1][1], 1000); - - // points = [ [<timestamp>, 1000] ] - points = body.series[2].points; - assert(Array.isArray(points)); - assert.equal(points.length, 1); - assert.equal(points[0].length, 2); - assert.equal(points[0][1], 1000); - }); - - it("should properly send metric type", function(){ - // Make our api call - var metrics = [ - { - metric: "metric.send.counter", - points: 5, - type: "count" - } - ]; - metric.send_all(metrics); - - // Assert we properly called `client.request` - assert(stub_request.calledOnce); - var call_args = stub_request.getCall(0).args; - // Method and endpoint are correct - assert.equal(call_args[0], "POST"); - assert.equal(call_args[1], "/series"); - - // Properly formatted body - // { body: series: [ {metric: "metric.send.counter", host: undefined, tags: undefined, type: "count"} ] } - // DEV: host/tags are optional and should be undefined for this case - var data = call_args[2]; - assert(data.hasOwnProperty("body")); - assert(data.body.hasOwnProperty("series")); - - // Assert we have only 1 series - // series = [ {metric: "", ...}, ... ] - var series = data.body.series; - assert(Array.isArray(series)); - assert.equal(series.length, 1); - - // Assert the first series is properly formatted - // first_series = {metric: "", type: "count", points: [], ...} - var first_series = series[0] - assert.equal(first_series.metric, "metric.send.counter"); - assert(Array.isArray(first_series.points)); - assert.deepEqual(first_series.type, "count"); - }); - - it("should properly send metric_type as type", function(){ - // Make our api call - var metrics = [ - { - metric: "metric.send.counter", - points: 5, - metric_type: "count" - } - ]; - metric.send_all(metrics); - - // Assert we properly called `client.request` - assert(stub_request.calledOnce); - var call_args = stub_request.getCall(0).args; - // Method and endpoint are correct - assert.equal(call_args[0], "POST"); - assert.equal(call_args[1], "/series"); - - // Properly formatted body - // { body: series: [ {metric: "metric.send.counter", host: undefined, tags: undefined, type: "count"} ] } - // DEV: host/tags are optional and should be undefined for this case - var data = call_args[2]; - assert(data.hasOwnProperty("body")); - assert(data.body.hasOwnProperty("series")); - - // Assert we have only 1 series - // series = [ {metric: "", ...}, ... ] - var series = data.body.series; - assert(Array.isArray(series)); - assert.equal(series.length, 1); - - // Assert the first series is properly formatted - // first_series = {metric: "", type: "count", points: [], ...} - var first_series = series[0] - assert.equal(first_series.metric, "metric.send.counter"); - assert(Array.isArray(first_series.points)); - assert.deepEqual(first_series.type, "count"); - }); + it('should properly send metric_type as type', function() { + // Make our api call + const metrics = [ + { + metric: 'metric.send.counter', + points: 5, + metric_type: 'count' + } + ]; + metric.send_all(metrics); + + // Assert we properly called `client.request` + assert(stub_request.calledOnce); + const call_args = stub_request.getCall(0).args; + // Method and endpoint are correct + assert.equal(call_args[0], 'POST'); + assert.equal(call_args[1], '/series'); + + // Properly formatted body + // { body: series: [ {metric: "metric.send.counter", host: undefined, tags: undefined, type: "count"} ] } + // DEV: host/tags are optional and should be undefined for this case + const data = call_args[2]; + assert(data.hasOwnProperty('body')); + assert(data.body.hasOwnProperty('series')); + + // Assert we have only 1 series + // series = [ {metric: "", ...}, ... ] + const series = data.body.series; + assert(Array.isArray(series)); + assert.equal(series.length, 1); + + // Assert the first series is properly formatted + // first_series = {metric: "", type: "count", points: [], ...} + const first_series = series[0]; + assert.equal(first_series.metric, 'metric.send.counter'); + assert(Array.isArray(first_series.points)); + assert.deepEqual(first_series.type, 'count'); }); + }); }); diff --git a/test/json.js b/test/json.js index a1e2dff..73ba430 100644 --- a/test/json.js +++ b/test/json.js @@ -1,26 +1,26 @@ -var assert = require("assert"); -var BigNumber = require("bignumber.js"); -var json = require("../lib/json"); +const assert = require('assert'); +const BigNumber = require('bignumber.js'); +const json = require('../lib/json'); -describe("json", function(){ - describe("#parse()", function(){ - it("should properly parse big integers", function(){ - // DEV: This test case is from: https://github.com/brettlangdon/node-dogapi/issues/16 - var data = "{\"id\": 2868860079149422351}"; - var parsed = json.parse(data); - // `parsed.id` is an instance of `BigNumber` - assert.equal(parsed.id.toString(), "2868860079149422351"); - }); +describe('json', function() { + describe('#parse()', function() { + it('should properly parse big integers', function() { + // DEV: This test case is from: https://github.com/brettlangdon/node-dogapi/issues/16 + const data = '{"id": 2868860079149422351}'; + const parsed = json.parse(data); + // `parsed.id` is an instance of `BigNumber` + assert.equal(parsed.id.toString(), '2868860079149422351'); }); + }); - describe("#stringify()", function(){ - it("should properly parse big integers", function(){ - // DEV: This test case is from: https://github.com/brettlangdon/node-dogapi/issues/16 - var data = {"id": new BigNumber('2868860079149422351')}; - var stringified = json.stringify(data); - // Yeah, it ends up being a string and not an int, but mostly we - // want to make sure it doesn't throw an error or provide the wrong number - assert.equal(stringified, "{\"id\":\"2868860079149422351\"}"); - }); + describe('#stringify()', function() { + it('should properly parse big integers', function() { + // DEV: This test case is from: https://github.com/brettlangdon/node-dogapi/issues/16 + const data = {id: new BigNumber('2868860079149422351')}; + const stringified = json.stringify(data); + // Yeah, it ends up being a string and not an int, but mostly we + // want to make sure it doesn't throw an error or provide the wrong number + assert.equal(stringified, '{"id":"2868860079149422351"}'); }); + }); });