-
Notifications
You must be signed in to change notification settings - Fork 11
/
tests.js
276 lines (238 loc) · 6.84 KB
/
tests.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* the endpoints for fetching test result data
* makes some assumptions about what is wanted (eg: charts)
*/
const express = require("express");
const router = express.Router();
const debug = require("debug")("wpt-api:tests");
const _ = require("lodash");
const async = require("async");
const jf = require("jsonfile");
const moment = require("moment");
const cache = require("memory-cache");
//change this to the data store you want to use
const dataStore = require("../data_store");
/*
* Settings for tests
*/
const masterConfig = jf.readFileSync(process.env.SUITE_CONFIG);
const availableChartTypes = [
"SpeedIndex",
"loadTime",
"fullyLoaded",
"TTFB",
"visualComplete",
"lighthouse.Performance",
"lighthouse.Performance.speed-index-metric",
"lighthouse.BestPractices",
"lighthouse.Performance.estimated-input-latency",
"lighthouse.Performance.first-interactive",
"lighthouse.Accessibility",
"lighthouse.Performance.consistently-interactive",
"lighthouse.SEO",
"lighthouse.ProgressiveWebApp",
"lighthouse.Performance.first-meaningful-paint",
"lighthouse.Performance"
];
const defaultChartConfig = {
type: "SpeedIndex",
dataRange: [0, Infinity],
dateCutoff: 30
};
const defaultHeaders = {
"Cache-Control": "public, max-age: 3600"
};
/**
* Get all the tests for a suite and data points
* within those tests.
*/
router.get("/:suiteId", function(req, res) {
let data = getCache(req);
if (data) {
encloseRenderSuite(req, res)(data);
} else {
data = dataStore.getSuite(req.params.suiteId, encloseRenderSuite(req, res));
}
});
/**
* Get a specific datapoint
*/
router.get("/:suiteId/:testId/:datapointId", function(req, res) {
let data = getCache(req);
if (data) {
encloseRenderDatapoint(req, res)(data);
} else {
dataStore.getDatapoint(
req.params.suiteId,
req.params.testId,
req.params.datapointId,
encloseRenderDatapoint(req, res)
);
}
});
module.exports = router;
function buildChartConfig(req, defaultConfig) {
let type = makeType(req.query.chartType);
let typeConfig = _.find(defaultConfig, { type: type });
let dateCutoff = makeDateCutoff(req.query.dateCutoff, typeConfig);
let dataRange = makeDataRange(req.query.dataRange, typeConfig);
return {
type: type,
dateCutoff: dateCutoff,
dataRange: dataRange
};
}
function makeType(type) {
return _.indexOf(availableChartTypes, type) != "-1"
? type
: defaultChartConfig.type;
}
function makeDateCutoff(cutoff, suiteConfig) {
const custom = parseInt(cutoff, 10);
const defaultVal =
suiteConfig && suiteConfig.dateCutoff
? suiteConfig.dateCutoff
: defaultChartConfig.dateCutoff;
return custom || defaultVal;
}
function makeDataRange(range, suiteConfig) {
range = range ? range : "0,0";
const defaultVal =
suiteConfig && suiteConfig.dataRange
? suiteConfig.dataRange
: defaultChartConfig.dataRange;
const dataRange = range.split(",").map(function(val) {
var parsed = parseInt(val, 10);
if (isNaN(parsed)) {
parsed = Infinity;
}
return parsed;
});
//valid range, or default for suite, or default for anything
var validRange = dataRange[0] < dataRange[1] ? dataRange : defaultVal;
return validRange;
}
function chartFromDatapoints(suiteId, testConfig, datapoints, chartConfig) {
let chart = {
suiteId: suiteId,
testId: testConfig.testId,
testDisplayName: testConfig.testDisplayName,
fvValues: [],
rvValues: [],
datapoints: []
};
let dateCutoff = moment().subtract(chartConfig.dateCutoff || 30, "days");
datapoints.forEach(function(dp) {
let dataDate = new Date(dp.data.completed * 1000);
//if older ignore.
if (dataDate < dateCutoff) {
return;
}
fvPointValue = parseFloat(dp.data.runs[1].firstView[chartConfig.type]);
//if test requests a repeat firstView
if (!testConfig.firstViewOnly) {
rvPointValue = parseFloat(dp.data.runs[1].repeatView[chartConfig.type]);
}
//this filtering should be moved to the data_store
if (
(inRange(fvPointValue, chartConfig.dataRange) &&
testConfig.firstViewOnly) ||
(inRange(fvPointValue, chartConfig.dataRange) &&
inRange(rvPointValue, chartConfig.dataRange))
) {
chart.fvValues.push([dataDate.getTime(), fvPointValue]);
if (!testConfig.firstViewOnly) {
chart.rvValues.push([dataDate.getTime(), rvPointValue]);
}
chart.datapoints.push(dp.datapointId);
}
});
return chart;
}
function inRange(value, range) {
debug("inRange comparing " + value + " to " + range.toString());
return value > range[0] && value < range[1];
}
function cacheKey(req) {
let paramsKey =
"suite" +
req.params.suiteId +
"test" +
req.params.testId +
"dp" +
req.params.datapointId;
let queryKey =
"ct" +
req.query.chartType +
"dr" +
req.query.dataRange +
"dc" +
req.query.dateCutoff;
return paramsKey + queryKey;
}
function getCache(req) {
const key = cacheKey(req);
const data = cache.get(key);
debug("getting cache key: " + key);
debug(data);
return data;
}
function setCache(req, data) {
const key = cacheKey(req);
debug("setting cache key: " + key);
debug(data);
return cache.put(key, data, 1000 * (60 * 60));
}
function encloseRenderSuite(req, res) {
return function renderSuite(data) {
const suiteConfig = _.find(masterConfig.testSuites, {
suiteId: data.suiteId
});
data.charts = [];
data.chartConfig = buildChartConfig(req, suiteConfig.chartConfig);
data.availableChartTypes = availableChartTypes;
data.suiteConfig = suiteConfig;
async.map(
data.tests,
function(testName, asyncCallback) {
dataStore.getSuiteTest(data.suiteId, testName, function(testData) {
data.charts.push(
chartFromDatapoints(
data.suiteId,
_.find(suiteConfig.testPages, { testId: testName }),
testData.datapoints,
data.chartConfig
)
);
asyncCallback();
});
},
function(err, results) {
setCache(req, data);
res.set(defaultHeaders);
res.json(data);
}
);
};
}
function encloseRenderDatapoint(req, res) {
return function renderDatapoint(data) {
const suiteConfig = _.find(masterConfig.testSuites, {
suiteId: data.suiteId
});
let testData;
dataStore.getSuiteTest(data.suiteId, data.testId, function(testData) {
data.testConfig = _.find(suiteConfig.testPages, { testId: data.testId });
data.chartConfig = buildChartConfig(req, suiteConfig.chartConfig);
data.chart = chartFromDatapoints(
data.suiteId,
_.find(suiteConfig.testPages, { testId: data.testId }),
testData.datapoints,
data.chartConfig
);
setCache(req, data);
res.set(defaultHeaders);
res.json(data);
});
};
}