vigiboard / vigiboard / controllers / plugins / date.py @ 011743be
History | View | Annotate | Download (4.71 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
# vim:set expandtab tabstop=4 shiftwidth=4:
|
3 |
# Copyright (C) 2007-2020 CS GROUP - France
|
4 |
# License: GNU GPL v2 <http://www.gnu.org/licenses/gpl-2.0.html>
|
5 |
|
6 |
"""
|
7 |
Un plugin pour VigiBoard qui ajoute une colonne avec la date à laquelle
|
8 |
est survenu un événement et la durée depuis laquelle l'événement est actif.
|
9 |
"""
|
10 |
from datetime import datetime, timedelta |
11 |
import tw.forms as twf |
12 |
from tg.i18n import ugettext as _, lazy_ugettext as l_ |
13 |
|
14 |
from vigilo.models import tables |
15 |
|
16 |
from vigiboard.controllers.plugins import VigiboardRequestPlugin, ITEMS |
17 |
from vigiboard.lib import dateformat, error_handler |
18 |
|
19 |
|
20 |
class ExampleDateFormat(object): |
21 |
"""
|
22 |
Une classe permettant d'obtenir un exemple de date
|
23 |
correspondant au format de la locale de l'utilisateur.
|
24 |
"""
|
25 |
def __str__(self): |
26 |
"""
|
27 |
Retourne l'heure courante au format attendu,
|
28 |
encodée en UTF-8.
|
29 |
|
30 |
@return: Heure courante au format attendu (en UTF-8).
|
31 |
@rtype: C{str}
|
32 |
"""
|
33 |
return unicode(self).encode('utf-8') |
34 |
|
35 |
def __unicode__(self): |
36 |
"""
|
37 |
Retourne l'heure courante au format attendu.
|
38 |
|
39 |
@return: Heure courante au format attendu.
|
40 |
@rtype: C{unicode}
|
41 |
"""
|
42 |
format = dateformat.get_date_format() |
43 |
date = datetime.strftime(datetime.utcnow(), format).decode('utf-8')
|
44 |
return _('Eg. %(date)s') % {'date': date} |
45 |
|
46 |
|
47 |
class PluginDate(VigiboardRequestPlugin): |
48 |
"""Plugin pour l'ajout d'une colonne Date."""
|
49 |
def get_search_fields(self): |
50 |
return [
|
51 |
twf.Label('date', text=l_('Last occurrence')), |
52 |
twf.CalendarDateTimePicker( |
53 |
'from_date',
|
54 |
label_text=l_('Between'),
|
55 |
button_text=l_("Choose"),
|
56 |
not_empty=False,
|
57 |
validator=dateformat.DateFormatConverter(if_missing=None),
|
58 |
date_format=dateformat.get_date_format, |
59 |
calendar_lang=dateformat.get_calendar_lang, |
60 |
attrs={ |
61 |
# Affiche un exemple de date au survol
|
62 |
# et en tant qu'indication (placeholder).
|
63 |
'title': ExampleDateFormat(),
|
64 |
'placeholder': ExampleDateFormat()
|
65 |
}, |
66 |
), |
67 |
twf.CalendarDateTimePicker( |
68 |
'to_date',
|
69 |
label_text=l_('And'),
|
70 |
button_text=l_("Choose"),
|
71 |
not_empty=False,
|
72 |
validator=dateformat.DateFormatConverter(if_missing=None),
|
73 |
date_format=dateformat.get_date_format, |
74 |
calendar_lang=dateformat.get_calendar_lang, |
75 |
attrs={ |
76 |
# Affiche un exemple de date au survol
|
77 |
# et en tant qu'indication (placeholder).
|
78 |
'title': ExampleDateFormat(),
|
79 |
'placeholder': ExampleDateFormat()
|
80 |
}, |
81 |
), |
82 |
] |
83 |
|
84 |
def handle_search_fields(self, query, search, state, subqueries): |
85 |
if state != ITEMS:
|
86 |
return
|
87 |
|
88 |
if search.get('from_date'): |
89 |
query.add_filter(tables.CorrEvent.timestamp_active >= |
90 |
search['from_date'])
|
91 |
|
92 |
# Ajout de contrôles sur la date de début
|
93 |
if search['from_date'] >= datetime.utcnow(): |
94 |
error_handler.handle_error_message( |
95 |
_('Start date cannot be greater than current date'))
|
96 |
|
97 |
if search.get('to_date') and \ |
98 |
search['from_date'] > search['to_date']: |
99 |
error_handler.handle_error_message( |
100 |
_('Start date cannot be greater than end date'))
|
101 |
|
102 |
if search.get('to_date'): |
103 |
query.add_filter(tables.CorrEvent.timestamp_active <= |
104 |
search['to_date'])
|
105 |
|
106 |
# Ajout de contrôles sur la date de fin
|
107 |
if search['to_date'] >= datetime.utcnow(): |
108 |
error_handler.handle_error_message( |
109 |
_('End date cannot be greater than current date'))
|
110 |
|
111 |
def get_data(self, event): |
112 |
state = tables.StateName.value_to_statename( |
113 |
event[0].cause.current_state)
|
114 |
# La résolution maximale de Nagios est la seconde.
|
115 |
# On supprime les microsecondes qui ne nous apportent
|
116 |
# aucune information et fausse l'affichage dans l'export CSV
|
117 |
# en créant un nouvel objet timedelta dérivé du premier.
|
118 |
duration = datetime.utcnow() - event[0].timestamp_active
|
119 |
duration = timedelta(days=duration.days, seconds=duration.seconds) |
120 |
return {
|
121 |
'state': state,
|
122 |
'date': event[0].cause.timestamp, |
123 |
'duration': duration,
|
124 |
} |
125 |
|
126 |
def get_sort_criterion(self, query, column): |
127 |
if column == 'date': |
128 |
return tables.Event.timestamp
|
129 |
return None |
130 |
|