Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / controllers / plugins / date.py @ 703bd599

History | View | Annotate | Download (3.42 KB)

1
# -*- coding: utf-8 -*-
2
# vim:set expandtab tabstop=4 shiftwidth=4:
3
################################################################################
4
#
5
# Copyright (C) 2007-2014 CS-SI
6
#
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License version 2 as
9
# published by the Free Software Foundation.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
################################################################################
20

    
21
"""
22
Un plugin pour VigiBoard qui ajoute une colonne avec la date à laquelle
23
est survenu un événement et la durée depuis laquelle l'événement est actif.
24
"""
25
from datetime import datetime, timedelta
26
import tw.forms as twf
27
from pylons.i18n import ugettext as _, lazy_ugettext as l_
28

    
29
from vigilo.models import tables
30

    
31
from vigiboard.controllers.plugins import VigiboardRequestPlugin, ITEMS
32
from vigiboard.lib import dateformat
33

    
34
class PluginDate(VigiboardRequestPlugin):
35
    """Plugin pour l'ajout d'une colonne Date."""
36
    def get_search_fields(self):
37
        return [
38
            twf.Label('date', text=l_('Last occurrence')),
39
            twf.CalendarDateTimePicker(
40
                'from_date',
41
                label_text=l_('Between'),
42
                button_text=l_("Choose"),
43
                not_empty=False,
44
                validator=dateformat.DateFormatConverter(if_missing=None),
45
                date_format=dateformat.get_date_format,
46
                calendar_lang=dateformat.get_calendar_lang,
47
            ),
48
            twf.CalendarDateTimePicker(
49
                'to_date',
50
                label_text=l_('And'),
51
                button_text=l_("Choose"),
52
                not_empty=False,
53
                validator=dateformat.DateFormatConverter(if_missing=None),
54
                date_format=dateformat.get_date_format,
55
                calendar_lang=dateformat.get_calendar_lang,
56
            ),
57
        ]
58

    
59
    def handle_search_fields(self, query, search, state, subqueries):
60
        if state != ITEMS:
61
            return
62

    
63
        if search.get('from_date'):
64
            query.add_filter(tables.CorrEvent.timestamp_active >=
65
                search['from_date'])
66
        if search.get('to_date'):
67
            query.add_filter(tables.CorrEvent.timestamp_active <=
68
                search['to_date'])
69

    
70
    def get_data(self, event):
71
        state = tables.StateName.value_to_statename(
72
                    event[0].cause.current_state)
73
        # La résolution maximale de Nagios est la seconde.
74
        # On supprime les microsecondes qui ne nous apportent
75
        # aucune information et fausse l'affichage dans l'export CSV
76
        # en créant un nouvel objet timedelta dérivé du premier.
77
        duration = datetime.now() - event[0].timestamp_active
78
        duration = timedelta(days=duration.days, seconds=duration.seconds)
79
        return {
80
            'state': state,
81
            'date': event[0].cause.timestamp,
82
            'duration': duration,
83
        }
84

    
85
    def get_sort_criterion(self, query, column):
86
        if column == 'date':
87
            return tables.Event.timestamp
88
        return None
89