diff options
Diffstat (limited to 'modules-available/statistics/inc')
-rw-r--r-- | modules-available/statistics/inc/filter.inc.php | 42 | ||||
-rw-r--r-- | modules-available/statistics/inc/filterset.inc.php | 14 | ||||
-rw-r--r-- | modules-available/statistics/inc/machine.inc.php | 5 | ||||
-rw-r--r-- | modules-available/statistics/inc/statistics.inc.php | 51 |
4 files changed, 89 insertions, 23 deletions
diff --git a/modules-available/statistics/inc/filter.inc.php b/modules-available/statistics/inc/filter.inc.php index 0afce572..6e437a71 100644 --- a/modules-available/statistics/inc/filter.inc.php +++ b/modules-available/statistics/inc/filter.inc.php @@ -29,7 +29,7 @@ class Filter $addendum = ''; /* check if we have to do some parsing*/ - if (Page_Statistics::$columns[$this->column]['type'] == 'date') { + if (Page_Statistics::$columns[$this->column]['type'] === 'date') { $args[$key] = strtotime($this->argument); } else { $args[$key] = $this->argument; @@ -180,21 +180,18 @@ class StateFilter extends Filter { public function __construct($operator, $argument) { - $this->operator = $operator; - $this->argument = $argument; + parent::__construct(null, $operator, $argument); } public function whereClause(&$args, &$joins) { + $map = [ 'on' => ['IDLE', 'OCCUPIED'], 'off' => ['OFFLINE'], 'idle' => ['IDLE'], 'occupied' => ['OCCUPIED'], 'standby' => ['STANDBY'] ]; $neg = $this->operator == '!=' ? 'NOT ' : ''; - if ($this->argument === 'on') { - return " $neg (lastseen + 600 > UNIX_TIMESTAMP() ) "; - } elseif ($this->argument === 'off') { - return " $neg (lastseen + 600 < UNIX_TIMESTAMP() ) "; - } elseif ($this->argument === 'idle') { - return " $neg (lastseen + 600 > UNIX_TIMESTAMP() AND logintime = 0 ) "; - } elseif ($this->argument === 'occupied') { - return " $neg (lastseen + 600 > UNIX_TIMESTAMP() AND logintime <> 0 ) "; + if (array_key_exists($this->argument, $map)) { + global $unique_key; + $key = $this->column . '_arg' . ($unique_key++); + $args[$key] = $map[$this->argument]; + return " machine.state $neg IN ( :$key ) "; } else { Message::addError('invalid-filter-argument', 'state', $this->argument); return ' 1'; @@ -216,8 +213,10 @@ class LocationFilter extends Filter $neg = $this->operator === '=' ? '' : 'NOT'; return "machine.locationid IS $neg NULL"; } else { - $args['lid'] = $this->argument; - return "machine.locationid {$this->operator} :lid"; + global $unique_key; + $key = $this->column . '_arg' . ($unique_key++); + $args[$key] = $this->argument; + return "machine.locationid {$this->operator} :$key"; } } } @@ -236,3 +235,20 @@ class SubnetFilter extends Filter } } +class IsClientFilter extends Filter +{ + public function __construct($argument) + { + parent::__construct(null, null, $argument); + } + + public function whereClause(&$args, &$joins) + { + if ($this->argument) { + $joins[] = ' LEFT JOIN runmode USING (machineuuid)'; + return "(runmode.isclient <> 0 OR runmode.isclient IS NULL)"; + } + $joins[] = ' INNER JOIN runmode USING (machineuuid)'; + return "runmode.isclient = 0"; + } +} diff --git a/modules-available/statistics/inc/filterset.inc.php b/modules-available/statistics/inc/filterset.inc.php index c73feeef..25c5c8fa 100644 --- a/modules-available/statistics/inc/filterset.inc.php +++ b/modules-available/statistics/inc/filterset.inc.php @@ -2,6 +2,9 @@ class FilterSet { + /** + * @var \Filter[] + */ private $filters; private $sortDirection; private $sortColumn; @@ -39,7 +42,7 @@ class FilterSet $where .= $sep . $filter->whereClause($args, $joins); } } - $join = implode('', array_unique($joins)); + $join = implode(' ', array_unique($joins)); $col = $this->sortColumn; $isMapped = array_key_exists('map_sort', Page_Statistics::$columns[$col]); @@ -72,4 +75,13 @@ class FilterSet { return $this->sortColumn; } + + public function filterNonClients() + { + if (Module::get('runmode') === false) + return; + // Runmode module exists, add filter + $this->filters[] = new IsClientFilter(true); + } + } diff --git a/modules-available/statistics/inc/machine.inc.php b/modules-available/statistics/inc/machine.inc.php index 8cb5e884..8605749b 100644 --- a/modules-available/statistics/inc/machine.inc.php +++ b/modules-available/statistics/inc/machine.inc.php @@ -51,6 +51,11 @@ class Machine public $logintime; /** + * @var string state of machine (OFFLINE, IDLE, OCCUPIED, STANDBY) + */ + public $state; + + /** * @var string json data of position inside room (if any), null/empty otherwise */ public $position; diff --git a/modules-available/statistics/inc/statistics.inc.php b/modules-available/statistics/inc/statistics.inc.php index 1c9ebf07..2500f16f 100644 --- a/modules-available/statistics/inc/statistics.inc.php +++ b/modules-available/statistics/inc/statistics.inc.php @@ -7,17 +7,12 @@ class Statistics private static $machineFields = false; - /** - * @param string $machineuuid - * @param int $returnData - * @return \Machine|false - */ - public static function getMachine($machineuuid, $returnData) + private static function initFields($returnData) { if (self::$machineFields === false) { $r = new ReflectionClass('Machine'); $props = $r->getProperties(ReflectionProperty::IS_PUBLIC); - self::$machineFields = array_flip(array_map(function($e) { return $e->getName(); }, $props)); + self::$machineFields = array_flip(array_map(function(/* @var ReflectionProperty $e */ $e) { return $e->getName(); }, $props)); } if ($returnData === Machine::NO_DATA) { unset(self::$machineFields['data']); @@ -26,8 +21,19 @@ class Statistics } else { Util::traceError('Invalid $returnData option passed'); } - $fields = implode(',', array_keys(self::$machineFields)); - $row = Database::queryFirst("SELECT * FROM machine WHERE machineuuid = :machineuuid", compact('machineuuid')); + return implode(',', array_keys(self::$machineFields)); + } + + /** + * @param string $machineuuid + * @param int $returnData What kind of data to return Machine::NO_DATA, Machine::RAW_DATA, ... + * @return \Machine|false + */ + public static function getMachine($machineuuid, $returnData) + { + $fields = self::initFields($returnData); + + $row = Database::queryFirst("SELECT $fields FROM machine WHERE machineuuid = :machineuuid", compact('machineuuid')); if ($row === false) return false; $m = new Machine(); @@ -37,4 +43,31 @@ class Statistics return $m; } + /** + * @param string $ip + * @param int $returnData What kind of data to return Machine::NO_DATA, Machine::RAW_DATA, ... + * @param string $sort something like 'lastseen ASC' - not sanitized, don't pass user input! + * @return \Machine[] list of matches + */ + public static function getMachinesByIp($ip, $returnData, $sort = false) + { + $fields = self::initFields($returnData); + + if ($sort === false) { + $sort = ''; + } else { + $sort = "ORDER BY $sort"; + } + $res = Database::simpleQuery("SELECT $fields FROM machine WHERE clientip = :ip $sort", compact('ip')); + $list = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $m = new Machine(); + foreach ($row as $key => $val) { + $m->{$key} = $val; + } + $list[] = $m; + } + return $list; + } + } |