@@ -66,7 +66,9 @@ SpeedTracker.prototype._getRemoteFile = function (file) {
66
66
}
67
67
68
68
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
+ }
70
72
71
73
let infractorsByAlert = { }
72
74
@@ -128,27 +130,47 @@ SpeedTracker.prototype._processSchedule = function (profile, schedule) {
128
130
return Promise . resolve ( null )
129
131
}
130
132
131
- SpeedTracker . prototype . _runWptTest = function ( url , parameters ) {
133
+ SpeedTracker . prototype . _runWptTest = function ( url , parameters , callback ) {
132
134
return new Promise ( ( resolve , reject ) => {
133
- this . wpt . runTest ( url , parameters , ( err , response ) => {
135
+ const wptResult = this . wpt . runTest ( url , parameters , ( err , response ) => {
134
136
//require('fs').readFile('result.json', (err, data) => {
135
137
// const response = JSON.parse(data)
136
138
if ( err ) return reject ( err )
137
139
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 )
139
161
} )
140
162
} )
141
163
}
142
164
143
165
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 )
148
170
149
- let path = `results/${ profile } /${ year } /${ month } .json`
171
+ const path = `results/${ profile } /${ year } /${ month } .json`
150
172
151
- let message = isScheduled ? 'Scheduled SpeedTracker test ' : 'Add SpeedTracker test'
173
+ const message = `Add SpeedTracker test ( ${ isScheduled ? 'scheduled ' : 'manual' } )`
152
174
153
175
return this . _getRemoteFile ( path ) . then ( data => {
154
176
try {
@@ -262,10 +284,11 @@ SpeedTracker.prototype.init = function () {
262
284
}
263
285
264
286
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' )
266
288
let wptKey = this . config . wptKey ? Utils . decrypt ( this . config . wptKey , this . options . key ) : config . get ( 'wpt.key' )
267
289
268
290
this . wpt = new WebPageTest ( wptUrl , wptKey )
291
+ this . wptUrl = wptUrl
269
292
}
270
293
271
294
SpeedTracker . prototype . runTest = function ( profile , isScheduled ) {
@@ -276,8 +299,7 @@ SpeedTracker.prototype.runTest = function (profile, isScheduled) {
276
299
277
300
let overrides = {
278
301
firstViewOnly : true ,
279
- pollResults : 5 ,
280
- video : true ,
302
+ video : true
281
303
}
282
304
283
305
return this . init ( ) . then ( ( ) => {
@@ -293,26 +315,23 @@ SpeedTracker.prototype.runTest = function (profile, isScheduled) {
293
315
294
316
if ( ! parameters . url ) return Promise . reject ( 'NO_URL' )
295
317
296
- // Override WPT URL
297
- if ( this . config . wptUrl ) {
298
- parameters . server = this . config . wptUrl
299
- }
300
-
301
318
let url = parameters . url
302
319
delete parameters . url
303
320
304
321
return this . options . scheduler . find ( profileData . _nwo , profileData . _id ) . then ( schedule => {
305
322
const runTest = ! isScheduled || ( profileData . interval && ( profileData . interval === schedule . interval ) )
306
323
324
+ let queue = Promise . resolve ( true )
325
+
307
326
if ( runTest ) {
308
- this . _runWptTest ( url , parameters ) . then ( response => {
327
+ queue = this . _runWptTest ( url , parameters , response => {
309
328
this . _buildResult ( response . data ) . then ( result => {
310
329
// Save test
311
330
this . _saveTest ( profile , result , isScheduled )
312
331
313
332
// Process budgets
314
333
this . _processBudgets ( profileData , result )
315
- } )
334
+ } )
316
335
} )
317
336
318
337
// Track event
@@ -324,13 +343,21 @@ SpeedTracker.prototype.runTest = function (profile, isScheduled) {
324
343
} )
325
344
}
326
345
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
+ }
332
356
333
- return response
357
+ return response
358
+ } )
359
+ } ) . catch ( err => {
360
+ return Promise . reject ( Utils . buildError ( 'WPT_ERROR' , err . statusText ) )
334
361
} )
335
362
} )
336
363
} )
0 commit comments