NDOJ/judge/management/commands/generate_data.py

61 lines
1.8 KiB
Python
Raw Permalink Normal View History

2022-04-12 02:18:01 +00:00
from django.core.management.base import BaseCommand
from judge.models import *
from collections import defaultdict
import csv
import os
from django.conf import settings
2022-09-14 21:11:15 +00:00
from django.db import connection
2022-04-12 02:18:01 +00:00
def gen_submissions():
2022-09-14 21:11:15 +00:00
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:
2022-09-14 21:11:15 +00:00
f = csv.writer(csvfile)
f.writerow(headers)
for row in cursor.fetchall():
f.writerow(row)
2022-05-14 17:57:27 +00:00
2022-04-12 02:18:01 +00:00
def gen_users():
2022-09-14 21:11:15 +00:00
print("Generating users")
2022-05-14 17:57:27 +00:00
headers = ["uid", "username", "rating", "points"]
with open(os.path.join(settings.ML_DATA_PATH, "profiles.csv"), "w") as csvfile:
2022-04-12 02:18:01 +00:00
f = csv.writer(csvfile)
f.writerow(headers)
2022-05-14 17:57:27 +00:00
2022-09-14 21:11:15 +00:00
for u in Profile.objects.all().iterator():
2022-04-12 02:18:01 +00:00
f.writerow([u.id, u.username, u.rating, u.performance_points])
2022-05-14 17:57:27 +00:00
2022-04-12 02:18:01 +00:00
def gen_problems():
2022-09-14 21:11:15 +00:00
print("Generating problems")
2022-05-14 17:57:27 +00:00
headers = ["pid", "code", "name", "points", "url"]
with open(os.path.join(settings.ML_DATA_PATH, "problems.csv"), "w") as csvfile:
2022-04-12 02:18:01 +00:00
f = csv.writer(csvfile)
f.writerow(headers)
2022-09-14 21:11:15 +00:00
for p in Problem.objects.all().iterator():
2022-05-14 17:57:27 +00:00
f.writerow(
[p.id, p.code, p.name, p.points, "lqdoj.edu.vn/problem/" + p.code]
)
2022-04-12 02:18:01 +00:00
class Command(BaseCommand):
2022-05-14 17:57:27 +00:00
help = "generate data for ML"
2022-04-12 02:18:01 +00:00
def handle(self, *args, **options):
gen_users()
gen_problems()
2022-05-14 17:57:27 +00:00
gen_submissions()