-
Notifications
You must be signed in to change notification settings - Fork 818
Expand file tree
/
Copy pathapp-metrics.js
More file actions
258 lines (229 loc) · 8.68 KB
/
app-metrics.js
File metadata and controls
258 lines (229 loc) · 8.68 KB
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
import './plotly-2.32.0.min.js'
export function initializeChart(id, traces, exemplarTrace, rangeStartTime, rangeEndTime, serverLocale, chartInterop) {
registerLocale(serverLocale);
var chartContainerDiv = document.getElementById(id);
if (!chartContainerDiv) {
console.log(`Couldn't find container '${id}' when initializing chart.`);
return;
}
// Reusing a div can create issues with chart lines appearing beyond the end range.
// Workaround this issue by replacing the chart div. Ensures we start from a new state.
var chartDiv = document.createElement("div");
chartContainerDiv.replaceChildren(chartDiv);
var themeColors = getThemeColors();
var data = [];
for (var i = 0; i < traces.length; i++) {
var name = traces[i].name || "Value";
var t = {
x: traces[i].x,
y: traces[i].y,
name: name,
text: traces[i].tooltips,
hoverinfo: 'text',
stackgroup: "one"
};
data.push(t);
}
var points = {
x: exemplarTrace.x,
y: exemplarTrace.y,
name: exemplarTrace.name,
text: exemplarTrace.tooltips,
hoverinfo: 'text',
traceData: exemplarTrace.traceData,
mode: 'markers',
type: 'scatter',
marker: {
size: 16,
color: themeColors.pointColor,
line: {
color: themeColors.backgroundColor,
width: 1
}
}
};
data.push(points);
// Width and height are set using ResizeObserver + ploty resize call.
var layout = {
paper_bgcolor: themeColors.backgroundColor,
plot_bgcolor: themeColors.backgroundColor,
margin: { t: 0, r: 0, b: 40, l: 50 },
xaxis: {
type: 'date',
range: [rangeEndTime, rangeStartTime],
fixedrange: true,
tickformat: "%X",
color: themeColors.textColor
},
yaxis: {
rangemode: "tozero",
fixedrange: true,
color: themeColors.textColor
},
hovermode: "closest",
showlegend: true,
legend: {
orientation: "h",
font: {
color: themeColors.textColor
},
traceorder: "normal",
itemclick: false,
itemdoubleclick: false
}
};
var options = { scrollZoom: false, displayModeBar: false };
var plot = Plotly.newPlot(chartDiv, data, layout, options);
fixTraceLineRendering(chartDiv);
// We only want a pointer cursor when the mouse is hovering over an exemplar point.
// Set the drag layer cursor back to the default and then use plotly_hover/ploty_unhover events to set to pointer.
var dragLayer = document.getElementsByClassName('nsewdrag')[0];
dragLayer.style.cursor = 'default';
// Use mousedown instead of plotly_click event because plotly_click has issues with updating charts.
// The current point is tracked by setting it with hover/unhover events and then mousedown uses the current value.
var currentPoint = null;
chartDiv.on('plotly_hover', function (data) {
var point = data.points[0];
if (point.fullData.name == exemplarTrace.name) {
currentPoint = point;
dragLayer.style.cursor = 'pointer';
}
});
chartDiv.on('plotly_unhover', function (data) {
var point = data.points[0];
if (point.fullData.name == exemplarTrace.name) {
currentPoint = null;
dragLayer.style.cursor = 'default';
}
});
chartDiv.addEventListener("mousedown", (e) => {
if (currentPoint) {
var point = currentPoint;
var pointTraceData = point.data.traceData[point.pointIndex];
chartInterop.invokeMethodAsync('ViewSpan', pointTraceData.traceId, pointTraceData.spanId);
}
});
const resizeObserver = new ResizeObserver(entries => {
for (let entry of entries) {
var display = window.getComputedStyle(entry.target).display;
var isHidden = !display || display === "none";
if (!isHidden) {
Plotly.Plots.resize(entry.target);
}
}
});
plot.then(plotyDiv => {
resizeObserver.observe(plotyDiv);
});
}
export function updateChart(id, traces, exemplarTrace, rangeStartTime, rangeEndTime) {
var chartContainerDiv = document.getElementById(id);
if (!chartContainerDiv) {
console.log(`Couldn't find container '${id}' when updating chart.`);
return;
}
var chartDiv = chartContainerDiv.firstChild;
if (!chartDiv) {
console.log(`Couldn't find div inside container '${id}' when updating chart. Chart may not have been successfully initialized.`);
return;
}
var themeColors = getThemeColors();
var xUpdate = [];
var yUpdate = [];
var tooltipsUpdate = [];
var traceData = [];
for (var i = 0; i < traces.length; i++) {
xUpdate.push(traces[i].x);
yUpdate.push(traces[i].y);
tooltipsUpdate.push(traces[i].tooltips);
traceData.push(traces.traceData);
}
xUpdate.push(exemplarTrace.x);
yUpdate.push(exemplarTrace.y);
tooltipsUpdate.push(exemplarTrace.tooltips);
traceData.push(exemplarTrace.traceData);
var data = {
x: xUpdate,
y: yUpdate,
text: tooltipsUpdate,
traceData: traceData
};
var layout = {
xaxis: {
type: 'date',
range: [rangeEndTime, rangeStartTime],
fixedrange: true,
tickformat: "%X",
color: themeColors.textColor
}
};
Plotly.update(chartDiv, data, layout);
fixTraceLineRendering(chartDiv);
}
function getThemeColors() {
// Get colors from the current light/dark theme.
var style = getComputedStyle(document.body);
return {
backgroundColor: style.getPropertyValue("--fill-color"),
textColor: style.getPropertyValue("--neutral-foreground-rest"),
pointColor: style.getPropertyValue("--accent-fill-rest")
};
}
function fixTraceLineRendering(chartDiv) {
// In stack area charts Plotly orders traces so the top line area overwrites the line of areas below it.
// This isn't the effect we want. When the P50, P90 and P99 values are the same, the line displayed is P99
// on the P50 area.
//
// The fix is to reverse the order of traces so the correct line is on top. There isn't a way to do this
// with CSS because SVG doesn't support z-index. Node order is what determines the rendering order.
//
// https://github.com/plotly/plotly.js/issues/6579
var parent = chartDiv.querySelector(".scatterlayer");
if (parent.childNodes.length > 0) {
for (var i = 1; i < parent.childNodes.length; i++) {
var child = parent.childNodes[i];
parent.insertBefore(child, parent.firstChild);
}
// Check if there is a trace with points. It should be top most.
for (var i = 0; i < parent.childNodes.length; i++) {
var child = parent.childNodes[i];
if (child.querySelector(".point")) {
// Append trace to parent to move to the last in the collection.
parent.appendChild(child);
}
}
}
}
function registerLocale(serverLocale) {
// Register the locale for Plotly.js. This is to enable localization of time format shown by the charts.
// Changing plotly.js time formatting is better than supplying values from the server which is very difficult to do correctly.
// Right now necessary changes are to:
// -Update AM/PM
// -Update time format to 12/24 hour.
var locale = {
moduleType: 'locale',
name: 'en',
dictionary: {
'Click to enter Colorscale title': 'Click to enter Colourscale title'
},
format: {
days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
periods: serverLocale.periods,
dateTime: '%a %b %e %X %Y',
date: '%d/%m/%Y',
time: serverLocale.time,
decimal: '.',
thousands: ',',
grouping: [3],
currency: ['$', ''],
year: '%Y',
month: '%b %Y',
dayMonth: '%b %-d',
dayMonthYear: '%b %-d, %Y'
}
};
Plotly.register(locale);
}