summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2017-01-11 12:19:20 +0100
committerSimon Rettberg2017-01-11 12:19:20 +0100
commita0e43d9fb99f65c0f6c9357d9fbaccab36deec5a (patch)
tree51b2ea1cb42b7451c2528132390eb07e2a06fc36
parent[baseconfig_bwlp] Update translations (diff)
downloadslx-admin-a0e43d9fb99f65c0f6c9357d9fbaccab36deec5a.tar.gz
slx-admin-a0e43d9fb99f65c0f6c9357d9fbaccab36deec5a.tar.xz
slx-admin-a0e43d9fb99f65c0f6c9357d9fbaccab36deec5a.zip
[statistics/locations] Update/set subnetlocationid field in DB if the DB has been updated
-rw-r--r--modules-available/locations/inc/autolocation.inc.php35
-rw-r--r--modules-available/locations/page.inc.php33
-rw-r--r--modules-available/statistics/install.inc.php8
3 files changed, 45 insertions, 31 deletions
diff --git a/modules-available/locations/inc/autolocation.inc.php b/modules-available/locations/inc/autolocation.inc.php
new file mode 100644
index 00000000..cccfe9b1
--- /dev/null
+++ b/modules-available/locations/inc/autolocation.inc.php
@@ -0,0 +1,35 @@
+<?php
+
+class AutoLocation
+{
+
+ public static function rebuildAll()
+ {
+ if (Module::get('statistics') === false)
+ return; // Nothing to do
+ $res = Database::simpleQuery("SELECT machineuuid, clientip FROM machine");
+ $updates = array();
+ $nulls = array();
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ $loc = Location::mapIpToLocation($row['clientip']);
+ if ($loc === false) {
+ $nulls[] = $row['machineuuid'];
+ } else {
+ if (!isset($updates[$loc])) {
+ $updates[$loc] = array();
+ }
+ $updates[$loc][] = $row['machineuuid'];
+ }
+ }
+ if (!empty($nulls)) {
+ $qs = '?' . str_repeat(',?', count($nulls) - 1);
+ Database::exec("UPDATE machine SET subnetlocationid = NULL WHERE machineuuid IN ($qs)", $nulls);
+ }
+ foreach ($updates as $lid => $machines) {
+ $qs = '?' . str_repeat(',?', count($machines) - 1);
+ $lid = (int)$lid;
+ Database::exec("UPDATE machine SET subnetlocationid = $lid WHERE machineuuid IN ($qs)", $machines);
+ }
+ }
+
+}
diff --git a/modules-available/locations/page.inc.php b/modules-available/locations/page.inc.php
index 1bb3026a..20af7b63 100644
--- a/modules-available/locations/page.inc.php
+++ b/modules-available/locations/page.inc.php
@@ -26,35 +26,6 @@ class Page_Locations extends Page
}
}
- private function updateAutoLocationId()
- {
- if (Module::get('statistics') === false)
- return; // Nothing to do
- $res = Database::simpleQuery("SELECT machineuuid, clientip FROM machine");
- $updates = array();
- $nulls = array();
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- $loc = Location::mapIpToLocation($row['clientip']);
- if ($loc === false) {
- $nulls[] = $row['machineuuid'];
- } else {
- if (!isset($updates[$loc])) {
- $updates[$loc] = array();
- }
- $updates[$loc][] = $row['machineuuid'];
- }
- }
- if (!empty($nulls)) {
- $qs = '?' . str_repeat(',?', count($nulls) - 1);
- Database::exec("UPDATE machine SET subnetlocationid = NULL WHERE machineuuid IN ($qs)", $nulls);
- }
- foreach ($updates as $lid => $machines) {
- $qs = '?' . str_repeat(',?', count($machines) - 1);
- $lid = (int)$lid;
- Database::exec("UPDATE machine SET subnetlocationid = $lid WHERE machineuuid IN ($qs)", $machines);
- }
- }
-
private function updateSubnets()
{
$count = 0;
@@ -84,7 +55,7 @@ class Page_Locations extends Page
$count += $stmt->rowCount();
}
}
- $this->updateAutoLocationId();
+ AutoLocation::rebuildAll();
Message::addSuccess('subnets-updated', $count);
Util::redirect('?do=Locations');
}
@@ -155,7 +126,7 @@ class Page_Locations extends Page
$change |= $this->updateLocationData($location);
if ($change) {
// In case subnets or tree layout changed, recalc this
- $this->updateAutoLocationId();
+ AutoLocation::rebuildAll();
}
Util::redirect('?do=Locations');
}
diff --git a/modules-available/statistics/install.inc.php b/modules-available/statistics/install.inc.php
index 0729d676..7baf046e 100644
--- a/modules-available/statistics/install.inc.php
+++ b/modules-available/statistics/install.inc.php
@@ -155,6 +155,14 @@ if ($addTrigger) {
if ($ret === false) {
finalResponse(UPDATE_FAILED, 'Adding locationid trigger to machine failed: ' . Database::lastError());
}
+ // This might be an update - calculate all subnetlocationid values (if location module is installed yet)
+ if (Module::isAvailable('locations')) {
+ if (tableExists('subnet')) {
+ AutoLocation::rebuildAll();
+ } else {
+ finalResponse(UPDATE_RETRY, 'Locations module not installed yet, retry later');
+ }
+ }
}
// Create response