Browse Source

fix some formatting issues

pull/4/head
Brett Langdon 12 years ago
parent
commit
3a267d6567
7 changed files with 105 additions and 105 deletions
  1. +7
    -7
      lib/api/alert.js
  2. +10
    -10
      lib/api/dash.js
  3. +26
    -26
      lib/api/event.js
  4. +5
    -5
      lib/api/metric.js
  5. +4
    -4
      lib/api/screen.js
  6. +25
    -25
      lib/api/tag.js
  7. +28
    -28
      lib/http_client.js

+ 7
- 7
lib/api/alert.js View File

@ -23,7 +23,7 @@ alert_api.prototype.add_alert = function(alert, callback){
} }
if(!alert['query']){ if(!alert['query']){
throw new Error('`alert["query"]` is required');
throw new Error('`alert["query"]` is required');
} }
this.request('POST', '/alert', {body: alert}, callback); this.request('POST', '/alert', {body: alert}, callback);
@ -50,7 +50,7 @@ alert_api.prototype.update_alert = function(alert_id, alert, callback){
} }
if(!alert['query']){ if(!alert['query']){
throw new Error('`alert["query"]` is required');
throw new Error('`alert["query"]` is required');
} }
this.request('PUT', util.format('/alert/%s', alert_id), {body: alert}, callback); this.request('PUT', util.format('/alert/%s', alert_id), {body: alert}, callback);
@ -67,7 +67,7 @@ alert_api.prototype.get_alert = function(alert_id, callback){
* `callback` an optional function to call with the results * `callback` an optional function to call with the results
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
this.request('GET', util.format('/alert/%s', alert_id), callback)
this.request('GET', util.format('/alert/%s', alert_id), callback);
}; };
alert_api.prototype.delete_alert = function(alert_id, callback){ alert_api.prototype.delete_alert = function(alert_id, callback){
@ -81,7 +81,7 @@ alert_api.prototype.delete_alert = function(alert_id, callback){
* `callback` an optional function to call with the results * `callback` an optional function to call with the results
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
this.request('DELETE', util.format('/alert/%s', alert_id), callback)
this.request('DELETE', util.format('/alert/%s', alert_id), callback);
}; };
alert_api.prototype.get_all_alerts = function(callback){ alert_api.prototype.get_all_alerts = function(callback){
@ -93,7 +93,7 @@ alert_api.prototype.get_all_alerts = function(callback){
* `callback` an optional function to call with the results * `callback` an optional function to call with the results
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
this.request('GET', '/alert', callback)
this.request('GET', '/alert', callback);
}; };
alert_api.prototype.mute_alerts = function(callback){ alert_api.prototype.mute_alerts = function(callback){
@ -105,7 +105,7 @@ alert_api.prototype.mute_alerts = function(callback){
* `callback` an optional function to call with the results * `callback` an optional function to call with the results
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
this.request('POST', '/mute_alerts', callback)
this.request('POST', '/mute_alerts', callback);
}; };
alert_api.prototype.unmute_alerts = function(callback){ alert_api.prototype.unmute_alerts = function(callback){
@ -117,7 +117,7 @@ alert_api.prototype.unmute_alerts = function(callback){
* `callback` an optional function to call with the results * `callback` an optional function to call with the results
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
this.request('POST', '/unmute_alerts', callback)
this.request('POST', '/unmute_alerts', callback);
}; };
return module.exports = alert_api; return module.exports = alert_api;

+ 10
- 10
lib/api/dash.js View File

@ -18,14 +18,14 @@ var validate_dashboard = function(dashboard){
} }
for(var i in dashboard['graphs']){ for(var i in dashboard['graphs']){
if(!dashboard['graphs'][i]['title']){
throw new Error(util.format('`dashboard["graphs"][%s]["title"]` is missing', i));
}
if(!dashboard['graphs'][i]['definition']){
throw new Error(util.format('`dashboard["graphs"][%s]["definition"]` is missing', i));
}
if(!dashboard['graphs'][i]['title']){
throw new Error(util.format('`dashboard["graphs"][%s]["title"]` is missing', i));
}
if(!dashboard['graphs'][i]['definition']){
throw new Error(util.format('`dashboard["graphs"][%s]["definition"]` is missing', i));
}
} }
}
};
var dash_api = function(){}; var dash_api = function(){};
@ -39,7 +39,7 @@ dash_api.prototype.get_dashboard = function(dash_id, callback){
* `callback` is an optional function to call with the results of the api call * `callback` is an optional function to call with the results of the api call
* callback(error, results, status_code) * callback(error, results, status_code)
*/ */
this.request('GET', util.format('/dash/%s', dash_id), callback)
this.request('GET', util.format('/dash/%s', dash_id), callback);
}; };
dash_api.prototype.get_all_dashboards = function(callback){ dash_api.prototype.get_all_dashboards = function(callback){
@ -51,7 +51,7 @@ dash_api.prototype.get_all_dashboards = function(callback){
* `callback` is an optional function to call with the results of the api call * `callback` is an optional function to call with the results of the api call
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
this.request('GET', '/dash', callback)
this.request('GET', '/dash', callback);
}; };
dash_api.prototype.create_dashboard = function(dashboard, callback){ dash_api.prototype.create_dashboard = function(dashboard, callback){
@ -97,7 +97,7 @@ dash_api.prototype.delete_dashboard = function(dash_id, callback){
* `callback` an optional function to call with the results of the api call * `callback` an optional function to call with the results of the api call
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
this.request('DELETE', util.format('/dash/%s', dash_id), callback)
this.request('DELETE', util.format('/dash/%s', dash_id), callback);
}; };
return module.exports = dash_api; return module.exports = dash_api;

+ 26
- 26
lib/api/event.js View File

@ -23,11 +23,11 @@ event_api.prototype.stream = function(start, end, filter, callback){
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
if(arguments.length < 2){ if(arguments.length < 2){
throw new Error('parameters `start` and `end` are required');
throw new Error('parameters `start` and `end` are required');
} }
query = { query = {
start: parseInt(start),
end: parseInt(end),
start: parseInt(start),
end: parseInt(end),
}; };
// this is the only case we have to check // this is the only case we have to check
@ -41,7 +41,7 @@ event_api.prototype.stream = function(start, end, filter, callback){
// validate the filters we were given and append to `query` // validate the filters we were given and append to `query`
// if they exist and meet their requirements // if they exist and meet their requirements
if(filter['priority'] && ['low', 'normal'].indexOf(filter['priority'].toLowerCase()) >= 0){ if(filter['priority'] && ['low', 'normal'].indexOf(filter['priority'].toLowerCase()) >= 0){
query['priority'] = filter['priority'].toLowerCase();
query['priority'] = filter['priority'].toLowerCase();
} }
if(filter['sources'] && typeof filter['sources'] == 'object'){ if(filter['sources'] && typeof filter['sources'] == 'object'){
query['sources'] = filter['sources'].join(); query['sources'] = filter['sources'].join();
@ -51,7 +51,7 @@ event_api.prototype.stream = function(start, end, filter, callback){
} }
params = { params = {
query: query,
query: query,
}; };
this.request('GET', '/events', params, callback); this.request('GET', '/events', params, callback);
}; };
@ -67,20 +67,20 @@ event_api.prototype.polling_stream = function(interval, filter, callback){
* `callback` is an optional function called with the results of the api call * `callback` is an optional function called with the results of the api call
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
if(arguments.length < 3 && v8type.is(arguments[1], v8type.FUNCTION)){
callback = arguments[1];
filter = {};
if(arguments.length < 3 && typeof arguments[1] == 'function'){
callback = arguments[1];
filter = {};
} }
if(!v8type.is(filter, v8type.OBJECT)){
throw new Error('`filter` parameter must be an object');
if(typeof filter != 'object'){
throw new Error('`filter` parameter must be an object');
} }
var last_run = new Date().getTime() / 1000; var last_run = new Date().getTime() / 1000;
var self = this; var self = this;
setInterval(function(){ setInterval(function(){
var start = last_run;
var end = last_run = new Date().getTime() / 1000;
self.stream(start, end, filter, callback);
var start = last_run;
last_run = new Date().getTime() / 1000;
self.stream(start, last_run, filter, callback);
}, interval * 1000); }, interval * 1000);
}; };
@ -96,7 +96,7 @@ event_api.prototype.get_event = function(event_id, callback){
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
if(!event_id){ if(!event_id){
throw new Error('`event_id` parameter is required');
throw new Error('`event_id` parameter is required');
} }
this.request('GET', util.format('/events/%s', event_id), callback); this.request('GET', util.format('/events/%s', event_id), callback);
@ -123,30 +123,30 @@ event_api.prototype.add_event = function(event, callback){
* `callback` is an optional function for the result * `callback` is an optional function for the result
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
if(!v8type.is(event, v8type.OBJECT)){
throw new Error('`event` parameter must be an object');
if(typeof event != 'object'){
throw new Error('`event` parameter must be an object');
} }
if(!event['title']){ if(!event['title']){
throw new Error('`title` property of `event` parameter is required');
throw new Error('`title` property of `event` parameter is required');
} }
if(!event['text']){ if(!event['text']){
throw new Error('`text` property of `event` parameter is required');
throw new Error('`text` property of `event` parameter is required');
} }
if(!event['date_happened']){ if(!event['date_happened']){
event['date_happened'] = (new Date().getTime()) / 1000;
event['date_happened'] = (new Date().getTime()) / 1000;
} }
if(event['priority']){ if(event['priority']){
if(['normal', 'low'].indexOf(event['priority'].toLowerCase()) == -1){
event['priority'] = undefined;
}
if(['normal', 'low'].indexOf(event['priority'].toLowerCase()) == -1){
event['priority'] = undefined;
}
} }
if(event['tags']){ if(event['tags']){
event['tags'] = event['tags'].join();
event['tags'] = event['tags'].join();
} }
this.request('POST', '/events', {body: event}, callback); this.request('POST', '/events', {body: event}, callback);
@ -166,12 +166,12 @@ event_api.prototype.add_comment = function(comment, callback){
* `callback` is an optional function for the result of the call * `callback` is an optional function for the result of the call
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
if(!v8type.is(comment, v8type.OBJECT)){
throw new Error('`comment` parameter must be an object');
if(typeof comment != 'object'){
throw new Error('`comment` parameter must be an object');
} }
if(!comment['message']){ if(!comment['message']){
throw new Error('`message` property of `comment` paramter is required');
throw new Error('`message` property of `comment` paramter is required');
} }
this.request('POST', '/comments', {body: comment}, callback); this.request('POST', '/comments', {body: comment}, callback);


+ 5
- 5
lib/api/metric.js View File

@ -17,7 +17,7 @@ metric_api.prototype.add_metric = function(metric, callback){
* callback(metric, [callback]) * callback(metric, [callback])
*/ */
var metrics = { var metrics = {
'series': [metric]
'series': [metric]
}; };
this.add_metrics(metrics, callback); this.add_metrics(metrics, callback);
}; };
@ -43,10 +43,10 @@ metric_api.prototype.add_metrics = function(metrics, callback){
} }
for(var i in metrics['series']){ for(var i in metrics['series']){
var metric = metrics['series'][i];
if(!metric['metric']){
throw new Error('metric["metric"] is required');
}
var metric = metrics['series'][i];
if(!metric['metric']){
throw new Error('metric["metric"] is required');
}
if(!metric['points'] || typeof metric['points'] != 'object'){ if(!metric['points'] || typeof metric['points'] != 'object'){
throw new Error('metric["points"] must be an array'); throw new Error('metric["points"] must be an array');


+ 4
- 4
lib/api/screen.js View File

@ -38,7 +38,7 @@ var validate_screenboard = function(screenboard){
throw new Error(util.format('`screenboard["widgets"][%s]["y"]` is missing', i)); throw new Error(util.format('`screenboard["widgets"][%s]["y"]` is missing', i));
} }
} }
}
};
var screen_api = function(){}; var screen_api = function(){};
@ -52,7 +52,7 @@ screen_api.prototype.get_screenboard = function(screen_id, callback){
* `callback` is an optional function to call with the results of the api call * `callback` is an optional function to call with the results of the api call
* callback(error, results, status_code) * callback(error, results, status_code)
*/ */
this.request('GET', util.format('/screen/%s', screen_id), callback)
this.request('GET', util.format('/screen/%s', screen_id), callback);
}; };
screen_api.prototype.get_all_screenboards = function(callback){ screen_api.prototype.get_all_screenboards = function(callback){
@ -64,7 +64,7 @@ screen_api.prototype.get_all_screenboards = function(callback){
* `callback` is an optional function to call with the results of the api call * `callback` is an optional function to call with the results of the api call
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
this.request('GET', '/screen', callback)
this.request('GET', '/screen', callback);
}; };
screen_api.prototype.create_screenboard = function(screenboard, callback){ screen_api.prototype.create_screenboard = function(screenboard, callback){
@ -110,7 +110,7 @@ screen_api.prototype.delete_screenboard = function(screen_id, callback){
* `callback` an optional function to call with the results of the api call * `callback` an optional function to call with the results of the api call
* callback(error, result, status_code) * callback(error, result, status_code)
*/ */
this.request('DELETE', util.format('/screen/%s', screen_id), callback)
this.request('DELETE', util.format('/screen/%s', screen_id), callback);
}; };
return module.exports = screen_api; return module.exports = screen_api;

+ 25
- 25
lib/api/tag.js View File

@ -19,9 +19,9 @@ tag_api.prototype.all_tags = function(source, callback){
} }
params = { params = {
query: {
source: source
}
query: {
source: source
}
}; };
this.request('GET', '/tags/hosts', params, callback); this.request('GET', '/tags/hosts', params, callback);
}; };
@ -43,9 +43,9 @@ tag_api.prototype.host_tags = function(host, source, callback){
} }
params = { params = {
query: {
source: source,
}
query: {
source: source,
}
}; };
this.request('GET', util.format('/tags/hosts/%s', host), params, callback); this.request('GET', util.format('/tags/hosts/%s', host), params, callback);
}; };
@ -67,10 +67,10 @@ tag_api.prototype.host_tags_by_source = function(host, source, callback){
} }
params = { params = {
query: {
source: source,
by_source: true,
}
query: {
source: source,
by_source: true,
}
}; };
this.request('GET', util.format('/tags/hosts/%s', host), params, callback); this.request('GET', util.format('/tags/hosts/%s', host), params, callback);
}; };
@ -97,12 +97,12 @@ tag_api.prototype.add_tags = function(host, tags, source, callback){
} }
params = { params = {
query: {
source: source,
},
body: {
tags: tags,
}
query: {
source: source,
},
body: {
tags: tags,
}
}; };
this.request('POST', util.format('/tags/hosts/%s', host), params, callback); this.request('POST', util.format('/tags/hosts/%s', host), params, callback);
@ -130,12 +130,12 @@ tag_api.prototype.update_tags = function(host, tags, source, callback){
} }
params = { params = {
query: {
source: source,
},
body: {
tags: tags,
}
query: {
source: source,
},
body: {
tags: tags,
}
}; };
this.request('PUT', util.format('/tags/hosts/%s', host), params, callback); this.request('PUT', util.format('/tags/hosts/%s', host), params, callback);
@ -158,9 +158,9 @@ tag_api.prototype.detach_tags = function(host, source, callback){
} }
params = { params = {
query: {
source: source,
},
query: {
source: source,
},
}; };
this.request('DELETE', util.format('/tags/hosts/%s', host), params, callback); this.request('DELETE', util.format('/tags/hosts/%s', host), params, callback);


