This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TeneoGoogleCharts-t5.html
406 lines (335 loc) · 12.4 KB
/
TeneoGoogleCharts-t5.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Basic Styling -->
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<!-- Use Google Charts for visualisation -->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript"> // Contains all the Inquire API calls
async function login(server, username, password) {
return fetch(`${backendUrl(server)}auth/login`, {
method: 'post',
headers: {
"Content-type": "application/json; charset=utf-8"
},
body: `{"pass":"${password}","user":"${username}"}`
})
.then(throwOnError)
.then(text);
}
async function runquery(server, lds, authentication, query) {
var startqueryresult = await fetch(`${backendUrl(server)}tql/submit?index=${encodeURI(lds)}&timeout=1200`, {
method: 'post',
headers: {
"Authorization": authentication,
"Content-type": "application/x-www-form-urlencoded"
},
body: `query=${encodeURIComponent(query)}&index=${encodeURI(lds)}`
})
.then(throwOnError)
.then(json);
console.log('Running query...', query);
const poll = async queryid => {
let response = null
do {
// console.log("polling", queryid);
response = await fetch(`${backendUrl(server)}tql/poll?id=${queryid}`, {
headers: {
"Authorization": authentication,
"Content-type": "application/json; charset=utf-8"
}
})
.then(throwOnError)
.then(json);
}
while (response.type != "final")
return response;
}
return poll(startqueryresult.id);
}
function throwOnError(response) {
if (response.ok) {
return response;
} else {
throw new Error(`[${response.status}]: ${response.statusText}`);
}
}
function json(response) {
return response.json()
}
function text(response) {
return response.text()
}
function backendUrl(server) {
return server + "teneo-inquire-query/rest/";
}
</script>
<script type="text/javascript"> // Page functionality starts here...
// You can pass parameters server / lds - if they are not set then defaults are used
var server = paramOrDefault("server", "https://your_team_name.data.teneo.ai/");
var lds = paramOrDefault("lds", "your_lds_here");
var authentication = "";
if (sessionStorage['authentication']) {
authentication = sessionStorage['authentication'];
}
// So the graphs can be redrawn without re-querying we store the data after it is loaded the first time
var sessionsData;
var flowWeightData;
var sessionPathData;
var inputBreakdownData;
var inputBreakdownOptions;
// Used to get a named parameter (or default value) from the page url parameters
function paramOrDefault(paramName, defaultValue) {
let pageUrl = new URL(window.location.href);
let paramValue = pageUrl.searchParams.get(paramName);
if (paramValue) return paramValue;
return defaultValue;
}
// Called on initial load of the page
async function load() {
titleElement.innerText = `${lds}`
google.charts.load('current', { packages: ['bar'] });
google.charts.load('current', { packages: ['wordtree'] });
google.charts.load("current", { packages: ["sankey"] });
google.charts.load('current', { packages: ['corechart'] });
try {
if (authentication) {
hideLoginPrompt();
// If we're already authenticated then don't ask.
// Only load charts once google chart libraries are fully loaded
google.charts.setOnLoadCallback(drawAllCharts);
}
}
catch (error) {
console.log("drawing from preloaded data", error);
google.charts.load('current', { packages: ['corechart'] });
google.charts.setOnLoadCallback(function () {
google.visualization.errors.addError(piechart, error.name, error.message, { 'showInTooltip': false });
});
return;
}
}
// Called when the body resizes
async function resize() {
drawAllCharts();
}
// Hides the login prompt
async function hideLoginPrompt() {
loginPopup.style.display = "none";
}
// Used by the login submit - to continue login and cancel the reload of the page
function submitLogin(f) {
loginAndContinue(f);
return false;
}
// Authenticate, then trigger the drawing of the graphs
async function loginAndContinue(f) {
try {
authentication = await login(server, f.username.value, f.password.value)
// store token in session storage so we can reload page without needing to login again
if (authentication) {
sessionStorage['authentication'] = authentication;
}
f.style.display = "none";
drawAllCharts();
}
catch (error) {
console.log("Login failed", error);
google.charts.load('current', { packages: ['corechart'] });
google.charts.setOnLoadCallback(function () {
google.visualization.errors.addError(loginPopup, error.name, error.message, { 'showInTooltip': false });
});
return;
}
}
// ----------- Graph drawing from here on -----------
function drawAllCharts() {
if (!authentication) return; // Don't try to do anything if we're not logged in
resultsContainer.style.visibility = "visible";
drawSessionsBarChart();
drawWordTree();
drawPieChart();
drawSankey();
}
// sessions per day
async function drawSessionsBarChart() {
// get data for chart
if (!sessionsData) {
console.log('Loading sessions per day data');
let queryresponse = await runquery(server, lds, authentication, 'd date : catd(model="date") s.beginTime as date order by date asc');
if (queryresponse) {
sessionsData = new google.visualization.DataTable();
// add columns
sessionsData.addColumn('string', 'Date');
sessionsData.addColumn('number', 'Count');
// iterate query results and add date and count to sessionData
queryresponse.result.forEach(function (day) {
sessionsData.addRow([day.date, day.count]);
});
}
}
// draw chart if data is available
if (sessionsData) {
console.log('Drawing sessions per day bar chart');
// Optional; add a title and set the width and height of the chart
var options = {
height: 400,
colors: ['#311b92'],
legend: { position: 'none' }
};
// Display the chart inside the <div> element with id="sessionsBarChart"
var chart = new google.charts.Bar(document.getElementById('sessionsPerDay'));
chart.draw(sessionsData, options);
sessionsPerDayLoading.style.visibility = "hidden";
console.log('Drawn sessions per day bar chart');
}
}
// Input words tree
async function drawWordTree() {
if (!inputBreakdownData) {
console.log('Loading input word data');
inputBreakdownOptions = {
wordtree: {
format: 'implicit',
type: 'double'
}
};
inputBreakdownData = new google.visualization.DataTable();
inputBreakdownData.addColumn('string', 'Inputs');
let wordqueryresult = await runquery(server, lds, authentication, 'd t.e.userInputWords as word : t.e.userInputWords != "" order by count desc limit 1');
if (wordqueryresult.result.length > 0) {
inputBreakdownOptions.wordtree.word = wordqueryresult.result[0].word;
}
let inputsqueryresult = await runquery(server, lds, authentication, "lu t.e.userInput as text");
inputsqueryresult.result.forEach(function (input) {
if (null != input.text) {
inputBreakdownData.addRow([input.text.toLowerCase()]);
}
});
}
console.log('Drawing input word tree');
var chart = new google.visualization.WordTree(document.getElementById('inputBreakdown'));
chart.draw(inputBreakdownData, inputBreakdownOptions);
inputBreakdownLoading.style.visibility = "hidden";
console.log('Drawn input word tree');
}
// Flow weightings pie chart
async function drawPieChart() {
if (!flowWeightData) {
console.log('Loading flow weighting data');
flowWeightData = new google.visualization.DataTable();
let queryresponse = await runquery(server, lds, authentication, 'd t.e.fname as name : t.e.pathType == "flow-trigger" order by count desc limit 10');
flowWeightData.addColumn('string', 'Flow Name');
flowWeightData.addColumn('number', 'Count');
queryresponse.result.forEach(function (flowcount) {
flowWeightData.addRow([flowcount.name, flowcount.count]);
});
}
console.log('Drawing flow weight pie chart');
// Optional; add a title and set the width and height of the chart
var options = { height: 600, chartArea: { width: '75%' }, legend: { position: 'labeled' } };
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('flowWeight'));
chart.draw(flowWeightData, options);
flowWeightLoading.style.visibility = "hidden";
console.log('Drawn flow weights');
}
async function drawSankey() {
if (!sessionPathData) {
console.log('Loading session path data');
sessionPathData = new google.visualization.DataTable();
sessionPathData.addColumn('string', 'From');
sessionPathData.addColumn('string', 'To');
sessionPathData.addColumn('number', 'Weight');
let queryresponse = await runquery(server, lds, authentication, 'd e1.fname as source, e2.fname as destination : e1.pathType == "raise-flow", e1-{pathType == "raise-flow"}>e2, e1.fname != e2.fname limit 50');
let edgeMap = new Map();
queryresponse.result.forEach(function (entry) {
let entrySet = edgeMap.get(entry.source);
if (entrySet == null) {
//console.log('adding map entry', entry.source);
entrySet = new Set();
edgeMap.set(entry.source, entrySet);
}
let doAdd = true;
if (findCycle(edgeMap, entry.source, entry.source)) {
// console.log('cyclic entry adjusted (source)', `${entry.source}:${entry.destination}`);
entry.source += " (cycle)";
}
else if (findCycle(edgeMap, entry.source, entry.destination)) {
// console.log('cyclic entry adjusted (destination)', `${entry.source}:${entry.destination}`);
entry.destination += " (cycle)";
}
if (doAdd) {
entrySet.add(entry.destination);
//console.log('adding row entry', `${entry.source}:${entry.destination} [${entry.count}]`);
sessionPathData.addRow([entry.source, entry.destination, entry.count]);
}
});
}
console.log('Drawing session path sankey');
// Set chart options
var options = { height: 500 };
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.Sankey(document.getElementById('sessionPath'));
chart.draw(sessionPathData, options);
sessionPathLoading.style.visibility = "hidden";
console.log('Drawn session path sankey');
}
function findCycle(edgeMap, originalsource, source) {
// foreach new entry
// check all existing entries, starting from source and following all destinations
let sourceSet = edgeMap.get(source);
if (null != sourceSet) {
if (sourceSet.has(originalsource)) {
return true;
}
else {
let cycleFound = false;
sourceSet.forEach(function (d) {
if (findCycle(edgeMap, originalsource, d)) cycleFound = true;
});
return cycleFound;
}
}
return false;
}
</script>
</head>
<body onload="load()" onresize="resize()">
<header class="w3-container w3-purple">
<h1 id="titleElement"></h1>
</header>
<div id="loginPopup">
<form id="loginForm" action="login" onsubmit="return submitLogin(this);">
<center>Username:</center>
<center><input name="username" size="26" /></center>
<center>Password:</center>
<center><input name="password" type="password" size="26" /></center>
<center><input type="submit" value="Login" style="margin: 2px" /></center>
</form>
</div>
<div id="resultsContainer" style="visibility: hidden">
<div class="w3-container">
<h4>Sessions Per Day</h4>
<h6 id="sessionsPerDayLoading">Loading...</h6>
</div>
<div id="sessionsPerDay" style="height: 25%; margin: 1%"></div>
<div class="w3-container">
<h4>Most popular flows</h4>
<h6 id="flowWeightLoading">Loading...</h6>
</div>
<div id="flowWeight" style="height: 25%; margin: 1%"></div>
<div class="w3-container">
<h4>Word Cloud</h4>
<h6 id="inputBreakdownLoading">Loading...</h6>
</div>
<div id="inputBreakdown" style="height: 25%; margin: 1%"></div>
<div class="w3-container">
<h4>Customer Journey</h4>
<h6 id="sessionPathLoading">Loading...</h6>
</div>
<div id="sessionPath" style="height: 25%; margin: 1%"></div>
</div>
</body>
<html>