Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / PLC / EventObjects.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: EventObjects.py 5574 2007-10-25 20:33:17Z thierry $
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 EventObject(Row):
17     """
18     Representation of a row in the event_object table. 
19     """
20     
21     table_name = 'event_object'
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         'runtime': Parameter(float, "Runtime of event"),
32         'time': Parameter(int, "Date and time that the event took place, in seconds since UNIX epoch", ro = True),
33         'object_id': Parameter(int, "ID of objects affected by this event"),
34         'object_type': Parameter(str, "What type of object is this event affecting")
35         }    
36
37 class EventObjects(Table):
38     """
39     Representation of row(s) from the event_object table in the database. 
40     """
41
42     def __init__(self, api, event_filter = None, columns = None):
43         Table.__init__(self, api, EventObject, columns)
44         
45         sql = "SELECT %s FROM view_event_objects WHERE True" % \
46             ", ".join(self.columns)
47         
48         if event_filter is not None:
49             if isinstance(event_filter, (list, tuple, set)):
50                 event_filter = Filter(EventObject.fields, {'event_id': event_filter})
51                 sql += " AND (%s) %s" % event_filter.sql(api, "OR")
52             elif isinstance(event_filter, dict):
53                 event_filter = Filter(EventObject.fields, event_filter)
54                 sql += " AND (%s) %s" % event_filter.sql(api, "AND")
55             elif isinstance (event_filter, int):
56                 event_filter = Filter(EventObject.fields, {'event_id':[event_filter]})
57                 sql += " AND (%s) %s" % event_filter.sql(api, "AND")
58             else:
59                 raise PLCInvalidArgument, "Wrong event object filter %r"%event_filter
60 # with new filtering, caller needs to set this explicitly
61 #       sql += " ORDER BY %s" % EventObject.primary_key
62         
63         self.selectall(sql)