vigiboard / vigiboard / controllers / plugins / hls.py @ 2bcebf54
History | View | Annotate | Download (6.52 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
# vim:set expandtab tabstop=4 shiftwidth=4:
|
3 |
################################################################################
|
4 |
#
|
5 |
# Copyright (C) 2007-2016 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 les services de haut
|
23 |
niveau (L{HighLevelService}) impactés par un événement.
|
24 |
"""
|
25 |
|
26 |
import tw.forms as twf |
27 |
from pylons.i18n import lazy_ugettext as l_ |
28 |
|
29 |
from vigiboard.controllers.plugins import VigiboardRequestPlugin, INNER |
30 |
from vigilo.models.session import DBSession |
31 |
from vigilo.models.functions import sql_escape_like |
32 |
from vigilo.models import tables |
33 |
from sqlalchemy.orm import aliased |
34 |
from sqlalchemy.sql import functions |
35 |
|
36 |
class PluginHLS(VigiboardRequestPlugin): |
37 |
"""
|
38 |
Plugin qui permet de voir les services de haut niveau impactés par
|
39 |
les événements affichés sur la page principale de VigiBoard.
|
40 |
"""
|
41 |
def get_search_fields(self): |
42 |
return [
|
43 |
twf.TextField( |
44 |
'hls',
|
45 |
label_text=l_('High-Level Service'),
|
46 |
validator=twf.validators.String(if_missing=None),
|
47 |
) |
48 |
] |
49 |
|
50 |
def handle_search_fields(self, query, search, state, subqueries): |
51 |
if state != INNER or not search.get('hls'): |
52 |
return
|
53 |
hls = sql_escape_like(search['hls'])
|
54 |
|
55 |
# Il s'agit d'un manager. On applique le filtre
|
56 |
# indépendamment aux 2 sous-requêtes.
|
57 |
if len(subqueries) == 2: |
58 |
subqueries[0] = subqueries[0].join( |
59 |
(tables.ImpactedPath, tables.ImpactedPath.idsupitem == \ |
60 |
tables.LowLevelService.idservice), |
61 |
(tables.ImpactedHLS, tables.ImpactedHLS.idpath == \ |
62 |
tables.ImpactedPath.idpath), |
63 |
(tables.HighLevelService, \ |
64 |
tables.HighLevelService.idservice == \ |
65 |
tables.ImpactedHLS.idhls), |
66 |
).filter(tables.HighLevelService.servicename.ilike(hls)) |
67 |
|
68 |
subqueries[1] = subqueries[1].join( |
69 |
(tables.ImpactedPath, tables.ImpactedPath.idsupitem == \ |
70 |
tables.Host.idhost), |
71 |
(tables.ImpactedHLS, tables.ImpactedHLS.idpath == \ |
72 |
tables.ImpactedPath.idpath), |
73 |
(tables.HighLevelService, |
74 |
tables.HighLevelService.idservice == \ |
75 |
tables.ImpactedHLS.idhls), |
76 |
).filter(tables.HighLevelService.servicename.ilike(hls)) |
77 |
|
78 |
# Il s'agit d'un utilisateur normal.
|
79 |
else:
|
80 |
subqueries[0] = subqueries[0].join( |
81 |
(tables.ImpactedPath, tables.ImpactedPath.idsupitem == \ |
82 |
tables.UserSupItem.idsupitem), |
83 |
(tables.ImpactedHLS, tables.ImpactedHLS.idpath == \ |
84 |
tables.ImpactedPath.idpath), |
85 |
(tables.HighLevelService, |
86 |
tables.HighLevelService.idservice == \ |
87 |
tables.ImpactedHLS.idhls), |
88 |
).filter(tables.HighLevelService.servicename.ilike(hls)) |
89 |
|
90 |
def get_bulk_data(self, events_ids): |
91 |
"""
|
92 |
Renvoie le nom des services de haut niveau impactés
|
93 |
par chacun des événements du tableau de VigiBoard.
|
94 |
|
95 |
@param events_ids: Liste des identifiants des événements corrélés
|
96 |
à afficher.
|
97 |
@type events_ids: C{int}
|
98 |
@return: Un dictionnaire associant à chaque identifiant d'évènement
|
99 |
la liste des noms des HLS de plus haut niveau qu'il impacte.
|
100 |
@rtype: C{dict}
|
101 |
"""
|
102 |
|
103 |
if not events_ids: |
104 |
return {}
|
105 |
|
106 |
imp_hls1 = aliased(tables.ImpactedHLS) |
107 |
imp_hls2 = aliased(tables.ImpactedHLS) |
108 |
|
109 |
# Sous-requête récupérant les identifiants des supitems
|
110 |
# impactés par les évènements passés en paramètre.
|
111 |
subquery = DBSession.query( |
112 |
tables.SupItem.idsupitem, |
113 |
tables.CorrEvent.idcorrevent |
114 |
).join( |
115 |
(tables.Event, tables.Event.idsupitem == \ |
116 |
tables.SupItem.idsupitem), |
117 |
(tables.CorrEvent, tables.CorrEvent.idcause == \ |
118 |
tables.Event.idevent), |
119 |
).filter(tables.CorrEvent.idcorrevent.in_(events_ids) |
120 |
).subquery() |
121 |
|
122 |
# Sous-requête récupérant les identifiants des SHN de plus
|
123 |
# haut niveau impactés par les évènements passés en paramètre.
|
124 |
# Fait appel à la sous-requête précédente (subquery).
|
125 |
subquery2 = DBSession.query( |
126 |
functions.max(imp_hls1.distance).label('distance'),
|
127 |
imp_hls1.idpath, |
128 |
subquery.c.idcorrevent |
129 |
).join( |
130 |
(tables.ImpactedPath, tables.ImpactedPath.idpath == imp_hls1.idpath) |
131 |
).join( |
132 |
(subquery, tables.ImpactedPath.idsupitem == subquery.c.idsupitem) |
133 |
).group_by(imp_hls1.idpath, subquery.c.idcorrevent |
134 |
).subquery() |
135 |
|
136 |
# Requête récupérant les noms des SHN de plus haut niveau
|
137 |
# impactés par chacun des évènements passés en paramètre.
|
138 |
# Fait appel à la sous-requête précédente (subquery2).
|
139 |
services = DBSession.query( |
140 |
tables.HighLevelService.servicename, |
141 |
subquery2.c.idcorrevent |
142 |
).distinct( |
143 |
).join( |
144 |
(imp_hls2, tables.HighLevelService.idservice == imp_hls2.idhls), |
145 |
(subquery2, subquery2.c.idpath == imp_hls2.idpath), |
146 |
).filter(imp_hls2.distance == subquery2.c.distance |
147 |
).order_by( |
148 |
tables.HighLevelService.servicename.asc() |
149 |
).all() |
150 |
|
151 |
# Construction d'un dictionnaire associant à chaque évènement
|
152 |
# le nom des SHN de plus haut niveau qu'il impacte.
|
153 |
hls = {} |
154 |
for event_id in events_ids: |
155 |
hls[event_id] = [] |
156 |
for service in services: |
157 |
hls[service.idcorrevent].append(service.servicename) |
158 |
|
159 |
return hls
|