From 5bf69a0c212059f69d3ecf75815fa2bd3da3bcba Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 8 Oct 2018 00:52:16 -0500 Subject: [PATCH] Add "recent readings" to email template context --- rattail_tempmon/server.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/rattail_tempmon/server.py b/rattail_tempmon/server.py index 82851ac..0246b11 100644 --- a/rattail_tempmon/server.py +++ b/rattail_tempmon/server.py @@ -32,6 +32,7 @@ import logging import six import humanize +from sqlalchemy import orm from sqlalchemy.exc import OperationalError from rattail.db import Session, api @@ -182,12 +183,13 @@ class TempmonServerDaemon(Daemon): return False def update_status(self, probe, status, reading=None): + now = localtime(self.config) data = { 'probe': probe, 'status': self.enum.TEMPMON_PROBE_STATUS[status], 'reading': reading, 'taken': localtime(self.config, reading.taken, from_utc=True) if reading else None, - 'now': localtime(self.config), + 'now': now, } prev_status = probe.status @@ -223,6 +225,18 @@ class TempmonServerDaemon(Daemon): data['status_since'] = since.strftime('%I:%M %p') data['status_since_delta'] = humanize.naturaltime(self.now - probe.status_changed) + # fetch last 90 minutes of readings + session = orm.object_session(probe) + recent_minutes = 90 # TODO: make configurable + cutoff = now - datetime.timedelta(seconds=(60 * recent_minutes)) + readings = session.query(tempmon.Reading)\ + .filter(tempmon.Reading.probe == probe)\ + .filter(tempmon.Reading.taken >= make_utc(cutoff))\ + .order_by(tempmon.Reading.taken.desc()) + data['recent_minutes'] = recent_minutes + data['recent_readings'] = readings + data['pretty_time'] = lambda dt: localtime(self.config, dt, from_utc=True).strftime('%Y-%m-%d %I:%M %p') + msgtypes = { self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP : 'tempmon_low_temp', self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP : 'tempmon_high_temp',