This commit is contained in:
undefined committed 2020-07-30 23:09:03 +08:00
1 parent 51dc2e972d
commit 43ee86d34b
17 files changed
+177 -88

No files matched your search

+1
View File
@@ -31,6 +31,7 @@ export async function load() {
for (const i of builtinLib) require(`../lib/${i}`);
await lib(pending, fail);
require('../service/gridfs');
require('../service/monitor');
const server = require('../service/server');
await server.prepare();
await service(pending, fail);
+1 -1
View File
@@ -13,7 +13,7 @@ class DomainRankHandler extends Handler {
@param('page', Types.UnsignedInt, true)
async get(domainId: string, page = 1) {
const [dudocs, upcount, ucount] = await paginate(
domain.getMultiInDomain(domainId).sort({ rating: -1 }),
domain.getMultiInDomain(domainId).sort({ rp: -1 }),
page,
100,
);
+1 -1
View File
@@ -95,7 +95,7 @@ class SystemScriptHandler extends SystemHandler {
validate(global.Hydro.script[id].validate, args);
const rid = await record.add(domainId, -1, this.user._id, '-', id, false, { input: 'raw' });
const report = (data) => judge.next({ domainId, rid, ...data });
report({ message: `Running script: ${id}` });
report({ message: `Running script: ${id}`, status: STATUS.STATUS_JUDGING });
const start = new Date().getTime();
// Maybe async?
global.Hydro.script[id].run(args, report)
+3 -2
View File
@@ -1,5 +1,5 @@
import { ObjectID } from 'mongodb';
import { PermissionError } from '../error';
import { PermissionError, RecordNotFoundError } from '../error';
import { PERM } from '../model/builtin';
import * as problem from '../model/problem';
import * as record from '../model/record';
@@ -63,6 +63,7 @@ class RecordDetailHandler extends RecordHandler {
async get(domainId: string, rid: ObjectID) {
this.response.template = 'record_detail.html';
const rdoc = await record.get(domainId, rid);
if (!rdoc) throw new RecordNotFoundError(rid);
if (rdoc.contest) {
const tdoc = await contest.get(domainId, rdoc.contest.tid, rdoc.contest.type);
if (!this.canShowRecord(tdoc, true)) throw new PermissionError(rid);
@@ -71,7 +72,7 @@ class RecordDetailHandler extends RecordHandler {
rdoc.code = null;
}
const [pdoc, udoc] = await Promise.all([
problem.get(domainId, rdoc.pid, null),
problem.get(domainId, rdoc.pid),
user.getById(domainId, rdoc.uid),
]);
this.response.body = {
+1
View File
@@ -135,6 +135,7 @@ export interface Pdoc {
nAccept: number,
tag: string[],
category: string[],
difficulty: number,
data?: ProblemData,
hidden: boolean,
config: ProblemConfig,
+1 -1
View File
@@ -1,4 +1,4 @@
function* ranked(diter, equ = (a, b) => a === b) {
function* ranked<T>(diter: T[], equ = (a: T, b: T) => a === b): Generator<[number, T], void> {
let last = null;
let r = 0;
let count = 0;
+20 -6
View File
@@ -1,15 +1,17 @@
// Based on https://github.com/QAQrz/Codeforces-Rating-System/blob/master/rating.py
class User {
uid: number;
rank: number;
old: number;
seed: number;
uid: number;
seed?: number;
delta?: number;
new: number;
new?: number;
constructor(rank: number, old: number, uid = 0) {
this.rank = rank;
@@ -22,7 +24,7 @@ class User {
class RatingCalculator {
users: User[];
constructor(users) {
constructor(users: User[]) {
this.users = [];
for (const user of users) {
this.users.push(new User(user.rank, user.old, user.uid));
@@ -106,8 +108,20 @@ interface RatingOutputUser {
}
export default function calculate(users: RatingInputUser[]): RatingOutputUser[] {
let last_idx = 0;
let last_rank = 1;
for (let i = 1; i < users.length; i++) {
if (users[i].rank > last_rank) {
for (let j = last_idx; j < i; j++) users[j].rank = i;
last_idx = i;
last_rank = users[i].rank;
}
}
for (let i = last_idx; i < users.length; i++) {
users[i].rank = users.length;
}
const calculator = new RatingCalculator(users);
return calculator.calculate();
return calculator.calculate() as RatingOutputUser[];
}
global.Hydro.lib.rating = calculate;
+2
View File
@@ -2,6 +2,7 @@ import fs from 'fs';
import path from 'path';
import serialize from 'serialize-javascript';
import nunjucks from 'nunjucks';
import html2text from 'html-to-text';
import { argv } from 'yargs';
import * as markdown from './markdown';
import * as misc from './misc';
@@ -37,6 +38,7 @@ class Nunjucks extends nunjucks.Environment {
this.addFilter('assign', (self, data) => Object.assign(self, data));
this.addFilter('markdown', (self, html = false) => markdown.render(self, html));
this.addFilter('ansi', (self) => misc.ansiToHtml(self));
this.addFilter('inline', (self) => html2text.fromString(self).replace(/\n/gmi, ' '));
this.addFilter('base64_encode', (s) => Buffer.from(s).toString('base64'));
this.addFilter('bitand', (self, val) => self & val);
}
+1
View File
@@ -18,6 +18,7 @@ export const pdocHidden: Pdoc = {
content: '',
nSubmit: 0,
nAccept: 0,
difficulty: 0,
tag: [],
category: [],
data: null,
+10 -4
View File
@@ -109,14 +109,20 @@ export function getMulti(domainId: string, query: any) {
export async function update(
domainId: string, _id: ObjectID,
$set: any = {}, $push: any = {}, $unset: any = {},
): Promise<Rdoc> {
): Promise<Rdoc | null> {
const $update: any = {};
if ($set && Object.keys($set).length) $update.$set = $set;
if ($push && Object.keys($push).length) $update.$push = $push;
if ($unset && Object.keys($unset).length) $update.$unset = $unset;
const res = await coll.findOneAndUpdate({ domainId, _id }, $update, { returnOriginal: false });
if (!res.value) throw new RecordNotFoundError(_id);
return res.value;
if (Object.keys($update).length) {
const res = await coll.findOneAndUpdate(
{ _id, domainId },
$update,
{ returnOriginal: false },
);
return res.value;
}
return await get(domainId, _id);
}
export function reset(domainId: string, rid: ObjectID, isRejudge: boolean) {
+1 -1
View File
@@ -107,7 +107,7 @@ DomainUserSetting(
Setting('setting_storage', 'nAccept', null, 0, 'number', 'nAccept', null, FLAG_HIDDEN | FLAG_DISABLED),
Setting('setting_storage', 'nSubmit', null, 0, 'number', 'nSubmit', null, FLAG_HIDDEN | FLAG_DISABLED),
Setting('setting_storage', 'nLike', null, 0, 'number', 'nLike', null, FLAG_HIDDEN | FLAG_DISABLED),
Setting('setting_storage', 'rating', null, 1500, 'number', 'rating', null, FLAG_HIDDEN | FLAG_DISABLED),
Setting('setting_storage', 'rp', null, 1500, 'number', 'RP', null, FLAG_HIDDEN | FLAG_DISABLED),
);
SystemSetting(
+39 -54
View File
@@ -1,47 +1,50 @@
/* eslint-disable no-await-in-loop */
import { NumericDictionary } from 'lodash';
import { NumericDictionary, filter } from 'lodash';
import { ObjectID } from 'mongodb';
import { Tdoc, Pdoc } from '../interface';
import * as domain from '../model/domain';
import * as contest from '../model/contest';
import * as problem from '../model/problem';
import rating from '../lib/rating';
import * as record from '../model/record';
import { STATUS } from '../model/builtin';
import { Tdoc, Pdoc } from '../interface';
import rating from '../lib/rating';
export const description = 'Calculate rating of a domain, or all domains';
export const description = 'Calculate rp of a domain, or all domains';
type ND = NumericDictionary<number>
function calc(udict: ND, rankedDocs: [number, number][]) {
const users = [];
for (const [rank, uid] of rankedDocs) {
users.push({ uid, rank, old: udict[uid] || 1500 });
for (const [rk, uid] of rankedDocs) {
users.push({ uid, rank: rk, old: udict[uid] || 1500 });
}
// FIXME sum(rating.new) always less than sum(rating.old)
const rated = rating(users);
for (const udoc of rated) {
udict[udoc.uid] = udoc.new;
}
}
async function runProblem(pdoc: Pdoc, udict: ND, uids: number[]): Promise<void>
async function runProblem(domainId: string, pid: number, udict: ND, uids: number[]): Promise<void>
async function runProblem(pdoc: Pdoc, udict: ND): Promise<void>
async function runProblem(domainId: string, pid: number, udict: ND): Promise<void>
async function runProblem(...arg: any[]) {
const pdoc: Pdoc = (typeof arg[0] === 'string')
? await contest.get(arg[0], arg[1], -1)
? await problem.get(arg[0], arg[1], -1)
: arg[0];
const udict: ND = (typeof arg[0] === 'string') ? arg[2] : arg[1];
const uids = (typeof arg[0] === 'string') ? arg[3] : arg[2];
const psdocs = await problem.getMultiStatus(
pdoc.domainId, { docId: pdoc.docId },
).sort({ score: -1, _id: 1 }).toArray();
const ranked = [];
const u = new Set(Array.from(uids));
for (const index in psdocs) {
ranked.push([index + 1, psdocs[index].uid]);
u.delete(psdocs[index].uid);
pdoc.domainId, { docId: pdoc.docId, rid: { $ne: null } },
).toArray();
if (!psdocs.length) return;
const rdict = await record.getList(pdoc.domainId, psdocs.map((psdoc) => psdoc.rid), true);
const nAccept = filter(psdocs, (psdoc) => psdoc.status === STATUS.STATUS_ACCEPTED).length;
const p = (pdoc.difficulty || 5) / (Math.sqrt(Math.sqrt(nAccept)) + 1);
for (const psdoc of psdocs) {
if (rdict[psdoc.rid]) {
const rp = rdict[psdoc.rid].score * p;
udict[psdoc.uid] = (udict[psdoc.uid] || 1500) + rp;
}
}
u.forEach((uid) => ranked.push(psdocs.length, uid));
calc(udict, ranked);
}
async function runContest(tdoc: Tdoc, udict: ND, report: Function): Promise<void>
@@ -57,6 +60,7 @@ async function runContest(...arg: any[]) {
const report = (typeof arg[0] === 'string') ? arg[3] : arg[2];
const tsdocs = await contest.getMultiStatus(tdoc.domainId, tdoc.docId, tdoc.docType)
.sort(contest.RULES[tdoc.rule].statusSort).toArray();
if (!tsdocs.length) return;
const rankedTsdocs = contest.RULES[tdoc.rule].rank(tsdocs);
const ranked = [];
for (const result of rankedTsdocs) {
@@ -74,40 +78,9 @@ async function runContest(...arg: any[]) {
});
}
async function getRelatedUsers(domainId: string) {
const uids: Set<number> = new Set();
const problems = await problem.getMulti(domainId, { hidden: false }).toArray();
for (const pdoc of problems) {
const psdocs = await problem.getMultiStatus(
pdoc.domainId, { docId: pdoc.docId, status: STATUS.STATUS_ACCEPTED },
).sort('rid', 1).toArray();
for (const psdoc of psdocs) uids.add(psdoc.uid);
}
const contests = await contest.getMulti(domainId, { rated: true }, -1).sort('endAt', -1).toArray();
for (const tdoc of contests) {
const tsdocs = await contest.getMultiStatus(tdoc.domainId, tdoc.docId, tdoc.docType)
.sort(contest.RULES[tdoc.rule].statusSort).toArray();
for (const tsdoc of tsdocs) uids.add(tsdoc.uid);
}
return Array.from(uids);
}
async function runInDomain(domainId: string, isSub: boolean, report: Function) {
await domain.setMultiUserInDomain(domainId, {}, { rating: 1500 });
const uids = await getRelatedUsers(domainId);
await domain.setMultiUserInDomain(domainId, {}, { rp: 1500 });
const udict: ND = {};
// TODO pagination
const problems = await problem.getMulti(domainId, { hidden: false }).toArray();
await report({ message: `Found ${problems.length} problems in ${domainId}` });
for (const i in problems) {
const pdoc = problems[i];
await runProblem(pdoc, udict, uids);
if (!isSub) {
await report({
progress: Math.floor(((parseInt(i, 10) + 1) / problems.length) * 100),
});
}
}
const contests = await contest.getMulti(domainId, { rated: true }, -1).sort('endAt', -1).toArray();
await report({ message: `Found ${contests.length} contests in ${domainId}` });
for (const i in contests) {
@@ -119,9 +92,21 @@ async function runInDomain(domainId: string, isSub: boolean, report: Function) {
});
}
}
// TODO pagination
const problems = await problem.getMulti(domainId, { hidden: false }).toArray();
await report({ message: `Found ${problems.length} problems in ${domainId}` });
for (const i in problems) {
const pdoc = problems[i];
await runProblem(pdoc, udict);
if (!isSub) {
await report({
progress: Math.floor(((parseInt(i, 10) + 1) / problems.length) * 100),
});
}
}
const tasks = [];
for (const uid in udict) {
tasks.push(domain.setUserInDomain(domainId, parseInt(uid, 10), { rating: udict[uid] }));
tasks.push(domain.setUserInDomain(domainId, parseInt(uid, 10), { rp: udict[uid] }));
}
await Promise.all(tasks);
}
@@ -144,7 +129,7 @@ export async function run({ domainId }, report: Function) {
progress: Math.floor(((parseInt(i, 10) + 1) / domains.length) * 100),
});
}
} else runInDomain(domainId, false, report);
} else await runInDomain(domainId, false, report);
return true;
}
@@ -155,4 +140,4 @@ export const validate = {
],
};
global.Hydro.script.rating = { run, description, validate };
global.Hydro.script.rp = { run, description, validate };
+4 -1
View File
@@ -29,7 +29,10 @@ export function publish(event: string, payload: any, isMaster = true) {
} else {
if (!bus[event]) bus[event] = {};
const funcs = Object.keys(bus[event]);
Promise.all(funcs.map((func) => bus[event][func]()));
Promise.all(funcs.map((func) => bus[event][func]({
event,
value: payload,
})));
}
}
+14 -11
View File
@@ -1,3 +1,4 @@
import cluster from 'cluster';
import * as db from './db';
import * as sysinfo from '../lib/sysinfo';
@@ -22,16 +23,18 @@ export function updateJudger(args) {
);
}
global.Hydro.postInit.push(
async () => {
const info = await sysinfo.get();
await coll.updateOne(
{ mid: info.mid, type: 'server' },
{ $set: { ...info, updateAt: new Date(), type: 'server' } },
{ upsert: true },
);
setInterval(update, 60 * 1000);
},
);
if (cluster.isMaster) {
global.Hydro.postInit.push(
async () => {
const info = await sysinfo.get();
await coll.updateOne(
{ mid: info.mid, type: 'server' },
{ $set: { ...info, updateAt: new Date(), type: 'server' } },
{ upsert: true },
);
setInterval(update, 60 * 1000);
},
);
}
global.Hydro.service.monitor = { update, updateJudger };