Skip to content
This repository has been archived by the owner on Jan 31, 2018. It is now read-only.

Commit

Permalink
[bug 1232435] Fixes based on Mike's comments
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
willkg committed Dec 30, 2015
1 parent 30dd259 commit 82f6181
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 26 deletions.
10 changes: 0 additions & 10 deletions fjord/base/email_utils.py

This file was deleted.

35 changes: 22 additions & 13 deletions 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

Expand Down Expand Up @@ -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):
Expand All @@ -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 = []

Expand All @@ -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):
Expand Down Expand Up @@ -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
})
Expand Down
27 changes: 27 additions & 0 deletions 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),
]
6 changes: 3 additions & 3 deletions fjord/heartbeat/tests/test_healthchecks.py
Expand Up @@ -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


Expand Down Expand Up @@ -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

0 comments on commit 82f6181

Please sign in to comment.