merge changes from head
[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: Events.py,v 1.10 2006/12/20 14:07:22 tmack Exp $
8 #
9
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Filter import Filter
13 from PLC.Debug import profile
14 from PLC.Table import Row, Table
15
16 class Event(Row):
17     """
18     Representation of a row in the events table. 
19     """
20     
21     table_name = 'events'
22     primary_key = 'event_id'
23     fields = {
24         'event_id': Parameter(int, "Event identifier"),
25         'person_id': Parameter(int, "Identifier of person responsible for event, if any"),
26         'node_id': Parameter(int, "Identifier of node responsible for event, if any"),
27         'fault_code': Parameter(int, "Event fault code"),
28         'call_name': Parameter(str, "Call responsible for this event"),
29         'call': Parameter(str, "Call responsible for this event, including paramters"),
30         'message': Parameter(str, "High level description of this event"),
31         'object_type': Parameter(str, "What type of object is this event affecting"),
32         'runtime': Parameter(float, "Runtime of event"),
33         'time': Parameter(int, "Date and time that the event took place, in seconds since UNIX epoch", ro = True),
34         'object_ids': Parameter([int], "IDs of objects affected by this event")
35         }    
36
37     def add_object(self, object_id, commit = True):
38         """
39         Relate object to this event.
40         """
41
42         assert 'event_id' in self
43
44         event_id = self['event_id']
45
46         if 'object_ids' not in self:
47             self['object_ids'] = []
48
49         if object_id not in self['object_ids']:
50             self.api.db.do("INSERT INTO event_object (event_id, object_id)" \
51                            " VALUES(%(event_id)d, %(object_id)d)",
52                            locals())
53
54             if commit:
55                 self.api.db.commit()
56
57             self['object_ids'].append(object_id)
58     
59 class Events(Table):
60     """
61     Representation of row(s) from the events table in the database. 
62     """
63
64     def __init__(self, api, event_filter = None, columns = None):
65         Table.__init__(self, api, Event, columns)
66
67         sql = "SELECT %s FROM view_events WHERE True" % \
68               ", ".join(self.columns)
69
70         if event_filter is not None:
71             if isinstance(event_filter, (list, tuple, set)):
72                 event_filter = Filter(Event.fields, {'event_id': event_filter})
73             elif isinstance(event_filter, dict):
74                 event_filter = Filter(Event.fields, event_filter)
75             sql += " AND (%s)" % event_filter.sql(api)
76         sql += " ORDER BY %s" % Event.primary_key
77         self.selectall(sql)