Browse Source

Merge pull request #34 from joshkurz/embed-api

Embed api
pull/40/head
Brett Langdon 10 years ago
parent
commit
4f793515f5
5 changed files with 259 additions and 158 deletions
  1. +170
    -0
      lib/api/embed.js
  2. +4
    -84
      lib/api/graph.js
  3. +1
    -0
      lib/api/index.js
  4. +84
    -0
      test/api/embed.js
  5. +0
    -74
      test/api/graph.js

+ 170
- 0
lib/api/embed.js View File

@ -0,0 +1,170 @@
var client = require("../client");
var extend = require("extend");
var json = require("../json");
var querystring = require("querystring");
/*section: embed
*comment: create an embed graph of a metric query
*params:
* graph_json: The request array to pass create in the embed
* options: optional, object of extra parameters to pass to the embed create (see options[*] params)
* options["timeframe"]: optional, one of ("1_hour", "4_hours", "1_day", "2_days", and "1_week")
* options["size"]: optional, one of ("small", "medium", "large", "xlarge")
* options["legend"]: optional, "yes" or "no"
* options["title"]: optional, the title of the embed
* callback: function(err, res)
*example: |
* ```javascript
* var dogapi = require("dogapi");
* var options = {
* api_key: "api_key",
* app_key: "app_key"
* };
* dogapi.initialize(options);
* var query = "system.cpu.idle{*}";
* var graphJSON = {
* viz: "timeseries",
* requests: [
* {
* q: query,
* aggregator: "avg",
* conditional_formats: [],
* type: "area"
* }
* ]
* }
* var options = {
* timeframe: "1_hour",
* size: "xlarge",
* legend: "yes",
* title: "my awesome embed"
* };
* dogapi.embed.create(graphJSON, options, function(err, res){
* console.dir(res);
* });
* ```
*/
function create(graphJSON, options, callback){
if(callback === undefined && typeof options === "function"){
callback = options;
options = {};
}
var 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
var params = {
body: querystring.stringify(body),
contentType: "application/x-www-form-urlencoded"
};
client.request("POST", "/graph/embed", params, callback);
}
/*section: embed
*comment: delete an embed with a specific id
*params:
* embedId: the id of the embed to delete
* callback: function(err, res)
*example: |
* ```javascript
* var embedid = "foo";
* dogapi.embed.revoke(embedid, function(err, res){
* console.dir(res);
* });
* ```
*/
function revoke(embedId, callback){
client.request("GET", "/graph/embed/" + embedId + "/revoke", callback);
}
/*section: embed
*comment: get all embeds from datadog
*params:
* callback: function(err, res)
*example: |
* ```javascript
* dogapi.embed.getAll(function(err, res){
* console.dir(res);
* });
* ```
*/
function getAll(callback) {
client.request("GET", "/graph/embed", callback);
}
/*section: embed
*comment: get a single embed
*params:
* embedId: the id of the embed to get
* callback: function(err, res)
*example: |
* ```javascript
* var embedId = "foo";
* dogapi.embed.get(embedId, function(err, res){
* console.dir(res);
* });
* ```
*/
function get(embedId, callback) {
client.request("GET", "/graph/embed/" + embedId, callback);
}
module.exports = {
create: create,
revoke: revoke,
getAll: getAll,
get: get,
getUsage: function(){
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: 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"
];
},
handleCli: function(subcommand, args, callback) {
if (args._.length > 4 && subcommand === "create") {
var graph_json = json.parse(args._[4]);
var 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") {
var embedId = args._[4];
revoke(embedId, callback);
} else if (args._.length > 4 && subcommand === "get") {
var 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);
}
}
};

+ 4
- 84
lib/api/graph.js View File

