-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.py
387 lines (319 loc) · 14.1 KB
/
query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"""
Query.py consists out of several useful classes to work with the User,
Script, Variable, Commit and CommitVar classes.
"""
# Find exact depends?
import sqlalchemy
from sqlalchemy import *
from classes import User, Script, Variable, Commit, CommitVar, \
UserScriptCache, UserScriptVariableCache
class StatsTool(object):
def __init__(self, sess):
"""
We need a SQLAlchemy session.
"""
self.s = sess
class UserTool(StatsTool):
"""
UserTool is a User helper class. It doesn't have to be OOP; and OOP
may be dropped later on depending whether I still plan to make a
caching mechanism or not.
"""
def top(self, _offset=0,_limit=10, cache=False):
"""
Return the top *limit* users based on time added.
"""
if cache:
coales = (func.coalesce(func.sum(UserScriptCache.time_sum), \
literal_column('0'))).label('TimeAdd')
obj = self.s.query(User, coales).outerjoin(
(UserScriptCache, UserScriptCache.user_id == User.id))
else:
coales = (func.coalesce(func.sum(Commit.timeadd), \
literal_column('0'))).label('TimeAdd')
obj = self.s.query(User, coales).outerjoin(
(Commit, Commit.user_id == User.id))
obj = obj.group_by(User)
obj = obj.order_by(desc(coales),asc(User.id))
obj = obj.offset(_offset)
if _limit > 0:
obj = obj.limit(_limit)
return obj.all()
def info(self, uid, cache=False):
"""
Returns the user object with id *uid* and the time and commit count
of the user.
"""
user = self.s.query(User).filter(User.id==uid).first()
if user is None:
return None
if cache:
time = self.s.query(func.sum(UserScriptCache.commit_amount),
func.sum(UserScriptCache.time_sum)).filter(
UserScriptCache.user_id == uid).group_by(UserScriptCache.user_id).first()
vars = self.s.query(func.sum(UserScriptVariableCache.amount),
Variable).join((Variable, Variable.id ==
UserScriptVariableCache.variable_id)).filter(
UserScriptVariableCache.user_id == uid).group_by(
Variable).all()
else:
time = self.s.query(func.count(Commit.timeadd),
func.sum(Commit.timeadd)).join(
(User, Commit.user_id == User.id)).filter(
User.id == uid).first()
vars = self.s.query(func.sum(CommitVar.amount), Variable).join(
(Variable, CommitVar.variable_id == Variable.id)).join(
(Commit, Commit.id == CommitVar.commit_id)).filter(
Commit.user_id == uid).group_by(Variable).all()
my_vars = [(int(x[0]), x[1]) for x in vars]
restime = \
{'commit_amount' : 0, 'commit_time' : 0} if time is None else \
{'commit_amount' : int(time[0]) if time[0] is not None else 0,
'commit_time' : int(time[1]) if time[1] is not None else 0}
return dict(zip(['user', 'time', 'vars'], [user, restime, my_vars]))
def info_script(self, uid, sid, cache=False):
"""
Returns the user object, script object, time statistics and
variables commits by the user with their amount.
"""
user = self.s.query(User).filter(User.id == uid).first()
if user is None:
return None
script = self.s.query(Script).filter(Script.id == sid).first()
if script is None:
return None
if cache:
time = self.s.query(UserScriptCache.commit_amount,
UserScriptCache.time_sum).filter(UserScriptCache.user_id == uid).filter(
UserScriptCache.script_id == sid).first()
vars = self.s.query(func.sum(UserScriptVariableCache.amount),
Variable.name).join((Variable, Variable.id == \
UserScriptVariableCache.variable_id)).filter(
UserScriptVariableCache.user_id == uid).filter(
UserScriptVariableCache.script_id == sid).group_by(
Variable.name).all()
else:
time = self.s.query(func.count(Commit.timeadd),
func.sum(Commit.timeadd)).filter(Commit.user_id == uid).filter(
Commit.script_id == sid).first()
vars = self.s.query(func.sum(CommitVar.amount), Variable.name).join(
(Variable, CommitVar.variable_id == Variable.id)).join(
(Commit, Commit.id == CommitVar.commit_id)).filter(Commit.user_id ==
uid).filter(Commit.script_id == sid).group_by(Variable.name).all()
my_vars = [(int(x[0]), x[1]) for x in vars]
restime = {'commit_amount' : int(time[0]) if time is not None else 0,
'commit_time' : int(time[1]) if time is not None else 0}
return dict(zip(['user', 'script', 'vars', 'time'], [user, script, \
my_vars, restime]))
def listc_script(self, uid, sid, _offset=0, _limit=10):
"""
Return the commits made by user to a specific script.
Returns the user object as *user*; the script as *script* and the
commits as *commits*.
"""
# Make sure the user exists.
user = self.s.query(User).filter(User.id == uid).first()
if user is None:
return None
# Make sure the script exists.
script = self.s.query(Script).filter(Script.id == sid).first()
if script is None:
return None
# Get all the commits.
commits = self.s.query(Commit).join(
(User, User.id==Commit.user_id)).filter(
User.id == uid).filter(
Commit.script_id == sid).order_by(
desc(Commit.id)).offset(
_offset).limit(_limit).all()
return dict(user=user, script=script,commits=commits)
def listc(self, user, _offset=0, _limit=10):
"""
Return the commits made by user.
"""
commits = self.s.query(Commit).join((User,
User.id==Commit.user_id)).filter(User.id == \
user.id).order_by(desc(Commit.id)).offset(
_offset).limit(_limit).all()
return commits
class ScriptTool(StatsTool):
"""
ScriptTool is a Script helper. It can return the top scripts and return
script specific information; such as vars and time. It can also list the
commits made to a script.
"""
def top(self, _offset=0, _limit=10, cache=False):
"""
Return the top scripts; based on the time committed to the script.
"""
if cache:
coales = (func.coalesce(func.sum(UserScriptCache.time_sum), \
literal_column('0'))).label('TimeAdd')
obj = self.s.query(Script.name, Script.id, coales).outerjoin(
(UserScriptCache, UserScriptCache.script_id == Script.id))
else:
coales = (func.coalesce(func.sum(Commit.timeadd), \
literal_column('0'))).label('TimeAdd')
obj = self.s.query(Script.name, Script.id, coales).outerjoin(
(Commit, Commit.script_id == Script.id))
obj = obj.group_by(Script.name, Script.id)
obj = obj.order_by(desc(coales),asc(Script.id))
obj = obj.offset(_offset)
if _limit > 0:
obj = obj.limit(_limit)
return obj.all()
def info(self, sid, cache=False):
"""
Return script information:
- Script Object
- Variables *committed* to the script
- Time committed to the script (the amount of commits and
their total time)
"""
script = self.s.query(Script).filter(Script.id==sid).first()
if script is None:
return None
if cache:
vars = self.s.query(func.sum(UserScriptVariableCache.amount),
Variable).join((Variable, UserScriptVariableCache.variable_id \
== Variable.id)).filter(UserScriptVariableCache.script_id \
== sid).group_by(Variable).all()
# Why do I need to group by all the Variable entities? XXX
else:
vars = self.s.query(func.sum(CommitVar.amount), Variable).join(
(Variable, CommitVar.variable_id==Variable.id)).join(
(Commit, Commit.id == CommitVar.commit_id)).filter(
Commit.script_id == sid).group_by(Variable).all()
my_vars = [(int(x[0]), x[1]) for x in vars]
if cache:
time = self.s.query(func.sum(UserScriptCache.commit_amount),
func.sum(UserScriptCache.time_sum)).filter(
UserScriptCache.script_id == sid).first()
else:
time = self.s.query(func.count(Commit.timeadd),
func.sum(Commit.timeadd)).join(
(Script, Commit.script_id == Script.id)).filter(
Script.id == sid).first()
restime = {'commit_amount' : int(time[0] if time[0] is not None else 0),
'commit_time' : int(time[1]) if time[1] is not None else 0}
return dict(zip(['script', 'vars', 'time'], [script, my_vars, restime]))
def listc(self, script, _offset=0, _limit=10):
"""
List the commits of the script.
"""
commits = self.s.query(Commit).join((Script,
Script.id==Commit.script_id)).filter(Script.id == \
script.id).order_by(desc(Commit.id)).offset(
_offset).limit(_limit).all()
return commits
class CommitTool(StatsTool):
"""
CommitTool is a Commit helper. It can add a commit; return the top
commits (latest) and return commit specific information.
"""
def top(self, _offset=0, _limit=10):
"""
Return the newest commits (ordered by the time they were added, or
rather; by their id; descending.
"""
obj = self.s.query(Commit).order_by(desc(Commit.id)).offset(
_offset)
if _limit > 0:
obj = obj.limit(_limit)
return obj.all()
def info(self, cid):
"""
Return commit object of the commit with id *cid*.
"""
return self.s.query(Commit).filter(Commit.id == cid).first()
def add(self, user, script, time, vars):
"""
Add a commit to the system.
"""
session = self.s()
c = Commit(time)
c.user = user
c.script = script
c.timeadd = time
# Increase cache time for script 'script' by 'user' user by 'time'.
v = session.query(UserScriptCache).filter(UserScriptCache.user_id==user.id).filter(\
UserScriptCache.script_id==script.id).first()
if v is None:
v = UserScriptCache(user.id, script.id, time, 1)
else:
v.time_sum += time
v.commit_amount += 1
session.add(v)
cv = []
for x, y in vars.iteritems():
commitvar = CommitVar(y)
# Increase cache for this variable for script 'script' and user
# 'user' by 'y'.
v =\
session.query(UserScriptVariableCache).filter(UserScriptVariableCache.user_id==user.id\
).filter(UserScriptVariableCache.script_id==script.id\
).filter(UserScriptVariableCache.variable_id==x.id).first()
if v is None:
v = UserScriptVariableCache(user.id, script.id, x.id, y)
else:
v.amount += y
session.add(v)
commitvar.commit = c
commitvar.variable = x
cv.append(commitvar)
for v in cv:
session.add(v)
session.add(c)
try:
session.commit()
except sqlalchemy.exc.IntegrityError as e:
session.rollback()
print 'Rollback in query.py; ct.add! ... ', user, script, time, vars
print 'Data:', user, script, time, vars
print 'Exception:', e
return True
class VariableTool(StatsTool):
"""
VariableTool is the Variable helper. It can return the top variables
based on the amount the variable has been committed; it can also return
variable specific information.
"""
def top(self, _offset=0, _limit=10, only_vars=False, cache=False):
"""
Return the top Variables.
"""
if cache:
coales = (func.coalesce(func.sum(UserScriptVariableCache.amount), \
literal_column('0'))).label('AmountSum')
obj = self.s.query(Variable, coales).outerjoin(
(UserScriptVariableCache,
UserScriptVariableCache.variable_id == Variable.id))
else:
coales = (func.coalesce(func.sum(CommitVar.amount), \
literal_column('0'))).label('AmountSum')
obj = self.s.query(Variable, coales).outerjoin(
(CommitVar, Variable.id == CommitVar.variable_id))
if only_vars:
obj = obj.filter(Variable.is_var==1)
obj = obj.group_by(Variable).order_by(desc(coales),
asc(Variable.id)).offset(_offset)
if _limit > 0:
obj = obj.limit(_limit)
return obj.all()
def info(self, variableid, cache=False):
"""
Return information about the variable: The total commit amount plus
the Variable object.
"""
variable = \
self.s.query(Variable).filter(Variable.id==variableid).first()
if variable is None:
return None
if cache:
amount = self.s.query(func.sum(UserScriptVariableCache.amount)
).filter(UserScriptVariableCache.variable_id == variableid
).group_by(UserScriptVariableCache.variable_id).first()
else:
amount = self.s.query(func.sum(CommitVar.amount)).filter(
CommitVar.variable_id==variableid).first()
return dict(zip(['variable', 'amount'], [variable, amount]))