]> git.zarvox.org Git - wp3.git/blob - analytics.py
Added Python docstrings for all classes.
[wp3.git] / analytics.py
1 from PyQt4.QtCore import *
2 from PyQt4.QtGui import *
3 from PyQt4.QtSql import *
4
5
6 class Analytics(QWidget):
7         def __init__(self,parent=None, database=None):
8                 QWidget.__init__(self, parent)
9                 # These are the room numbers in the respective dorms that people can live in
10                 self.roomlists = { "Lechner" : [111, 112, 113, 114, 115, 116, 117, 121, 122, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 205, 206, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 221, 222, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 305, 306, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 321, 322, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 405, 406, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440] ,
11                                 "McFadden" : [151, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 170, 171, 172, 173, 175, 176, 177, 178, 179, 182, 183, 184, 185, 186, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 274, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 374, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 474] ,
12                                 "Clements" : [101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 332, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 432]
13                                 }
14                 self.db = database
15                 self.reportbox = QTextEdit()
16                 self.reportbox.setTextInteractionFlags( Qt.TextSelectableByMouse | Qt.TextSelectableByKeyboard )
17                 self.verbosereportbox = QTextEdit()
18                 self.verbosereportbox.setTextInteractionFlags( Qt.TextSelectableByMouse | Qt.TextSelectableByKeyboard )
19                 self.goback = QPushButton("Return to Main Menu")
20                 QObject.connect(self.goback, SIGNAL("clicked()"), self.finished)
21                 self.layout = QGridLayout()
22                 self.layout.addWidget(QLabel("<h2><center>Summary:</center></h2>"),0,0,1,1)
23                 self.summary = QLabel()
24                 self.layout.addWidget(self.summary,1,0,1,1)
25                 self.layout.addWidget(QLabel("<h2><center>Details:</center></h2>"),2,0,1,1)
26                 self.layout.addWidget(self.verbosereportbox,3,0,1,1)
27                 self.layout.addWidget(self.goback,4,0,1,1)
28                 self.setLayout(self.layout)
29
30         def updateDB(self, newdbname):
31                 """Give the widget a handle to the changed database, so analytics will be run on the right DB"""
32                 self.db.close()
33                 self.db.setDatabaseName(newdbname)
34                 self.db.open()
35
36         def vlog(self, line): # append to the verbose log only
37                 """Append line to the verbose log only"""
38                 self.verbosereport = self.verbosereport + line + "\n"
39
40         def log(self, line): # append to the verbose and report logs
41                 """Append line to both the report and verbose logs"""
42                 self.verbosereport = self.verbosereport + line + "\n"
43                 self.report = self.report + line + "\n"
44
45         def generateReport(self):
46                 """Runs analytics on the current database and displays them in the report and verbose detail boxes."""
47                 self.report = ""
48                 self.verbosereport = ""
49                 q = QSqlQuery(self.db)
50                 dormsfound = []
51                 for d in self.roomlists.keys():
52                         q.prepare("SELECT id FROM people WHERE dorm=:dorm")
53                         q.bindValue(":dorm",QVariant(QString(d)))
54                         q.exec_()
55                         found = False
56                         while q.next():
57                                 found = True
58                         if found:
59                                 dormsfound.append(d)
60                 if len(dormsfound) != 0:
61                         self.log("Found data for people from " + ", ".join(dormsfound) + "\n")
62                 else:
63                         self.log("No records found")
64                 for d in dormsfound: # Check each room in roomlists[d] for two residents
65                         self.log(d + ":" )
66                         q.prepare("SELECT id FROM people WHERE dorm=:dorm")
67                         q.bindValue(":dorm",QVariant(QString(d)))
68                         q.exec_()
69                         pop = 0
70                         while q.next():
71                                 pop = pop + 1
72                         maxpop = len(self.roomlists[d]) * 2
73                         self.log("Registered Population: {0} of {1} ({2}%)".format(pop , maxpop, pop * 100 / maxpop ) )
74
75                         # The following code assumes four floors and a numbering scheme where the
76                         # hundreds digit of the room number is the floor.  If this changes, this
77                         # code will need to be rewritten.
78                         self.log("Breakdown by floor:")
79                         floors = [] # Note: sorted from bottom floor to top
80                         floors.append( filter(lambda x: x >= 100 and x < 200, self.roomlists[d]) )
81                         floors.append( filter(lambda x: x >= 200 and x < 300, self.roomlists[d]) )
82                         floors.append( filter(lambda x: x >= 300 and x < 400, self.roomlists[d]) )
83                         floors.append( filter(lambda x: x >= 400 and x < 500, self.roomlists[d]) )
84                         floorpop = []
85                         for floor in floors: # calculate the registered population
86                                 thisfloorpop = 0
87                                 for room in floor:
88                                         q.prepare("SELECT id FROM people WHERE dorm=:dorm AND room=:room")
89                                         q.bindValue(":dorm",QVariant(QString(d)))
90                                         q.bindValue(":room",QVariant(room))
91                                         q.exec_()
92                                         while q.next():
93                                                 thisfloorpop = thisfloorpop + 1
94                                 floorpop.append(thisfloorpop)
95                         self.log("First floor: {0} of {1} registered ({2}%)".format( floorpop[0], len(floors[0]) * 2, floorpop[0] * 50 / len(floors[0])))
96                         self.log("Second floor: {0} of {1} registered ({2}%)".format( floorpop[1], len(floors[1]) * 2, floorpop[1] * 50 / len(floors[1])))
97                         self.log("Third floor: {0} of {1} registered ({2}%)".format( floorpop[2], len(floors[2]) * 2, floorpop[2] * 50 / len(floors[2])))
98                         self.log("Fourth floor: {0} of {1} registered ({2}%)".format( floorpop[3], len(floors[3]) * 2, floorpop[3] * 50 / len(floors[3])))
99
100                         # Breakdown by room only in verbose log
101                         self.vlog("Breakdown by room:")
102                         for room in self.roomlists[d]:
103                                 q.prepare("SELECT id, surname, forename FROM people WHERE dorm=:dorm AND room=:room")
104                                 q.bindValue(":dorm",QVariant(QString(d)))
105                                 q.bindValue(":room",QVariant(room))
106                                 q.exec_()
107                                 r = q.record()
108                                 col_id = r.indexOf("id")
109                                 col_surname = r.indexOf("surname")
110                                 col_forename = r.indexOf("forename")
111                                 records = []
112                                 while q.next():
113                                         records.append( (q.value(col_id).toString(), q.value(col_forename).toString(), q.value(col_surname).toString() ) )
114                                 if len(records) == 0:
115                                         self.vlog("\tRoom " + str(room) + ": Need data for both residents")
116                                 if len(records) == 1:
117                                         self.vlog("\tRoom " + str(room) + ": Need data for resident NOT named " + str(records[0][1]) + " " + str(records[0][2]) )
118                                 if len(records) == 2:
119                                         self.vlog("\tRoom " + str(room) + ": done")
120                                 if len(records) > 2:
121                                         self.vlog("\tRoom " + str(room) + ": There seem to be more than two people living here!")
122                                         self.vlog("\t\t" + ", ".join( [ str(temp[1]) + " " + str(temp[2]) for temp in records] ) )
123
124                         self.log("") # put a newline between each dorm
125                 self.summary.setText(self.report)
126                 self.verbosereportbox.setPlainText(self.verbosereport)
127
128         def finished(self):
129                 """Slot that indicates this widget is done being used (Cancel has been pressed)"""
130                 self.emit(SIGNAL("done()"))
131