142fee7d4aa0e016ae3bf1cecdc55d643dee4257
[plcapi.git] / PLC / EventObjects.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: EventObjects.py,v 1.2 2007/05/07 20:07:42 tmack Exp $
8 #
9
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
15
16 class EventObject(Row):
17     """
18     Representation of a row in the event_object table. 
19     """
20     
21     table_name = 'event_object'
22     primary_key = 'event_id'
23     fields = {
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")
35         }    
36
37 class EventObjects(Table):
38     """
39     Representation of row(s) from the event_object table in the database. 
40     """
41
42     def __init__(self, api, event_filter = None, columns = None):
43         Table.__init__(self, api, EventObject, columns)
44         
45         all_fields = EventObject.fields.keys()
46         if not columns:
47             columns = all_fields
48         else:
49             columns = filter(lambda column: column in all_fields, columns)
50         
51         # Since we are querying a table (not a view) ensure that timestamps
52         # are converted to ints in the db before being returned
53         timestamps = ['time']
54         for col in columns:
55             if col in timestamps:
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
61                          
62         sql = "SELECT %s FROM event_object, events WHERE True" % \
63             ", ".join(columns)
64         
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
73         
74         self.selectall(sql)