- whitespace nits
[plcapi.git] / PLC / Events.py
1 #
2 # Functions for interacting with the events table in the database
3 #
4 # Tony Mack <tmack@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7 # $Id$
8 #
9
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Debug import profile
13 from PLC.Table import Row, Table
14
15 class Event(Row):
16     """
17     Representation of a row in the events table. 
18     """
19     
20     table_name = 'events'
21     primary_key = 'event_id'
22     fields = {
23         'event_id': Parameter(int, "Event identifier"),
24         'person_id': Parameter(int, "Identifier of person responsible for event, if any"),
25         'node_id': Parameter(int, "Identifier of node responsible for event, if any"),
26         'event_type': Parameter(str, "Type of event"),
27         'object_type': Parameter(str, "Type of object affected by this event"),
28         'fault_code': Parameter(int, "Event fault code"),
29         'call': Parameter(str, "Call responsible for this event"),
30         'runtime': Parameter(float, "Runtime of event"),
31         'time': Parameter(int, "Date and time that the event took place, in seconds since UNIX epoch"),
32         'object_ids': Parameter([int], "IDs of objects affected by this event")
33         }    
34
35     def add_object(self, object_id, commit = True):
36         """
37         Relate object to this event.
38         """
39
40         assert 'event_id' in self
41
42         event_id = self['event_id']
43
44         if 'object_ids' not in self:
45             self['object_ids'] = []
46
47         if object_id not in self['object_ids']:
48             self.api.db.do("INSERT INTO event_object (event_id, object_id)" \
49                            " VALUES(%(event_id)d, %(object_id)d)",
50                            locals())
51
52             if commit:
53                 self.api.db.commit()
54
55             self['object_ids'].append(object_id)
56     
57 class Events(Table):
58     """
59     Representation of row(s) from the events table in the database. 
60     """
61
62     def __init__(self, api,
63                  event_ids = None,
64                  person_ids = None, node_ids = None,
65                  event_types = None,
66                  object_types = None, object_ids = None,
67                  fault_codes = None):
68         self.api = api
69     
70         sql = "SELECT %s from view_events WHERE True" % \
71               ", ".join(Event.fields)
72         
73         if event_ids:
74             sql += " AND event_id IN (%s)" % ", ".join(map(str, event_ids))
75
76         if person_ids:
77             sql += " AND person_id IN (%s)" % ", ".join(map(str, person_ids))
78
79         if node_ids:
80             sql += " AND node_id IN (%s)" % ", ".join(map(str, node_ids))
81
82         if object_ids:
83             sql += " AND object_ids in (%s)" % ", ".join(map(str, object_ids))    
84
85         if event_types:
86             sql += " AND event_type in (%s)" % ", ".join(api.db.quote(event_types))
87
88         if object_types:
89             sql += " AND object_type in (%s)" % ", ".join(api.db.quote(object_types))
90         
91         if fault_codes:
92             sql += " And fault_code in (%s)" % ", ".join(map(str, fault_codes))
93     
94         rows = self.api.db.selectall(sql)
95     
96         for row in rows:
97             self[row['event_id']] = event = Event(api, row)
98             for aggregate in ['object_ids']:
99                 if not event.has_key(aggregate) or event[aggregate] is None:
100                     event[aggregate] = []
101                 else:
102                     elements = event[aggregate].split(',')
103                     try:
104                         event[aggregate] = map(int, elements)
105                     except ValueError:
106                         event[aggregate] = elements