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