From c1482eaacdc2ad5f6133c574936d1a8bbe81f8ae Mon Sep 17 00:00:00 2001 From: tedder Date: Tue, 6 Nov 2018 15:37:45 -0800 Subject: [PATCH 1/4] add throttle_state to tracking - weave the throttle_state through pi_support and tracking. This should come in handy to see how many throttled Pis are out there, as well as if it is correllated with canceled prints. - upgraded disclosure of tracking information so it shows as INFO-level in the logs and all the information is dumped. If tracking.octoprint.org gets it, I should have it too :) --- src/octoprint/plugins/pi_support/__init__.py | 22 ++++++++++++++----- src/octoprint/plugins/tracking/__init__.py | 23 +++++++++++++++++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/octoprint/plugins/pi_support/__init__.py b/src/octoprint/plugins/pi_support/__init__.py index 28b4f3d03..04f85ea29 100644 --- a/src/octoprint/plugins/pi_support/__init__.py +++ b/src/octoprint/plugins/pi_support/__init__.py @@ -35,10 +35,9 @@ _FLAG_PAST_UNDERVOLTAGE = 1 << 16 _FLAG_PAST_FREQ_CAPPED = 1 << 17 _FLAG_PAST_THROTTLED = 1 << 18 - class ThrottleState(object): @classmethod - def from_value(cls, value): + def from_value(cls, value=0): if value == 0: return ThrottleState() @@ -47,10 +46,12 @@ class ThrottleState(object): throttled=_FLAG_THROTTLED & value == _FLAG_THROTTLED, past_undervoltage=_FLAG_PAST_UNDERVOLTAGE & value == _FLAG_PAST_UNDERVOLTAGE, past_freq_capped=_FLAG_PAST_FREQ_CAPPED & value == _FLAG_PAST_FREQ_CAPPED, - past_throttled=_FLAG_PAST_THROTTLED & value == _FLAG_PAST_THROTTLED) + past_throttled=_FLAG_PAST_THROTTLED & value == _FLAG_PAST_THROTTLED, + raw_value=value) return ThrottleState(**kwargs) def __init__(self, **kwargs): + self._raw_value = kwargs.get('raw_value', -1) self._undervoltage = False self._freq_capped = False self._throttled = False @@ -111,7 +112,8 @@ class ThrottleState(object): and self._past_throttled == other._past_throttled def as_dict(self): - return dict(current_undervoltage=self.current_undervoltage, + return dict(raw_value=self._raw_value, + current_undervoltage=self.current_undervoltage, past_undervoltage=self.past_undervoltage, current_overheat=self.current_overheat, past_overheat=self.past_overheat, @@ -242,6 +244,10 @@ class PiSupportPlugin(octoprint.plugin.EnvironmentDetectionPlugin, def _check_throttled_state_condition(self): return self._throttle_functional + def get_throttle_state(self, run_now=False): + '''public method to pass around in plugin_helpers''' + return self._throttle_state.as_dict() + def _check_throttled_state(self): command = self._settings.get(["vcgencmd_throttle_check_command"]) @@ -299,5 +305,11 @@ def __plugin_check__(): return "raspberry pi" in proc_dt_model.lower() def __plugin_load__(): + plugin = PiSupportPlugin() global __plugin_implementation__ - __plugin_implementation__ = PiSupportPlugin() + __plugin_implementation__ = plugin + + global __plugin_helpers__ + __plugin_helpers__ = dict( + get_throttled=plugin.get_throttle_state + ) diff --git a/src/octoprint/plugins/tracking/__init__.py b/src/octoprint/plugins/tracking/__init__.py index 108b275e5..30e65d46b 100644 --- a/src/octoprint/plugins/tracking/__init__.py +++ b/src/octoprint/plugins/tracking/__init__.py @@ -82,15 +82,23 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, self._environment = environment ##~~ StartupPlugin + def on_startup(self, *args, **kwargs): + self._helpers_get_throttle_state = None def on_after_startup(self): - self._track_startup() - ping = self._settings.get_int(["ping"]) if ping: self._ping_worker = RepeatedTimer(ping, self._track_ping) self._ping_worker.start() + # cautiously look for the get_throttled helper from pi_support + pi_helper = self._plugin_manager.get_helpers("pi_support", "get_throttled") + if pi_helper and 'get_throttled' in pi_helper: + self._helpers_get_throttle_state = pi_helper['get_throttled'] + + # now that we have everything set up, phone home. + self._track_startup() + ##~~ ShutdownPlugin def on_shutdown(self): @@ -160,8 +168,12 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, freq=self._environment[b"hardware"][b"freq"], ram=self._environment[b"hardware"][b"ram"]) + if self._helpers_get_throttle_state: + payload[b"throttle_state"] = self._helpers_get_throttle_state() + if b"plugins" in self._environment and b"pi_support" in self._environment[b"plugins"]: payload[b"pi_model"] = self._environment[b"plugins"][b"pi_support"][b"model"] + if b"octopi_version" in self._environment[b"plugins"][b"pi_support"]: payload[b"octopi_version"] = self._environment[b"plugins"][b"pi_support"][b"octopi_version"] @@ -219,6 +231,11 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, track_event = "print_failed" elif event == Events.PRINT_CANCELLED: track_event = "print_cancelled" + else: + logger.info("unknown event, skipping: {}".format(event)) + + if self._helpers_get_throttle_state: + args[b"throttle_state"] = self._helpers_get_throttle_state() if track_event is not None: self._track(track_event, **args) @@ -254,7 +271,7 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, params=params, timeout=3.1, headers=headers) - self._logger.debug("Sent tracking event to {}".format(url)) + self._logger.info("Sent tracking event to {}, params: {}".format(url, kwargs)) except: if self._logger.isEnabledFor(logging.DEBUG): self._logger.exception("Error while sending event to anonymous usage tracking".format(url)) From 76f48b3b137691679efdeb88d6d38aed34a1647f Mon Sep 17 00:00:00 2001 From: tedder Date: Thu, 8 Nov 2018 01:29:15 +0000 Subject: [PATCH 2/4] PR feedback: throttle state and uuid - add a way to get the raw value only from throttle state (glad I already plumbed it into the object!) - add warning about not sending uuid, modify the log entry too --- src/octoprint/plugins/pi_support/__init__.py | 8 ++++++-- src/octoprint/plugins/tracking/__init__.py | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/octoprint/plugins/pi_support/__init__.py b/src/octoprint/plugins/pi_support/__init__.py index 04f85ea29..69c3a9448 100644 --- a/src/octoprint/plugins/pi_support/__init__.py +++ b/src/octoprint/plugins/pi_support/__init__.py @@ -244,8 +244,12 @@ class PiSupportPlugin(octoprint.plugin.EnvironmentDetectionPlugin, def _check_throttled_state_condition(self): return self._throttle_functional - def get_throttle_state(self, run_now=False): - '''public method to pass around in plugin_helpers''' + def get_throttle_state(self, raw_value_only=False, run_now=False): + '''public method to pass around in plugin_helpers.''' + + # Not using the `run_now` yet, but leaving it as a placeholder. + + if raw_value_only: return self._throttle_state._raw_value return self._throttle_state.as_dict() def _check_throttled_state(self): diff --git a/src/octoprint/plugins/tracking/__init__.py b/src/octoprint/plugins/tracking/__init__.py index 30e65d46b..fbe7561a4 100644 --- a/src/octoprint/plugins/tracking/__init__.py +++ b/src/octoprint/plugins/tracking/__init__.py @@ -169,7 +169,7 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, ram=self._environment[b"hardware"][b"ram"]) if self._helpers_get_throttle_state: - payload[b"throttle_state"] = self._helpers_get_throttle_state() + payload[b"throttle_state"] = self._helpers_get_throttle_state(raw_value_only=True) if b"plugins" in self._environment and b"pi_support" in self._environment[b"plugins"]: payload[b"pi_model"] = self._environment[b"plugins"][b"pi_support"][b"model"] @@ -263,15 +263,18 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, server = self._settings.get([b"server"]) url = server.format(id=self._settings.get([b"unique_id"]), event=event) + # don't print the URL or uuid! That would expose the uuid in forums if pasted. + # (it's okay for the user to know their uuid, but it shouldn't be shared) headers = {"User-Agent": "OctoPrint/{}".format(get_octoprint_version_string())} try: params = urlencode(kwargs, doseq=True).replace("+", "%20") + requests.get(url, params=params, timeout=3.1, headers=headers) - self._logger.info("Sent tracking event to {}, params: {}".format(url, kwargs)) + self._logger.info("Sent tracking event to /{}, params: {}".format(event, kwargs)) except: if self._logger.isEnabledFor(logging.DEBUG): self._logger.exception("Error while sending event to anonymous usage tracking".format(url)) From 98c84b6bb9304919929d4602631dd7b4f473a113 Mon Sep 17 00:00:00 2001 From: tedder Date: Thu, 8 Nov 2018 01:34:45 +0000 Subject: [PATCH 3/4] fix test, match dict --- tests/plugins/pi_support/test_pi_support.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/plugins/pi_support/test_pi_support.py b/tests/plugins/pi_support/test_pi_support.py index be33f8c71..c7607bde8 100644 --- a/tests/plugins/pi_support/test_pi_support.py +++ b/tests/plugins/pi_support/test_pi_support.py @@ -150,4 +150,5 @@ class ThrottleStateTestCase(unittest.TestCase): current_overheat=False, past_overheat=True, current_issue=True, - past_issue=True)) + past_issue=True, + raw_value=-1)) From 38bb81cfe7544b18f2b5aad64881a03c946e1c12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 8 Nov 2018 15:49:11 +0100 Subject: [PATCH 4/4] tracking: add throttle events, slightly change throttle payload Also adjusted helper signature in pi_support plugin slightly. Addon to #2878 --- src/octoprint/plugins/pi_support/__init__.py | 15 ++--- src/octoprint/plugins/tracking/__init__.py | 55 ++++++++++++++----- .../templates/tracking_settings.jinja2 | 3 + 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/octoprint/plugins/pi_support/__init__.py b/src/octoprint/plugins/pi_support/__init__.py index 69c3a9448..8ea0af7be 100644 --- a/src/octoprint/plugins/pi_support/__init__.py +++ b/src/octoprint/plugins/pi_support/__init__.py @@ -244,12 +244,14 @@ class PiSupportPlugin(octoprint.plugin.EnvironmentDetectionPlugin, def _check_throttled_state_condition(self): return self._throttle_functional - def get_throttle_state(self, raw_value_only=False, run_now=False): - '''public method to pass around in plugin_helpers.''' + def get_throttle_state(self, run_now=False): + """Exposed as public helper.""" + if run_now: + self._check_throttled_state() - # Not using the `run_now` yet, but leaving it as a placeholder. + if not self._throttle_functional: + return False - if raw_value_only: return self._throttle_state._raw_value return self._throttle_state.as_dict() def _check_throttled_state(self): @@ -288,6 +290,7 @@ class PiSupportPlugin(octoprint.plugin.EnvironmentDetectionPlugin, self._plugin_manager.send_plugin_message(self._identifier, dict(type="throttle_state", state=self._throttle_state.as_dict())) + self._event_bus.fire("plugin_pi_support_throttle_state", self._throttle_state.as_dict()) __plugin_name__ = "Pi Support Plugin" @@ -314,6 +317,4 @@ def __plugin_load__(): __plugin_implementation__ = plugin global __plugin_helpers__ - __plugin_helpers__ = dict( - get_throttled=plugin.get_throttle_state - ) + __plugin_helpers__ = dict(get_throttled=plugin.get_throttle_state) diff --git a/src/octoprint/plugins/tracking/__init__.py b/src/octoprint/plugins/tracking/__init__.py index fbe7561a4..436ab1ed1 100644 --- a/src/octoprint/plugins/tracking/__init__.py +++ b/src/octoprint/plugins/tracking/__init__.py @@ -39,6 +39,8 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, def __init__(self): self._environment = None + self._throttle_state = None + self._helpers_get_throttle_state = None self._printer_connection_parameters = None self._url = None self._ping_worker = None @@ -60,7 +62,8 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, printjob=True, plugin=True, update=True, - printer=True)) + printer=True, + throttled=True)) def get_settings_restricted_paths(self): return dict(admin=[["enabled"], ["unique_id"], ["events"]], @@ -82,8 +85,6 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, self._environment = environment ##~~ StartupPlugin - def on_startup(self, *args, **kwargs): - self._helpers_get_throttle_state = None def on_after_startup(self): ping = self._settings.get_int(["ping"]) @@ -109,8 +110,11 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, def on_event(self, event, payload): if event.startswith("plugin_pluginmanager_"): self._track_plugin_event(event, payload) - if event.startswith("plugin_softwareupdate_"): + elif event.startswith("plugin_softwareupdate_"): self._track_update_event(event, payload) + elif event in ("plugin_pi_support_throttle_state",): + self._throttle_state = payload + self._track_throttle_event(event, payload) elif event in (Events.PRINT_STARTED, Events.PRINT_DONE, Events.PRINT_FAILED, Events.PRINT_CANCELLED): self._track_printjob_event(event, payload) elif event in (Events.CONNECTED,): @@ -168,9 +172,6 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, freq=self._environment[b"hardware"][b"freq"], ram=self._environment[b"hardware"][b"ram"]) - if self._helpers_get_throttle_state: - payload[b"throttle_state"] = self._helpers_get_throttle_state(raw_value_only=True) - if b"plugins" in self._environment and b"pi_support" in self._environment[b"plugins"]: payload[b"pi_model"] = self._environment[b"plugins"][b"pi_support"][b"model"] @@ -207,6 +208,26 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, elif event.endswith("_update_failed"): self._track("update_failed", target=payload.get("target"), from_version=payload.get("from_version"), to_version=payload.get("to_version")) + def _track_throttle_event(self, event, payload): + if not self._settings.get_boolean(["events", "throttled"]): + return + + args = dict(throttled_now=payload[b"current_issue"], + throttled_past=payload[b"past_issue"], + throttled_mask=payload[b"raw_value"], + throttled_voltage_now=payload[b"current_undervoltage"], + throttled_voltage_past=payload[b"past_undervoltage"], + throttled_overheat_now=payload[b"current_overheat"], + throttled_overheat_past=payload[b"past_overheat"]) + + if payload[b"current_issue"]: + track_event = "system_throttled" + else: + track_event = "system_unthrottled" + + if track_event is not None: + self._track(track_event, **args) + def _track_printjob_event(self, event, payload): if not self._settings.get_boolean(["events", "printjob"]): return @@ -231,11 +252,17 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, track_event = "print_failed" elif event == Events.PRINT_CANCELLED: track_event = "print_cancelled" - else: - logger.info("unknown event, skipping: {}".format(event)) - if self._helpers_get_throttle_state: - args[b"throttle_state"] = self._helpers_get_throttle_state() + if callable(self._helpers_get_throttle_state): + try: + throttle_state = self._helpers_get_throttle_state(run_now=True) + if throttle_state and (throttle_state.get(b"current_issue", False) or throttle_state.get(b"past_issue", False)): + args[b"throttled_now"] = throttle_state[b"current_issue"] + args[b"throttled_past"] = throttle_state[b"past_issue"] + args[b"throttled_mask"] = throttle_state[b"raw_value"] + except: + # ignored + pass if track_event is not None: self._track(track_event, **args) @@ -263,8 +290,8 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, server = self._settings.get([b"server"]) url = server.format(id=self._settings.get([b"unique_id"]), event=event) - # don't print the URL or uuid! That would expose the uuid in forums if pasted. - # (it's okay for the user to know their uuid, but it shouldn't be shared) + # Don't print the URL or UUID! That would expose the UUID in forums/tickets + # if pasted. It's okay for the user to know their uuid, but it shouldn't be shared. headers = {"User-Agent": "OctoPrint/{}".format(get_octoprint_version_string())} try: @@ -274,7 +301,7 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, params=params, timeout=3.1, headers=headers) - self._logger.info("Sent tracking event to /{}, params: {}".format(event, kwargs)) + self._logger.info("Sent tracking event {}, payload: {!r}".format(event, kwargs)) except: if self._logger.isEnabledFor(logging.DEBUG): self._logger.exception("Error while sending event to anonymous usage tracking".format(url)) diff --git a/src/octoprint/plugins/tracking/templates/tracking_settings.jinja2 b/src/octoprint/plugins/tracking/templates/tracking_settings.jinja2 index 3a5fa8a08..217bc26c6 100644 --- a/src/octoprint/plugins/tracking/templates/tracking_settings.jinja2 +++ b/src/octoprint/plugins/tracking/templates/tracking_settings.jinja2 @@ -37,6 +37,9 @@ +