This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
aggregator.js
174 lines (160 loc) · 5.13 KB
/
aggregator.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
import forEach from 'lodash.foreach';
const metrics = ['TTFB', 'render', 'fullyLoaded', 'SpeedIndex'];
export class Aggregator {
constructor(statsHelpers, log) {
this.statsHelpers = statsHelpers;
this.log = log;
this.timingStats = {};
this.assetStats = {};
this.customStats = {};
this.assetGroups = {};
this.timingGroups = {};
this.customGroups = {};
this.connectivity;
this.location;
}
addToAggregate(group, wptData, connectivity, location, wptOptions) {
const log = this.log;
const statsHelpers = this.statsHelpers;
// TODO this will break if we run multiple locations/connectivity per run
this.location = location;
this.connectivity = connectivity;
if (this.assetGroups[group] === undefined) {
this.assetGroups[group] = {};
this.timingGroups[group] = {};
this.customGroups[group] = {};
}
forEach(wptData.data.runs, (run, index) => {
// TODO remove this if check once issue with 0 stats, but 200 response is fixed upstream.
// It seems to be cases when users tries to navigate away before fullyLoaded has happened
const speedIndex = run.firstView.SpeedIndex || 0;
if (wptOptions && wptOptions.video && speedIndex <= 0) {
log.error(
`Incomplete first view data for WPT test ${wptData.data.id}, run ${index}`
);
return false;
}
forEach(run, (viewData, viewName) => {
forEach(metrics, metric =>
statsHelpers.pushGroupStats(
this.timingStats,
this.timingGroups[group],
[viewName, metric],
viewData[metric]
)
);
forEach(viewData.userTimes, (timingData, timingName) =>
statsHelpers.pushGroupStats(
this.timingStats,
this.timingGroups[group],
[viewName, timingName],
timingData
)
);
forEach(viewData.breakdown, (contentType, typeName) =>
forEach(['requests', 'bytes'], property =>
statsHelpers.pushGroupStats(
this.assetStats,
this.assetGroups[group],
[viewName, typeName, property],
contentType[property]
)
)
);
forEach(viewData.custom, metricName => {
// eslint-disable-next-line unicorn/prefer-number-properties
if (!isNaN(viewData[metricName]) && viewData[metricName] !== null) {
// https://github.com/sitespeedio/sitespeed.io/issues/2985
if (
Array.isArray(viewData[metricName]) &&
viewData[metricName].length === 0
) {
log.debug('Empty array for ' + metricName);
} else {
statsHelpers.pushGroupStats(
this.customStats,
this.customGroups[group],
[viewName, 'custom', metricName],
viewData[metricName]
);
}
}
});
});
});
}
summarize() {
const summary = {
groups: {
total: {
timing: this.summarizePerTimingType(this.timingStats),
asset: this.summarizePerAssetType(this.assetStats),
custom: this.summarizePerCustomType(this.customStats)
}
}
};
for (let group of Object.keys(this.timingGroups)) {
if (!summary.groups[group]) summary.groups[group] = {};
summary.groups[group].timing = this.summarizePerTimingType(
this.timingGroups[group]
);
}
for (let group of Object.keys(this.assetGroups)) {
if (!summary.groups[group]) summary.groups[group] = {};
summary.groups[group].asset = this.summarizePerAssetType(
this.assetGroups[group]
);
}
if (this.customGroups) {
for (let group of Object.keys(this.customGroups)) {
if (!summary.groups[group]) summary.groups[group] = {};
summary.groups[group].custom = this.summarizePerCustomType(
this.customGroups[group]
);
}
}
return summary;
}
summarizePerAssetType(type) {
const statsHelpers = this.statsHelpers;
const summary = {};
forEach(type, (view, viewName) =>
forEach(view, (contentType, contentTypeName) =>
forEach(contentType, (stats, propertyName) =>
statsHelpers.setStatsSummary(
summary,
[viewName, 'breakdown', contentTypeName, propertyName],
stats
)
)
)
);
return summary;
}
summarizePerTimingType(type) {
const statsHelpers = this.statsHelpers;
const summary = {};
forEach(type, (view, viewName) =>
forEach(view, (stats, name) =>
statsHelpers.setStatsSummary(summary, [viewName, name], stats)
)
);
return summary;
}
summarizePerCustomType(type) {
const statsHelpers = this.statsHelpers;
const summary = {};
forEach(type, (view, viewName) =>
forEach(view, (metricName, name) =>
forEach(metricName, (stats, propertyName) => {
statsHelpers.setStatsSummary(
summary,
[viewName, name, propertyName],
stats
);
})
)
);
return summary;
}
}