From 67e2279e9006502cebad341d73eaffe028e45302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Cheng=20=28=E9=84=AD=E9=83=81=E9=82=A6=29?= Date: Tue, 30 Jan 2018 13:21:41 +0800 Subject: [PATCH 1/2] Fix Monocle/Alternative out of memory with KEEP_GYM_HISTORY When `KEEP_GYM_HISTORY` is on in Monocle/Alternative config, the `fort_sighting` and `raid` may contain multiple records of the same gym. Only the latest gym / fort status should be retrieved to avoid out of memory problem. --- lib/Monocle_Alternate.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/Monocle_Alternate.php b/lib/Monocle_Alternate.php index 43c185980..120210f20 100644 --- a/lib/Monocle_Alternate.php +++ b/lib/Monocle_Alternate.php @@ -216,7 +216,8 @@ public function query_gyms($conds, $params) { global $db; - $query = "SELECT f.external_id AS gym_id, + $query = "SELECT + f.external_id AS gym_id, fs.last_modified AS last_modified, updated AS last_scanned, f.lat AS latitude, @@ -235,10 +236,11 @@ public function query_gyms($conds, $params) r.move_1 AS raid_pokemon_move_1, r.move_2 AS raid_pokemon_move_2 FROM forts f - LEFT JOIN fort_sightings fs ON fs.fort_id = f.id - LEFT JOIN raids r ON r.fort_id = f.id + LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id)) + LEFT JOIN raids r ON (r.fort_id = f.id AND r.time_end >= :time) WHERE :conditions"; + $query = str_replace(":time", time(), $query); $query = str_replace(":conditions", join(" AND ", $conds), $query); $gyms = $db->query($query, $params)->fetchAll(\PDO::FETCH_ASSOC); From 6f6e10762f26e227621a3ac1992799ceb59d3ea1 Mon Sep 17 00:00:00 2001 From: Daniel Cheng Date: Tue, 27 Feb 2018 18:40:33 +0800 Subject: [PATCH 2/2] Add config for KEEP_GYM_HISTORY --- config/default.php | 1 + config/example.config.php | 1 + lib/Monocle_Alternate.php | 9 +++++++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/config/default.php b/config/default.php index ff3b45ed1..d3f8f9340 100644 --- a/config/default.php +++ b/config/default.php @@ -238,3 +238,4 @@ //----------------------------------------------------- $fork = "default"; // default/asner/sloppy +$alternateKeepGymHistory = false; // alternate - KEEP_GYM_HISTORY is on diff --git a/config/example.config.php b/config/example.config.php index 41669502e..32a520d7f 100644 --- a/config/example.config.php +++ b/config/example.config.php @@ -228,6 +228,7 @@ $map = "monocle"; // monocle/rm $fork = "default"; // default/asner/sloppy/alternate +$alternateKeepGymHistory = false; // alternate - KEEP_GYM_HISTORY is on $db = new Medoo([// required 'database_type' => 'mysql', // mysql/mariadb/pgsql/sybase/oracle/mssql/sqlite diff --git a/lib/Monocle_Alternate.php b/lib/Monocle_Alternate.php index 120210f20..ad6b7be83 100644 --- a/lib/Monocle_Alternate.php +++ b/lib/Monocle_Alternate.php @@ -214,7 +214,7 @@ public function get_gyms($swLat, $swLng, $neLat, $neLng, $tstamp = 0, $oSwLat = public function query_gyms($conds, $params) { - global $db; + global $db, $alternateKeepGymHistory; $query = "SELECT f.external_id AS gym_id, @@ -236,10 +236,15 @@ public function query_gyms($conds, $params) r.move_1 AS raid_pokemon_move_1, r.move_2 AS raid_pokemon_move_2 FROM forts f - LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id)) + LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND :fort_condition) LEFT JOIN raids r ON (r.fort_id = f.id AND r.time_end >= :time) WHERE :conditions"; + if ($alternateKeepGymHistory) { + $query = str_replace(":fort_condition", "fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id)", $query); + } else { + $query = str_replace(":fort_condition", "1=1", $query); + } $query = str_replace(":time", time(), $query); $query = str_replace(":conditions", join(" AND ", $conds), $query); $gyms = $db->query($query, $params)->fetchAll(\PDO::FETCH_ASSOC);