added ability to filter all alarms with parameters in modal

Change-Id: If19885a32ac3d085d1ddfe0cdc6d4609817ab6ed
This commit is contained in:
SamKirsch10 2015-07-14 14:25:20 -06:00
parent 8ee4a178ad
commit 1d82ccb07f
7 changed files with 169 additions and 34 deletions

View File

@ -192,12 +192,21 @@ class DeleteAlarm(tables.DeleteAction):
api.monitor.alarm_delete(request, obj_id)
class AlarmsFilterAction(tables.FilterAction):
def filter(self, table, alarms, filter_string):
"""Naive case-insensitive search."""
q = filter_string.lower()
return [alarm for alarm in alarms
if q in alarm['metrics'][0]['name'].lower()]
class AlarmsFilterAction(tables.LinkAction):
name = "filter"
verbose_name = _("Filter Alarms")
classes = ("ajax-modal",)
icon = "plus"
policy_rules = (("alarm", "alarm:filter"),)
ajax = True
def get_link_url(self):
return urlresolvers.reverse(constants.URL_PREFIX + 'alarm_filter',
args=())
def allowed(self, request, datum=None):
return True
class AlarmsTable(tables.DataTable):

View File

@ -0,0 +1,60 @@
{% extends "horizon/common/_modal_form.html" %}
{% load i18n %}
{% load url from future %}
{% block modal-body %}
<script>
function isEmpty(str) {
return (!str || 0 === str.length);
}
function sendID() {
var filterID = document.forms["form0"]["filterID"].value;
window.location = "/monitoring/alarms/alarm/id=" + filterID;
}
function sendDimensions() {
var dimension = document.forms["form1"]["dimension"].value;
var metric = document.forms["form1"]["metric"].value;
var location = "/monitoring/alarms/alarm/";
if (!isEmpty(metric)) {
if (!isEmpty(dimension)) {
location = location + "metric=" + metric + ",";
}
else {
location = location + "metric=" + metric;
}
}
if (!isEmpty(dimension)) {
location = location + dimension;
}
window.location = location;
}
</script>
<div class = "filter_form" >
<form></form>
<form method="GET" class="form-group" style="width:400px" onsubmit="sendID()" name="form0">
<h3>Alarm ID</h3>
Value: <input class="form-control" name="filterID" placeholder="54fdec2f-ae34-4dba-9c85-7b4ad67eea03">
<button type="submit" class="btn btn-default">Submit</button>
</form>
<hr>
<hr>
<form method="GET" class="form-group" style="width:400px" onsubmit="sendDimensions()" name="form1">
<h3>Dimension(s)</h3>
Key:Value
<input class="form-control" name="dimension" placeholder="hostname=devstack, service=mysql" data-toggle="tooltip" data-placement="right" title="Example: hostname=devstack, service=mysql">
<br />
Metric Name: <input class="form-control" name="metric" placeholder="http_status">
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
{% endblock %}
{% block modal-footer %}
{% endblock %}

View File

@ -0,0 +1,10 @@
{% extends 'base.html' %}
{% load i18n %}
{% block page_header %}
{% include "horizon/common/_page_header.html" with title="Filter Alarms" %}
{% endblock page_header %}
{% block main %}
{% include 'monitoring/alarms/_filter.html' %}
{% endblock %}

View File

@ -7,19 +7,19 @@ from monitoring.alarms import constants
INDEX_URL = urlresolvers.reverse(
constants.URL_PREFIX + 'index')
ALARMS_URL_BY_SERVICE = urlresolvers.reverse(
ALARMS_URL_BY_DIMENSION = urlresolvers.reverse(
constants.URL_PREFIX + 'alarm', args=('nova',))
ALARMS_URL = urlresolvers.reverse(
constants.URL_PREFIX + 'alarm', args=('all',))
class AlarmsTest(helpers.TestCase):
def test_alarms_get_by_service(self):
def test_alarms_get_by_dimension(self):
with patch('monitoring.api.monitor', **{
'spec_set': ['alarm_list_by_service'],
'alarm_list_by_service.return_value': [],
'spec_set': ['alarm_list_by_dimension'],
'alarm_list_by_dimension.return_value': [],
}) as mock:
res = self.client.get(ALARMS_URL_BY_SERVICE)
self.assertEqual(mock.alarm_list_by_service.call_count, 2)
res = self.client.get(ALARMS_URL_BY_DIMENSION)
self.assertEqual(mock.alarm_list_by_dimension.call_count, 2)
self.assertTemplateUsed(
res, 'monitoring/alarms/alarm.html')

View File

@ -21,10 +21,14 @@ from monitoring.alarms import views
urlpatterns = patterns(
'',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^alarm/filter/$',
views.AlarmFilterView.as_view(),
name='alarm_filter'),
url(r'^alarm/(?P<service>[^/]+)/$',
views.AlarmServiceView.as_view(),
name='alarm'),
url(r'^history/(?P<name>[^/]+)/(?P<id>[^/]+)$',
views.AlarmHistoryView.as_view(),
name='history'),
)

