Skip to content

Commit

Permalink
v2.1.1 handle more date formats and limit async
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdemartini committed Jan 12, 2017
1 parent 1f5102f commit 71b1ff4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
21 changes: 14 additions & 7 deletions command-results-by-tag.coffee
Expand Up @@ -10,8 +10,8 @@ packageJSON = require './package.json'
program
.version packageJSON.version
.usage '[options] <hostname>'
.option '-t, --to <date-string>', 'End of period. Defaults to current time. (must be parsable by moment())'
.option '-f, --from <date-string>', 'Start of period. Defaults to 1 day ago. (must be parsable by moment())'
.option '-t, --to <date>', 'End of period. Defaults to current time. (must be parsable by moment())'
.option '-f, --from <date>', 'Start of period. Defaults to 1 day ago. (must be parsable by moment())'
.option '-a, --app-key <string>', 'Pingdom app key. (env: PINGDOM_APP_KEY)'
.option '-u, --username <string>', 'Pingdom Username. (env: PINGDOM_USERNAME)'
.option '-p, --password <string>', 'Pingdom Password. (env: PINGDOM_PASSWORD)'
Expand Down Expand Up @@ -42,11 +42,18 @@ class Command
return { appKey, username, password, to, from }

parseDate: (time) =>
return unless time?
time = _.toNumber(time) if _.isNumber(_.toNumber(time))
date = moment(time)
return date.unix() unless date.format('YYYY') == '1970'
return time
try
timeNum = _.toNumber(time)
if _.isNumber timeNum
date = moment.unix(timeNum)
return date.unix() if date.isValid()
try
date = moment(time)
return date.unix() if date.isValid()
try
date = moment(time, moment.ISO_8601)
return date.unix() if date.isValid()
throw new Error 'Invalid date for moment'

run: =>
{ to, from } = @parseOptions()
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "pingdom-util",
"version": "2.1.0",
"version": "2.1.1",
"description": "CLI utility for dealing with Pingdom",
"keywords": [
"pingdom"
Expand Down
22 changes: 15 additions & 7 deletions src/pingdom-service.coffee
Expand Up @@ -37,10 +37,11 @@ class PingdomService
resultsByTag: ({ to, from }, callback) =>
@_getChecksByTag {}, (error, tags) =>
return callback error if error?
async.mapValues tags, async.apply(@_getResultsForTag, { to, from }), callback
async.mapValuesLimit tags, 10, async.apply(@_getResultsForTag, { to, from }), callback

_getResultsForTag: (options, checks, key, callback) =>
async.mapValues checks, async.apply(@_getResultsForCheck, options), (error, allResults) =>
debug 'get results for tag'
async.mapValuesLimit checks, 20, async.apply(@_getResultsForCheck, options), (error, allResults) =>
return callback error if error?
minutes = {}
_.each _.values(allResults), (checkResults) =>
Expand All @@ -60,16 +61,21 @@ class PingdomService
passes,
}

_getResultsForCheck: ({ offset, to, from, previous }, { id }, key, callback) =>
options = _.pickBy { offset, to, from }
_getResultsForCheck: ({ offset, to, from, previous, i=0 }, { id }, key, callback) =>
limit = 1000
debug 'get results options', { i, to, from, offset, limit }
options = _.pickBy { offset, to, from, limit }
@_request { method: 'GET', uri: "/results/#{id}", qs: options }, (error, body) =>
return callback error if error?
results = _.get body, 'results', []
count = _.size results
results = _.union results, previous if previous?
if count < 1000
debug 'results count', count
if count < limit
return callback null, results
@_getResultsForCheck { offset: count, to, from, previous: results }, { id }, key, callback
i += 1
count = _.size results
@_getResultsForCheck { offset: count, to, from, previous: results, i }, { id }, key, callback

_getTime: ({ time }) =>
return time - (time % (60))
Expand Down Expand Up @@ -118,6 +124,8 @@ class PingdomService
debug 'pingdom response', { error, statusCode: response?.statusCode }
return callback error if error?
return callback new Error('Invalid response') if response.statusCode > 499
callback null, JSON.parse body
body = JSON.parse body
return callback new Error _.get(body, 'error.errormessage') if response.statusCode > 399
callback null, body

module.exports = PingdomService

0 comments on commit 71b1ff4

Please sign in to comment.