Initial checkin of new API implementation
[plcapi.git] / PLC / Events.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 #
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter
10 from PLC.Debug import profile
11 from PLC.Table import Row, Table
12
13 class Event(Row):
14         """
15         Representation of a row in the events table. 
16         """
17         
18         table_name = 'events'
19         primary_key = 'event_id'
20         fields = {
21                 'event_id': Parameter(int, "Event identifier"),
22                 'person_id': Parameter(int, "Identifier of person responsible for event"),
23                 'event_type': Parameter(str, "Type of event"),
24                 'object_type': Parameter(str, "Type of object affected by this event"),
25                 'fault_code': Parameter(int, "Event fault code"),
26                 'call': Parameter(str, "Call responsible for this event"),
27                 'time': Parameter(str, "Date/Time the event took place"),
28                 'object_ids': Parameter([int], "Ids of objects affected by this event")
29         }       
30         
31         def __init__(self, api, fields = {}):
32                 Row.__init__(self, fields)
33                 self.api = api
34
35         
36
37 class Events(Table):
38         """
39         Representation of row(s) from the events table in the database. 
40         """
41
42         def __init__(self, api, event_ids = None, person_ids = None, event_types = None, \
43                      object_types = None, object_ids = None, fault_codes = None):
44         
45                 self.api = api
46                         
47                 sql = "SELECT %s from view_events WHERE True" % ", ".join(Event.fields)
48                 
49                 if event_ids:
50                         sql += " AND event_id IN (%s)" % ", ".join(map(str, event_ids))
51
52                 if person_ids:
53                         sql += " AND person_id IN (%s)" % ", ".join(map(str, person_ids))
54                 
55                 if object_ids:
56                         sql += " AND object_ids in (%s)" % ", ".join(map(str, object_ids))      
57
58                 if event_types:
59                         sql += " AND event_type in (%s)" % ", ".join(api.db.quote(event_types))
60
61                 if object_types:
62                         sql += " AND object_type in (%s)" % ", ".join(api.db.quote(object_types))
63
64                 rows = self.api.db.selectall(sql)
65                         
66                 for row in rows:
67                         self[row['event_id']] = event = Event(api, row)
68                         for aggregate in ['object_ids']:
69                                 if not event.has_key(aggregate) or event[aggregate] is None:
70                                         event[aggregate] = []
71                                 else:
72                                         elements = event[aggregate].split(',')
73                                         try:
74                                                 event[aggregate] = map(int, elements)
75                                         except ValueError:
76                                                 event[aggregate] = elements