View File

@ -131,15 +131,26 @@ class AlarmServiceView(tables.DataTableView):
messages.error(self.request, _("Could not retrieve alarms"))
return contacts
else:
try:
results = api.monitor.alarm_list_by_service(self.request,
self.service,
page_offset,
LIMIT)
except Exception:
messages.error(self.request, _("Could not retrieve alarms"))
results = []
return results
if self.service[:2] == 'id':
try:
name , value = self.service.split("=")
results = [api.monitor.alarm_show(self.request, value)]
except Exception as e:
messages.error(self.request, _("Could not retrieve alarms"))
results = []
print "ERROR"
print e
return results
else:
try:
results = api.monitor.alarm_list_by_dimension(self.request,
self.service,
page_offset,
LIMIT)
except Exception:
messages.error(self.request, _("Could not retrieve alarms"))
results = []
return results
def get_context_data(self, **kwargs):
context = super(AlarmServiceView, self).get_context_data(**kwargs)
@ -160,14 +171,22 @@ class AlarmServiceView(tables.DataTableView):
except Exception:
messages.error(self.request, _("Could not retrieve alarms"))
else:
try:
results = api.monitor.alarm_list_by_service(self.request,
self.service,
page_offset,
LIMIT)
except Exception:
messages.error(self.request, _("Could not retrieve alarms"))
results = []
if self.service[:2] == 'id':
try:
name , value = self.service.split("=")
results = [api.monitor.alarm_show(self.request, value)]
except Exception as e:
messages.error(self.request, _("Could not retrieve alarms"))
results = []
else:
try:
results = api.monitor.alarm_list_by_dimension(self.request,
self.service,
page_offset,
LIMIT)
except Exception:
messages.error(self.request, _("Could not retrieve alarms"))
results = []
context["contacts"] = results
context["service"] = self.service
@ -258,3 +277,18 @@ class AlarmHistoryView(tables.DataTableView):
return context
class AlarmFilterView(forms.ModalFormView):
template_name = constants.TEMPLATE_PREFIX + 'filter.html'
form_class = alarm_forms.CreateAlarmForm
def get_context_data(self, **kwargs):
context = super(AlarmFilterView, self).get_context_data(**kwargs)
context["cancel_url"] = self.get_success_url()
context["action_url"] = reverse(constants.URL_PREFIX + 'alarm_filter',
args=())
return context
def get_success_url(self):
return reverse_lazy(constants.URL_PREFIX + 'index',
args=())

View File

@ -65,15 +65,33 @@ def alarm_list(request, offset=0, limit=10000, marker=None, paginate=False):
return result['elements'] if type(result) is dict else result
def alarm_list_by_service(request, service_name, offset=0, limit=10000,
def alarm_list_by_dimension(request, dimensions, offset=0, limit=10000,
marker=None, paginate=False):
name, value = service_name.split('=')
service_dim = {name: value}
result = monascaclient(request).alarms.list(offset=offset, limit=limit,
metric_dimensions=service_dim)
dim_dict = {}
metric = None
dimensions = dimensions.replace(" ", "")
dimensions = dimensions.split(",")
for item in dimensions:
name, value = item.split("=")
if name == 'metric':
metric = value
else:
dim_dict[name] = value
if metric:
result = monascaclient(request).alarms.list(offset=offset, limit=limit,
metric_dimensions=dim_dict,
metric_name=metric)
else:
result = monascaclient(request).alarms.list(offset=offset, limit=limit,
metric_dimensions=dim_dict)
return result['elements'] if type(result) is dict else result
def alarm_show(request, alarm_id):
result = monascaclient(request).alarms.get(alarm_id=alarm_id)
return result
def alarm_delete(request, alarm_id):
return monascaclient(request).alarms.delete(alarm_id=alarm_id)