Skip to content

Commit

Permalink
Extend comments, clean up logic, fixup the template, and add a simple…
Browse files Browse the repository at this point in the history
… HTTP status code test
  • Loading branch information
Drew Hubl committed Jun 24, 2015
1 parent 7280bb8 commit da35841
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 36 deletions.
7 changes: 7 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,10 @@ def test_response_404_when_none_specified(self):

def tearDown(self):
pass


class TestWatchmanDashboard(unittest.TestCase):
def test_dashboard_response_code(self):
request = RequestFactory().get('/')
response = views.dashboard(request)
self.assertEqual(response.status_code, 200)
24 changes: 12 additions & 12 deletions watchman/templates/watchman/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
{% block content %}
<div class="container">
<h2>{% trans 'System Status' %}</h2>
<h3 class="{% if overall_status == 'OK' %}text-success{% else %}text-danger{% endif %}">
{% if overall_status == 'OK' %}
<h3 class="{% if overall_status %}text-success{% else %}text-danger{% endif %}">
{% if overall_status %}
<span class="glyphicon glyphicon-ok"></span> {% trans 'All systems up and running!' %}
{% else %}
<span class="glyphicon glyphicon-alert"></span> {% trans 'WARNING - Some systems down!' %}
{% endif %}
{% endif %}{# overall_status #}
</h3>
<small></small>

Expand All @@ -32,18 +32,18 @@ <h3 class="{% if overall_status == 'OK' %}text-success{% else %}text-danger{% en
<tbody>
{% for type in checks %}
{% for thing in type.statuses %}
<tr class="{% if type.overall_status == 'OK' %}success{% else %}danger{% endif %}">
<tr class="{% if type.ok %}success{% else %}danger{% endif %}">
<td>{{ type.type|title }}</td>
<td>{{ thing.name|title }}</td>

{% if thing.status == 'OK' %}
{% if thing.ok %}
<td class="success">{% trans 'OK' %}</td>

{% else %}
<td class="danger">
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#{{ type.type }}-{{ thing.name }}">{% trans 'ERROR!' %}</button>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#{{ type.type }}{% if thing.name %}-{{ thing.name }}{% endif %}">{% trans 'ERROR!' %}</button>
</td>
{% endif %}{# thing.status == 'OK' #}
{% endif %}{# thing.ok #}

</tr>
{% endfor %}{# for thing in type.statuses #}
Expand All @@ -55,20 +55,20 @@ <h3 class="{% if overall_status == 'OK' %}text-success{% else %}text-danger{% en
</tbody>
</table>

{% if overall_status != 'OK' %}<span class="pull-right"><small>{% trans 'Click an error button to see the full traceback' %}</small></span>{% endif %}{# overall_status != 'OK' #}
{% if not overall_status %}<span class="pull-right"><small>{% trans 'Click an error button to see the full traceback' %}</small></span>{% endif %}{# overall_status #}
</div>
{% endblock %}{# status_content #}


{% for type in checks %}
{% for thing in type.statuses %}
{% if thing.status != 'OK' %}
<div class="modal fade" id="{{ type.type }}-{{ thing.name }}" tabindex="-1" role="dialog" aria-labelledby="{{ type.type }}-{{ thing.name }}-title">
{% if not thing.ok %}
<div class="modal fade" id="{{ type.type }}{% if thing.name %}-{{ thing.name }}{% endif %}" tabindex="-1" role="dialog" aria-labelledby="{{ type.type }}{% if thing.name %}-{{ thing.name }}{% endif %}-title">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="{{ type.type }}-{{ thing.name }}-title">{{ type.type|title }} - {{ thing.name|title }}</h4>
<h4 class="modal-title" id="{{ type.type }}{% if thing.name %}-{{ thing.name }}{% endif %}-title">{{ type.type|title }}{% if thing.name %} - {{ thing.name|title }}{% endif %}</h4>
</div><!-- class="modal-header" -->
<div class="modal-body">
<h4><pre>{{ thing.error }}</pre></h4>
Expand All @@ -80,7 +80,7 @@ <h4><pre>{{ thing.error }}</pre></h4>
</div><!-- class="modal-content" -->
</div><!-- class="modal-dialog" -->
</div><!-- class="modal fade" -->
{% endif %}{# thing.status != 'OK' #}
{% endif %}{# not thing.ok #}
{% endfor %}{# for type in checks #}
{% endfor %}{# for thing in type.statuses #}
</body>
Expand Down
86 changes: 62 additions & 24 deletions watchman/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,73 @@ def dashboard(request):
_check = check()

for _type in _check:
# _check[_type] is a list of dictionaries:
# Example: [{'default': {'ok': True}}]
# For other systems (eg: email, storage) _check[_type] is a
# dictionary of status
#
# Example:
# {
# 'ok': True, # Status
# }
#
# Example:
# {
# 'ok': False, # Status
# 'error': "RuntimeError",
# 'stacktrace': "...",
# }
#
# For some systems (eg: cache, database) _check[_type] is a
# list of dictionaries of dictionaries of statuses
#
# Example:
# [
# {
# 'default': { # Cache/database name
# 'ok': True, # Status
# }
# },
# {
# 'non-default': { # Cache/database name
# 'ok': False, # Status
# 'error': "RuntimeError",
# 'stacktrace': "...",
# }
# },
# ]
#
statuses = []

for a in _check[_type]:
status = {}

# Systems like storage don't have names
if a == 'ok':
status['name'] = ''
status['status'] = 'OK'
status['error'] = ''
status['stacktrace'] = ''
else:
for name in a:
status['name'] = name
status['status'] = 'OK' if a[name]['ok'] else 'ERROR'
status['error'] = a[name]['error'] if not a[name]['ok'] else ''
status['stacktrace'] = a[name]['stacktrace'] if not a[name]['ok'] else ''

statuses.append(status)

type_overall_status = 'OK' if all([s['status'] == 'OK' for s in statuses]) else 'ERROR'
if type(_check[_type]) == dict:
result = _check[_type]
statuses = [{
'name': '',
'ok': result['ok'],
'error': '' if result['ok'] else result['error'],
'stacktrace': '' if result['ok'] else result['stacktrace'],
}]

type_overall_status = _check[_type]['ok']

elif type(_check[_type]) == list:
for result in _check[_type]:
for name in result:
statuses.append({
'name': name,
'ok': result[name]['ok'],
'error': '' if result[name]['ok'] else result[name]['error'],
'stacktrace': '' if result[name]['ok'] else result[name]['stacktrace'],
})

type_overall_status = all([s['ok'] for s in statuses])

check_types.append({
'type': _type,
'overall_status': type_overall_status,
'ok': type_overall_status,
'statuses': statuses})

overall_status = 'OK' if all([type_status['overall_status'] == 'OK' for type_status in check_types]) else 'ERROR'
overall_status = all([type_status['ok'] for type_status in check_types])

return render(request, 'watchman/dashboard.html', {'checks': check_types, 'overall_status': overall_status})
return render(request, 'watchman/dashboard.html', {
'checks': check_types,
'overall_status': overall_status
})

0 comments on commit da35841

Please sign in to comment.