+ 28
- 28
lib/http_client.js View File

@ -30,22 +30,22 @@ var client = function(options){
this.api_key = (options['api_key'])? options['api_key'] : process.env['DD_API_KEY']; this.api_key = (options['api_key'])? options['api_key'] : process.env['DD_API_KEY'];
if(!this.api_key){ if(!this.api_key){
throw new Error('`api_key` is not present, either provide `api_key` in `options` or use environment variable `DD_API_KEY`');
throw new Error('`api_key` is not present, either provide `api_key` in `options` or use environment variable `DD_API_KEY`');
} }
this.app_key = (options['app_key'])? options['app_key'] : process.env['DD_APP_KEY']; this.app_key = (options['app_key'])? options['app_key'] : process.env['DD_APP_KEY'];
if(!this.api_key){ if(!this.api_key){
throw new Error('`app_key` is not present, either provide `app_key` in `options` or use environment variable `DD_APP_KEY`');
throw new Error('`app_key` is not present, either provide `app_key` in `options` or use environment variable `DD_APP_KEY`');
} }
this.api_version = (options['api_version'])? options['api_version'] : process.env['DD_API_VERSION']; this.api_version = (options['api_version'])? options['api_version'] : process.env['DD_API_VERSION'];
if(!this.api_version){ if(!this.api_version){
this.api_version = 'v1';
this.api_version = 'v1';
} }
this.api_host = (options['api_host'])? options['api_host'] : process.env['DD_API_HOST']; this.api_host = (options['api_host'])? options['api_host'] : process.env['DD_API_HOST'];
if(!this.api_host){ if(!this.api_host){
this.api_host = 'app.datadoghq.com';
this.api_host = 'app.datadoghq.com';
} }
@ -75,8 +75,8 @@ client.prototype.request = function(method, path, params, callback){
params = params || {}; params = params || {};
var body = (typeof params['body'] == 'object')? JSON.stringify(params['body']) : params['body']; var body = (typeof params['body'] == 'object')? JSON.stringify(params['body']) : params['body'];
var query = { var query = {
'api_key': this.api_key,
'application_key': this.app_key,
'api_key': this.api_key,
'application_key': this.app_key,
}; };
if(typeof params['query'] == 'object'){ if(typeof params['query'] == 'object'){
@ -84,22 +84,22 @@ client.prototype.request = function(method, path, params, callback){
} }
path = url.format({ path = url.format({
'pathname': util.format('/api/%s%s', this.api_version, path),
'query': query,
'pathname': util.format('/api/%s%s', this.api_version, path),
'query': query,
}); });
var http_options = { var http_options = {
hostname: this.api_host,
port: 443,
method: method.toUpperCase(),
path: path,
hostname: this.api_host,
port: 443,
method: method.toUpperCase(),
path: path,
}; };
if(['POST', 'PUT'].indexOf(http_options['method']) >= 0){ if(['POST', 'PUT'].indexOf(http_options['method']) >= 0){
http_options['headers'] = {
'Content-Type': 'application/json',
'Content-Length': body.length,
};
http_options['headers'] = {
'Content-Type': 'application/json',
'Content-Length': body.length,
};
} }
var req = https.request(http_options, function(res){ var req = https.request(http_options, function(res){
@ -109,18 +109,18 @@ client.prototype.request = function(method, path, params, callback){
} }
}); });
var data = '';
res.on('data', function(chunk){
data += chunk;
});
var data = '';
res.on('data', function(chunk){
data += chunk;
});
res.on('end', function(){
error = null;
try{ data = JSON.parse(data); }catch(e){}
if(data['errors']){
error = data['errors'];
data = null;
}
res.on('end', function(){
error = null;
try{ data = JSON.parse(data); }catch(e){}
if(data['errors']){
error = data['errors'];
data = null;
}
if(typeof callback == 'function'){ if(typeof callback == 'function'){
callback(error, data, res.statusCode); callback(error, data, res.statusCode);
@ -129,7 +129,7 @@ client.prototype.request = function(method, path, params, callback){
}); });
if(['POST', 'PUT'].indexOf(http_options['method']) >= 0){ if(['POST', 'PUT'].indexOf(http_options['method']) >= 0){
req.write(body);
req.write(body);
} }
req.end() req.end()
}; };


Loading…
Cancel
Save