Setting tag plcapi-5.4-2
[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
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter
10 from PLC.Filter import Filter
11 from PLC.Debug import profile
12 from PLC.Table import Row, Table
13
14 class EventObject(Row):
15     """
16     Representation of a row in the event_object table.
17     """
18
19     table_name = 'event_object'
20     primary_key = 'event_id'
21     fields = {
22         'event_id': Parameter(int, "Event identifier"),
23         'person_id': Parameter(int, "Identifier of person responsible for event, if any"),
24         'node_id': Parameter(int, "Identifier of node responsible for event, if any"),
25         'fault_code': Parameter(int, "Event fault code"),
26         'call_name': Parameter(str, "Call responsible for this event"),
27         'call': Parameter(str, "Call responsible for this event, including paramters"),
28         'message': Parameter(str, "High level description of this event"),
29         'runtime': Parameter(float, "Runtime of event"),
30         'time': Parameter(int, "Date and time that the event took place, in seconds since UNIX epoch", ro = True),
31         'object_id': Parameter(int, "ID of objects affected by this event"),
32         'object_type': Parameter(str, "What type of object is this event affecting")
33         }
34
35 class EventObjects(Table):
36     """
37     Representation of row(s) from the event_object table in the database.
38     """
39
40     def __init__(self, api, event_filter = None, columns = None):
41         Table.__init__(self, api, EventObject, columns)
42
43         sql = "SELECT %s FROM view_event_objects WHERE True" % \
44             ", ".join(self.columns)
45
46         if event_filter is not None:
47             if isinstance(event_filter, (list, tuple, set, int, long)):
48                 event_filter = Filter(EventObject.fields, {'event_id': event_filter})
49                 sql += " AND (%s) %s" % event_filter.sql(api, "OR")
50             elif isinstance(event_filter, dict):
51                 event_filter = Filter(EventObject.fields, event_filter)
52                 sql += " AND (%s) %s" % event_filter.sql(api, "AND")
53             else:
54                 raise PLCInvalidArgument, "Wrong event object filter %r"%event_filter
55
56         self.selectall(sql)