From 706c72f11e81647f68a5fe2518bea648db0c746f Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Thu, 26 Mar 2015 07:27:08 -0400 Subject: [PATCH] add start to timeboard api --- lib/api/index.js | 1 + lib/api/timeboard.js | 245 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 lib/api/timeboard.js diff --git a/lib/api/index.js b/lib/api/index.js index 4aa6891..41dec64 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -9,6 +9,7 @@ var api = { monitor: require("./monitor"), serviceCheck: require("./serviceCheck"), tag: require("./tag"), + timeboard: require("./timeboard"), user: require("./user"), }; diff --git a/lib/api/timeboard.js b/lib/api/timeboard.js new file mode 100644 index 0000000..1a28fe7 --- /dev/null +++ b/lib/api/timeboard.js @@ -0,0 +1,245 @@ +var client = require("../client"); +var util = require("util"); + +/*section: timeboard + *comment: add a new timeboard + *params: + * title: the title of the timeboard + * description: the description of the timeboard + * graphs: | + * an array of objects with the following keys + * * title: the name of the graph + * * definition: an object containing the graph definition, e.g. `{"requests": [{"q": "system.cpu.idle{*} by {host}"}` + * templateVariables: | + * optional, an array of objects with the following keys + * * name: the name of the variable + * * prefix: optional, the tag prefix for this variable + * * default: optional, the default value for this tag + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var title = "Time Keeps on Slipping"; + * var description = "Into the Future"; + * var graphs = [ + * { + * definition: { + * events: [], + * requests: [ + * {q: "avg:system.mem.free{*}"} + * ], + * viz: "timeseries" + * }, + * title: "Average Memory Free" + * } + * ]; + * var templateVariables = [ + * { + * name: "host1", + * prefix: "host", + * default: "host:my-host" + * } + * ]; + * dogapi.timeboard.create( + * title, description, graphs, templateVariables, + * function(err, res){ + * console.dir(res); + * } + * ); + * ``` + */ +function create(title, description, graphs, templateVariables, callback){ + if(arguments.length < 5 && typeof arguments[3] === "function"){ + callback = templateVariables; + templateVariables = []; + } + + var params = { + body: { + title: title, + description: description, + graphs: graphs + } + }; + if(Array.isArray(templateVariables) && templateVariables.length){ + params.body.template_variables = templateVariables; + } + + client.request("POST", "/dash", params, callback); +} + +/*section: timeboard + *comment: update an existing timeboard + *params: + * dashId: the id of the timeboard to update + * title: the title of the timeboard + * description: the description of the timeboard + * graphs: | + * an array of objects with the following keys + * * title: the name of the graph + * * definition: an object containing the graph definition, e.g. `{"requests": [{"q": "system.cpu.idle{*} by {host}"}` + * templateVariables: | + * optional, an array of objects with the following keys + * * name: the name of the variable + * * prefix: optional, the tag prefix for this variable + * * default: optional, the default value for this tag + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var title = "Time Keeps on Slipping"; + * var description = "Into the Future"; + * var graphs = [ + * { + * definition: { + * events: [], + * requests: [ + * {q: "avg:system.mem.free{*}"} + * ], + * viz: "timeseries" + * }, + * title: "Average Memory Free" + * } + * ]; + * var templateVariables = [ + * { + * name: "host1", + * prefix: "host", + * default: "host:my-host" + * } + * ]; + * dogapi.timeboard.update( + * 1234, title, description, graphs, templateVariables, + * function(err, res){ + * console.dir(res); + * } + * ); + * ``` + */ +function update(dashId, title, description, graphs, templateVariables, callback){ + if(arguments.length < 6 && typeof arguments[4] === "function"){ + callback = templateVariables; + templateVariables = []; + } + + var params = { + body: { + title: title, + description: description, + graphs: graphs + } + }; + if(Array.isArray(templateVariables) && templateVariables.length){ + params.body.template_variables = templateVariables; + } + + client.request("POST", util.format("/dash/%s", dashId), params, callback); +} + +/*section: timeboard + *comment: remove an existing timeboard + *params: + * dashId: the id of the timeboard to remove + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.timeboard.remove(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function remove(dashId, callback){ + client.request("DELETE", util.format("/dash/%s", dashId), params, callback); +} + +/*section: timeboard + *comment: get all existing timeboards + *params: + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.timeboard.getAll(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function getAll(callback){ + client.request("GET", "/dash", params, callback); +} + +/*section: timeboard + *comment: get an existing timeboard + *params: + * dashId: the id of the timeboard to get + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.timeboard.get(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function get(dashId, callback){ + client.request("GET", util.format("/dash/%s", dashId), params, callback); +} + +module.exports = { + create: create, + update: update, + remove: remove, + getAll: getAll, + get: get, + getUsage: function(){ + return [ + " dogapi timeboard get ", + " dogapi timeboard getall", + " dogapi timeboard remove ", + " dogapi timeboard create <description> <graphs> [--tmpvars <templateVariables>]", + " dogapi timeboard update <dash-id> <title> <description> <graphs> [--tmpvars <templateVariables>]", + ]; + }, + getHelp: function(){ + return [ + "Timeboard:", + " Subcommands:", + " get <dash-id>", + " getall", + " remove <dash-id>", + " create <title> <description> <graphs>", + " update <dash-id> <title> <description> <graphs>", + " Options:", + " --tmpvars <templateVariables>" + ]; + }, + handleCli: function(subcommand, args, callback){ + + } +};