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.
This commit is contained in:
Lance Edgar 2018-10-06 18:09:02 -05:00
parent 440abf2b56
commit 76e40063ee

View file

@ -63,7 +63,8 @@ class TempmonServerDaemon(Daemon):
try: try:
clients = session.query(tempmon.Client)\ clients = session.query(tempmon.Client)\
.filter(tempmon.Client.enabled == True) .filter(tempmon.Client.enabled == True)\
.filter(tempmon.Client.archived == False)
for client in clients: for client in clients:
self.check_readings_for_client(session, client) self.check_readings_for_client(session, client)
except: except:
@ -75,11 +76,18 @@ class TempmonServerDaemon(Daemon):
session.close() session.close()
def check_readings_for_client(self, session, client): 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 delay = client.delay or 60
cutoff = self.now - datetime.timedelta(seconds=delay + 60) cutoff = self.now - datetime.timedelta(seconds=delay + 60)
online = True online = False
for probe in client.enabled_probes(): 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" # 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 # 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): def check_readings_for_probe(self, session, probe, cutoff):
readings = session.query(tempmon.Reading)\ """
.filter(tempmon.Reading.probe == probe)\ Check readings for the given probe, within the time window defined by
.filter(tempmon.Reading.taken >= cutoff)\ the given cutoff.
.all() """
if readings: # we really only care about the latest reading
# we really only care about the latest reading reading = session.query(tempmon.Reading)\
reading = sorted(readings, key=lambda r: r.taken)[-1] .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 if (reading.degrees_f <= probe.critical_temp_min or
reading.degrees_f >= probe.critical_temp_max): reading.degrees_f >= probe.critical_temp_max):
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP, reading) 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: elif reading.degrees_f < probe.good_temp_min:
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP, reading) 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: elif reading.degrees_f > probe.good_temp_max:
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP, reading) self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP, reading)
else: # temp is good else: # temp is good
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP, reading) self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP, reading)
else: # no readings for probe return True
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR)
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): def update_status(self, probe, status, reading=None):
data = { data = {