From b576f6c4d78bb550aa20dcb4bf1ad6fc8d0b436b Mon Sep 17 00:00:00 2001 From: Rob Raymond Date: Tue, 3 Jun 2014 15:29:18 -0600 Subject: [PATCH] Refactor to make alarms and notifications as panels --- monitoring/alarms/__init__.py | 0 monitoring/{ => alarms}/constants.py | 0 monitoring/alarms/forms.py | 331 ++++++++++++++++ monitoring/{ => alarms}/panel.py | 0 monitoring/alarms/tables.py | 178 +++++++++ .../templates/alarms}/_scripts.html | 0 .../templates/alarms}/alarm.html | 0 .../templates/alarms}/alarm_history.html | 0 .../templates/alarms}/alarm_meter.html | 0 .../templates/alarms}/alarms/_create.html | 0 .../templates/alarms}/alarms/_detail.html | 0 .../templates/alarms}/alarms/_edit.html | 0 .../templates/alarms}/alarms/create.html | 0 .../templates/alarms}/alarms/detail.html | 0 .../templates/alarms}/alarms/edit.html | 0 .../templates/alarms}/alarms/index.html | 0 .../templates/alarms}/index.html | 4 +- .../templates/alarms}/monitor.html | 2 +- .../templates/alarms}/monitorng.html | 0 .../templates/alarms}/newmonitor.html | 0 monitoring/{ => alarms}/urls.py | 3 - monitoring/alarms/views.py | 371 ++++++++++++++++++ monitoring/notifications/constants.py | 48 +++ monitoring/{ => notifications}/forms.py | 0 monitoring/{ => notifications}/tables.py | 0 .../templates}/notifications/_create.html | 0 .../templates}/notifications/_detail.html | 0 .../templates}/notifications/create.html | 0 .../templates}/notifications/detail.html | 0 monitoring/notifications/urls.py | 19 - monitoring/{ => notifications}/views.py | 0 31 files changed, 931 insertions(+), 25 deletions(-) create mode 100644 monitoring/alarms/__init__.py rename monitoring/{ => alarms}/constants.py (100%) create mode 100644 monitoring/alarms/forms.py rename monitoring/{ => alarms}/panel.py (100%) create mode 100644 monitoring/alarms/tables.py rename monitoring/{templates/monitoring => alarms/templates/alarms}/_scripts.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/alarm.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/alarm_history.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/alarm_meter.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/alarms/_create.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/alarms/_detail.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/alarms/_edit.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/alarms/create.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/alarms/detail.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/alarms/edit.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/alarms/index.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/index.html (59%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/monitor.html (93%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/monitorng.html (100%) rename monitoring/{templates/monitoring => alarms/templates/alarms}/newmonitor.html (100%) rename monitoring/{ => alarms}/urls.py (93%) create mode 100644 monitoring/alarms/views.py create mode 100644 monitoring/notifications/constants.py rename monitoring/{ => notifications}/forms.py (100%) rename monitoring/{ => notifications}/tables.py (100%) rename monitoring/{templates/monitoring => notifications/templates}/notifications/_create.html (100%) rename monitoring/{templates/monitoring => notifications/templates}/notifications/_detail.html (100%) rename monitoring/{templates/monitoring => notifications/templates}/notifications/create.html (100%) rename monitoring/{templates/monitoring => notifications/templates}/notifications/detail.html (100%) rename monitoring/{ => notifications}/views.py (100%) diff --git a/monitoring/alarms/__init__.py b/monitoring/alarms/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/monitoring/constants.py b/monitoring/alarms/constants.py similarity index 100% rename from monitoring/constants.py rename to monitoring/alarms/constants.py diff --git a/monitoring/alarms/forms.py b/monitoring/alarms/forms.py new file mode 100644 index 00000000..d1f69d02 --- /dev/null +++ b/monitoring/alarms/forms.py @@ -0,0 +1,331 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2013 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from django import forms as django_forms +from django.utils.html import escape +from django.utils.html import format_html +from django.utils.translation import ugettext_lazy as _ # noqa + +from horizon import exceptions +from horizon import forms +from horizon import messages + +from monitoring import api +from monitoring import constants + + +def get_expression(meter): + expr = meter['name'] + args = None + for name, value in meter['dimensions'].items(): + if name != 'detail': + if args: + args += ', ' + else: + args = '' + args += "%s=%s" % (name, value) + return "%s{%s}" % (expr, args) + + +class SimpleExpressionWidget(django_forms.MultiWidget): + def __init__(self, meters=None, attrs=None): + choices = [(get_expression(m), get_expression(m)) for m in meters] + comparators = [('>', '>'), ('>=', '>='), ('<', '<'), ('<=', '<=')] + func = [('min', _('min')), ('max', _('max')), ('sum', _('sum')), + ('count', _('count')), ('avg', _('avg'))] + _widgets = ( + django_forms.widgets.Select(attrs=attrs, choices=func), + django_forms.widgets.Select(attrs=attrs, choices=choices), + django_forms.widgets.Select(attrs=attrs, choices=comparators), + django_forms.widgets.TextInput(attrs=attrs), + ) + super(SimpleExpressionWidget, self).__init__(_widgets, attrs) + + def decompress(self, expr): + return [None, None, None] + + def format_output(self, rendered_widgets): + return ''.join(rendered_widgets) + + def value_from_datadict(self, data, files, name): + values = [ + widget.value_from_datadict(data, files, name + '_%s' % i) + for i, widget in enumerate(self.widgets)] + try: + expression = '%s(%s)%s%s' % (values[0], + values[1], + values[2], + values[3]) + except ValueError: + return '' + else: + return expression + + +class NotificationTableWidget(forms.Widget): + FIELD_ID_IDX = 0 + FIELD_NAME_IDX = 1 + + def __init__(self, *args, **kwargs): + self.fields = kwargs.pop('fields') + super(NotificationTableWidget, self).__init__(*args, **kwargs) + + def render(self, name, value, attrs): + output = '' + for field in self.fields: + output += '' % unicode(field[self.FIELD_NAME_IDX]) + output += '' + if value: + for notification in value: + output += "" + for field in self.fields: + field_value = notification[field[self.FIELD_ID_IDX]] + output += '' % escape(field_value) + output += "" + output += '
%s
%s
' + return format_html(output) + + +class NotificationField(forms.MultiValueField): + def __init__(self, *args, **kwargs): + super(NotificationField, self).__init__(*args, **kwargs) + + def _get_choices(self): + return self._choices + + def _set_choices(self, value): + # Setting choices also sets the choices on the widget. + # choices can be any iterable, but we call list() on it because + # it will be consumed more than once. + self._choices = self.widget.choices = list(value) + + choices = property(_get_choices, _set_choices) + + def compress(self, data_list): + return data_list + + def clean(self, value): + return value + + +class NotificationCreateWidget(forms.Select): + def __init__(self, *args, **kwargs): + super(NotificationCreateWidget, self).__init__(*args, **kwargs) + + def render(self, name, value, attrs=None, choices=()): + output = '' + output += '' % \ + unicode(_("Name")) + if value: + idx = 1 + for notification in value: + output += '' + idx += 1 + else: + output += '
%s
' + output += ('' + output += '
' + output += '