Skip to content

Commit

Permalink
Merge pull request #1751 from Odianosen25/plugin-perf
Browse files Browse the repository at this point in the history
Tidy perf code
  • Loading branch information
acockburn committed Apr 16, 2023
2 parents 8c6ce33 + 8b7291b commit 19f2c61
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 62 deletions.
31 changes: 31 additions & 0 deletions appdaemon/plugin_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,40 @@ def __init__(self, ad: AppDaemon, name, args):
self.AD = ad
self.logger = self.AD.logging.get_child(name)

# Performance Data

self.bytes_sent = 0
self.bytes_recv = 0
self.requests_sent = 0
self.updates_recv = 0
self.last_check_ts = 0

def set_log_level(self, level):
self.logger.setLevel(self.AD.logging.log_levels[level])

async def perf_data(self):
data = {
"bytes_sent": self.bytes_sent,
"bytes_recv": self.bytes_recv,
"requests_sent": self.requests_sent,
"updates_recv": self.updates_recv,
"duration": await self.AD.sched.get_now_ts() - self.last_check_ts,
}

self.bytes_sent = 0
self.bytes_recv = 0
self.requests_sent = 0
self.updates_recv = 0
self.last_check_ts = await self.AD.sched.get_now_ts()

return data

def update_perf(self, **kwargs):
self.bytes_sent += kwargs.get("bytes_sent", 0)
self.bytes_recv += kwargs.get("bytes_recv", 0)
self.requests_sent += kwargs.get("requests_sent", 0)
self.updates_recv += kwargs.get("updates_recv", 0)


class Plugins:
required_meta = ["latitude", "longitude", "elevation", "time_zone"]
Expand Down
31 changes: 0 additions & 31 deletions appdaemon/plugins/hass/hassplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,39 +83,8 @@ def __init__(self, ad: AppDaemon, name, args):
self.reading_messages = False
self.stopping = False

# Performance Data

self.bytes_sent = 0
self.bytes_recv = 0
self.requests_sent = 0
self.updates_recv = 0
self.last_check_ts = 0

self.logger.info("HASS Plugin initialization complete")

async def perf_data(self):
data = {
"bytes_sent": self.bytes_sent,
"bytes_recv": self.bytes_recv,
"requests_sent": self.requests_sent,
"updates_recv": self.updates_recv,
"duration": await self.AD.sched.get_now_ts() - self.last_check_ts,
}

self.bytes_sent = 0
self.bytes_recv = 0
self.requests_sent = 0
self.updates_recv = 0
self.last_check_ts = await self.AD.sched.get_now_ts()

return data

def update_perf(self, **kwargs):
self.bytes_sent += kwargs.get("bytes_sent", 0)
self.bytes_recv += kwargs.get("bytes_recv", 0)
self.requests_sent += kwargs.get("requests_sent", 0)
self.updates_recv += kwargs.get("updates_recv", 0)

async def am_reading_messages(self):
return self.reading_messages

Expand Down
31 changes: 0 additions & 31 deletions appdaemon/plugins/mqtt/mqttplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ def __init__(self, ad: AppDaemon, name, args):
self.mqtt_connected = False
self.state = {}

# Performance Data

self.bytes_sent = 0
self.bytes_recv = 0
self.requests_sent = 0
self.updates_recv = 0
self.last_check_ts = 0

self.logger.info("MQTT Plugin Initializing")

self.name = name
Expand Down Expand Up @@ -144,29 +136,6 @@ def __init__(self, ad: AppDaemon, name, args):

self.mqtt_connect_event = None

async def perf_data(self):
data = {
"bytes_sent": self.bytes_sent,
"bytes_recv": self.bytes_recv,
"requests_sent": self.requests_sent,
"updates_recv": self.updates_recv,
"duration": await self.AD.sched.get_now_ts() - self.last_check_ts,
}

self.bytes_sent = 0
self.bytes_recv = 0
self.requests_sent = 0
self.updates_recv = 0
self.last_check_ts = await self.AD.sched.get_now_ts()

return data

def update_perf(self, **kwargs):
self.bytes_sent += kwargs.get("bytes_sent", 0)
self.bytes_recv += kwargs.get("bytes_recv", 0)
self.requests_sent += kwargs.get("requests_sent", 0)
self.updates_recv += kwargs.get("updates_recv", 0)

def stop(self):
self.logger.debug("stop() called for %s", self.name)
self.stopping = True
Expand Down

0 comments on commit 19f2c61

Please sign in to comment.