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.2 2007/05/07 20:07:42 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 all_fields = EventObject.fields.keys()
49 columns = filter(lambda column: column in all_fields, columns)
51 # Since we are querying a table (not a view) ensure that timestamps
52 # are converted to ints in the db before being returned
56 index = columns.index(col)
57 columns[index] = "CAST(date_part('epoch', events.time) AS bigint) AS time"
58 elif col in [EventObject.primary_key]:
59 index = columns.index(col)
60 columns[index] = EventObject.table_name+"."+EventObject.primary_key
62 sql = "SELECT %s FROM event_object, events WHERE True" % \
65 if event_filter is not None:
66 if isinstance(event_filter, (list, tuple, set)):
67 event_filter = Filter(EventObject.fields, {'event_id': event_filter})
68 elif isinstance(event_filter, dict):
69 event_filter = Filter(EventObject.fields, event_filter)
70 sql += " AND (%s) " % event_filter.sql(api)
71 sql += " AND events.event_id = event_object.event_id "
72 sql += " ORDER BY %s" % EventObject.table_name+"."+EventObject.primary_key