Version 0.9.1
<?php
error_reporting(E_ERROR | E_PARSE);
if(isset($argv[1])) $log = $argv[1]; //log file
if(!$log) $log = '/var/log/smartHome/smart.log';
if(isset($argv[2])) $rows = $argv[2]; //last x rows
echo "Log: $log \n";
if(!is_file($log)) exit('$1 Must be a File');
$lines = file($log);
$by_date = [];
$by_user = [];
$by_device = [];
$length = [];
$length['by_date'] = [];
$length['by_user'] = [];
$length['by_device'] = [];
$smartModes = ['turn_off', 'turn_on', 'set_device', 'set_scene', 'control_app', 'on_for', 'unknown'];
/*
smart.log
date <%Y-%m-%d_%H:%M:%S.%N> USER mode value
*/
$i = 0;
$i = (isset($rows))? count($lines) - $rows : $i;
if($i < 0) $i = 0;
for(;$i < count($lines);$i++) {
$line = $lines[$i];
$e = explode(' ',$line);
$date = substr($line,0,10);
$user = $e[1];
$mode = $e[2];
$device = $e[3];
if($user == "") $user = "unknown";
if(! in_array($mode,$smartModes)) $mode = "unknown";
(!isset($by_date[$date][$mode]))? $by_date[$date][$mode] = 1 : $by_date[$date][$mode]++;
(!isset($by_user[$user][$mode]))? $by_user[$user][$mode] = 1 : $by_user[$user][$mode]++;
(!isset($by_device[$device][$mode]))? $by_device[$device][$mode] = 1 : $by_device[$device][$mode]++;
}
foreach($by_date as $k => $v) {
if($length['by_date'][0] < strlen($k)) $length['by_date'][0] = strlen($k);
foreach($smartModes as $s) {
echo $v[$s] ."\t";
if($length['by_date'][$s] < strlen($v[$s])) $length['by_date'][$s] = strlen($v[$s]);
if($length['by_date'][$s] < strlen($s)) $length['by_date'][$s] = strlen($s);
}
}
foreach($by_user as $k => $v) {
if($length['by_user'][0] < strlen($k)) $length['by_user'][0] = strlen($k);
foreach($smartModes as $s) {
echo $v[$s] ."\t";
if($length['by_user'][$s] < strlen($v[$s])) $length['by_user'][$s] = strlen($v[$s]);
if($length['by_user'][$s] < strlen($s)) $length['by_user'][$s] = strlen($s);
}
}
foreach($by_device as $k => $v) {
if($length['by_device'][0] < strlen($k)) $length['by_device'][0] = strlen($k);
foreach($smartModes as $s) {
echo $v[$s] ."\t";
if($length['by_device'][$s] < strlen($v[$s])) $length['by_device'][$s] = strlen($v[$s]);
if($length['by_device'][$s] < strlen($s)) $length['by_device'][$s] = strlen($s);
}
}
/*
* ===================================
* ======= Per-Day Summary
* ===================================
*/
function xx($name, $arr, $lenArr) {
global $smartModes;
echo "
Per-$name Summary
-----------------------
$name";
$i = 0;
while($i < $lenArr[0] - strlen($name) +3) {
echo " ";
$i++;
}
foreach($smartModes as $s) {
echo "$s";
$i = 0;
while($i < $lenArr[$s] - strlen($s) +3) {
echo " ";
$i++;
}
}
echo "\n";
foreach($arr as $k => $v) {
echo "$k";
$i = 0;
while($i < $lenArr[0] - strlen($k) +3) {
echo " ";
$i++;
}
foreach($smartModes as $s) {
echo $v[$s];
$i = 0;
while($i < $lenArr[$s] - strlen($v[$s]) +3) {
echo " ";
$i++;
}
}
echo "\n";
}
}
function bsortA($arr) {
$tmp = "";
for($i = 0;$i < count($arr);$i++) {
for($j = 0;$j < count($arr); $j++) {
if($arr[$j][0] > $arr[$i][0]) { $tmp = $arr[$j]; $arr[$j] = $arr[$i]; $arr[$i] = $tmp; }
}
}
return $arr;
}
xx('Day', $by_date, $length['by_date']);
xx('User', $by_user, $length['by_user']);
xx('Device', $by_device, $length['by_device']);
echo "\n";
?>