vigiboard / dashboard / model / bdd_dashboard / events.py @ 70c5f034
History | View | Annotate | Download (2.82 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
"""Model For Events Table"""
|
3 |
|
4 |
from sqlalchemy.orm import mapper, relation |
5 |
from sqlalchemy import Table, ForeignKeyConstraint, Column, Index, DefaultClause, ForeignKey |
6 |
from sqlalchemy.types import Integer, String, Text, DateTime, Boolean |
7 |
|
8 |
from sqlalchemy.databases.mysql import MSEnum |
9 |
|
10 |
from dashboard.model import metadata |
11 |
from datetime import datetime |
12 |
|
13 |
# Generation par SQLAutoCode
|
14 |
|
15 |
events = Table('events', metadata,
|
16 |
Column(u'idevent', Integer(), primary_key=True, nullable=False), |
17 |
Column(u'hostname', String(length=100, convert_unicode=False, assert_unicode=None), primary_key=False, nullable=False), |
18 |
Column(u'servicename', String(length=100, convert_unicode=False, assert_unicode=None), primary_key=False), |
19 |
Column(u'server_source', String(length=100, convert_unicode=False, assert_unicode=None), primary_key=False), |
20 |
Column(u'severity', Integer(), primary_key=False, nullable=False), |
21 |
Column(u'status', MSEnum('None','Acknowledged','AAClosed'), primary_key=False, nullable=False, server_default=DefaultClause('None', for_update=False)), |
22 |
Column(u'active', Boolean(), primary_key=False, server_default=DefaultClause('TRUE', for_update=False)), |
23 |
Column(u'timestamp', DateTime(timezone=False), primary_key=False), |
24 |
Column(u'output', Text(length=None, convert_unicode=False, assert_unicode=None), primary_key=False, nullable=False), |
25 |
Column(u'event_timestamp', DateTime(timezone=False), primary_key=False), |
26 |
Column(u'last_check', DateTime(timezone=False), primary_key=False), |
27 |
Column(u'recover_output', Text(length=None, convert_unicode=False, assert_unicode=None), primary_key=False), |
28 |
Column(u'timestamp_active', DateTime(timezone=False), primary_key=False), |
29 |
Column(u'timestamp_cleared', DateTime(timezone=False), primary_key=False), |
30 |
Column(u'trouble_ticket', String(length=20, convert_unicode=False, assert_unicode=None), primary_key=False), |
31 |
Column(u'occurence', Integer(), primary_key=False), |
32 |
ForeignKeyConstraint([u'servicename'], [u'service.name'], name=u'events_ibfk_1'), |
33 |
ForeignKeyConstraint([u'hostname'], [u'host.name'], name=u'events_ibfk_2') |
34 |
|
35 |
) |
36 |
|
37 |
Index(u'hostname', events.c.hostname, events.c.servicename, unique=False) |
38 |
Index(u'servicename', events.c.servicename, unique=False) |
39 |
|
40 |
# Classe a mapper
|
41 |
|
42 |
class Events(object): |
43 |
|
44 |
def GetDate(self,element): |
45 |
element = self.__dict__[element]
|
46 |
date = datetime.now() - element |
47 |
if date.days < 7 : |
48 |
return element.strftime('%a %H:%M:%S') |
49 |
else :
|
50 |
return element.strftime('%d %b %H:%M:%S') |
51 |
|
52 |
def GetSinceDate(self,element): |
53 |
date = datetime.now() - self.__dict__[element]
|
54 |
minutes, seconds = divmod(date.seconds, 60) |
55 |
hours, minutes = divmod(minutes, 60) |
56 |
return "%dd %dh %d'" % (date.days , hours , minutes) |
57 |
|
58 |
mapper(Events,events) |