Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / controllers / plugins / hls.py @ 25892058

History | View | Annotate | Download (6.47 KB)

1
# -*- coding: utf-8 -*-
2
# vim:set expandtab tabstop=4 shiftwidth=4:
3
################################################################################
4
#
5
# Copyright (C) 2007-2013 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
        imp_hls1 = aliased(tables.ImpactedHLS)
104
        imp_hls2 = aliased(tables.ImpactedHLS)
105

    
106
        # Sous-requête récupérant les identifiants des supitems
107
        # impactés par les évènements passés en paramètre.
108
        subquery = DBSession.query(
109
                tables.SupItem.idsupitem,
110
                tables.CorrEvent.idcorrevent
111
            ).join(
112
                (tables.Event, tables.Event.idsupitem == \
113
                    tables.SupItem.idsupitem),
114
                (tables.CorrEvent, tables.CorrEvent.idcause == \
115
                    tables.Event.idevent),
116
            ).filter(tables.CorrEvent.idcorrevent.in_(events_ids)
117
            ).subquery()
118

    
119
        # Sous-requête récupérant les identifiants des SHN de plus
120
        # haut niveau impactés par les évènements passés en paramètre.
121
        # Fait appel à la sous-requête précédente (subquery).
122
        subquery2 = DBSession.query(
123
            functions.max(imp_hls1.distance).label('distance'),
124
            imp_hls1.idpath,
125
            subquery.c.idcorrevent
126
        ).join(
127
            (tables.ImpactedPath, tables.ImpactedPath.idpath == imp_hls1.idpath)
128
        ).join(
129
            (subquery, tables.ImpactedPath.idsupitem == subquery.c.idsupitem)
130
        ).group_by(imp_hls1.idpath, subquery.c.idcorrevent
131
        ).subquery()
132

    
133
        # Requête récupérant les noms des SHN de plus haut niveau
134
        # impactés par chacun des évènements passés en paramètre.
135
        # Fait appel à la sous-requête précédente (subquery2).
136
        services = DBSession.query(
137
            tables.HighLevelService.servicename,
138
            subquery2.c.idcorrevent
139
        ).distinct(
140
        ).join(
141
            (imp_hls2, tables.HighLevelService.idservice == imp_hls2.idhls),
142
            (subquery2, subquery2.c.idpath == imp_hls2.idpath),
143
        ).filter(imp_hls2.distance == subquery2.c.distance
144
        ).order_by(
145
            tables.HighLevelService.servicename.asc()
146
        ).all()
147

    
148
        # Construction d'un dictionnaire associant à chaque évènement
149
        # le nom des SHN de plus haut niveau qu'il impacte.
150
        hls = {}
151
        for event_id in events_ids:
152
            hls[event_id] = []
153
        for service in services:
154
            hls[service.idcorrevent].append(service.servicename)
155

    
156
        return hls