diff options
author | Udo Walter | 2017-01-12 12:41:21 +0100 |
---|---|---|
committer | Udo Walter | 2017-01-12 12:41:21 +0100 |
commit | 9ccef127acd6dca0971e12726b019e2beaf7e4ce (patch) | |
tree | 506f14129c595665e00714bac2b1c9794342457e /modules-available | |
parent | [statistics_reporting] improved readability, added hashed values to queries (diff) | |
download | slx-admin-9ccef127acd6dca0971e12726b019e2beaf7e4ce.tar.gz slx-admin-9ccef127acd6dca0971e12726b019e2beaf7e4ce.tar.xz slx-admin-9ccef127acd6dca0971e12726b019e2beaf7e4ce.zip |
[statistics_reporting] moved some functions to a new class
Diffstat (limited to 'modules-available')
-rw-r--r-- | modules-available/statistics_reporting/inc/getdata.inc.php | 98 | ||||
-rw-r--r-- | modules-available/statistics_reporting/inc/queries.inc.php (renamed from modules-available/statistics_reporting/inc/statisticreporting.inc.php) | 24 | ||||
-rw-r--r-- | modules-available/statistics_reporting/page.inc.php | 55 |
3 files changed, 107 insertions, 70 deletions
diff --git a/modules-available/statistics_reporting/inc/getdata.inc.php b/modules-available/statistics_reporting/inc/getdata.inc.php new file mode 100644 index 00000000..a1df66d0 --- /dev/null +++ b/modules-available/statistics_reporting/inc/getdata.inc.php @@ -0,0 +1,98 @@ +<?php + +class GetData +{ + public static $cutOff = 7; + public static $lowerTimeBound = 0; + public static $upperTimeBound = 24; + + + // total + public static function total($anonymize = false) { + // total time online, average time online, total number of logins + $res = Queries::getOverallStatistics(self::$cutOff, self::$lowerTimeBound, self::$upperTimeBound); + $row = $res->fetch(PDO::FETCH_ASSOC); + $data = array('time' => self::formatSeconds($row['sum']), 'medianTime' => self::formatSeconds(self::calcMedian($row['median'])), 'sessions' => $row['longSessions'], 'shortSessions' => $row['shortSessions']); + + //total time offline + $res = Queries::getTotalOfflineStatistics(self::$cutOff, self::$lowerTimeBound, self::$upperTimeBound); + $row = $res->fetch(PDO::FETCH_ASSOC); + $data = array_merge($data, array('totalOfftime' => self::formatSeconds($row['timeOff']))); + + return $data; + } + + // per location + public static function perLocation($anonymize = false) { + $res = Queries::getLocationStatistics(self::$cutOff, self::$lowerTimeBound, self::$upperTimeBound); + $data = array(); + $loc = $anonymize ? 'locHash' : 'locName'; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $median = self::calcMedian(self::calcMedian($row['medianTime'])); + $data[] = array('location' => $row[$loc], 'time' => self::formatSeconds($row['timeSum']), 'timeInSeconds' => $row['timeSum'], + 'medianTime' => self::formatSeconds($median), 'medianTimeInSeconds' => $median, 'offTime' => self::formatSeconds($row['offlineSum']), 'offlineTimeInSeconds' => $row['offlineSum'], 'sessions' => $row['longSessions'], 'shortSessions' => $row['shortSessions']); + } + return $data; + } + + // per client + public static function perClient($anonymize = false) { + $res = Queries::getClientStatistics(self::$cutOff, self::$lowerTimeBound, self::$upperTimeBound); + $data = array(); + $name = $anonymize ? 'clientHash' : 'hostname'; + $loc = $anonymize ? 'locHash' : 'locName'; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $median = self::calcMedian(self::calcMedian($row['medianTime'])); + $data[] = array('hostname' => $row[$name], 'time' => self::formatSeconds($row['timeSum']), 'timeInSeconds' => $row['timeSum'], + 'medianTime' => self::formatSeconds($median), 'medianTimeInSeconds' => $median, 'offTime' => self::formatSeconds($row['offlineSum']), 'offlineTimeInSeconds' => $row['offlineSum'], 'lastStart' => date(DATE_RSS,$row['lastStart']), 'lastStartUnixtime' => $row['lastStart'], + 'lastLogout' => date(DATE_RSS,$row['lastLogout']), 'lastLogoutUnixtime' => $row['lastLogout'], 'sessions' => $row['longSessions'], 'shortSessions' => $row['shortSessions'], 'locationName' => $row[$loc]); + } + return $data; + } + + // per user + public static function perUser($anonymize = false) { + $res = Queries::getUserStatistics(self::$cutOff, self::$lowerTimeBound, self::$upperTimeBound); + $data = array(); + $user = $anonymize ? 'userHash' : 'name'; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $data[] = array('user' => $row['name'], 'sessions' => $row['count']); + } + return $data; + } + + + // per vm + public static function perVM() { + $res = Queries::getVMStatistics(self::$cutOff, self::$lowerTimeBound, self::$upperTimeBound); + $data = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $data[] = array('vm' => $row['name'], 'sessions' => $row['count']); + } + return $data; + } + + + + // Format $seconds into ".d .h .m .s" format (day, hour, minute, second) + private static function formatSeconds($seconds) + { + return intdiv($seconds, 3600*24).'d '.intdiv($seconds%(3600*24), 3600).'h '.intdiv($seconds%3600, 60).'m '.($seconds%60).'s'; + } + + // Calculate Median + private static function calcMedian($string) { + $arr = explode(",", $string); + sort($arr, SORT_NUMERIC); + $count = count($arr); //total numbers in array + $middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value + if($count % 2) { // odd number, middle is the median + $median = $arr[(int) $middleval]; + } else { // even number, calculate avg of 2 medians + $low = $arr[(int) $middleval]; + $high = $arr[(int) $middleval+1]; + $median = (($low+$high)/2); + } + return round($median); + } +}
\ No newline at end of file diff --git a/modules-available/statistics_reporting/inc/statisticreporting.inc.php b/modules-available/statistics_reporting/inc/queries.inc.php index c209c5ef..23afceed 100644 --- a/modules-available/statistics_reporting/inc/statisticreporting.inc.php +++ b/modules-available/statistics_reporting/inc/queries.inc.php @@ -1,7 +1,7 @@ <?php -class StatisticReporting +class Queries { // Client Data: Name, Time Online, Median Time Online, Time Offline, last start, last logout, Last Time Booted, Number of Sessions > 60Sec, Number of Sessions < 60Sec, name of location, id of location (anonymized), machine uuid (anonymized) @@ -79,28 +79,6 @@ class StatisticReporting return $res; } - // Format $seconds into ".d .h .m .s" format (day, hour, minute, second) - public static function formatSeconds($seconds) - { - return intdiv($seconds, 3600*24).'d '.intdiv($seconds%(3600*24), 3600).'h '.intdiv($seconds%3600, 60).'m '.($seconds%60).'s'; - } - - // Calculate Median - public static function calcMedian($string) { - $arr = explode(",", $string); - sort($arr, SORT_NUMERIC); - $count = count($arr); //total numbers in array - $middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value - if($count % 2) { // odd number, middle is the median - $median = $arr[(int) $middleval]; - } else { // even number, calculate avg of 2 medians - $low = $arr[(int) $middleval]; - $high = $arr[(int) $middleval+1]; - $median = (($low+$high)/2); - } - return round($median); - } - // query string which provides table with time-cutoff and time-bounds private static function getBoundedTableQueryString($typeid, $lowerTimeBound, $upperTimeBound, $cutOff) { diff --git a/modules-available/statistics_reporting/page.inc.php b/modules-available/statistics_reporting/page.inc.php index e2c76b09..82c03acc 100644 --- a/modules-available/statistics_reporting/page.inc.php +++ b/modules-available/statistics_reporting/page.inc.php @@ -24,55 +24,16 @@ class Page_Statistics_Reporting extends Page { // timespan you want to see = Days selected * seconds per Day (86400) // default = 14 days - $cutOff = Request::get('cutoff', 14, 'int') - 1; + GetData::$cutOff = Request::get('cutoff', 14, 'int') - 1; + GetData::$lowerTimeBound = Request::get('lower', 0, 'int'); + GetData::$upperTimeBound = Request::get('upper', 24, 'int'); - $lowerTimeBound = Request::get('lower', 0, 'int'); + $data = array_merge(GetData::total(), array('perLocation' => array(), 'perClient' => array(), 'perUser' => array(), 'perVM' => array())); + $data['perLocation'] = GetData::perLocation(); + $data['perClient'] = GetData::perClient(); + $data['perUser'] = GetData::perUser(); + $data['perVM'] = GetData::perVM(); - $upperTimeBound = Request::get('upper', 24, 'int'); - - - // total time online, average time online, total number of logins - $res = StatisticReporting::getOverallStatistics($cutOff, $lowerTimeBound, $upperTimeBound); - $row = $res->fetch(PDO::FETCH_ASSOC); - $data = array('time' => StatisticReporting::formatSeconds($row['sum']), 'medianTime' => StatisticReporting::formatSeconds(StatisticReporting::calcMedian($row['median'])), 'sessions' => $row['longSessions'], 'shortSessions' => $row['shortSessions']); - - //total time offline - $res = StatisticReporting::getTotalOfflineStatistics($cutOff, $lowerTimeBound, $upperTimeBound); - $row = $res->fetch(PDO::FETCH_ASSOC); - $data = array_merge($data, array('totalOfftime' => StatisticReporting::formatSeconds($row['timeOff']))); - - // per location - $res = StatisticReporting::getLocationStatistics($cutOff, $lowerTimeBound, $upperTimeBound); - $data[] = array('perLocation' => array()); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $median = StatisticReporting::calcMedian(StatisticReporting::calcMedian($row['medianTime'])); - $data['perLocation'][] = array('location' => $row['locName'], 'time' => StatisticReporting::formatSeconds($row['timeSum']), 'timeInSeconds' => $row['timeSum'], - 'medianTime' => StatisticReporting::formatSeconds($median), 'medianTimeInSeconds' => $median, 'offTime' => StatisticReporting::formatSeconds($row['offlineSum']), 'offlineTimeInSeconds' => $row['offlineSum'], 'sessions' => $row['longSessions'], 'shortSessions' => $row['shortSessions']); - } - - // per client - $res = StatisticReporting::getClientStatistics($cutOff, $lowerTimeBound, $upperTimeBound); - $data[] = array('perClient' => array()); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $median = StatisticReporting::calcMedian(StatisticReporting::calcMedian($row['medianTime'])); - $data['perClient'][] = array('hostname' => $row['clientName'], 'time' => StatisticReporting::formatSeconds($row['timeSum']), 'timeInSeconds' => $row['timeSum'], - 'medianTime' => StatisticReporting::formatSeconds($median), 'medianTimeInSeconds' => $median, 'offTime' => StatisticReporting::formatSeconds($row['offlineSum']), 'offlineTimeInSeconds' => $row['offlineSum'], 'lastStart' => date(DATE_RSS,$row['lastStart']), 'lastStartUnixtime' => $row['lastStart'], - 'lastLogout' => date(DATE_RSS,$row['lastLogout']), 'lastLogoutUnixtime' => $row['lastLogout'], 'sessions' => $row['longSessions'], 'shortSessions' => $row['shortSessions'], 'locationName' => $row['locName']); - } - - // per user - $res = StatisticReporting::getUserStatistics($cutOff, $lowerTimeBound, $upperTimeBound); - $data[] = array('perUser' => array()); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $data['perUser'][] = array('user' => $row['name'], 'sessions' => $row['count']); - } - - // per vm - $res = StatisticReporting::getVMStatistics($cutOff, $lowerTimeBound, $upperTimeBound); - $data[] = array('perVM' => array()); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $data['perVM'][] = array('vm' => $row['name'], 'sessions' => $row['count']); - } Render::addTemplate('columnChooser'); Render::addTemplate('_page', $data); |