From 76e40063eeec123511ff6edcb1232a7048d16f2a Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 6 Oct 2018 18:09:02 -0500 Subject: [PATCH] Tweak server logic for checking client readings do not check readings for "archived" clients. do not consider the client "offline" unless it has *no* current probe readings. previously we were assuming offline if any probe readings were missing, even if some were found. --- rattail_tempmon/server.py | 42 +++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/rattail_tempmon/server.py b/rattail_tempmon/server.py index 34c4a24..58808f8 100644 --- a/rattail_tempmon/server.py +++ b/rattail_tempmon/server.py @@ -63,7 +63,8 @@ class TempmonServerDaemon(Daemon): try: clients = session.query(tempmon.Client)\ - .filter(tempmon.Client.enabled == True) + .filter(tempmon.Client.enabled == True)\ + .filter(tempmon.Client.archived == False) for client in clients: self.check_readings_for_client(session, client) except: @@ -75,11 +76,18 @@ class TempmonServerDaemon(Daemon): session.close() def check_readings_for_client(self, session, client): + """ + Check readings for all (enabled) probes for the given client. + """ + # cutoff is calculated as the client delay (i.e. how often it takes + # readings) plus one minute. we "should" have a reading for each probe + # within that time window. if no readings are found we will consider + # the client to be (possibly) offline. delay = client.delay or 60 cutoff = self.now - datetime.timedelta(seconds=delay + 60) - online = True + online = False for probe in client.enabled_probes(): - online = online and bool(self.check_readings_for_probe(session, probe, cutoff)) + online = online or bool(self.check_readings_for_probe(session, probe, cutoff)) # if client was previously marked online, but we have no "new" # readings, then let's look closer to see if it's been long enough to @@ -103,31 +111,39 @@ class TempmonServerDaemon(Daemon): def check_readings_for_probe(self, session, probe, cutoff): - readings = session.query(tempmon.Reading)\ - .filter(tempmon.Reading.probe == probe)\ - .filter(tempmon.Reading.taken >= cutoff)\ - .all() - if readings: - # we really only care about the latest reading - reading = sorted(readings, key=lambda r: r.taken)[-1] + """ + Check readings for the given probe, within the time window defined by + the given cutoff. + """ + # we really only care about the latest reading + reading = session.query(tempmon.Reading)\ + .filter(tempmon.Reading.probe == probe)\ + .filter(tempmon.Reading.taken >= cutoff)\ + .order_by(tempmon.Reading.taken.desc())\ + .first() + if reading: + # is reading below critical min, or above critical max? if (reading.degrees_f <= probe.critical_temp_min or reading.degrees_f >= probe.critical_temp_max): self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP, reading) + # is reading below "good" min? elif reading.degrees_f < probe.good_temp_min: self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP, reading) + # is reading above "good" max? elif reading.degrees_f > probe.good_temp_max: self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP, reading) else: # temp is good self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP, reading) - else: # no readings for probe - self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR) + return True - return readings + else: # no current readings for probe + self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR) + return False def update_status(self, probe, status, reading=None): data = {