Skip to content

Commit fc926c1

Browse files
Merge pull request #1 from speedtracker/v1.0.1
Improve error handling
2 parents afd776f + 8e14d57 commit fc926c1

File tree

2 files changed

+55
-28
lines changed

2 files changed

+55
-28
lines changed

lib/SpeedTracker.js

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ SpeedTracker.prototype._getRemoteFile = function (file) {
6666
}
6767

6868
SpeedTracker.prototype._processBudgets = function (profileData, result) {
69-
if (!profileData.budgets) return Promise.resolve(true)
69+
if (!profileData.budgets || !(profileData.budgets instanceof Array)) {
70+
return Promise.resolve(true)
71+
}
7072

7173
let infractorsByAlert = {}
7274

@@ -128,27 +130,47 @@ SpeedTracker.prototype._processSchedule = function (profile, schedule) {
128130
return Promise.resolve(null)
129131
}
130132

131-
SpeedTracker.prototype._runWptTest = function (url, parameters) {
133+
SpeedTracker.prototype._runWptTest = function (url, parameters, callback) {
132134
return new Promise((resolve, reject) => {
133-
this.wpt.runTest(url, parameters, (err, response) => {
135+
const wptResult = this.wpt.runTest(url, parameters, (err, response) => {
134136
//require('fs').readFile('result.json', (err, data) => {
135137
// const response = JSON.parse(data)
136138
if (err) return reject(err)
137139

138-
resolve(response)
140+
if (!response.statusCode || (response.statusCode !== 200)) {
141+
return reject(response)
142+
}
143+
144+
const interval = setInterval(() => {
145+
this.wpt.getTestResults(response.data.testId, (err, results) => {
146+
// Check for errors
147+
if (err || (results.statusCode >= 300)) {
148+
return clearInterval(interval)
149+
}
150+
151+
// Check for completion
152+
if ((results.statusCode >= 200) && (results.statusCode < 300)) {
153+
clearInterval(interval)
154+
155+
return callback(results)
156+
}
157+
})
158+
}, 5000)
159+
160+
return resolve(response)
139161
})
140162
})
141163
}
142164

143165
SpeedTracker.prototype._saveTest = function (profile, content, isScheduled) {
144-
let date = new Date(content.date * 1000)
145-
let year = date.getFullYear()
146-
let month = Utils.padWithZeros(date.getMonth() + 1, 2)
147-
let day = Utils.padWithZeros(date.getDate(), 2)
166+
const date = new Date(content.date * 1000)
167+
const year = date.getFullYear()
168+
const month = Utils.padWithZeros(date.getMonth() + 1, 2)
169+
const day = Utils.padWithZeros(date.getDate(), 2)
148170

149-
let path = `results/${profile}/${year}/${month}.json`
171+
const path = `results/${profile}/${year}/${month}.json`
150172

151-
let message = isScheduled ? 'Scheduled SpeedTracker test' : 'Add SpeedTracker test'
173+
const message = `Add SpeedTracker test (${isScheduled ? 'scheduled' : 'manual'})`
152174

153175
return this._getRemoteFile(path).then(data => {
154176
try {
@@ -262,10 +284,11 @@ SpeedTracker.prototype.init = function () {
262284
}
263285

264286
SpeedTracker.prototype.initWpt = function () {
265-
let wptUrl = this.config.wptUrl || config.get('wpt.url')
287+
let wptUrl = this.config.wptUrl ? Utils.decrypt(this.config.wptUrl, this.options.key) : config.get('wpt.url')
266288
let wptKey = this.config.wptKey ? Utils.decrypt(this.config.wptKey, this.options.key) : config.get('wpt.key')
267289

268290
this.wpt = new WebPageTest(wptUrl, wptKey)
291+
this.wptUrl = wptUrl
269292
}
270293

271294
SpeedTracker.prototype.runTest = function (profile, isScheduled) {
@@ -276,8 +299,7 @@ SpeedTracker.prototype.runTest = function (profile, isScheduled) {
276299

277300
let overrides = {
278301
firstViewOnly: true,
279-
pollResults: 5,
280-
video: true,
302+
video: true
281303
}
282304

283305
return this.init().then(() => {
@@ -293,26 +315,23 @@ SpeedTracker.prototype.runTest = function (profile, isScheduled) {
293315

294316
if (!parameters.url) return Promise.reject('NO_URL')
295317

296-
// Override WPT URL
297-
if (this.config.wptUrl) {
298-
parameters.server = this.config.wptUrl
299-
}
300-
301318
let url = parameters.url
302319
delete parameters.url
303320

304321
return this.options.scheduler.find(profileData._nwo, profileData._id).then(schedule => {
305322
const runTest = !isScheduled || (profileData.interval && (profileData.interval === schedule.interval))
306323

324+
let queue = Promise.resolve(true)
325+
307326
if (runTest) {
308-
this._runWptTest(url, parameters).then(response => {
327+
queue = this._runWptTest(url, parameters, response => {
309328
this._buildResult(response.data).then(result => {
310329
// Save test
311330
this._saveTest(profile, result, isScheduled)
312331

313332
// Process budgets
314333
this._processBudgets(profileData, result)
315-
})
334+
})
316335
})
317336

318337
// Track event
@@ -324,13 +343,21 @@ SpeedTracker.prototype.runTest = function (profile, isScheduled) {
324343
})
325344
}
326345

327-
return this._processSchedule(profileData, schedule).then(nextRun => {
328-
const response = {
329-
success: runTest,
330-
nextRun
331-
}
346+
return queue.then(testResult => {
347+
return this._processSchedule(profileData, schedule).then(nextRun => {
348+
let response = {
349+
success: runTest,
350+
nextRun
351+
}
352+
353+
if (testResult.data && testResult.data.testId) {
354+
response.testId = testResult.data.testId
355+
}
332356

333-
return response
357+
return response
358+
})
359+
}).catch(err => {
360+
return Promise.reject(Utils.buildError('WPT_ERROR', err.statusText))
334361
})
335362
})
336363
})

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "speedtracker-app",
3-
"version": "1.0.0",
2+
"name": "speedtracker-api",
3+
"version": "1.0.1",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)