forked from Azure/azure-kusto-trender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploreeventspayg.html
261 lines (224 loc) · 11.6 KB
/
exploreeventspayg.html
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
<!DOCTYPE html>
<html style="height: 100%;">
<head>
<title>Explore Events PAYG</title>
<!-- styles are only used for styling header and auth elements, where possible -->
<link rel="stylesheet" type="text/css" href="../styles.css" />
<!-- boilerplate headers are injected with head.js, grab them from the live example header, or include a link to head.js -->
<script src="../boilerplate/head.js"></script>
<!-- boilerplate auth code is injected with auth.js, check it out for auth setup -->
<script src="../boilerplate/auth.js"></script>
</head>
<body style="font-family: 'Segoe UI', sans-serif; height: 100%; position: relative; overflow: hidden;">
<div id="availability" style="width: 100%; height: 200px; margin-top: 40px; background: #fff"></div>
<div id="intervalSliderWrapper"
style="width: 300px; height: 40px; position: absolute; margin-top: -40px; font-size: 12px; margin-left: 50px;">
Interval Size
<div style="position: relative; top: 22px; left: 20px;" id="intervalSlider"></div>
</div>
<div id="hierarchy" style="width: 300px; height: calc(100% - 240px);background: #fff; border-top: 1px solid #ccc;">
</div>
<div id="chart1"
style="width: calc(100% - 300px); height: calc(100% - 240px);position: absolute; left: 300px; top: 200px; background: #fff; border: 1px solid #ccc;">
</div>
<script>
window.onload = async function () {
initAuth('Line chart with events exploration. Drag a region and select "Explore Events"'); // initiate auth objects, header, and login modal
var kustoTrender = new KustoTrender(), lineChart;
var startDate = new Date('2017-04-14T13:00:00Z');
var endDate = new Date(startDate.valueOf() + 1000 * 60 * 60 * 24);
var tsqExpressions = [];
var colors = ['#3195E3', '#D869CB', '#FF8C00', '#8FE6D7', '#008272', '#F7727E', '#E0349E', '#C8E139', '#60B9AE',
'#93CFFB', '#854CC7', '#258225', '#0078D7', '#FF2828', '#FFF100'];
var currentTsqECount = 0;
const cluster = new ADX.ADXTrenderClient(
"https://help.kusto.windows.net/",
authContext.getTsiToken,
"Trender"
);
var brushActions = [
{
name: "Zoom",
action: (from, to) => {
getResults(new Date(from), new Date(to));
availabilityChart.setBrush(new Date(from), new Date(to));
}
}
];
var availabilityChart = new kustoTrender.ux.AvailabilityChart(document.getElementById('availability'));
var lineChart = new kustoTrender.ux.LineChart(document.getElementById('chart1'));
// get results from server and render them in a line chart
var getResults = async (from, to, bs = null) => {
if (!from && !to) {
from = startDate;
to = endDate;
}
if (from && to && !bs) {
var newBucketSizes = getValidBucketSizes(from.valueOf(), to.valueOf());
var newBucketSize = getNewBucketSize(validBucketSizes, newBucketSizes, bucketSize);
renderIntervalSlider(from, to, newBucketSizes, newBucketSize);
}
else {
tsqExpressions.forEach(tsqe => { tsqe.searchSpan.bucketSize = bs });
bucketSize = bs;
}
const result = await cluster.getAggregates(
tsqExpressions.flatMap((exp) => exp.instanceObject.timeSeriesId),
from,
to,
bucketSize
);
lineChart.render(result, { theme: 'light', grid: true, tooltip: true, brushContextMenuActions: brushActions, autoTriggerBrushContextMenu: true, offset: 'Local', color: '#136BFB' });
startDate = from;
endDate = to;
}
var delegate = new ADX.HierarchyDelegate(cluster)
var hierarchy = new kustoTrender.ux.HierarchyNavigation(document.getElementById('hierarchy'));
hierarchy.render(delegate, { theme: 'light', onInstanceClick: instance => onInstanceClick(instance) })
var addInstanceAction = (type, vName, instance) => {
addInstanceAsQuery(instance, type, instance.name, vName);
};
var addInstanceAsQuery = (instance, type, nodeName, variableName) => {
let tsq, variableObject, chartDataOptions = {};
if (type.variables.avg && type.variables.avg.aggregation && type.variables.avg.aggregation.tsx === 'avg($value)') {
variableObject = type.variables;
} else {
variableObject = { [variableName]: type.variables[variableName] };
}
let alias = instance.name ? instance.name : instance.timeSeriesId.join(',');
chartDataOptions['alias'] = alias;
chartDataOptions['tsid'] = instance.timeSeriesId.join(',');
chartDataOptions['color'] = colors[currentTsqECount];
chartDataOptions['includeEnvelope'] = true;
currentTsqECount++;
var tsqExp = new kustoTrender.ux.TsqExpression(instance, variableObject, {}, chartDataOptions);
tsqExpressions.push(tsqExp);
getResults();
}
var getLtsExploreEventsPayload = (queries) => {
var fromTime = startDate;
var toTime = endDate;
var maxEvents = 5000;
let tsqArray = queries.map(q => q.toTsq(true, true));
tsqArray.forEach((tsq, i) => { tsq.getEvents.searchSpan.from = fromTime; tsq.getEvents.searchSpan.to = toTime; tsq.getEvents.take = (Math.floor(maxEvents / tsqArray.length) + (i < maxEvents % tsqArray.length ? 1 : 0)) });
return tsqArray;
}
function hideEvents() {
document.getElementById('eventsWrapper').style.transform = 'translateX(100%)';
setTimeout(function () {
document.getElementById('events').innerHTML = '';
}, 400);
}
// Utility functions for determining bucket size when you adjust the timeframe
var validTimeIntervals = (() => {
let validTimeIntervals = [];
for (let i = 1; i < 1000; i++) {
if (1000 % i === 0) {
validTimeIntervals.push(i + 'ms');
}
}
for (let i = 1; i < 60; i++) {
if (60 % i === 0) {
validTimeIntervals.push(i + 's');
}
}
for (let i = 1; i < 60; i++) {
if (60 % i === 0) {
validTimeIntervals.push(i + 'm');
}
}
for (let i = 1; i < 24; i++) {
if (24 % i === 0) {
validTimeIntervals.push(i + 'h');
}
}
for (let i = 1; i < 8; i++) {
validTimeIntervals.push(i + 'd');
}
return validTimeIntervals;
})();
var getDimensionAndIntegerForRangeAndBuckets = (zoomMin, zoomMax, targetBuckets) => {
let timeRangeInMillis = Math.max(zoomMax - zoomMin, 1);
let bucketSizeInMillis = Math.ceil(timeRangeInMillis / targetBuckets);
let int, dim;
if (bucketSizeInMillis < 1000) {
dim = 'ms';
int = bucketSizeInMillis;
}
else if (bucketSizeInMillis < 1000 * 60) {
dim = 's';
int = Math.ceil(bucketSizeInMillis / 1000);
}
else if (bucketSizeInMillis < 1000 * 60 * 60) {
dim = 'm';
int = Math.ceil(bucketSizeInMillis / (1000 * 60));
}
else if (bucketSizeInMillis < 1000 * 60 * 60 * 24) {
dim = 'h';
int = Math.ceil(bucketSizeInMillis / (1000 * 60 * 60));
}
else {
dim = 'd';
int = Math.ceil(bucketSizeInMillis / (1000 * 60 * 60 * 24));
}
// round to next smallest interval that is a valid interval
let idx = -1;
while (idx === -1) {
idx = validTimeIntervals.indexOf(int + dim);
if (idx === -1) {
int--;
}
}
return validTimeIntervals[idx];
}
var getValidBucketSizes = (fromMillis, toMillis) => {
let minIntAndDim = getDimensionAndIntegerForRangeAndBuckets(fromMillis, toMillis, 1000);
let maxIntAndDim = getDimensionAndIntegerForRangeAndBuckets(fromMillis, toMillis, 1);
let minIdx = validTimeIntervals.indexOf(minIntAndDim);
let maxIdx = validTimeIntervals.indexOf(maxIntAndDim);
return validTimeIntervals.slice(minIdx, maxIdx + 1);
}
var getNewBucketSize = (oldValidBucketSizes, newValidBucketSizes, oldBucketSize) => {
let prevPercentile = (oldBucketSize && oldValidBucketSizes.indexOf(oldBucketSize) !== -1) ? (oldValidBucketSizes.indexOf(oldBucketSize)) / oldValidBucketSizes.length : .1;
let newBucketIndex = Math.max(Math.min(Math.round(prevPercentile * newValidBucketSizes.length), newValidBucketSizes.length - 1), 0);
return newValidBucketSizes[newBucketIndex];
}
// interval slider
var intervalSlider = new kustoTrender.ux.Slider(document.getElementById('intervalSlider'));
var renderIntervalSlider = (from, to, newValidBucketSizes, newBucketSize) => {
validBucketSizes = newValidBucketSizes;
bucketSize = newBucketSize;
intervalSlider.render(validBucketSizes.map(n => ({ label: n.toString(), action: () => getResults(null, null, n) })), { theme: 'light', throttleSlider: true }, 300, bucketSize);
}
var validBucketSizes = getValidBucketSizes(startDate.valueOf(), endDate.valueOf());
var bucketSize = getDimensionAndIntegerForRangeAndBuckets(startDate.valueOf(), endDate.valueOf(), 100);
renderIntervalSlider(startDate, endDate, validBucketSizes, bucketSize);
var onInstanceClick = function (instance) {
hierarchy.drawContextMenu([{
name: "Average",
action: () => addInstanceAction(instance.type, "Average", instance)
}], {});
}
// get availability, then get results
const result = await cluster.getAvailability();
availabilityChart.render(
result.availability,
{
theme: "light",
color: "#136BFB",
legend: "hidden",
brushMoveEndAction: (from, to) => {
getResults(from, to);
},
},
{
range: result.range,
intervalSize: "1d",
}
);
endDate = new Date(result.range.to);
startDate = new Date(endDate.valueOf() - 1000 * 60 * 60 * 24);
};
</script>
</body>
</html>