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 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| import $ from 'jquery';
import _ from 'underscore';
import * as d3 from 'd3';
import InfoPane from './InfoPane';
import TrendPane from './TrendPane';
import ResultTablePane from './ResultTablePane';
import TopInfoBar from './TopInfoBar';
import { sanitizeSelector, deArray } from './utility.js';
import layout from './templates/layout.jade';
import VisComponent from '../../VisComponent';
// Calculate an percentile value from an array of numbers sorted in numerically
// increasing order, p should be a ratio percentile, e.g. 50th percentile is p
// = 0.5.
const calcPercentile = (arr, p) => {
if (arr.length === 0) return 0;
if (typeof p !== 'number') throw new TypeError('p must be a number');
if (p <= 0) return arr[0];
if (p >= 1) return arr[arr.length - 1];
let index = Math.round(p * arr.length) - 1;
// Ind may be below 0, in this case the closest value is the 0th index.
index = index < 0 ? 0 : index;
return arr[index];
};
// Synthesize aggregate metrics from the supplied trend values, which will
// result in a percentile value per trend if an aggregate metric isn't already
// supplied for the trend.
const synthesizeMissingAggTrends = (aggTrends, trendMap, trendValuesByDataset, percentile) => {
const byTrend = _.groupBy(trendValuesByDataset, 'trend');
const trends = _.keys(byTrend);
if (!aggTrends) {
aggTrends = [];
}
const aggTrendsByTrendName = _.indexBy(aggTrends, 'trend_name');
for (let i = 0; i < trends.length; i++) {
if (!_.has(aggTrendsByTrendName, trends[i])) {
const aggTrend = _.clone(trendMap[trends[i]]);
const trendVals = _.chain(byTrend[aggTrend.name])
.pluck('current')
.map((value) => {
return deArray(value, d3.median);
})
// '+' converts values to numeric for a numeric sort.
.sortBy((num) => { return +num; })
.value();
aggTrend.history = [calcPercentile(trendVals, percentile / 100)];
aggTrend.title = `Default of ${percentile} percentile key metric value (${aggTrend.name}), No saved aggregate metrics for trend`;
aggTrend.synth = true;
aggTrends.push(aggTrend);
}
}
return aggTrends;
};
// Creates a valid display_name and id_selector per trend, create a mouseover
// title property, and determines if the threshold is correctly defined.
const sanitizeTrend = (trend) => {
if (!trend.abbreviation) {
trend.display_name = trend.name;
if (!trend.title) {
trend.title = 'No abbreviation defined';
}
} else {
trend.display_name = trend.abbreviation;
if (!trend.title) {
trend.title = trend.name;
}
}
if (!_.has(trend, 'warning') || !_.has(trend, 'fail')) {
trend.incompleteThreshold = true;
trend.title += ' & Incomplete threshold definition';
}
trend.id_selector = sanitizeSelector(trend.display_name);
return trend;
};
/**
* Ensures that an aggregate metric has a max value set, as a fallback
* it will be set to the last value in the history.
*/
const sanitizeAggregateThreshold = (aggTrend) => {
if (_.isNaN(parseFloat(aggTrend.max))) {
aggTrend.max = aggTrend.history[aggTrend.history.length - 1];
if (!aggTrend.incompleteThreshold) {
aggTrend.incompleteThreshold = true;
aggTrend.title += ' & Incomplete threshold definition';
}
}
return aggTrend;
};
class TrackerDash extends VisComponent {
constructor (el, settings) {
super(el);
this.$el = $(this.el);
// Perform all the data munging at the outset so that it is consistent as it
// gets passed down throughout the application.
if (!settings.trends) {
settings.trends = [];
}
// trendMap maps full trend name to a sanitized trend object.
settings.trendMap = {};
_.each(settings.trends, function (trend) {
settings.trendMap[trend.name] = sanitizeTrend(trend);
});
// Create trends for any scalars that don't supply them, setting
// the max as the max input value for that trend.
_.each(settings.trendValuesByDataset, function (trendValue) {
if (!_.has(settings.trendMap, trendValue.trend)) {
const current = deArray(trendValue.current, d3.median);
const syntheticTrend = sanitizeTrend({
name: trendValue.trend,
synth: true,
max: current
});
settings.trendMap[syntheticTrend.name] = syntheticTrend;
settings.trends.push(syntheticTrend);
} else {
const current = deArray(trendValue.current, d3.median);
const trend = settings.trendMap[trendValue.trend];
if (trend.synth && trend.max < current) {
trend.max = current;
}
}
});
// Sort trends now that they have display_name property.
settings.trends = _.sortBy(settings.trends, 'display_name');
// Order the individual trend dataset values by trend display_name.
settings.trendValuesByDataset = _.sortBy(settings.trendValuesByDataset, function (val) {
return settings.trendMap[val.trend].display_name;
});
// Generate aggregate trends if needed.
const percentile = 50.0;
const aggTrends = synthesizeMissingAggTrends(settings.agg_trends, settings.trendMap, settings.trendValuesByDataset, percentile);
settings.aggTrends = _.chain(aggTrends)
.map(sanitizeTrend)
.map(sanitizeAggregateThreshold)
.sortBy('display_name')
.value();
this.trackData = settings;
delete this.trackData.el;
this.$el.html(layout());
this.topInfoBar = new TopInfoBar(this.$el.find('.top-info-bar').get(0), this.trackData);
this.infoPane = new InfoPane(this.$el.find('.info-pane').get(0), this.$el.find('.status-bar-widget').get(0), this.trackData);
this.trendPane = new TrendPane(this.$el.find('.trend-pane').get(0), this.trackData);
this.resultPane = new ResultTablePane(this.$el.find('.result-table-pane').get(0), this.trackData);
this.render();
}
render () {
this.topInfoBar.render();
this.infoPane.render();
this.trendPane.render();
this.resultPane.render();
}
}
export default TrackerDash;
|