feat: more sort rules
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from flask import Flask, request, send_from_directory, abort, jsonify
|
||||
from sort_rule import sort_by_rule
|
||||
import csv
|
||||
import datetime
|
||||
import os
|
||||
@@ -99,68 +100,39 @@ def upload_score():
|
||||
@app.route("/get_scores/<project>", methods=["GET"])
|
||||
def get_scores(project):
|
||||
try:
|
||||
# CSV 文件路径
|
||||
# 获取排序字段
|
||||
sort_by = request.args.get("sort_by", "time")
|
||||
limit = request.args.get("limit")
|
||||
|
||||
# 获取 CSV 文件
|
||||
csv_file_path = get_csv_file_path(project)
|
||||
if not os.path.exists(csv_file_path):
|
||||
return jsonify([])
|
||||
|
||||
with open(csv_file_path, mode="r", encoding="utf-8") as csvfile:
|
||||
reader = csv.DictReader(csvfile)
|
||||
data = list(reader)
|
||||
# 时间倒序
|
||||
data.sort(
|
||||
key=lambda x: datetime.datetime.strptime(
|
||||
x["time"], "%Y-%m-%d %H:%M:%S"
|
||||
),
|
||||
reverse=True,
|
||||
)
|
||||
scores = jsonify(data[:30])
|
||||
# 预转换数据类型
|
||||
data = [
|
||||
{
|
||||
**row,
|
||||
"time": datetime.datetime.strptime(
|
||||
row["time"], "%Y-%m-%d %H:%M:%S"
|
||||
),
|
||||
"score": float(row["score"]),
|
||||
"running_time": float(row["running_time"].replace("ms", "")),
|
||||
}
|
||||
for row in reader
|
||||
]
|
||||
|
||||
scores.headers.add("Access-Control-Allow-Origin", "*")
|
||||
scores.headers.add("Cache-Control", "public, max-age=300")
|
||||
return scores
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
# 根据 sort_by 排序
|
||||
scores = sort_by_rule(data, sort_by, limit)
|
||||
|
||||
# 设置缓存和跨域头
|
||||
response = jsonify(scores)
|
||||
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||
response.headers.add("Cache-Control", "public, max-age=300")
|
||||
return response
|
||||
|
||||
@app.route("/get_top_scores/<project>", methods=["GET"])
|
||||
def get_top_scores(project):
|
||||
try:
|
||||
# CSV 文件路径
|
||||
csv_file_path = get_csv_file_path(project)
|
||||
if not os.path.exists(csv_file_path):
|
||||
return jsonify([])
|
||||
|
||||
with open(csv_file_path, mode="r", encoding="utf-8") as csvfile:
|
||||
reader = csv.DictReader(csvfile)
|
||||
scores = list(reader)
|
||||
|
||||
# 数据处理
|
||||
for score in scores:
|
||||
score["score"] = float(score["score"])
|
||||
score["running_time"] = float(score["running_time"].replace("ms", ""))
|
||||
|
||||
# 分数降序,运行时间升序
|
||||
scores_sorted = sorted(
|
||||
scores, key=lambda x: (-x["score"], x["running_time"])
|
||||
)
|
||||
|
||||
# 存储最佳记录
|
||||
best_scores_by_id = {}
|
||||
for score in scores_sorted:
|
||||
student_id = score["id"]
|
||||
if student_id not in best_scores_by_id:
|
||||
best_scores_by_id[student_id] = score
|
||||
|
||||
# 取前十名
|
||||
for score in scores:
|
||||
score["running_time"] = str(score["running_time"]) + "ms"
|
||||
top_scores = list(best_scores_by_id.values())[:10]
|
||||
|
||||
response = jsonify(top_scores)
|
||||
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||
response.headers.add("Cache-Control", "public, max-age=300")
|
||||
return response
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
Reference in New Issue
Block a user