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