Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add throttle_state to tracking #2878

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/octoprint/plugins/pi_support/__init__.py
Expand Up @@ -35,10 +35,9 @@
_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()

Expand All @@ -47,10 +46,12 @@ def from_value(cls, value):
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
Expand Down Expand Up @@ -111,7 +112,8 @@ def __eq__(self, other):
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,
Expand Down Expand Up @@ -242,6 +244,14 @@ def _check_throttled_state_interval(self):
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.'''

# 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):
command = self._settings.get(["vcgencmd_throttle_check_command"])

Expand Down Expand Up @@ -299,5 +309,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
)
26 changes: 23 additions & 3 deletions src/octoprint/plugins/tracking/__init__.py
Expand Up @@ -82,15 +82,23 @@ def on_environment_detected(self, environment, *args, **kwargs):
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):
Expand Down Expand Up @@ -160,8 +168,12 @@ def _track_startup(self):
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"]

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"]

Expand Down Expand Up @@ -219,6 +231,11 @@ def _track_printjob_event(self, event, payload):
track_event = "print_failed"
elif event == Events.PRINT_CANCELLED:
track_event = "print_cancelled"
else:
logger.info("unknown event, skipping: {}".format(event))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be debug, otherwise we'll spam the log.


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)
Expand Down Expand Up @@ -246,15 +263,18 @@ def _do_track(self, event, **kwargs):

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.debug("Sent tracking event to {}".format(url))
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))
Expand Down
3 changes: 2 additions & 1 deletion tests/plugins/pi_support/test_pi_support.py
Expand Up @@ -150,4 +150,5 @@ def test_as_dict(self):
current_overheat=False,
past_overheat=True,
current_issue=True,
past_issue=True))
past_issue=True,
raw_value=-1))