2 # Functions for interacting with the events table in the database
4 # Tony Mack <tmack@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
7 # $Id: Events.py 5574 2007-10-25 20:33:17Z thierry $
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
18 Representation of a row in the events table.
22 primary_key = 'event_id'
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 'auth_type': Parameter(int, "Type of auth used. i.e. AuthMethod"),
28 'fault_code': Parameter(int, "Event fault code"),
29 'call_name': Parameter(str, "Call responsible for this event"),
30 'call': Parameter(str, "Call responsible for this event, including paramters"),
31 'message': Parameter(str, "High level description of this event"),
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 'object_types': Parameter([str], "What type of object were affected by this event")
38 def add_object(self, object_type, object_id, commit = True):
40 Relate object to this event.
43 assert 'event_id' in self
45 event_id = self['event_id']
47 if 'object_ids' not in self:
48 self['object_ids'] = []
50 if object_id not in self['object_ids']:
51 self.api.db.do("INSERT INTO event_object (event_id, object_id, object_type)" \
52 " VALUES(%(event_id)d, %(object_id)d, %(object_type)s)",
58 self['object_ids'].append(object_id)
62 Representation of row(s) from the events table in the database.
65 def __init__(self, api, event_filter = None, columns = None):
66 Table.__init__(self, api, Event, columns)
68 sql = "SELECT %s FROM view_events WHERE True" % \
69 ", ".join(self.columns)
71 if event_filter is not None:
72 if isinstance(event_filter, (list, tuple, set)):
73 event_filter = Filter(Event.fields, {'event_id': event_filter})
74 elif isinstance(event_filter, dict):
75 event_filter = Filter(Event.fields, event_filter)
76 sql += " AND (%s) %s" % event_filter.sql(api)
77 # with new filtering, caller needs to set this explicitly
78 # sql += " ORDER BY %s" % Event.primary_key