| @ -1,216 +1,143 @@ | |||
| var extend = require('extend'); | |||
| var util = require('util'); | |||
| var event_api = function(){}; | |||
| event_api.prototype.stream = function(start, end, filter, callback){ | |||
| /* | |||
| * event_api.stream( start, end, [[filter], callback] ) | |||
| * | |||
| * function used to retrieve all events that have occured between | |||
| * `start` and `end` (POSIX timestamps) | |||
| * | |||
| * optionally filter the query with `filter`: | |||
| * { | |||
| * 'priority': ("low" or "normal"), | |||
| * 'sources': ["sources", "as", "a", "list"], | |||
| * 'tags': ["tags", "as", "a", "list"], | |||
| * } | |||
| * | |||
| * optionally provide a `callback` function to get the result | |||
| * of this api call: | |||
| * callback(error, result, status_code) | |||
| */ | |||
| if(arguments.length < 2){ | |||
| throw new Error('parameters `start` and `end` are required'); | |||
| } | |||
| query = { | |||
| start: parseInt(start), | |||
| end: parseInt(end), | |||
| var client = require("../client"); | |||
| var util = require("util"); | |||
| /*section: event | |||
| *comment: | | |||
| * create a new event | |||
| *params: | |||
| * title: the title of the event | |||
| * text: the body of the event | |||
| * properties: | | |||
| * an optional object continaing any of the following additional optional properties | |||
| * * date_happened: POSIX timestamp of when it happened | |||
| * * priority: "normal" or "low" [defualt: "normal"] | |||
| * * host: the host name to associate with the event | |||
| * * tags: array of "tag:value"'s to associate with the event | |||
| * * alert_type: "error", "warning", "info" or "success" [defualt: "info"] | |||
| * * aggregation_key: an arbitrary string used to aggregate like events | |||
| * * source_type_name: options: "nagios", "hudson", "jenkins", "user", "my apps", "feed", "chef", "puppet", "git", "bitbucket", "fabric", "capistrano" | |||
| * 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 = "some new event"; | |||
| * var text = "IT HAPPENED!"; | |||
| * dogapi.event.create(title, text, function(err, res){ | |||
| * console.dir(res); | |||
| * }); | |||
| * title = "another event"; | |||
| * text = "IT HAPPENED AGAIN!"; | |||
| * var properties = { | |||
| * tags: ["some:tag"], | |||
| * alert_type: "error" | |||
| * }; | |||
| * dogapi.event.create(title, text, properties, function(err, res){ | |||
| * console.dir(res); | |||
| * }); | |||
| * ``` | |||
| */ | |||
| function create(title, text, properties, callback){ | |||
| if(arguments.length < 4 && typeof arguments[2] === "function"){ | |||
| callback = properties; | |||
| properties = {}; | |||
| } | |||
| if(typeof properties !== "object"){ | |||
| properties = {}; | |||
| } | |||
| properties.title = tile; | |||
| properties.text = text; | |||
| var params = { | |||
| body: properties | |||
| }; | |||
| // this is the only case we have to check | |||
| // if we have `event_api(1234, 5678, callback)` then | |||
| // we want to push callback back | |||
| if(arguments.length == 3 && typeof arguments[2] == 'function'){ | |||
| callback = arguments[2]; | |||
| filter = {}; | |||
| } | |||
| // validate the filters we were given and append to `query` | |||
| // if they exist and meet their requirements | |||
| if(filter['priority'] && ['low', 'normal'].indexOf(filter['priority'].toLowerCase()) >= 0){ | |||
| query['priority'] = filter['priority'].toLowerCase(); | |||
| } | |||
| if(filter['sources'] && typeof filter['sources'] == 'object'){ | |||
| query['sources'] = filter['sources'].join(); | |||
| } | |||
| if(filter['tags'] && typeof filter['tags'] == 'object'){ | |||
| query['tags'] = filter['tags'].join(); | |||
| } | |||
| params = { | |||
| query: query, | |||
| client.request("POST", "/events", params, callback); | |||
| } | |||
| /*section: event | |||
| *comment: | | |||
| * get event details from the provided event id | |||
| *params: | |||
| * eventId: | | |||
| * the id of the event to fetch | |||
| * callback: | | |||
| * function(err, res) | |||
| *example: | | |||
| * ```javascript | |||
| * var dogapi = require("dogapi"); | |||
| * var options = { | |||
| * api_key: "api_key", | |||
| * app_key: "app_key" | |||
| * }; | |||
| * dogapi.initialize(options); | |||
| * dogapi.event.get(10005, function(err, res){ | |||
| * console.dir(res); | |||
| * }); | |||
| * ``` | |||
| */ | |||
| function get(eventId, callback){ | |||
| client.request("GET", util.format("/events/%s", eventId), callback); | |||
| } | |||
| /*section: event | |||
| *comment: | | |||
| * query the event stream | |||
| *params: | |||
| * start: POSIX timestamp for start of query | |||
| * end: POSIX timestamp for end of query | |||
| * parameters: | | |||
| * optional parameters to use for the query | |||
| * * priority: "low" or "normal" | |||
| * * sources: comma separated list of sources (e.g. "jenkins,user") | |||
| * * tags: comma separated list of tags (e.g. "tag:value1,tag:value2") | |||
| * callback: | | |||
| * function(err, res) | |||
| *example: | | |||
| * ```javascript | |||
| * var dogapi = require("dogapi"); | |||
| * var options = { | |||
| * api_key: "api_key", | |||
| * app_key: "app_key" | |||
| * }; | |||
| * dogapi.initialize(options); | |||
| * var now = parseInt(new Date().getTime() / 1000); | |||
| * var then = now - 3600; // an hour ago | |||
| * var parameters = { | |||
| * tags: "some:tag", | |||
| * sources: "jenkins" | |||
| * }; | |||
| * dogapi.event.query(then, now, parameters, function(err, res){ | |||
| * console.dir(res); | |||
| * }); | |||
| * ``` | |||
| */ | |||
| 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; | |||
| var params = { | |||
| query: parameters | |||
| }; | |||
| this.request('GET', '/events', params, callback); | |||
| }; | |||
| event_api.prototype.polling_stream = function(interval, filter, callback){ | |||
| /* | |||
| * event_api.polling_stream(interval, [[filter], callback] ) | |||
| * | |||
| * function used to continuously call `stream` for new events | |||
| * | |||
| * `interval` seconds between each call | |||
| * `filter` is an object to limit the results by | |||
| * `callback` is an optional function called with the results of the api call | |||
| * callback(error, result, status_code) | |||
| */ | |||
| if(arguments.length < 3 && typeof arguments[1] == 'function'){ | |||
| callback = arguments[1]; | |||
| filter = {}; | |||
| } | |||
| if(typeof filter != 'object'){ | |||
| throw new Error('`filter` parameter must be an object'); | |||
| } | |||
| var last_run = new Date().getTime() / 1000; | |||
| var self = this; | |||
| setInterval(function(){ | |||
| var start = last_run; | |||
| last_run = new Date().getTime() / 1000; | |||
| self.stream(start, last_run, filter, callback); | |||
| }, interval * 1000); | |||
| }; | |||
| event_api.prototype.get_event = function(event_id, callback){ | |||
| /* | |||
| * event_api.get_event(event_id, callback) | |||
| * | |||
| * method used to retrieve a single event's data | |||
| * | |||
| * `event_id` the id of the event to get the information for | |||
| * `callback` an optional function called with the results | |||
| * callback(error, result, status_code) | |||
| */ | |||
| if(!event_id){ | |||
| throw new Error('`event_id` parameter is required'); | |||
| } | |||
| client.request("GET", "/events", params, callback); | |||
| } | |||
| this.request('GET', util.format('/events/%s', event_id), callback); | |||
| module.exports = { | |||
| create: create, | |||
| get: get, | |||
| query: query | |||
| }; | |||
| event_api.prototype.add_event = function(event, callback){ | |||
| /* | |||
| * event_api.add_event(event, callback) | |||
| * | |||
| * method used to add a new event to datadog | |||
| * | |||
| * `event` is an object containing any of the following: | |||
| * title: *required*, string, title for the new event | |||
| * text: *required*, string, event message | |||
| * date_happened: int, when the event occurred. if unset defaults to the current time. (POSIX timestamp) | |||
| * handle: string, user to post the event as. defaults to owner of the application key used to submit. | |||
| * priority: string, priority to post the event as. ("normal" or "low", defaults to "normal") | |||
| * related_event_id: post event as a child of the given event | |||
| * tags: array, tags to post the event with | |||
| * host: string, host to post the event with | |||
| * device_name: string, device_name to post the event with | |||
| * aggregation_ket: string, key to aggregate this event on | |||
| * | |||
| * `callback` is an optional function for the result | |||
| * callback(error, result, status_code) | |||
| */ | |||
| if(typeof event != 'object'){ | |||
| throw new Error('`event` parameter must be an object'); | |||
| } | |||
| if(!event['title']){ | |||
| throw new Error('`title` property of `event` parameter is required'); | |||
| } | |||
| if(!event['text']){ | |||
| throw new Error('`text` property of `event` parameter is required'); | |||
| } | |||
| if(!event['date_happened']){ | |||
| event['date_happened'] = Math.round((new Date()).getTime() / 1000); | |||
| } | |||
| if(event['priority']){ | |||
| if(['normal', 'low'].indexOf(event['priority'].toLowerCase()) == -1){ | |||
| event['priority'] = undefined; | |||
| } | |||
| } | |||
| if(event['tags']){ | |||
| event['tags'] = event['tags'].join(); | |||
| } | |||
| this.request('POST', '/events', {body: event}, callback); | |||
| }; | |||
| event_api.prototype.add_comment = function(comment, callback){ | |||
| /* | |||
| * event_api.add_comment(comment, [callback]) | |||
| * | |||
| * method used to add a new comment to datadog | |||
| * | |||
| * `comment` is a object representation of the new comment: | |||
| * message: *require*, string, comment text body | |||
| * handle: string, optional handle name to use instead of owner of the app key | |||
| * related_event_id: string, optional event id to attach this comment to | |||
| * | |||
| * `callback` is an optional function for the result of the call | |||
| * callback(error, result, status_code) | |||
| */ | |||
| if(typeof comment != 'object'){ | |||
| throw new Error('`comment` parameter must be an object'); | |||
| } | |||
| if(!comment['message']){ | |||
| throw new Error('`message` property of `comment` paramter is required'); | |||
| } | |||
| this.request('POST', '/comments', {body: comment}, callback); | |||
| }; | |||
| event_api.prototype.update_comment = function(comment_id, comment, callback){ | |||
| /* | |||
| * event_api.update_comment(comment_id, comment, callback) | |||
| * | |||
| * method used to update a comment that already exists in datadog | |||
| * | |||
| * `comment_id` is the id of the comment to update | |||
| * `comment` an object representation of the comment changes to make | |||
| * message: string, the new message text for the comment | |||
| * handle: string, the new owner of the comment | |||
| * | |||
| * `callback` an optional callback to call with the result | |||
| * callback(error, result, status_code) | |||
| */ | |||
| if(typeof comment != 'object'){ | |||
| throw new Error('`comment` parameter must be an object'); | |||
| } | |||
| this.request('PUT', util.format('/comments/%s', comment_id), {body: comment}, callback); | |||
| }; | |||
| event_api.prototype.delete_comment = function(comment_id, callback){ | |||
| /* | |||
| * event_api.delete_comment(comment_id, callback) | |||
| * | |||
| * method used to remove a comment from datadog | |||
| * | |||
| * `comment_id` the comment id for the comment to remove | |||
| * `callback` an optional function to handle the result | |||
| * callback(error, result, status_code) | |||
| * | |||
| */ | |||
| this.request('DELETE', util.format('/comments/%s', comment_id), callback); | |||
| }; | |||
| return module.exports = event_api; | |||