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
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Debug import profile
13 from PLC.Table import Row, Table
17 Representation of a row in the events table.
21 primary_key = 'event_id'
23 'event_id': Parameter(int, "Event identifier"),
24 'person_id': Parameter(int, "Identifier of person responsible for event, if any"),
25 'node_id': Parameter(int, "Identifier of node responsible for event, if any"),
26 'event_type': Parameter(str, "Type of event"),
27 'object_type': Parameter(str, "Type of object affected by this event"),
28 'fault_code': Parameter(int, "Event fault code"),
29 'call': Parameter(str, "Call responsible for 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"),
32 'object_ids': Parameter([int], "IDs of objects affected by this event")
35 def add_object(self, object_id, commit = True):
37 Relate object to this event.
40 assert 'event_id' in self
42 event_id = self['event_id']
44 if 'object_ids' not in self:
45 self['object_ids'] = []
47 if object_id not in self['object_ids']:
48 self.api.db.do("INSERT INTO event_object (event_id, object_id)" \
49 " VALUES(%(event_id)d, %(object_id)d)",
55 self['object_ids'].append(object_id)
59 Representation of row(s) from the events table in the database.
62 def __init__(self, api,
64 person_ids = None, node_ids = None,
66 object_types = None, object_ids = None,
70 sql = "SELECT %s from view_events WHERE True" % \
71 ", ".join(Event.fields)
74 sql += " AND event_id IN (%s)" % ", ".join(map(str, event_ids))
77 sql += " AND person_id IN (%s)" % ", ".join(map(str, person_ids))
80 sql += " AND node_id IN (%s)" % ", ".join(map(str, node_ids))
83 sql += " AND object_ids in (%s)" % ", ".join(map(str, object_ids))
86 sql += " AND event_type in (%s)" % ", ".join(api.db.quote(event_types))
89 sql += " AND object_type in (%s)" % ", ".join(api.db.quote(object_types))
92 sql += " And fault_code in (%s)" % ", ".join(map(str, fault_codes))
94 rows = self.api.db.selectall(sql)
97 self[row['event_id']] = event = Event(api, row)
98 for aggregate in ['object_ids']:
99 if not event.has_key(aggregate) or event[aggregate] is None:
100 event[aggregate] = []
102 elements = event[aggregate].split(',')
104 event[aggregate] = map(int, elements)
106 event[aggregate] = elements