From 8d8362dd7ce06c4f9771c619ec4f2625ff82b877 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 21 Dec 2017 13:23:18 +0100 Subject: [statistics] Properly mark disconnected sceens as such if the output name changed --- modules-available/statistics/api.inc.php | 46 +++++---- modules-available/statistics/page.inc.php | 113 ++++++++------------- .../statistics/pages/projectors.inc.php | 58 +++++++++++ .../statistics/templates/machine-main.html | 4 +- .../statistics/templates/page-replace.html | 104 +++++++++++++++++++ .../statistics/templates/projector-list.html | 2 +- 6 files changed, 239 insertions(+), 88 deletions(-) create mode 100644 modules-available/statistics/pages/projectors.inc.php create mode 100644 modules-available/statistics/templates/page-replace.html (limited to 'modules-available/statistics') diff --git a/modules-available/statistics/api.inc.php b/modules-available/statistics/api.inc.php index a614658a..3cb1e961 100644 --- a/modules-available/statistics/api.inc.php +++ b/modules-available/statistics/api.inc.php @@ -263,6 +263,7 @@ if ($type{0} === '~') { // `devicetype`, `devicename`, `subid`, `machineuuid` // Make sure all screens are in the general hardware table $hwids = array(); + $ports = array(); foreach ($screens as $port => $screen) { if (!array_key_exists('name', $screen)) continue; @@ -274,6 +275,7 @@ if ($type{0} === '~') { $hwids[$screen['name']] = $hwid; } // Now add new entries + $ports[] = $port; $machinehwid = Database::insertIgnore('machine_x_hw', 'machinehwid', array( 'hwid' => $hwid, 'machineuuid' => $uuid, @@ -302,30 +304,40 @@ if ($type{0} === '~') { Database::exec("DELETE FROM machine_x_hw_prop WHERE machinehwid = :machinehwid AND prop NOT LIKE '@%'", array('machinehwid' => $machinehwid)); } else { - $qs = '?' . str_repeat(',?', count($validProps) - 1); - array_unshift($validProps, $machinehwid); - Database::exec("DELETE FROM machine_x_hw_prop" - . " WHERE machinehwid = ? AND prop NOT LIKE '@%' AND prop NOT IN ($qs)", - $validProps); + Database::exec("DELETE FROM machine_x_hw_prop + WHERE machinehwid = :mhwid AND prop NOT LIKE '@%' AND prop NOT IN (:props)", array( + 'mhwid' => $machinehwid, + 'props' => array_values($validProps), + )); } } // Remove/disable stale entries - if (empty($hwids)) { + if (empty($ports)) { // No screens connected at all, purge all screen entries for this machine - Database::exec("UPDATE machine_x_hw x, statistic_hw h" - . " SET x.disconnecttime = UNIX_TIMESTAMP()" - . " WHERE x.machineuuid = :uuid AND x.hwid = h.hwid AND h.hwtype = :type AND x.disconnecttime = 0", + Database::exec("UPDATE machine_x_hw x, statistic_hw h + SET x.disconnecttime = UNIX_TIMESTAMP() + WHERE x.machineuuid = :uuid AND x.hwid = h.hwid AND h.hwtype = :type AND x.disconnecttime = 0", array('uuid' => $uuid, 'type' => DeviceType::SCREEN)); } else { // Some screens connected, make sure old entries get removed - $params = array_values($hwids); - array_unshift($params, $uuid); - array_unshift($params, DeviceType::SCREEN); - $qs = '?' . str_repeat(',?', count($hwids) - 1); - Database::exec("UPDATE machine_x_hw x, statistic_hw h" - . " SET x.disconnecttime = UNIX_TIMESTAMP()" - . " WHERE h.hwid = x.hwid AND x.disconnecttime = 0 AND h.hwtype = ? AND x.machineuuid = ? AND x.hwid NOT IN ($qs)", $params); - + Database::exec("UPDATE machine_x_hw x, statistic_hw h + SET x.disconnecttime = UNIX_TIMESTAMP() + WHERE h.hwid = x.hwid AND x.disconnecttime = 0 AND h.hwtype = :type + AND x.machineuuid = :uuid AND x.devpath NOT IN (:ports)", array( + 'ports' => array_values($ports), + 'uuid' => $uuid, + 'type' => DeviceType::SCREEN, + )); + if (!empty($hwids)) { + Database::exec("UPDATE machine_x_hw x, statistic_hw h + SET x.disconnecttime = UNIX_TIMESTAMP() + WHERE h.hwid = x.hwid AND x.disconnecttime = 0 AND h.hwtype = :type + AND x.machineuuid = :uuid AND x.hwid NOT IN (:hwids)", array( + 'hwids' => array_values($hwids), + 'uuid' => $uuid, + 'type' => DeviceType::SCREEN, + )); + } } } } else if ($type === '~suspend') { diff --git a/modules-available/statistics/page.inc.php b/modules-available/statistics/page.inc.php index bd67123e..ea5b6f03 100644 --- a/modules-available/statistics/page.inc.php +++ b/modules-available/statistics/page.inc.php @@ -21,6 +21,11 @@ class Page_Statistics extends Page private $query; + /** + * @var bool whether we have a SubPage from the pages/ subdir + */ + private $haveSubpage; + /* PHP sucks, no static, const array definitions... Or am I missing something? */ public function initConstants() { @@ -125,53 +130,6 @@ class Page_Statistics extends Page /* TODO ... */ } - /* - * TODO: Move to separate unit... hardware configurator? - */ - - protected function handleProjector($action) - { - $hwid = Request::post('hwid', false, 'int'); - if ($hwid === false) { - Util::traceError('Param hwid missing'); - } - if ($action === 'addprojector') { - Database::exec('INSERT INTO statistic_hw_prop (hwid, prop, value)' - . ' VALUES (:hwid, :prop, :value)', array( - 'hwid' => $hwid, - 'prop' => 'projector', - 'value' => 'true', - )); - } else { - Database::exec('DELETE FROM statistic_hw_prop WHERE hwid = :hwid AND prop = :prop', array( - 'hwid' => $hwid, - 'prop' => 'projector', - )); - } - if (Module::isAvailable('sysconfig')) { - ConfigTgz::rebuildAllConfigs(); - } - Util::redirect('?do=statistics&show=projectors'); - } - - protected function showProjectors() - { - $res = Database::simpleQuery('SELECT h.hwname, h.hwid FROM statistic_hw h' - . " INNER JOIN statistic_hw_prop p ON (h.hwid = p.hwid AND p.prop = :projector)" - . " WHERE h.hwtype = :screen ORDER BY h.hwname ASC", array( - 'projector' => 'projector', - 'screen' => DeviceType::SCREEN, - )); - $data = array( - 'projectors' => $res->fetchAll(PDO::FETCH_ASSOC) - ); - Render::addTemplate('projector-list', $data); - } - - /* - * End TODO - */ - protected function doPreprocess() { $this->initConstants(); @@ -180,24 +138,42 @@ class Page_Statistics extends Page Message::addError('main.no-permission'); Util::redirect('?do=Main'); } - $action = Request::post('action'); - if ($action === 'setnotes') { - $uuid = Request::post('uuid', '', 'string'); - $text = Request::post('content', '', 'string'); - if (empty($text)) { - $text = null; + + $show = Request::any('show', 'stat', 'string'); + $show = preg_replace('/[^a-z0-9_\-]/', '', $show); + + if (file_exists('modules/statistics/pages/' . $show . '.inc.php')) { + + require_once 'modules/statistics/pages/' . $show . '.inc.php'; + $this->haveSubpage = true; + SubPage::doPreprocess(); + + } else { + + $action = Request::post('action'); + if ($action === 'setnotes') { + $uuid = Request::post('uuid', '', 'string'); + $text = Request::post('content', '', 'string'); + if (empty($text)) { + $text = null; + } + Database::exec('UPDATE machine SET notes = :text WHERE machineuuid = :uuid', array( + 'uuid' => $uuid, + 'text' => $text, + )); + Message::addSuccess('notes-saved'); + Util::redirect('?do=Statistics&uuid=' . $uuid); + } elseif ($action === 'delmachines') { + $this->deleteMachines(); + Util::redirect('?do=statistics', true); } - Database::exec('UPDATE machine SET notes = :text WHERE machineuuid = :uuid', array( - 'uuid' => $uuid, - 'text' => $text, - )); - Message::addSuccess('notes-saved'); - Util::redirect('?do=Statistics&uuid=' . $uuid); - } elseif ($action === 'addprojector' || $action === 'delprojector') { - $this->handleProjector($action); - } elseif ($action === 'delmachines') { - $this->deleteMachines(); - Util::redirect('?do=statistics', true); + + } + + if (Request::isPost()) { + // Make sure we don't render any content for POST requests - should be handled above and then + // redirected properly + Util::redirect('?do=statistics'); } } @@ -229,6 +205,11 @@ class Page_Statistics extends Page protected function doRender() { + if ($this->haveSubpage) { + SubPage::doRender(); + return; + } + $uuid = Request::get('uuid', false, 'string'); if ($uuid !== false) { $this->showMachine($uuid); @@ -236,10 +217,6 @@ class Page_Statistics extends Page } $show = Request::get('show', 'stat', 'string'); - if ($show === 'projectors') { - $this->showProjectors(); - return; - } /* read filter */ $this->query = Request::any('filters', false); diff --git a/modules-available/statistics/pages/projectors.inc.php b/modules-available/statistics/pages/projectors.inc.php new file mode 100644 index 00000000..cde542c6 --- /dev/null +++ b/modules-available/statistics/pages/projectors.inc.php @@ -0,0 +1,58 @@ + $hwid, + 'prop' => 'projector', + 'value' => 'true', + )); + } else { + Database::exec('DELETE FROM statistic_hw_prop WHERE hwid = :hwid AND prop = :prop', array( + 'hwid' => $hwid, + 'prop' => 'projector', + )); + } + if (Module::isAvailable('sysconfig')) { + ConfigTgz::rebuildAllConfigs(); + } + Util::redirect('?do=statistics&show=projectors'); + } + + public static function doRender() + { + self::showProjectors(); + } + + private static function showProjectors() + { + $res = Database::simpleQuery('SELECT h.hwname, h.hwid FROM statistic_hw h' + . " INNER JOIN statistic_hw_prop p ON (h.hwid = p.hwid AND p.prop = :projector)" + . " WHERE h.hwtype = :screen ORDER BY h.hwname ASC", array( + 'projector' => 'projector', + 'screen' => DeviceType::SCREEN, + )); + $data = array( + 'projectors' => $res->fetchAll(PDO::FETCH_ASSOC) + ); + Render::addTemplate('projector-list', $data); + } + +} \ No newline at end of file diff --git a/modules-available/statistics/templates/machine-main.html b/modules-available/statistics/templates/machine-main.html index 19deb8b3..d8f2c521 100644 --- a/modules-available/statistics/templates/machine-main.html +++ b/modules-available/statistics/templates/machine-main.html @@ -144,11 +144,11 @@ {{lang_screens}} -
+
-
+
diff --git a/modules-available/statistics/templates/page-replace.html b/modules-available/statistics/templates/page-replace.html new file mode 100644 index 00000000..f87610a2 --- /dev/null +++ b/modules-available/statistics/templates/page-replace.html @@ -0,0 +1,104 @@ +

{{lang_replaceMachinesHeading}}

+ + + +
+ + + + + + + + + + + + {{#pairs}} + + + + + + + {{/pairs}} + +
{{lang_replaceOld}}{{lang_replaceNew}}
+
{{oldhost}} {{oldip}}
+
{{oldmodel}}
+
{{oldmac}} – {{olduuid}}
+
{{lang_lastSeen}}: {{oldlastseen_s}}
+
+
{{newhost}} {{newip}}
+
{{newmodel}}
+
{{newmac}} – {{newuuid}}
+
{{lang_firstSeen}}: {{newfirstseen_s}}
+
+
+ + +
+
+ +
+ +
+ + +
+ + \ No newline at end of file diff --git a/modules-available/statistics/templates/projector-list.html b/modules-available/statistics/templates/projector-list.html index bc9ecdbd..c33aae09 100644 --- a/modules-available/statistics/templates/projector-list.html +++ b/modules-available/statistics/templates/projector-list.html @@ -1,7 +1,7 @@
{{lang_projectors}}
-
+

{{lang_thoseAreProjectors}}

-- cgit v1.2.3-55-g7522