This repository has been archived by the owner on May 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-firebase.rb
100 lines (80 loc) · 2.67 KB
/
sync-firebase.rb
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
#!/usr/bin/env ruby
require 'faraday'
require 'json'
require_relative 'prg_api'
require_relative 'sync_config'
FALLBACK_PROBLEM_NAME = 'nonexistance'
FALLBACK_LANGUAGE = 'cpp'
# judge/rails initialization
GRADER_ENV = 'grading'
require File.join(File.dirname(__FILE__),'config/environment')
RAILS_ENV = 'development'
require RAILS_ROOT + '/config/environment'
def create_submission(res, user)
problem_name = res['problem_id']
problem = Problem.find_by name: problem_name
language = Language.find_by name: res['language']
if !problem
problem = Problem.find_by name: FALLBACK_PROBLEM_NAME
end
if !language
language = Language.find_by name: FALLBACK_LANGUAGE
end
submission = Submission.new
submission.language = language
submission.problem = problem
submission.user = user
submission.source = res['code']
submission.source.encode!('UTF-8', 'UTF-8', invalid: :replace, replace: '')
submission.submitted_at = Time.new.gmtime
submission.save!
Task.create(submission: submission,
status: Task::STATUS_INQUEUE)
return submission
end
def sync_loop(user, submission_statuses)
response = call_api_get_oldest_submissions_in_queue(BASE_URL)
if !response or response.status != 200
puts 'Request error'
else
results = JSON.parse(response.body)['result']
results.each do |res|
next if !(res.has_key? 'submission_id')
submission_id = res['submission_id']
if !submission_statuses.has_key? submission_id
sub = create_submission(res, user)
puts "created #{submission_id}"
submission_statuses[submission_id] = {
status: :created,
sub_id: sub.id
}
end
end
end
submission_statuses.each do |submission_id, data|
if data[:status] == :created
submission = Submission.find(data[:sub_id])
if submission and submission.graded_at
puts "#{submission_id} - graded: #{submission.grader_comment}"
submission_statuses[submission_id][:status] = :graded
call_api_update_submission_status(BASE_URL,
FIREBASE_CREDENTIAL_FILE,
submission_id,
submission.grader_comment,
submission.points,
submission.max_runtime,
submission.peak_memory,
submission.graded_at)
end
end
end
end
def main
user = User.find_by login: BOT_USER
submission_statuses = {}
while true
sync_loop(user, submission_statuses)
sleep(SLEEP_TIME)
end
end
main