From 82f6181047dba3cac284ffd4b9339f5c2907f251 Mon Sep 17 00:00:00 2001 From: Will Kahn-Greene Date: Wed, 30 Dec 2015 14:25:56 -0500 Subject: [PATCH] [bug 1232435] Fixes based on Mike's comments * switches the counter out for has_any boolean * changes the email subject to make it easier to read * use render_to_string * add data migration to create needed mailing list --- fjord/base/email_utils.py | 10 ------ fjord/heartbeat/healthchecks.py | 35 ++++++++++++------- ...ealth_email.html => heartbeat_health.html} | 0 .../migrations/0011_auto_20151230_1128.py | 27 ++++++++++++++ fjord/heartbeat/tests/test_healthchecks.py | 6 ++-- 5 files changed, 52 insertions(+), 26 deletions(-) delete mode 100644 fjord/base/email_utils.py rename fjord/heartbeat/jinja2/heartbeat/email/{heartbeat_health_email.html => heartbeat_health.html} (100%) create mode 100644 fjord/heartbeat/migrations/0011_auto_20151230_1128.py diff --git a/fjord/base/email_utils.py b/fjord/base/email_utils.py deleted file mode 100644 index 4bb94a28..00000000 --- a/fjord/base/email_utils.py +++ /dev/null @@ -1,10 +0,0 @@ -from django.shortcuts import render -from django.test.client import RequestFactory - - -def render_email(template, context): - """Renders a template for email.""" - req = RequestFactory() - req.META = {} - resp = render(req, template, context) - return resp.content diff --git a/fjord/heartbeat/healthchecks.py b/fjord/heartbeat/healthchecks.py index b1fa3519..c73b0807 100644 --- a/fjord/heartbeat/healthchecks.py +++ b/fjord/heartbeat/healthchecks.py @@ -1,12 +1,12 @@ -from collections import Counter, namedtuple +from collections import namedtuple from datetime import datetime, timedelta import logging from django.conf import settings from django.core.mail import send_mail from django.db import connection +from django.template.loader import render_to_string -from fjord.base.email_utils import render_email from fjord.heartbeat.models import Answer from fjord.mailinglist.utils import get_recipients @@ -47,7 +47,7 @@ def check(cls): @register_check class CheckAnyAnswers(Check): """Are there any heartbeat answers? If not, that's very bad.""" - name = 'any answers check' + name = 'Are there any heartbeat answers?' @classmethod def check(cls): @@ -70,6 +70,17 @@ def check(cls): def tableify(table): + """Takes a list of lists and converts it into a formatted table + + :arg table: list (rows) of lists (columns) + + :returns: string + + .. Note:: + + This is text formatting--not html formatting. + + """ num_cols = 0 maxes = [] @@ -96,8 +107,8 @@ def fix_row(maxes, row): @register_check class CheckMissingVotes(Check): - """should freak out if votes are 0 for any 'large' cell.""" - name = 'missing votes check' + """FIXME: I don't understand this check""" + name = 'Are there votes of 0 for large cells?' @classmethod def check(cls): @@ -168,21 +179,19 @@ def run_healthchecks(): def email_healthchecks(results): - counter = Counter() - counter.update([result.severity for result in results]) + has_high = any([result.severity == SEVERITY_HIGH for result in results]) # The subject should indicate very very obviously whether the sky is # falling or not. - subject = ' '.join([ - '[hb health]', - ('SEVERITY HIGH' if counter.get(SEVERITY_HIGH) else ''), - datetime.now().strftime('(%Y-%m-%d %H:%M)') - ]) + subject = '[hb health] %s (%s)' % ( + ('RED ALERT' if has_high else 'fine'), + datetime.now().strftime('%Y-%m-%d %H:%M') + ) # We do the entire email body in HTML because some output will want to # preserve whitespace and use a fixed-width font. Further, this lets # us make it super easy to spot SEVERITY_HIGH situations. - html_body = render_email('heartbeat/email/heartbeat_health_email.html', { + html_body = render_to_string('heartbeat/email/heartbeat_health.html', { 'severity_name': SEVERITY, 'results': results }) diff --git a/fjord/heartbeat/jinja2/heartbeat/email/heartbeat_health_email.html b/fjord/heartbeat/jinja2/heartbeat/email/heartbeat_health.html similarity index 100% rename from fjord/heartbeat/jinja2/heartbeat/email/heartbeat_health_email.html rename to fjord/heartbeat/jinja2/heartbeat/email/heartbeat_health.html diff --git a/fjord/heartbeat/migrations/0011_auto_20151230_1128.py b/fjord/heartbeat/migrations/0011_auto_20151230_1128.py new file mode 100644 index 00000000..dced62b0 --- /dev/null +++ b/fjord/heartbeat/migrations/0011_auto_20151230_1128.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +def add_heartbeat_health(apps, schema_editor): + MailingList = apps.get_model('mailinglist', 'MailingList') + ml = MailingList.objects.create(name='heartbeat_health') + ml.save() + + +def remove_heartbeat_health(apps, schema_editor): + MailingList = apps.get_model('mailinglist', 'MailingList') + MailingList.objects.filter(name='heartbeat_health').delete() + + +class Migration(migrations.Migration): + + dependencies = [ + ('heartbeat', '0010_auto_20150806_0712'), + ('mailinglist', '0001_initial'), + ] + + operations = [ + migrations.RunPython(add_heartbeat_health, remove_heartbeat_health), + ] diff --git a/fjord/heartbeat/tests/test_healthchecks.py b/fjord/heartbeat/tests/test_healthchecks.py index e3174921..b448d4d2 100644 --- a/fjord/heartbeat/tests/test_healthchecks.py +++ b/fjord/heartbeat/tests/test_healthchecks.py @@ -8,7 +8,7 @@ SEVERITY_LOW, SEVERITY_HIGH, ) -from fjord.heartbeat.tests import AnswerFactory, SurveyFactory +from fjord.heartbeat.tests import AnswerFactory from fjord.mailinglist.tests import MailingListFactory @@ -54,6 +54,6 @@ def test_with_recipients(self): email_healthchecks(run_healthchecks()) assert len(mail.outbox) == 1 assert mail.outbox[0].to == [u'foo@example.com'] - # Severity should be HIGH since there's no hb items--at least + # Severity should be RED ALERT since there's no hb items--at least # one of the checks should be all like, "OMG! RED ALERT!" - assert 'HIGH' in mail.outbox[0].subject + assert 'RED ALERT' in mail.outbox[0].subject