diff options
Diffstat (limited to 'modules-available/statistics/hooks/cron.inc.php')
-rw-r--r-- | modules-available/statistics/hooks/cron.inc.php | 58 |
1 files changed, 41 insertions, 17 deletions
diff --git a/modules-available/statistics/hooks/cron.inc.php b/modules-available/statistics/hooks/cron.inc.php index 0de233a8..4ba5e2f6 100644 --- a/modules-available/statistics/hooks/cron.inc.php +++ b/modules-available/statistics/hooks/cron.inc.php @@ -9,12 +9,42 @@ function logstats() $join = 'LEFT JOIN runmode r USING (machineuuid)'; $where = 'AND (r.isclient IS NULL OR r.isclient <> 0)'; } - $known = Database::queryFirst("SELECT Count(*) AS val FROM machine m $join WHERE m.lastseen > $cutoff $where"); - $on = Database::queryFirst("SELECT Count(*) AS val FROM machine m $join WHERE m.state IN ('IDLE', 'OCCUPIED') $where"); - $used = Database::queryFirst("SELECT Count(*) AS val FROM machine m $join WHERE m.state = 'OCCUPIED' $where"); + // Get total/online/in-use + $known = Database::queryKeyValueList("SELECT locationid, Count(*) AS val FROM machine m + $join WHERE m.lastseen > $cutoff $where + GROUP BY locationid"); + $on = Database::queryKeyValueList("SELECT locationid, Count(*) AS val FROM machine m + $join WHERE m.state IN ('IDLE', 'OCCUPIED') $where + GROUP BY locationid"); + $used = Database::queryKeyValueList("SELECT locationid, Count(*) AS val FROM machine m + $join WHERE m.state = 'OCCUPIED' $where + GROUP BY locationid"); + // Get calendar data if available + if (Module::isAvailable('locationinfo')) { + // Refresh all calendars around 07:00 + $calendars = LocationInfo::getAllCalendars(date('G') != 7 || date('i') >= 10); + } + // Mash together + $data = ['usage' => []]; + foreach ($known as $lid => $val) { + $entry = ['t' => $val]; + if (isset($on[$lid])) { + $entry['o'] = $on[$lid]; + } + if (isset($used[$lid])) { + $entry['u'] = $used[$lid]; + } + if (isset($calendars[$lid])) { + $title = LocationInfo::extractCurrentEvent($calendars[$lid]); + if (!empty($title)) { + $entry['event'] = $title; + } + } + $data['usage'][$lid] = $entry; + } Database::exec("INSERT INTO statistic (dateline, typeid, clientip, username, data) VALUES (:now, '~stats', '', '', :vals)", array( 'now' => $NOW, - 'vals' => $known['val'] . '#' . $on['val'] . '#' . $used['val'], + 'vals' => json_encode($data), )); } @@ -26,18 +56,12 @@ function state_cleanup() // Query for logging $res = Database::simpleQuery("SELECT machineuuid, clientip, state, logintime, lastseen, live_memfree, live_swapfree, live_tmpfree FROM machine WHERE lastseen < If(state = 'STANDBY', $standby, $on) AND state <> 'OFFLINE'"); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - Database::exec('INSERT INTO clientlog (dateline, logtypeid, clientip, machineuuid, description, extra) - VALUES (UNIX_TIMESTAMP(), :type, :client, :uuid, :description, :longdesc)', array( - 'type' => 'machine-mismatch-cron', - 'client' => $row['clientip'], - 'description' => 'Client timed out, last known state is ' . $row['state'] - . '. Free RAM: ' . Util::readableFileSize($row['live_memfree'], -1, 2) - . ', free Swap: ' . Util::readableFileSize($row['live_swapfree'], -1, 2) - . ', free ID44: ' . Util::readableFileSize($row['live_tmpfree'], -1, 2), - 'longdesc' => '', - 'uuid' => $row['machineuuid'], - )); + foreach ($res as $row) { + ClientLog::write($row, 'machine-mismatch-cron', + 'Client timed out, last known state is ' . $row['state'] + . '. Free RAM: ' . Util::readableFileSize($row['live_memfree'], -1, 2) + . ', free Swap: ' . Util::readableFileSize($row['live_swapfree'], -1, 2) + . ', free ID44: ' . Util::readableFileSize($row['live_tmpfree'], -1, 2)); if ($row['state'] === 'OCCUPIED') { $length = $row['lastseen'] - $row['logintime']; if ($length > 0 && $length < 86400 * 7) { @@ -59,7 +83,7 @@ state_cleanup(); logstats(); if (mt_rand(1, 10) === 1) { - Database::exec("DELETE FROM statistic WHERE (UNIX_TIMESTAMP() - 86400 * 190) > dateline"); + Database::exec("DELETE FROM statistic WHERE (UNIX_TIMESTAMP() - 86400 * 365 * 2) > dateline"); if (mt_rand(1, 100) === 1) { Database::exec("OPTIMIZE TABLE statistic"); } |