-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
summary-wrapper.js
91 lines (80 loc) · 3.18 KB
/
summary-wrapper.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
(function () {
var jslib = {};
(function (module, exports) {
/*JSLIB_SUMMARY_CODE*/;
})({ exports: jslib }, jslib);
var forEach = function (obj, callback) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (callback(key, obj[key])) {
break;
}
}
}
}
var transformGroup = function (group) {
if (Array.isArray(group.groups)) {
var newFormatGroups = group.groups;
group.groups = {};
for (var i = 0; i < newFormatGroups.length; i++) {
group.groups[newFormatGroups[i].name] = transformGroup(newFormatGroups[i]);
}
}
if (Array.isArray(group.checks)) {
var newFormatChecks = group.checks;
group.checks = {};
for (var i = 0; i < newFormatChecks.length; i++) {
group.checks[newFormatChecks[i].name] = newFormatChecks[i];
}
}
return group;
};
var oldJSONSummary = function (data) {
// Quick copy of the data, since it's easiest to modify it in place.
var results = JSON.parse(JSON.stringify(data));
delete results.options;
delete results.state;
forEach(results.metrics, function (metricName, metric) {
var oldFormatMetric = metric.values;
if (metric.thresholds && Object.keys(metric.thresholds).length > 0) {
var newFormatThresholds = metric.thresholds;
oldFormatMetric.thresholds = {};
forEach(newFormatThresholds, function (thresholdName, threshold) {
oldFormatMetric.thresholds[thresholdName] = !threshold.ok;
});
}
if (metric.type == 'rate' && oldFormatMetric.hasOwnProperty('rate')) {
oldFormatMetric.value = oldFormatMetric.rate; // sigh...
delete oldFormatMetric.rate;
}
results.metrics[metricName] = oldFormatMetric;
});
results.root_group = transformGroup(results.root_group);
return JSON.stringify(results, null, 4);
};
return function (exportedSummaryCallback, jsonSummaryPath, data) {
var getDefaultSummary = function () {
var enableColors = (!data.options.noColor && data.state.isStdOutTTY);
return {
'stdout': '\n' + jslib.textSummary(data, { indent: ' ', enableColors: enableColors }) + '\n\n',
};
};
var result = {};
if (exportedSummaryCallback) {
try {
result = exportedSummaryCallback(data);
} catch (e) {
console.error('handleSummary() failed with error "' + e + '", falling back to the default summary');
result = getDefaultSummary();
}
} else {
result = getDefaultSummary();
}
// TODO: ensure we're returning a map of strings or null/undefined...
// and if not, log an error and generate the default summary?
if (jsonSummaryPath != '') {
result[jsonSummaryPath] = oldJSONSummary(data);
}
return result;
};
})();