Aggiunta visualizzazione presenza device ultimi 30 giorni

This commit is contained in:
loviuz 2020-08-31 22:28:28 +02:00
parent bdf4f5b9e1
commit 157be8042c
2 changed files with 152 additions and 20 deletions

View File

@ -7,41 +7,54 @@ if( $_GET['op'] == 'get-details' && !empty($_GET['mac']) ){
?> ?>
<!-- Page Heading --> <!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-4"> <div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Orari</h1> <h1 class="h3 mb-0 text-gray-800">Orari</h1>
</div> </div>
<!-- Dispositivi per orario --> <!-- Dispositivi per orario -->
<div class="row"> <div class="row">
<div class="col-lg-12"> <div class="col-lg-12">
<div class="card shadow mb-4"> <div class="card shadow mb-4">
<div class="card-header py-3"> <div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Visibilità per orario</h6> <h6 class="m-0 font-weight-bold text-primary">Visibilità per orario</h6>
</div> </div>
<div class="card-body"> <div class="card-body">
<div class="chart-bar"> <div class="chart-bar">
<canvas id="chart-orari"></canvas> <canvas id="chart-orari"></canvas>
</div>
</div>
</div> </div>
</div> </div>
</div>
</div>
</div> </div>
<!-- Dispositivi per giorno --> <!-- Dispositivi per giorno -->
<div class="row"> <div class="row">
<div class="col-lg-12"> <div class="col-lg-12">
<div class="card shadow mb-4"> <div class="card shadow mb-4">
<div class="card-header py-3"> <div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Visibilità per giorno</h6> <h6 class="m-0 font-weight-bold text-primary">Visibilità per giorno</h6>
</div> </div>
<div class="card-body"> <div class="card-body">
<div class="chart-bar"> <div class="chart-bar">
<canvas id="chart-giorno"></canvas> <canvas id="chart-giorno"></canvas>
</div>
</div>
</div> </div>
</div> </div>
</div>
</div> </div>
<!-- Dispositivi per giorno (tabella) -->
<div class="row">
<div class="col-lg-12">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Presenza per giorno</h6>
</div>
<div class="card-body">
<div id="presence-per-day"></div>
</div>
</div>
</div> </div>
@ -49,6 +62,60 @@ if( $_GET['op'] == 'get-details' && !empty($_GET['mac']) ){
<script> <script>
drawBarChart( 'chart-orari', 'get-activity-by-hour', '<?php echo $_GET['mac']; ?>' ); drawBarChart( 'chart-orari', 'get-activity-by-hour', '<?php echo $_GET['mac']; ?>' );
drawBarChart( 'chart-giorno', 'get-activity-by-weekday', '<?php echo $_GET['mac']; ?>' ); drawBarChart( 'chart-giorno', 'get-activity-by-weekday', '<?php echo $_GET['mac']; ?>' );
var interval = 30;
var date_start = moment().subtract(interval, 'days').format('YYYY-MM-DD');
var date_end = moment().format('YYYY-MM-DD');
$.post(
reader_url,
{
op: 'get-presence-per-day',
mac: '<?php echo $_GET['mac']; ?>',
date_start: date_start,
date_end: date_end
}, function(response){
result = $.parseJSON(response);
if (result.status == 'OK') {
$('#presence-per-day').html(
'<table class="table table-striped table-condensed" id="table-presence-per-day">\
<thead>\
<tr>\
<th width="80%">Giorno</th>\
<th></th>\
</tr>\
</thead>\
<tbody></tbody>\
</table>'
);
// Lista dispositivi
var giorno = moment().subtract(interval, 'days');
console.log( result.records );
for (i=0; i<interval; i++) {
icon = '<i class="fa fa-times text-danger"></i>';
if( result.records[ giorno.format('YYYY-MM-DD') ] !== undefined ){
icon = '<i class="fa fa-check text-success"></i>';
}
$('#table-presence-per-day tbody').append(
'<tr>\
<td>' + giorno.format('DD/MM/YYYY') + '</td>\
<td class="text-center">' + icon + '</td>\
</tr>'
);
giorno = giorno.add(1, 'days');
}
} else {
$('#presence-per-day').html('Errore durante il recupero dati.');
}
}
);
</script> </script>
<?php <?php
} }

View File

@ -470,4 +470,69 @@ switch( $_POST['op'] ){
$stmt->close(); $stmt->close();
$mysqli->close(); $mysqli->close();
break; break;
case 'get-presence-per-day':
// Connessione al database
$mysqli = mysqli_connect($config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name']);
// Errore nella connessione a database
if (mysqli_connect_errno($mysqli)) {
$result = [
'status' => 'ERR',
'message' => mysqli_connect_error(),
];
} else {
// Lettura presenza per giorno
$sql = "
SELECT
DATE_FORMAT(received_at, '%Y-%m-%d') AS giorno,
COUNT(id) AS valore
FROM
`logs`
WHERE
`mac` = ? AND received_at BETWEEN ? AND ?
GROUP BY
DATE_FORMAT(received_at, '%Y-%m-%d')
ORDER BY
received_at ASC";
$stmt = $mysqli->prepare($sql);
// Errore nella preparazione query
if (!$stmt) {
$result = [
'status' => 'ERR',
'message' => $mysqli->error,
];
} else {
if ($stmt->bind_param('sss', $mac, $date_start, $date_end)) {
$mac = $_POST['mac'];
$date_start = $_POST['date_start'];
$date_end = $_POST['date_end'];
// Esecuzione statement
$stmt->execute();
$rs = $stmt->get_result();
while ($row = $rs->fetch_assoc()) {
$records[ $row['giorno'] ] = $row;
}
$result = [
'status' => 'OK',
'records' => (array)$records,
];
}
}
}
echo json_encode($result);
$stmt->close();
$mysqli->close();
break;
} }