class ProfilingMiddleware: report = None state = 'new' def check_filters(self, view): for f in settings.PROFILER_FILTERS: if f.match(view): return True return False def process_view(self, request, view_func, view_args, view_kwargs): view = resolve(request.META['PATH_INFO']).url_name method = request.method if self.state != 'new' or not settings.PROFILER or self.check_filters(view): return self.report = Report(view=view, method=method) self.report.start() self.state = 'view' def process_response(self, request, response): if self.state == 'view' and self.report: self.report.end() self.report.save() self.state = 'new' return response def process_exception(self, request, exception): self.state = 'invalid' return None
def handle(self, *args, **options): # Parse arguments if len(args) > 0 and args[0] == "regrade": where = {'status': 'failed'} if len(args) > 1: where['submission__attempt__problem__slug'] = args[1] try: res = Result.objects.all().filter(**where) print "Deleting %d results" % len(res) res.delete() except: pass # Setup colored logging log = defaultdict(lambda: '%s', {'success': '\033[32m%s\033[0m'}) # Setup stat reporting COMMIT_DELTA = 5 stats = Report(view='grade_submissions', method='CONSOLE') next_commit = { 'calls': stats.calls + 1, 'time': datetime.datetime.now() + datetime.timedelta(seconds=COMMIT_DELTA) } # Begin processing while True: stats.start() S = Submission.objects \ .select_related('attempt', 'attempt__problem', 'result') \ .filter(result=None) stats.end(increment=False) for current_submission in S: stats.start() attempt = current_submission.attempt # Create the result calculated_result = Result(submission=current_submission) if calculated_result.grade(): cache.delete('scoreboard') print log[ calculated_result.status] % calculated_result.log_string # Increment profiler each iteration stats.end() # Commit profiler every 5 graded if we are running profiler if settings.PROFILER: if stats.calls >= next_commit[ 'calls'] and datetime.datetime.now( ) >= next_commit['time']: stats.save() next_commit['calls'] = stats.calls + 1 next_commit['time'] = datetime.datetime.now( ) + datetime.timedelta(seconds=COMMIT_DELTA) sleep(1)
def handle(self, *args, **options): # Parse arguments if len(args) > 0 and args[0] == "regrade": where = { 'status': 'failed' } if len(args) > 1: where['submission__attempt__problem__slug'] = args[1] try: res = Result.objects.all().filter(**where) print "Deleting %d results" % len(res) res.delete() except: pass # Setup colored logging log = defaultdict(lambda: '%s', {'success': '\033[32m%s\033[0m'}) # Setup stat reporting COMMIT_DELTA = 5 stats = Report(view='grade_submissions', method='CONSOLE') next_commit = {'calls': stats.calls + 1, 'time': datetime.datetime.now() + datetime.timedelta(seconds=COMMIT_DELTA) } # Begin processing while True: stats.start() S = Submission.objects \ .select_related('attempt', 'attempt__problem', 'result') \ .filter(result=None) stats.end(increment=False) for current_submission in S: stats.start() attempt = current_submission.attempt # Create the result calculated_result = Result(submission=current_submission) if calculated_result.grade(): cache.delete('scoreboard') print log[calculated_result.status] % calculated_result.log_string # Increment profiler each iteration stats.end() # Commit profiler every 5 graded if we are running profiler if settings.PROFILER: if stats.calls >= next_commit['calls'] and datetime.datetime.now() >= next_commit['time']: stats.save() next_commit['calls'] = stats.calls + 1 next_commit['time'] = datetime.datetime.now() + datetime.timedelta(seconds=COMMIT_DELTA) sleep(1)