@ -1,7 +1,5 @@
var client = require("../client");
var extend = require("extend");
var json = require("../json");
var querystring = require("querystring");
var embed = require("./embed");
/*section: graph
*comment: take a snapshot of a metric query
@ -46,75 +44,12 @@ function snapshot(query, from, to, eventQuery, callback){
client.request("GET", "/graph/snapshot", params, callback);
}
/*section: graph
*comment: create an embed graph of a metric query
*params:
* graph_json: The request array to pass create in the embed
* options: optional, object of extra parameters to pass to the embed create (see options[*] params)
* options["timeframe"]: optional, one of ("1_hour", "4_hours", "1_day", "2_days", and "1_week")
* options["size"]: optional, one of ("small", "medium", "large", "xlarge")
* options["legend"]: optional, "yes" or "no"
* options["title"]: optional, the title of the embed
* callback: function(err, res)
*example: |
* ```javascript
* var dogapi = require("dogapi");
* var options = {
* api_key: "api_key",
* app_key: "app_key"
* };
* dogapi.initialize(options);
* var query = "system.cpu.idle{*}";
* var graphJSON = {
* viz: "timeseries",
* requests: [
* {
* q: query,
* aggregator: "avg",
* conditional_formats: [],
* type: "area"
* }
* ]
* }
* var options = {
* timeframe: "1_hour",
* size: "xlarge",
* legend: "yes",
* title: "my awesome embed"
* };
* dogapi.graph.createEmbed(graphJSON, options, function(err, res){
* console.dir(res);
* });
* ```
*/
function createEmbed(graphJSON, options, callback){
if(callback === undefined && typeof options === 'function'){
callback = options;
options = {};
}
var 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
var params = {
body: querystring.stringify(body),
contentType: "application/x-www-form-urlencoded"
};
client.request("POST", "/graph/embed", params, callback);
}
module.exports = {
snapshot: snapshot,
createEmbed: createEmbed,
createEmbed: embed.create,
getUsage: function(){
return [
" dogapi graph snapshot <query> <from> <to> [--events <event-query>]",
" dogapi graph create_embed <graph_json> [--timeframe <timeframe>] [--size <size>] [--legend <legend>] [--title <title>]"
" dogapi graph snapshot <query> <from> <to> [--events <event-query>]"
];
},
getHelp: function(){
@ -122,14 +57,8 @@ module.exports = {
"Graph:",
" Subcommands:",
" snapshot <query> <from> <to> --events <event-query> | take a snapshot of a graph",
" create_embed <graph_json> --timeframe <timeframe> --size <size> --legend <legend> --title <title> | create a new graph embed",
" 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 graph 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"
" --events <event-query> a query for event bands to add to the snapshot"
];
},
handleCli: function(subcommand, args, callback){
@ -139,15 +68,6 @@ module.exports = {
var to = parseInt(args._[6]);
var eventQuery = args["events"];
snapshot(query, from, to, eventQuery, callback);
} else if (args._.length > 4 && subcommand === "create_embed") {
var graph_json = json.parse(args._[4]);
var options = {
timeframe: args["timeframe"],
size: args["size"],
legend: args["legend"],
title: args["title"]
};
createEmbed(graph_json, options, callback);
} else {
callback("unknown subcommand or arguments try `dogapi graph --help` for help", false);
}


+ 1
- 0
lib/api/index.js View File

@ -1,6 +1,7 @@
var api = {
comment: require("./comment"),
downtime: require("./downtime"),
embed: require("./embed"),
event: require("./event"),
graph: require("./graph"),
host: require("./host"),


+ 84
- 0
test/api/embed.js View File

@ -0,0 +1,84 @@
var assert = require("assert");
var client = require("../../lib/client");
var extend = require("extend");
var embed = require("../../lib/api/embed");
var sinon = require("sinon");
var querystring = require("querystring");
describe("api/embed", function(){
var 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(){
var graphJSON = {
viz: "timeseries",
requests: [
{
q: "system.cpu.idle{*}"
}
]
};
var options = {
timeframe: "1_hour",
size: "large",
legend: "yes",
title: "test graph embed"
};
// Make our api call
embed.create(graphJSON, options);
// 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], "/graph/embed");
// Properly formatted body and content-type
var params = call_args[2];
var 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(){
var graphJSON = {
viz: "timeseries",
requests: [
{
q: "system.cpu.idle{*}"
}
]
};
// Make our api call
embed.create(graphJSON);
// Assert we properly called `client.request`
assert(stub_request.calledOnce);
var call_args = stub_request.getCall(0).args;
// Properly formatted body
var params = call_args[2];
var expectedBody = {
graph_json: JSON.stringify(graphJSON)
};
assert.deepEqual(querystring.parse(params.body), expectedBody);
});
});
});

+ 0
- 74
test/api/graph.js View File

@ -6,79 +6,5 @@ var sinon = require("sinon");
var querystring = require("querystring");
describe("api/graph", function(){
var 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("#creaetEmbed", function(){
it("should make a valid api call", function(){
var graphJSON = {
viz: "timeseries",
requests: [
{
q: "system.cpu.idle{*}"
}
]
};
var options = {
timeframe: "1_hour",
size: "large",
legend: "yes",
title: "test graph embed"
};
// Make our api call
graph.createEmbed(graphJSON, options);
// 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], "/graph/embed");
// Properly formatted body and content-type
var params = call_args[2];
var 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(){
var graphJSON = {
viz: "timeseries",
requests: [
{
q: "system.cpu.idle{*}"
}
]
};
// Make our api call
graph.createEmbed(graphJSON);
// Assert we properly called `client.request`
assert(stub_request.calledOnce);
var call_args = stub_request.getCall(0).args;
// Properly formatted body
var params = call_args[2];
var expectedBody = {
graph_json: JSON.stringify(graphJSON)
};
assert.deepEqual(querystring.parse(params.body), expectedBody);
});
});
});

Loading…
Cancel
Save