Use raw sql to gen data
This commit is contained in:
parent
88c1f6324d
commit
196e2a9bb0
1 changed files with 21 additions and 17 deletions
|
@ -4,41 +4,45 @@ from collections import defaultdict
|
||||||
import csv
|
import csv
|
||||||
import os
|
import os
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.db import connection
|
||||||
|
|
||||||
|
|
||||||
def gen_submissions():
|
def gen_submissions():
|
||||||
headers = ["uid", "pid"]
|
print("Generating submissions")
|
||||||
|
query = """
|
||||||
|
SELECT user_id as uid, problem_id as pid from
|
||||||
|
(SELECT user_id, problem_id, max(date) as max_date
|
||||||
|
from judge_submission
|
||||||
|
group by user_id, problem_id) t
|
||||||
|
order by user_id, -max_date;
|
||||||
|
"""
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
cursor.execute(query)
|
||||||
|
headers = [i[0] for i in cursor.description]
|
||||||
with open(os.path.join(settings.ML_DATA_PATH, "submissions.csv"), "w") as csvfile:
|
with open(os.path.join(settings.ML_DATA_PATH, "submissions.csv"), "w") as csvfile:
|
||||||
f = csv.writer(csvfile)
|
f = csv.writer(csvfile)
|
||||||
f.writerow(headers)
|
f.writerow(headers)
|
||||||
|
for row in cursor.fetchall():
|
||||||
last_pid = defaultdict(int)
|
f.writerow(row)
|
||||||
for u in Profile.objects.all():
|
|
||||||
used = set()
|
|
||||||
print("Processing user", u.id)
|
|
||||||
for s in Submission.objects.filter(user=u).order_by("-date"):
|
|
||||||
if s.problem.id not in used:
|
|
||||||
used.add(s.problem.id)
|
|
||||||
f.writerow([u.id, s.problem.id])
|
|
||||||
|
|
||||||
|
|
||||||
def gen_users():
|
def gen_users():
|
||||||
|
print("Generating users")
|
||||||
headers = ["uid", "username", "rating", "points"]
|
headers = ["uid", "username", "rating", "points"]
|
||||||
with open(os.path.join(settings.ML_DATA_PATH, "profiles.csv"), "w") as csvfile:
|
with open(os.path.join(settings.ML_DATA_PATH, "profiles.csv"), "w") as csvfile:
|
||||||
f = csv.writer(csvfile)
|
f = csv.writer(csvfile)
|
||||||
f.writerow(headers)
|
f.writerow(headers)
|
||||||
|
|
||||||
for u in Profile.objects.all():
|
for u in Profile.objects.all().iterator():
|
||||||
f.writerow([u.id, u.username, u.rating, u.performance_points])
|
f.writerow([u.id, u.username, u.rating, u.performance_points])
|
||||||
|
|
||||||
|
|
||||||
def gen_problems():
|
def gen_problems():
|
||||||
|
print("Generating problems")
|
||||||
headers = ["pid", "code", "name", "points", "url"]
|
headers = ["pid", "code", "name", "points", "url"]
|
||||||
with open(os.path.join(settings.ML_DATA_PATH, "problems.csv"), "w") as csvfile:
|
with open(os.path.join(settings.ML_DATA_PATH, "problems.csv"), "w") as csvfile:
|
||||||
f = csv.writer(csvfile)
|
f = csv.writer(csvfile)
|
||||||
f.writerow(headers)
|
f.writerow(headers)
|
||||||
|
for p in Problem.objects.all().iterator():
|
||||||
for p in Problem.objects.all():
|
|
||||||
f.writerow(
|
f.writerow(
|
||||||
[p.id, p.code, p.name, p.points, "lqdoj.edu.vn/problem/" + p.code]
|
[p.id, p.code, p.name, p.points, "lqdoj.edu.vn/problem/" + p.code]
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue