From d38392998869319677cc884836c5952441f6ac62 Mon Sep 17 00:00:00 2001 From: Loris D'Antonio Date: Thu, 25 Aug 2016 16:08:55 +0200 Subject: [PATCH 1/6] Support for log_delay for all tasks --- pokemongo_bot/base_task.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/pokemongo_bot/base_task.py b/pokemongo_bot/base_task.py index e643ee7c07..eda1652af6 100644 --- a/pokemongo_bot/base_task.py +++ b/pokemongo_bot/base_task.py @@ -1,5 +1,7 @@ import logging +import time + class BaseTask(object): TASK_API_VERSION = 1 @@ -17,6 +19,7 @@ def __init__(self, bot, config): self._validate_work_exists() self.logger = logging.getLogger(type(self).__name__) self.enabled = config.get('enabled', True) + self.last_log_time = time.time() self.initialize() def _validate_work_exists(self): @@ -27,13 +30,17 @@ def _validate_work_exists(self): def emit_event(self, event, sender=None, level='info', formatted='', data={}): if not sender: sender=self - self.bot.event_manager.emit( - event, - sender=sender, - level=level, - formatted=formatted, - data=data - ) + + # Print log only if X seconds are passed from last log + if (time.time() - self.last_log_time) > self.config.get('log_delay', 0): + self.last_log_time = time.time() + self.bot.event_manager.emit( + event, + sender=sender, + level=level, + formatted=formatted, + data=data + ) def initialize(self): pass From 3fc40e16a997887b71ecd117a2735373f1697261 Mon Sep 17 00:00:00 2001 From: Loris D'Antonio Date: Thu, 25 Aug 2016 16:15:25 +0200 Subject: [PATCH 2/6] Added configuration for log_delay --- configs/config.json.example | 3 ++- configs/config.json.map.example | 3 ++- configs/config.json.optimizer.example | 3 ++- configs/config.json.pokemon.example | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/configs/config.json.example b/configs/config.json.example index 29256ebea5..c756d0c9e4 100644 --- a/configs/config.json.example +++ b/configs/config.json.example @@ -169,7 +169,8 @@ "enabled": true, "lure_attraction": true, "lure_max_distance": 2000, - "walker": "StepWalker" + "walker": "StepWalker", + "log_delay": 5 } }, { diff --git a/configs/config.json.map.example b/configs/config.json.map.example index d4abf5e6bb..18d30d4417 100644 --- a/configs/config.json.map.example +++ b/configs/config.json.map.example @@ -400,7 +400,8 @@ "config": { "enabled": true, "lure_attraction": true, - "lure_max_distance": 2000 + "lure_max_distance": 2000, + "log_delay": 5 } }, { diff --git a/configs/config.json.optimizer.example b/configs/config.json.optimizer.example index 0e6656ffaa..6f0c6fa268 100644 --- a/configs/config.json.optimizer.example +++ b/configs/config.json.optimizer.example @@ -215,7 +215,8 @@ "config": { "enabled": true, "lure_attraction": true, - "lure_max_distance": 2000 + "lure_max_distance": 2000, + "log_delay": 5 } }, { diff --git a/configs/config.json.pokemon.example b/configs/config.json.pokemon.example index df51153b77..f270768ccc 100644 --- a/configs/config.json.pokemon.example +++ b/configs/config.json.pokemon.example @@ -153,7 +153,8 @@ "config":{ "enabled": true, "lure_attraction": true, - "lure_max_distance": 2000 + "lure_max_distance": 2000, + "log_delay": 5 } }, { From a0938b721482e33d421681254eef21fd46142418 Mon Sep 17 00:00:00 2001 From: Loris D'Antonio Date: Thu, 25 Aug 2016 17:50:18 +0200 Subject: [PATCH 3/6] Added tests for log_delay --- pokemongo_bot/test/log_delay_test.py | 62 ++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 pokemongo_bot/test/log_delay_test.py diff --git a/pokemongo_bot/test/log_delay_test.py b/pokemongo_bot/test/log_delay_test.py new file mode 100644 index 0000000000..cb8e758933 --- /dev/null +++ b/pokemongo_bot/test/log_delay_test.py @@ -0,0 +1,62 @@ +import unittest +from datetime import timedelta, datetime +from time import sleep + +from mock import patch, MagicMock + +from pokemongo_bot.cell_workers import MoveToFort +from tests import FakeBot + + +class LogDelayTestCase(unittest.TestCase): + + config = { + "enabled": 'true', + "lure_attraction": 'true', + "lure_max_distance": 2000, + "walker": "StepWalker", + "log_delay": 3 + } + + def setUp(self): + self.bot = FakeBot() + self.bot.event_manager = MagicMock() + self.worker = MoveToFort(self.bot, self.config) + + def test_read_correct_delay_config(self): + self.worker.config['log_delay'] = 3 + self.assertEqual(self.config.get('log_delay'), 3) + + def test_log_with_no_delay(self): + self.worker.config['log_delay'] = 3 + # All those call should not happen cause without any delay between each other + self.worker.emit_event('moving_to_fort', formatted="just an example") + self.worker.emit_event('moving_to_fort', formatted="just an example") + + # Let's try to subtract 2 of 3 sec and see if event_manager.emit() get no call at all + self.worker.last_log_time -= 2 + self.worker.emit_event('moving_to_fort', formatted="just an example") + self.worker.emit_event('moving_to_fort', formatted="just an example") + + self.assertEqual(self.bot.event_manager.emit.call_count, 0) + assert not self.bot.event_manager.emit.called + + def test_correct_delay_wait(self): + + self.worker.config['log_delay'] = 2 + + # to avoid use sleep() function here, we subtract expected log_delay to last_log_time + self.worker.last_log_time -= self.worker.config['log_delay'] + + for number_of_checks in range(10): + self.worker.emit_event('moving_to_fort', formatted="just an example") + self.worker.last_log_time -= 2 + + self.assertEqual(self.bot.event_manager.emit.call_count, 10) + + # i think should be better assert in this way, but can't get it working + # assert self.bot.event_manager.emit.assert_any_call('moving_to_fort', + # sender=None, + # level='info', + # formatted="just an example",data={}) + From 3287687efae6c69cdb303f94723ee56ae7e2472d Mon Sep 17 00:00:00 2001 From: Loris D'Antonio Date: Fri, 26 Aug 2016 11:34:58 +0200 Subject: [PATCH 4/6] Rename log_delay in log_interval --- configs/config.json.example | 2 +- configs/config.json.map.example | 2 +- configs/config.json.optimizer.example | 2 +- configs/config.json.pokemon.example | 2 +- pokemongo_bot/base_task.py | 2 +- .../{log_delay_test.py => log_interval_test.py} | 14 +++++++------- 6 files changed, 12 insertions(+), 12 deletions(-) rename pokemongo_bot/test/{log_delay_test.py => log_interval_test.py} (85%) diff --git a/configs/config.json.example b/configs/config.json.example index c756d0c9e4..e62c7d9bb4 100644 --- a/configs/config.json.example +++ b/configs/config.json.example @@ -170,7 +170,7 @@ "lure_attraction": true, "lure_max_distance": 2000, "walker": "StepWalker", - "log_delay": 5 + "log_interval": 5 } }, { diff --git a/configs/config.json.map.example b/configs/config.json.map.example index 18d30d4417..f61303a009 100644 --- a/configs/config.json.map.example +++ b/configs/config.json.map.example @@ -401,7 +401,7 @@ "enabled": true, "lure_attraction": true, "lure_max_distance": 2000, - "log_delay": 5 + "log_interval": 5 } }, { diff --git a/configs/config.json.optimizer.example b/configs/config.json.optimizer.example index 6f0c6fa268..7d88aefec6 100644 --- a/configs/config.json.optimizer.example +++ b/configs/config.json.optimizer.example @@ -216,7 +216,7 @@ "enabled": true, "lure_attraction": true, "lure_max_distance": 2000, - "log_delay": 5 + "log_interval": 5 } }, { diff --git a/configs/config.json.pokemon.example b/configs/config.json.pokemon.example index f270768ccc..110ff49d27 100644 --- a/configs/config.json.pokemon.example +++ b/configs/config.json.pokemon.example @@ -154,7 +154,7 @@ "enabled": true, "lure_attraction": true, "lure_max_distance": 2000, - "log_delay": 5 + "log_interval": 5 } }, { diff --git a/pokemongo_bot/base_task.py b/pokemongo_bot/base_task.py index eda1652af6..8703566f10 100644 --- a/pokemongo_bot/base_task.py +++ b/pokemongo_bot/base_task.py @@ -32,7 +32,7 @@ def emit_event(self, event, sender=None, level='info', formatted='', data={}): sender=self # Print log only if X seconds are passed from last log - if (time.time() - self.last_log_time) > self.config.get('log_delay', 0): + if (time.time() - self.last_log_time) > self.config.get('log_interval', 0): self.last_log_time = time.time() self.bot.event_manager.emit( event, diff --git a/pokemongo_bot/test/log_delay_test.py b/pokemongo_bot/test/log_interval_test.py similarity index 85% rename from pokemongo_bot/test/log_delay_test.py rename to pokemongo_bot/test/log_interval_test.py index cb8e758933..21d47c7625 100644 --- a/pokemongo_bot/test/log_delay_test.py +++ b/pokemongo_bot/test/log_interval_test.py @@ -15,7 +15,7 @@ class LogDelayTestCase(unittest.TestCase): "lure_attraction": 'true', "lure_max_distance": 2000, "walker": "StepWalker", - "log_delay": 3 + "log_interval": 3 } def setUp(self): @@ -24,11 +24,11 @@ def setUp(self): self.worker = MoveToFort(self.bot, self.config) def test_read_correct_delay_config(self): - self.worker.config['log_delay'] = 3 - self.assertEqual(self.config.get('log_delay'), 3) + self.worker.config['log_interval'] = 3 + self.assertEqual(self.config.get('log_interval'), 3) def test_log_with_no_delay(self): - self.worker.config['log_delay'] = 3 + self.worker.config['log_interval'] = 3 # All those call should not happen cause without any delay between each other self.worker.emit_event('moving_to_fort', formatted="just an example") self.worker.emit_event('moving_to_fort', formatted="just an example") @@ -43,10 +43,10 @@ def test_log_with_no_delay(self): def test_correct_delay_wait(self): - self.worker.config['log_delay'] = 2 + self.worker.config['log_interval'] = 2 - # to avoid use sleep() function here, we subtract expected log_delay to last_log_time - self.worker.last_log_time -= self.worker.config['log_delay'] + # to avoid use sleep() function here, we subtract expected log_interval to last_log_time + self.worker.last_log_time -= self.worker.config['log_interval'] for number_of_checks in range(10): self.worker.emit_event('moving_to_fort', formatted="just an example") From 4e0348abb78fdc9c618bb80ab97582ae6aa19d67 Mon Sep 17 00:00:00 2001 From: Loris D'Antonio Date: Fri, 26 Aug 2016 12:11:04 +0200 Subject: [PATCH 5/6] Added log_interval to docs --- docs/configuration_files.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/configuration_files.md b/docs/configuration_files.md index d931ee5a7f..7d8dbc22d2 100644 --- a/docs/configuration_files.md +++ b/docs/configuration_files.md @@ -125,7 +125,30 @@ The behaviors of the bot are configured via the `tasks` key in the `config.json` * `min_free_slot`: Default `5` | Once the pokebag has less empty slots than this amount, the transfer process is triggered. | Big values (i.e 9999) will trigger the transfer process after each catch. * UpdateLiveStats * [UpdateLiveInventory](#updateliveinventory-settings) - +* All tasks + * `log_interval`: Default `0` | Minimum seconds interval before next log of the current task will be printed + + +### Specify a custom log_interval for specific task + + ``` + { + "type": "MoveToFort", + "config": { + "enabled": true, + "lure_attraction": true, + "lure_max_distance": 2000, + "walker": "StepWalker", + "log_interval": 5 + } + } + ``` + + Result: + + 2016-08-26 11:43:18,199 [MoveToFort] [INFO] [moving_to_fort] Moving towards pokestop ... - 0.07km + 2016-08-26 11:43:23,641 [MoveToFort] [INFO] [moving_to_fort] Moving towards pokestop ... - 0.06km + 2016-08-26 11:43:28,198 [MoveToFort] [INFO] [moving_to_fort] Moving towards pokestop ... - 0.05km ### Example configuration: [[back to top](#table-of-contents)] From 6d783f4f723be847e4de57e540a6590682164648 Mon Sep 17 00:00:00 2001 From: Loris D'Antonio Date: Fri, 26 Aug 2016 23:41:58 +0200 Subject: [PATCH 6/6] Restored submodule reference --- web | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web b/web index 607397a13f..6ba5609c61 160000 --- a/web +++ b/web @@ -1 +1 @@ -Subproject commit 607397a13f344c0fdc44bd9961332c1efe205de9 +Subproject commit 6ba5609c6151507b5b832a74e471b6b7b1a182c9