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: EventObjects.py,v 1.1 2007/02/27 18:54:32 tmack Exp $
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
16 class EventObject(Row):
18 Representation of a row in the event_object table.
21 table_name = 'event_object'
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 'fault_code': Parameter(int, "Event fault code"),
28 'call_name': Parameter(str, "Call responsible for this event"),
29 'call': Parameter(str, "Call responsible for this event, including paramters"),
30 'message': Parameter(str, "High level description of this event"),
31 'runtime': Parameter(float, "Runtime of event"),
32 'time': Parameter(int, "Date and time that the event took place, in seconds since UNIX epoch", ro = True),
33 'object_id': Parameter(int, "ID of objects affected by this event"),
34 'object_type': Parameter(str, "What type of object is this event affecting")
37 class EventObjects(Table):
39 Representation of row(s) from the event_object table in the database.
42 def __init__(self, api, event_filter = None, columns = None):
43 Table.__init__(self, api, EventObject, columns)
45 # Since we are querying a table (not a view) ensure that timestamps
46 # are converted to ints in the db before being returned
48 for col in self.columns:
50 if isinstance(self.columns, (list, tuple, set)):
51 index = self.columns.index(col)
52 self.columns[index] = "CAST(date_part('epoch', events.time) AS bigint) AS time"
53 elif isinstance(self.columns, dict):
54 type = self.columns.pop(col)
55 self.columns["CAST(date_part('epoch', events.time) AS bigint) AS time"] = type
56 elif col in [EventObject.primary_key]:
57 if isinstance(self.columns, (list, tuple, set)):
58 index = self.columns.index(col)
59 self.columns[index] = EventObject.table_name+"."+EventObject.primary_key
60 elif isinstance(self.columns, dict):
61 type = self.columns.pop(col)
62 self.columns[EventObject.table_name+"."+EventObject.primary_key] = type
64 sql = "SELECT %s FROM event_object, events WHERE True" % \
65 ", ".join(self.columns)
67 if event_filter is not None:
68 if isinstance(event_filter, (list, tuple, set)):
69 event_filter = Filter(EventObject.fields, {'event_id': event_filter})
70 elif isinstance(event_filter, dict):
71 event_filter = Filter(EventObject.fields, event_filter)
72 sql += " AND (%s) " % event_filter.sql(api)
73 sql += " AND events.event_id = event_object.event_id "
74 sql += " ORDER BY %s" % EventObject.table_name+"."+EventObject.primary_key