-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
624 lines (572 loc) · 26.2 KB
/
index.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<title>Smart Study Rooms</title>
</head>
<body style="background: rgb(243, 243, 243)">
<div id="app">
<header>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="#" style="font-weight: bold;">Distributed Systems 18/19</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="#">Grupo 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Grupo 2</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Grupo 3</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Grupo 4</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Grupo 5</a>
</li>
</ul>
<ul class="nav navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link">Good morning <b>Gertrudes Almeida</b></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Logout</a>
</li>
</ul>
</div>
</nav>
</header>
<div class="container" style="margin-top: 80px" v-show="view == 'charts'">
<nav>
<ol class="breadcrumb bg-dark">
<li class="breadcrumb-item text-white">Smart Study Rooms</li>
<li class="breadcrumb-item text-white">Charts</li>
</ol>
</nav>
<div class="row mb-2">
<div class="col-md-2 my-2">
<label>Time Range</label>
<div class="dropdown">
<button class="btn btn-dark dropdown-toggle" type="button" data-toggle="dropdown" :disabled="loading">
{{ time_range }}
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#" @click="time_range = 'Now'">Now</a>
<a class="dropdown-item" href="#" @click="time_range = 'Last 1h'">Last 1h</a>
<a class="dropdown-item" href="#" @click="time_range = 'Last 3h'">Last 3h</a>
<a class="dropdown-item" href="#" @click="time_range = 'Last 6h'">Last 6h</a>
<a class="dropdown-item" href="#" @click="time_range = 'Last 12h'">Last 12h</a>
<a class="dropdown-item" href="#" @click="time_range = 'Last 24h'">Last 24h</a>
<a class="dropdown-item" href="#" @click="time_range = 'Last 2d'">Last 2d</a>
<a class="dropdown-item" href="#" @click="time_range = 'Last 7d'">Last 7d</a>
<a class="dropdown-item" href="#" @click="time_range = 'Last 30d'">Last 30d</a>
<a class="dropdown-item" href="#" @click="time_range = 'Custom'">Custom</a>
</div>
</div>
</div>
<div v-if="time_range == 'Custom'" class="col-md-3 my-2">
<label>From</label>
<input type="date" class="form-control" v-model="custom_from" :disabled="loading">
</div>
<div v-if="time_range == 'Custom'" class="col-md-3 my-2">
<label>To</label>
<input type="date" class="form-control" v-model="custom_to" :disabled="loading">
</div>
</div>
<div class="row" v-if="error != ''">
<div class="col-md-12 my-2">
<div class="alert alert-danger">
<strong>Error:</strong> {{ error }}
</div>
</div>
</div>
<div class="row" v-show="time_range == 'Now'">
<div class="col-md-6 my-2">
<div class="p-2 bg-white">
<canvas id="occupation-rate-chart"></canvas>
</div>
</div>
<div class="col-md-6 my-2">
<div class="p-2 bg-white">
<canvas id="seats-chart"></canvas>
</div>
</div>
</div>
<div class="row mb-3" v-show="time_range == 'Now'">
<div class="col-md-6 my-2">
<div class="p-2 bg-white">
<canvas id="daily-occupation-chart"></canvas>
</div>
</div>
<div class="col-md-6 my-2">
<div class="p-2 bg-white">
<canvas id="occupation-rate-predicted-chart"></canvas>
</div>
</div>
</div>
<div class="row mb-3" v-show="time_range != 'Now'">
<div class="col-md-6 my-2">
<div class="p-2 bg-white">
<canvas id="occupation-history-chart"></canvas>
</div>
</div>
<div class="col-md-6 my-2">
<div class="p-2 bg-white">
<canvas id="occupation-rate-history-chart"></canvas>
</div>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
view: 'charts',
loading: false,
time_range: '',
occupation_rate_chart: null,
seats_chart: null,
daily_occupation_chart: null,
occupation_rate_predicted_chart: null,
occupation_history_chart: null,
occupation_rate_history_chart: null,
custom_from: null,
custom_to: null,
error: ''
},
watch: {
time_range: function () {
console.log(this.time_range)
if (this.time_range == 'Now') {
//Vue.nextTick(() => {
this.fetchData()
//})
} else {
if (this.time_range != 'Custom') {
//Vue.nextTick(() => {
this.fetchHistoryData()
//})
}
}
},
custom_from: function () {
this.customUpdated()
},
custom_to: function () {
this.customUpdated()
}
},
methods: {
customUpdated() {
if (this.time_range == 'Custom' && this.custom_from != null && this.custom_to != null) {
console.log(this.custom_from, this.custom_to)
this.fetchHistoryData()
}
},
clearChartData(chart) {
chart.data = {
labels: [],
datasets: []
}
chart.update()
},
initCharts() {
/*----------------------------------------*/
/* Occupation Rate Chart
/*----------------------------------------*/
this.occupation_rate_chart = new Chart('occupation-rate-chart', {
type: 'bar',
options: {
scales: {
yAxes: [{
ticks: {
min: 0,
max: 100
}
}]
},
title: {
display: true,
text: 'Taxa de ocupação',
fontSize: 16
},
legend: {
display: false
}
}
})
/*----------------------------------------*/
/* Seats Chart
/*----------------------------------------*/
this.seats_chart = new Chart('seats-chart', {
type: 'bar',
options: {
responsive: true,
legend: {
position: 'bottom',
},
title: {
display: true,
text: 'Lugares',
fontSize: 16
},
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true
}]
},
tooltips: {
mode: 'index',
intersect: true
}
}
})
/*----------------------------------------*/
/* Daily Occupation Chart
/*----------------------------------------*/
this.daily_occupation_chart = new Chart('daily-occupation-chart', {
type: 'line',
options: {
responsive: true,
legend: {
position: 'bottom',
labels: {
usePointStyle: true
}
},
title: {
display: true,
text: 'Ocupação diária',
fontSize: 16
},
tooltips: {
mode: 'index',
intersect: true
}
}
})
/*----------------------------------------*/
/* Occupation Rate Predicted Chart
/*----------------------------------------*/
this.occupation_rate_predicted_chart = new Chart('occupation-rate-predicted-chart', {
type: 'line',
options: {
responsive: true,
legend: {
position: 'bottom',
labels: {
usePointStyle: true
}
},
title: {
display: true,
text: 'Taxa de ocupação prevista',
fontSize: 16
},
tooltips: {
mode: 'index',
intersect: true
},
scales: {
yAxes: [{
ticks: {
min: 0,
max: 100
}
}],
xAxes: [{
type: 'time'
}]
}
}
})
/*----------------------------------------*/
/* Occupation History Chart
/*----------------------------------------*/
this.occupation_history_chart = new Chart('occupation-history-chart', {
type: 'line',
options: {
responsive: true,
legend: {
position: 'bottom',
labels: {
usePointStyle: true
}
},
title: {
display: true,
text: 'Ocupação',
fontSize: 16
},
scales: {
xAxes: [{
type: 'time'
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Occupied Seats'
}
}]
}
}
})
/*----------------------------------------*/
/* Occupation Rate History Chart
/*----------------------------------------*/
this.occupation_rate_history_chart = new Chart('occupation-rate-history-chart', {
type: 'line',
options: {
responsive: true,
legend: {
position: 'bottom',
labels: {
usePointStyle: true
}
},
title: {
display: true,
text: 'Taxa de ocupação',
fontSize: 16
},
scales: {
yAxes: [{
ticks: {
min: 0,
max: 100
},
scaleLabel: {
display: true,
labelString: '%'
}
}],
xAxes: [{
type: 'time'
}]
}
}
})
},
async fetchData() {
console.log('fetchData')
this.loading = true
this.error = ''
this.clearChartData(this.occupation_rate_chart)
this.clearChartData(this.seats_chart)
this.clearChartData(this.daily_occupation_chart)
this.clearChartData(this.occupation_rate_predicted_chart)
let rooms = []
let rooms_last_event = []
try {
const rooms_response = await axios.get('http://smartrooms.ddns.net:5000/api/rooms')
rooms = rooms_response.data
rooms_last_event = await Promise.all(rooms.map(async r => {
last_event_response = await axios.get(`http://smartrooms.ddns.net:5000/api/room/${r.id}/last-event`)
if (last_event_response.data && last_event_response.data.length) return last_event_response.data[0]
return null
}))
} catch (e) {
this.loading = false
this.error = e.message
return
}
// Occupation Rate Chart
this.occupation_rate_chart.data = {
labels: rooms.map(r => r.nome_sala),
datasets: [{
data: rooms_last_event.map(r => {
if (r.occupied_seats >= 0 && r.empty_seats >= 0) {
return Math.round((r.occupied_seats / (r.occupied_seats + r.empty_seats)) * 100)
} else {
return null
}
}),
backgroundColor: rooms.map(r => r.color)
}]
}
this.occupation_rate_chart.update()
// Seats Chart
this.seats_chart.data = {
labels: rooms.map(r => r.nome_sala),
datasets: [{
label: 'Livres',
data: rooms_last_event.map(r => r.empty_seats),
backgroundColor: 'rgb(141, 198, 121)'
}, {
label: 'Ocupados',
data: rooms_last_event.map(r => r.occupied_seats),
backgroundColor: 'rgb(85, 161, 195)'
}]
}
this.seats_chart.update()
// Daily occupation Chart
this.daily_occupation_chart.data = {
labels: ["Segunda", "Terça", "Quarta", "Quinta", "Sexta"],
datasets: [/*
{
label: 'Piso 0',
data: [80, 70, 35, 80, 50],
fill: false,
backgroundColor: piso_0_color,
borderColor: piso_0_color
},
{
label: 'Piso 1',
data: [60, 70, 30, 60, 50],
fill: false,
backgroundColor: piso_1_color,
borderColor: piso_1_color
},...
*/]
}
this.daily_occupation_chart.update()
// occupation Rate Predicted Chart
this.occupation_rate_predicted_chart.data = {
labels: [
new Date(2018, 11, 29, 12),
new Date(2018, 11, 29, 13),
new Date(2018, 11, 29, 14),
new Date(2018, 11, 29, 15),
new Date(2018, 11, 29, 16),
],
datasets: [/*
{
label: 'Piso 0',
data: [75, 70, 35, 80, 50],
fill: false,
backgroundColor: piso_0_color,
borderColor: piso_0_color
},
{
label: 'Piso 1',
data: [70, 72.5, 30, 62.5, 55],
fill: false,
backgroundColor: piso_1_color,
borderColor: piso_1_color
},...
*/]
}
this.occupation_rate_predicted_chart.update()
// loading false
this.loading = false
},
async fetchHistoryData() {
console.log('fetchHistoryData')
this.loading = true
this.error = ''
let from = null
let to = null
if (this.time_range == 'Custom') {
from = moment(this.custom_from).startOf('day')
to = moment(this.custom_to).endOf('day')
} else {
to = moment()
switch (this.time_range) {
case ('Last 1h'):
from = to.clone().subtract(1, 'hours')
break
case ('Last 3h'):
from = to.clone().subtract(3, 'hours')
break
case ('Last 6h'):
from = to.clone().subtract(6, 'hours')
break
case ('Last 12h'):
from = to.clone().subtract(12, 'hours')
break
case ('Last 24h'):
from = to.clone().subtract(24, 'hours')
break
case ('Last 2d'):
from = to.clone().subtract(2, 'days')
break
case ('Last 7d'):
from = to.clone().subtract(7, 'days')
break
case ('Last 30d'):
from = to.clone().subtract(30, 'days')
break
}
}
this.clearChartData(this.occupation_history_chart)
this.clearChartData(this.occupation_rate_history_chart)
let rooms = []
let rooms_history_events = []
try {
const rooms_response = await axios.get('http://smartrooms.ddns.net:5000/api/rooms')
rooms = rooms_response.data
rooms_history_events = await Promise.all(rooms.map(async r => {
const room_history_events_response = await axios.get(`http://smartrooms.ddns.net:5000/api/room/${r.id}/events/${from.toJSON()}/${to.toJSON()}`)
return room_history_events_response.data
}))
} catch (e) {
this.loading = false
this.error = e.message
return
}
// Occupation History Chart
this.occupation_history_chart.data.datasets = rooms.map((r, i) => {
return {
label: r.nome_sala,
data: rooms_history_events[i].map(e => {
return {
t: new Date(e.time),
y: (e.occupied_seats >= 0 && e.empty_seats >= 0) ? Math.round((e.occupied_seats / (e.occupied_seats + e.empty_seats)) * 100) : null
}
}),
fill: false,
backgroundColor: r.color,
borderColor: r.color
}
})
this.occupation_history_chart.update()
// Occupation Rate History Chart
this.occupation_rate_history_chart.data.datasets = rooms.map((r, i) => {
return {
label: r.nome_sala,
data: rooms_history_events[i].map(e => {
return {
t: new Date(e.time),
y: e.occupied_seats
}
}),
fill: false,
backgroundColor: r.color,
borderColor: r.color
}
})
this.occupation_rate_history_chart.update()
// loading false
this.loading = false
}
},
mounted: function () {
this.initCharts()
this.time_range = 'Now'
}
})
</script>
</body>
</html>