Skip to content

Commit

Permalink
Merge pull request #1554 from sigmunau/1240-location-status
Browse files Browse the repository at this point in the history
Added a Locations with active alerts widget
  • Loading branch information
John-Magne Bredal committed Aug 8, 2017
2 parents fe33177 + 978e069 commit f8397e0
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 17 deletions.
2 changes: 1 addition & 1 deletion htdocs/sass/nav/navlets.scss
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ $fullscreen-color: #EEE;
fieldset { padding: .3em .8em; }
}

.RoomStatus {
.RoomStatus, .LocationStatus {
// Override the jquery ui css styles
.ui-accordion {
a { color: $primary-color; }
Expand Down
1 change: 1 addition & 0 deletions python/nav/django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
'nav.web.navlets.ups.UpsWidget',
'nav.web.navlets.pdu.PduWidget',
'nav.web.navlets.roomstatus.RoomStatus',
'nav.web.navlets.locationstatus.LocationStatus',
'nav.web.navlets.env_rack.EnvironmentRackWidget',
)

Expand Down
61 changes: 61 additions & 0 deletions python/nav/web/navlets/locationstatus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#
# Copyright (C) 2016 UNINETT AS
#
# This file is part of Network Administration Visualized (NAV).
#
# NAV is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
# This program 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 NAV. If not, see <http://www.gnu.org/licenses/>.
#
"""Module containing RoomStatus"""

from datetime import datetime
from django import forms
from itertools import groupby
from operator import attrgetter

from nav.models.event import AlertHistory

from nav.web.navlets.roomstatus import RoomStatus


class LocationStatus(RoomStatus):
"""Widget displaying status for locations"""

title = 'Locations with active alerts'
description = 'Displays a list of locations with active alerts'
refresh_interval = 30000 # 30 seconds
is_title_editable = False

def get_template_basename(self):
return 'room_location_status'

def get_context_data_view(self, context):
context = super(LocationStatus, self).get_context_data_view(context)
assert 'results' in context

result_ids = [r.get('id') for r in context['results']]
alerts = AlertHistory.objects.filter(
pk__in=result_ids).exclude(netbox__isnull=True).order_by(
'netbox__room')
locations = []
for location, alertlist in groupby(alerts, attrgetter('netbox.room.location')):
location.alerts = sorted(alertlist, key=attrgetter('start_time'))
for alert in location.alerts:
alert.sms_message = alert.messages.get(type='sms',
language='en')
locations.append(location)

context['items'] = locations
context['last_update'] = datetime.now()
context['name'] = 'room'
context['name_plural'] = 'rooms'
context['history_route'] = 'devicehistory-view-location'
context['info_route'] = 'location-info'
return context
8 changes: 6 additions & 2 deletions python/nav/web/navlets/roomstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RoomStatus(Status2Widget):
is_title_editable = False

def get_template_basename(self):
return 'roomstatus'
return 'room_location_status'

def get_context_data_view(self, context):
context = super(RoomStatus, self).get_context_data_view(context)
Expand All @@ -56,8 +56,12 @@ def get_context_data_view(self, context):
language='en')
rooms.append(room)

context['rooms'] = rooms
context['items'] = rooms
context['last_update'] = datetime.now()
context['name'] = 'room'
context['name_plural'] = 'rooms'
context['history_route'] = 'devicehistory-view-room'
context['info_route'] = 'room-info'
return context

def get_context_data_edit(self, context):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

{% block navlet-content %}

{% if rooms %}
{% if items %}

<div class="accordion">
{% for room in rooms %}
<h3 data-roomid="{{ room.pk }}">
{{ room }}
{% for item in items %}
<h3 data-itemid="{{ item.pk }}">
{{ item }}
<small class="label radius alert"
title="{{ room.alerts|length }} alerts in {{ room.pk }}">
{{ room.alerts|length }}
title="{{ item.alerts|length }} alerts in {{ item.pk }}">
{{ item.alerts|length }}
</small>
</h3>
<div>
<a href="{% url 'devicehistory-view-room' room.pk %}"
title="Go to device history for this room">Alerts</a>
<a href="{% url history_route item.pk %}"
title="Go to device history for this {{name}}">Alerts</a>
in
<a href="{% url 'room-info' room.pk %}"
title="Go to room info for this room">{{ room.pk }}</a>
<a href="{% url info_route item.pk %}"
title="Go to {{name}} info for this {{name}}">{{ item.pk }}</a>

<ul>
{% for alert in room.alerts %}
{% for alert in item.alerts %}
<li>
{{ alert.start_time }} &ndash;
<a href="{{ alert.netbox.get_absolute_url }}"
Expand All @@ -46,7 +46,7 @@ <h3 data-roomid="{{ room.pk }}">
heightStyle: 'content',
activate: function (event, ui) {
if (ui.newHeader.length) {
$navlet.data('active-header', ui.newHeader.data('roomid'));
$navlet.data('active-header', ui.newHeader.data('itemid'));
} else {
$navlet.data('active-header', null);
}
Expand All @@ -58,7 +58,7 @@ <h3 data-roomid="{{ room.pk }}">
var active = false;
if (activeHeader) {
$navlet.find('.accordion h3').each(function (index, element) {
if ($(element).data('roomid') === activeHeader) {
if ($(element).data('itemid') === activeHeader) {
active = index;
}
});
Expand All @@ -71,7 +71,7 @@ <h3 data-roomid="{{ room.pk }}">


{% else %}
<div class="alert-box success with-icon">No alerts in any rooms</div>
<div class="alert-box success with-icon">No alerts in any {{name_plural}}</div>
{% endif %}

<small class="right">
Expand Down

0 comments on commit f8397e0

Please sign in to comment.