Setting tag plcapi-5.4-2
[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
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 Event(Row):
15     """
16     Representation of a row in the events table.
17     """
18
19     table_name = 'events'
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         'auth_type': Parameter(int, "Type of auth used. i.e. AuthMethod"),
26         'fault_code': Parameter(int, "Event fault code"),
27         'call_name': Parameter(str, "Call responsible for this event"),
28         'call': Parameter(str, "Call responsible for this event, including paramters"),
29         'message': Parameter(str, "High level description of 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", ro = True),
32         'object_ids': Parameter([int], "IDs of objects affected by this event"),
33         'object_types': Parameter([str], "What type of object were affected by this event")
34         }
35
36     def add_object(self, object_type, object_id, commit = True):
37         """
38         Relate object to this event.
39         """
40
41         assert 'event_id' in self
42
43         event_id = self['event_id']
44
45         if 'object_ids' not in self:
46             self['object_ids'] = []
47
48         if object_id not in self['object_ids']:
49             self.api.db.do("INSERT INTO event_object (event_id, object_id, object_type)" \
50                            " VALUES(%(event_id)d, %(object_id)d, %(object_type)s)",
51                            locals())
52
53             if commit:
54                 self.api.db.commit()
55
56             self['object_ids'].append(object_id)
57
58 class Events(Table):
59     """
60     Representation of row(s) from the events table in the database.
61     """
62
63     def __init__(self, api, event_filter = None, columns = None):
64         Table.__init__(self, api, Event, columns)
65
66         sql = "SELECT %s FROM view_events WHERE True" % \
67               ", ".join(self.columns)
68
69         if event_filter is not None:
70             if isinstance(event_filter, (list, tuple, set, int, long)):
71                 event_filter = Filter(Event.fields, {'event_id': event_filter})
72             elif isinstance(event_filter, dict):
73                 event_filter = Filter(Event.fields, event_filter)
74             else:
75                 raise PLCInvalidArgument, "Wrong event object filter %r"%event_filter
76             sql += " AND (%s) %s" % event_filter.sql(api)
77         self.selectall(sql)