(most) all functions now take SessionAuth in addition to PasswordAuth
[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 class Events(Table):
32         """
33         Representation of row(s) from the events table in the database. 
34         """
35
36         def __init__(self, api, event_ids = None, person_ids = None, event_types = None, \
37                      object_types = None, object_ids = None, fault_codes = None):
38         
39                 self.api = api
40         
41                 sql = "SELECT %s from view_events WHERE True" % ", ".join(Event.fields)
42                 
43                 if event_ids:
44                         sql += " AND event_id IN (%s)" % ", ".join(map(str, event_ids))
45
46                 if person_ids:
47                         sql += " AND person_id IN (%s)" % ", ".join(map(str, person_ids))
48                 
49                 if object_ids:
50                         sql += " AND object_ids in (%s)" % ", ".join(map(str, object_ids))      
51
52                 if event_types:
53                         sql += " AND event_type in (%s)" % ", ".join(api.db.quote(event_types))
54
55                 if object_types:
56                         sql += " AND object_type in (%s)" % ", ".join(api.db.quote(object_types))
57                 
58                 if fault_codes:
59                         sql += " And fault_code in (%s)" % ", ".join(map(str, fault_codes))
60         
61                 rows = self.api.db.selectall(sql)
62         
63                 for row in rows:
64                         self[row['event_id']] = event = Event(api, row)
65                         for aggregate in ['object_ids']:
66                                 if not event.has_key(aggregate) or event[aggregate] is None:
67                                         event[aggregate] = []
68                                 else:
69                                         elements = event[aggregate].split(',')
70                                         try:
71                                                 event[aggregate] = map(int, elements)
72                                         except ValueError:
73                                                 event[aggregate] = elements