Add "recent readings" to email template context

This commit is contained in:
Lance Edgar 2018-10-08 00:52:16 -05:00
parent b4c52319c6
commit 5bf69a0c21

View file

@ -32,6 +32,7 @@ import logging
import six import six
import humanize import humanize
from sqlalchemy import orm
from sqlalchemy.exc import OperationalError from sqlalchemy.exc import OperationalError
from rattail.db import Session, api from rattail.db import Session, api
@ -182,12 +183,13 @@ class TempmonServerDaemon(Daemon):
return False return False
def update_status(self, probe, status, reading=None): def update_status(self, probe, status, reading=None):
now = localtime(self.config)
data = { data = {
'probe': probe, 'probe': probe,
'status': self.enum.TEMPMON_PROBE_STATUS[status], 'status': self.enum.TEMPMON_PROBE_STATUS[status],
'reading': reading, 'reading': reading,
'taken': localtime(self.config, reading.taken, from_utc=True) if reading else None, 'taken': localtime(self.config, reading.taken, from_utc=True) if reading else None,
'now': localtime(self.config), 'now': now,
} }
prev_status = probe.status prev_status = probe.status
@ -223,6 +225,18 @@ class TempmonServerDaemon(Daemon):
data['status_since'] = since.strftime('%I:%M %p') data['status_since'] = since.strftime('%I:%M %p')
data['status_since_delta'] = humanize.naturaltime(self.now - probe.status_changed) 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 = { msgtypes = {
self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP : 'tempmon_low_temp', self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP : 'tempmon_low_temp',
self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP : 'tempmon_high_temp', self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP : 'tempmon_high_temp',