18
18
from anubis .utils .elastic import log_endpoint
19
19
from anubis .utils .redis_queue import enqueue_webhook_rpc
20
20
from anubis .utils .logger import logger
21
- from anubis .utils .data import fix_dangling
21
+ from anubis .utils .data import fix_dangling , bulk_stats , get_students
22
22
23
23
private = Blueprint ('private' , __name__ , url_prefix = '/private' )
24
24
25
25
26
- @cache .memoize (timeout = 30 )
27
- def stats_for (student_id , assignment_id ):
28
- # TODO rewrite this
29
- raise
30
-
31
-
32
- @cache .cached (timeout = 30 )
33
- def get_students ():
34
- return [s .data for s in User .query .all ()]
35
-
36
-
37
26
@private .route ('/' )
38
27
def private_index ():
39
28
return 'super duper secret'
40
29
41
30
42
- if is_debug ():
43
- @ private . route ( '/token/< netid>' )
44
- def private_token_netid (netid ):
45
- user = User . query . filter_by ( netid = netid ). first ()
46
- if user is None :
47
- return error_response ( 'User does not exist' )
48
- res = Response (json .dumps (success_response (get_token ( user . netid ) )), headers = {'Content-Type' : 'application/json' })
49
- res .set_cookie ('token' , get_token ( user . netid ) , httponly = True )
50
- return res
31
+ @ private . route ( '/token/<netid>' )
32
+ def private_token_netid ( netid ):
33
+ user = User . query . filter_by (netid = netid ). first ()
34
+ if user is None :
35
+ return error_response ( 'User does not exist' )
36
+ token = get_token ( user . netid )
37
+ res = Response (json .dumps (success_response (token )), headers = {'Content-Type' : 'application/json' })
38
+ res .set_cookie ('token' , token , httponly = True )
39
+ return res
51
40
52
41
53
42
@private .route ('/assignment/sync' , methods = ['POST' ])
@@ -102,7 +91,7 @@ def private_assignment_sync(assignment_data: dict, tests: List[str]):
102
91
Assignment .id == a .id ,
103
92
AssignmentTest .name == test_name ,
104
93
).join (Assignment ).first ()
105
-
94
+
106
95
if at is None :
107
96
at = AssignmentTest (assignment = a , name = test_name )
108
97
db .session .add (at )
@@ -124,7 +113,7 @@ def private_dangling():
124
113
"""
125
114
126
115
dangling = Submission .query .filter (
127
- Submission .student_id == None ,
116
+ Submission .owner_id == None ,
128
117
).all ()
129
118
dangling = [a .data for a in dangling ]
130
119
@@ -134,6 +123,38 @@ def private_dangling():
134
123
})
135
124
136
125
126
+ @private .route ('/reset-dangling' )
127
+ @log_endpoint ('reset-dangling' , lambda : 'reset-dangling' )
128
+ @json_response
129
+ def private_reset_dangling ():
130
+ resets = []
131
+ for s in Submission .query .filter_by (owner_id = None ).all ():
132
+ s .init_submission_models ()
133
+ resets .append (s .data )
134
+ return success_response ({'reset' : resets })
135
+
136
+
137
+ @private .route ('/regrade-submission/<commit>' )
138
+ @log_endpoint ('cli' , lambda : 'regrade-commit' )
139
+ @json_response
140
+ def private_regrade_submission (commit ):
141
+ s = Submission .query .filter (
142
+ Submission .commit == commit ,
143
+ Submission .owner_id != None ,
144
+ ).first ()
145
+
146
+ if s is None :
147
+ return error_response ('not found' )
148
+
149
+ s .init_submission_models ()
150
+ enqueue_webhook_rpc (s .id )
151
+
152
+ return success_response ({
153
+ 'submission' : s .data ,
154
+ 'user' : s .owner .data
155
+ })
156
+
157
+
137
158
@private .route ('/regrade/<assignment_name>' )
138
159
@log_endpoint ('cli' , lambda : 'regrade' )
139
160
@json_response
@@ -159,8 +180,9 @@ def private_regrade_assignment(assignment_name):
159
180
if assignment is None :
160
181
return error_response ('cant find assignment' )
161
182
162
- submission = Submission .query .filter_by (
163
- assignment = assignment
183
+ submission = Submission .query .filter (
184
+ Submission .assignment_id == assignment .id ,
185
+ Submission .owner_id != None
164
186
).all ()
165
187
166
188
response = []
@@ -184,12 +206,11 @@ def private_fix_dangling():
184
206
return fix_dangling ()
185
207
186
208
187
- @private .route ('/stats/<assignment_name >' )
188
- @private .route ('/stats/<assignment_name >/<netid>' )
209
+ @private .route ('/stats/<assignment_id >' )
210
+ @private .route ('/stats/<assignment_id >/<netid>' )
189
211
@log_endpoint ('cli' , lambda : 'stats' )
190
- @cache .memoize (timeout = 60 , unless = lambda : request .args .get ('netids' , None ) is not None )
191
212
@json_response
192
- def private_stats_assignment (assignment_name , netid = None ):
213
+ def private_stats_assignment (assignment_id , netid = None ):
193
214
netids = request .args .get ('netids' , None )
194
215
195
216
if netids is not None :
@@ -199,47 +220,7 @@ def private_stats_assignment(assignment_name, netid=None):
199
220
else :
200
221
netids = list (map (lambda x : x ['netid' ], get_students ()))
201
222
202
- students = get_students ()
203
- students = filter (
204
- lambda x : x ['netid' ] in netids ,
205
- students
206
- )
207
-
208
- bests = {}
209
-
210
- assignment = Assignment .query .filter_by (name = assignment_name ).first ()
211
- if assignment is None :
212
- return error_response ('assignment does not exist' )
213
-
214
- for student in students :
215
- submissionid = stats_for (student ['id' ], assignment .id )
216
- netid = student ['netid' ]
217
- if submissionid is None :
218
- # no submission
219
- bests [netid ] = None
220
- else :
221
- submission = Submission .query .filter_by (
222
- id = submissionid
223
- ).first ()
224
- build = len (submission .builds ) > 0
225
- best_count = sum (map (lambda x : 1 if x .passed else 0 , submission .reports ))
226
- late = 'past due' if assignment .due_date < submission .timestamp else False
227
- late = 'past grace' if assignment .grace_date < submission .timestamp else late
228
- bests [netid ] = {
229
- 'submission' : submission .data ,
230
- 'builds' : build ,
231
- 'reports' : [rep .data for rep in submission .reports ],
232
- 'total_tests_passed' : best_count ,
233
- 'repo_url' : submission .repo ,
234
- 'master' : 'https://github.com/{}' .format (
235
- submission .repo [submission .repo .index (':' ) + 1 :- len ('.git' )],
236
- ),
237
- 'commit_tree' : 'https://github.com/{}/tree/{}' .format (
238
- submission .repo [submission .repo .index (':' ) + 1 :- len ('.git' )],
239
- submission .commit
240
- ),
241
- 'late' : late
242
- }
223
+ bests = bulk_stats (assignment_id , netids )
243
224
return success_response ({'stats' : bests })
244
225
245
226
0 commit comments