Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / controllers / plugins / hls.py @ 011743be

History | View | Annotate | Download (5.83 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 les services de haut
8
niveau (L{HighLevelService}) impactés par un événement.
9
"""
10

    
11
import tw.forms as twf
12
from tg.i18n import lazy_ugettext as l_
13

    
14
from vigiboard.controllers.plugins import VigiboardRequestPlugin, INNER
15
from vigilo.models.session import DBSession
16
from vigilo.models.functions import sql_escape_like
17
from vigilo.models import tables
18
from sqlalchemy.orm import aliased
19
from sqlalchemy.sql import functions
20

    
21
class PluginHLS(VigiboardRequestPlugin):
22
    """
23
    Plugin qui permet de voir les services de haut niveau impactés par
24
    les événements affichés sur la page principale de VigiBoard.
25
    """
26
    def get_search_fields(self):
27
        return [
28
            twf.TextField(
29
                'hls',
30
                label_text=l_('High-Level Service'),
31
                validator=twf.validators.UnicodeString(if_missing=None),
32
            )
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

    
75
    def get_bulk_data(self, events_ids):
76
        """
77
        Renvoie le nom des services de haut niveau impactés
78
        par chacun des événements du tableau de VigiBoard.
79

80
        @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
        """
87

    
88
        if not events_ids:
89
            return {}
90

    
91
        imp_hls1 = aliased(tables.ImpactedHLS)
92
        imp_hls2 = aliased(tables.ImpactedHLS)
93

    
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
                tables.SupItem.idsupitem,
98
                tables.CorrEvent.idcorrevent
99
            ).join(
100
                (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
            ).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
            (tables.ImpactedPath, tables.ImpactedPath.idpath == imp_hls1.idpath)
116
        ).join(
117
            (subquery, tables.ImpactedPath.idsupitem == subquery.c.idsupitem)
118
        ).group_by(imp_hls1.idpath, subquery.c.idcorrevent
119
        ).subquery()
120

    
121
        # 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
            tables.HighLevelService.servicename,
126
            subquery2.c.idcorrevent
127
        ).distinct(
128
        ).join(
129
            (imp_hls2, tables.HighLevelService.idservice == imp_hls2.idhls),
130
            (subquery2, subquery2.c.idpath == imp_hls2.idpath),
131
        ).filter(imp_hls2.distance == subquery2.c.distance
132
        ).order_by(
133
            tables.HighLevelService.servicename.asc()
134
        ).all()
135

    
136
        # 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