-
Notifications
You must be signed in to change notification settings - Fork 5
/
pagerduty.js
132 lines (115 loc) · 3.87 KB
/
pagerduty.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*jslint node: true */
/*globals module: true */
var oncallsParams = {
"time_zone": 'UTC',
"include[]": 'users',
"schedule_ids[]" : {}
};
//
var request = require('request');
var async = require('async');
var _ = require('underscore');
var querystring = require('querystring');
var debug = require('debug')('pagerduty');
const NodeCache = require( "node-cache" );
/**
* params object:
* domain: String (required)
* token: String (required)
*
**/
var PagerDuty = function (options) {
this.headers = {'Accept': 'application/vnd.pagerduty+json;version=2', 'Content-Type': 'application/json', 'Authorization': 'Token token=' + options.pagerduty_token};
this.endpoint = "https://api.pagerduty.com";
this.cache = new NodeCache();
oncallsParams["schedule_ids[]"] = options.schedule_ids;
this.token = options.pagerduty_token;
this.cacheInterval = options.cache_interval_seconds;
};
PagerDuty.prototype.getAllPaginatedData = function (options) {
options.params = options.params || {};
options.params.limit = 100; // 100 is the max limit allowed by pagerduty
options.params.offset = 0;
var total = null,
items = [],
items_map = {},
self = this,
requestOptions = {
headers: self.headers,
json: true,
total: true
};
var pagedCallback = function (error, content) {
if (error) {
debug("Issues with pagedCallback: " + error);
return options.callback(error);
}
if (!content || !content[options.contentIndex]) {
error = "Page does not have valid data: " + JSON.stringify(content);
debug(error);
return options.callback(new Error(error));
}
if (content[options.contentIndex].length > 0) {
items = items.concat(content[options.contentIndex]);
}
options.params.offset = content.offset + content.limit; // Update the offset for the next paging request
total = content.total;
// Index the results as a map from id: item
if (options.sortBy) {
items.sort(function(a,b) {
return a[options.sortBy] - b[options.sortBy];
});
}
_.each(items, function(item, i) {
index = item.id || item[options.secondaryIndex].id;
if(options.sortBy) {
index = item[options.sortBy] + '-' + index;
}
items_map[index] = item;
});
if (options.params.offset >= total) {
options.callback(error, items_map);
} else {
requestAnotherPage();
}
};
var requestAnotherPage = function () {
// must use node's built in querystring since qs doesn't build arrays like PagerDuty expects.
requestOptions.url = self.endpoint + options.uri + "?" + querystring.stringify(options.params);
request(requestOptions, function (error, response, body) {
if (!error && response.statusCode == 200) {
pagedCallback(null, body);
} else {
pagedCallback(error);
}
});
};
requestAnotherPage();
};
PagerDuty.prototype.getOnCalls = function (params, callback) {
var options = {contentIndex: "oncalls", secondaryIndex: 'user', sortBy: 'escalation_level', uri: "/oncalls", callback: callback, params: params || oncallsParams };
var self = this;
async.auto({
getCacheData: function(cb) {
debug("getCacheData");
self.cache.get(options.contentIndex, cb);
},
checkCacheData: ['getCacheData', function (results, cb) {
debug("checkCacheData");
if (results.getCacheData == undefined) {
options.callback = cb;
self.getAllPaginatedData(options);
} else {
callback(null, results.getCacheData);
}
}],
setCacheData: ['checkCacheData', function (results, cb) {
debug("setCacheData");
var cacheableResult = results.checkCacheData;
self.cache.set(options.contentIndex, cacheableResult, self.cacheInterval, cb(null,cacheableResult));
}]
}, function(err, result) {
callback(null, result.setCacheData);
});
};
module.exports = PagerDuty;