From fd7cd5cadd79932efcf85b1c2157f54d4716ec84 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 18 Nov 2017 22:12:40 -0600 Subject: [PATCH] Add problem report for disabled clients/probes --- rattail_tempmon/commands.py | 13 +++++ rattail_tempmon/emails.py | 33 +++++++++--- rattail_tempmon/problems.py | 51 +++++++++++++++++++ .../mail/tempmon_disabled_probes.html.mako | 31 +++++++++++ setup.py | 1 + 5 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 rattail_tempmon/problems.py create mode 100644 rattail_tempmon/templates/mail/tempmon_disabled_probes.html.mako diff --git a/rattail_tempmon/commands.py b/rattail_tempmon/commands.py index d830d8b..312c4d3 100644 --- a/rattail_tempmon/commands.py +++ b/rattail_tempmon/commands.py @@ -145,3 +145,16 @@ class TempmonServer(commands.Subcommand): daemon.start(args.daemonize) elif args.subcommand == 'stop': daemon.stop() + + +class TempmonProblems(commands.Subcommand): + """ + Email report(s) of various Tempmon data problems + """ + name = 'tempmon-problems' + description = __doc__.strip() + + def run(self, args): + from rattail_tempmon import problems + + problems.disabled_probes(self.config, progress=self.progress) diff --git a/rattail_tempmon/emails.py b/rattail_tempmon/emails.py index 691d616..6f76ec2 100644 --- a/rattail_tempmon/emails.py +++ b/rattail_tempmon/emails.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8; -*- ################################################################################ # # Rattail -- Retail Software Framework @@ -30,8 +30,10 @@ from rattail.db import model from rattail.mail import Email from rattail.time import localtime +from rattail_tempmon.db import model as tempmon -class tempmon(object): + +class TempmonBase(object): """ Generic base class for all tempmon-related emails; adds common sample data. """ @@ -50,7 +52,7 @@ class tempmon(object): } -class tempmon_critical_temp(tempmon, Email): +class tempmon_critical_temp(TempmonBase, Email): """ Sent when a tempmon probe takes a reading which is "critical" in either the high or low sense. @@ -63,7 +65,7 @@ class tempmon_critical_temp(tempmon, Email): return data -class tempmon_error(tempmon, Email): +class tempmon_error(TempmonBase, Email): """ Sent when a tempmon probe is noticed to have some error, i.e. no current readings. """ @@ -76,7 +78,7 @@ class tempmon_error(tempmon, Email): return data -class tempmon_good_temp(tempmon, Email): +class tempmon_good_temp(TempmonBase, Email): """ Sent whenever a tempmon probe first takes a "good temp" reading, after having previously had some bad reading(s). @@ -89,7 +91,7 @@ class tempmon_good_temp(tempmon, Email): return data -class tempmon_high_temp(tempmon, Email): +class tempmon_high_temp(TempmonBase, Email): """ Sent when a tempmon probe takes a reading which is above the "maximum good temp" range, but still below the "critically high temp" threshold. @@ -102,7 +104,7 @@ class tempmon_high_temp(tempmon, Email): return data -class tempmon_low_temp(tempmon, Email): +class tempmon_low_temp(TempmonBase, Email): """ Sent when a tempmon probe takes a reading which is below the "minimum good temp" range, but still above the "critically low temp" threshold. @@ -113,3 +115,20 @@ class tempmon_low_temp(tempmon, Email): data = super(tempmon_low_temp, self).sample_data(request) data['status'] = self.enum.TEMPMON_PROBE_STATUS[self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP] return data + + +class tempmon_disabled_probes(Email): + """ + Notifies of any Tempmon client devices or probes which are disabled. + """ + default_subject = "Disabled probes" + + def sample_data(self, request): + return { + 'disabled_clients': [ + tempmon.Client(config_key='foo', hostname='foo.example.com'), + ], + 'disabled_probes': [ + tempmon.Probe(description="north wall of walk-in cooler"), + ], + } diff --git a/rattail_tempmon/problems.py b/rattail_tempmon/problems.py new file mode 100644 index 0000000..7999b22 --- /dev/null +++ b/rattail_tempmon/problems.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8; -*- +################################################################################ +# +# Rattail -- Retail Software Framework +# Copyright © 2010-2017 Lance Edgar +# +# This file is part of Rattail. +# +# Rattail is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. +# +# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# Rattail. If not, see . +# +################################################################################ +""" +Tempmon data problems +""" + +from __future__ import unicode_literals, absolute_import + +from rattail.mail import send_email +from rattail_tempmon.db import Session as TempmonSession, model as tempmon + + +def disabled_probes(config, progress=None): + """ + Notifies if any Tempmon client devices or probes are disabled. + """ + tempmon_session = TempmonSession() + clients = tempmon_session.query(tempmon.Client)\ + .filter(tempmon.Client.enabled == False)\ + .all() + probes = tempmon_session.query(tempmon.Probe)\ + .join(tempmon.Client)\ + .filter(tempmon.Client.enabled == True)\ + .filter(tempmon.Probe.enabled == False)\ + .all() + if clients or probes: + send_email(config, 'tempmon_disabled_probes', { + 'disabled_clients': clients, + 'disabled_probes': probes, + }) + tempmon_session.close() diff --git a/rattail_tempmon/templates/mail/tempmon_disabled_probes.html.mako b/rattail_tempmon/templates/mail/tempmon_disabled_probes.html.mako new file mode 100644 index 0000000..6948aaf --- /dev/null +++ b/rattail_tempmon/templates/mail/tempmon_disabled_probes.html.mako @@ -0,0 +1,31 @@ +## -*- coding: utf-8; -*- + + +

Disabled Tempmon Probes

+

+ We checked if there were any offline temperature probes at your location.  + Someone wanted these deactivated at some point, but you should make sure it is + okay that they still are.  Here are the results: +

+ + % if disabled_probes: +

Disabled Probes

+ + % endif + + % if disabled_clients: +

Disabled Clients

+ + % endif + +

Please contact the Tech Department with any questions.

+ + diff --git a/setup.py b/setup.py index b92c6fb..2c7d7cf 100644 --- a/setup.py +++ b/setup.py @@ -101,6 +101,7 @@ setup( 'export-hotcooler = rattail_tempmon.commands:ExportHotCooler', 'purge-tempmon = rattail_tempmon.commands:PurgeTempmon', 'tempmon-client = rattail_tempmon.commands:TempmonClient', + 'tempmon-problems = rattail_tempmon.commands:TempmonProblems', 'tempmon-server = rattail_tempmon.commands:TempmonServer', ], 'rattail.config.extensions': [