优化rating计算速度

This commit is contained in:
masnn committed 2020-06-09 13:22:04 +08:00
1 parent 45f1184d68
commit 55912b087d
22 files changed
+309 -178

No files matched your search

+2 -2
View File
@@ -282,7 +282,7 @@ class ContestCreateHandler extends ContestHandler {
}
async post({
domainId, title, content, rule, beginAtDate, beginAtTime, duration, pids,
domainId, title, content, rule, beginAtDate, beginAtTime, duration, pids, rated = false,
}) {
let beginAt;
try {
@@ -294,7 +294,7 @@ class ContestCreateHandler extends ContestHandler {
pids = await this.verifyProblems(domainId, pids);
const tid = await contest.add(
domainId, title, content,
this.user._id, rule, beginAt, endAt, pids,
this.user._id, rule, beginAt, endAt, pids, rated,
);
this.response.body = { tid };
this.response.redirect = `/c/${tid}`;
+7 -2
View File
@@ -95,7 +95,12 @@ class DomainUserHandler extends ManageHandler {
const uids = [];
const rudocs = {};
const [udocs, roles] = await Promise.all([
user.getInDomain(domainId),
user.getMultiInDomain(domainId, {
$and: [
{ role: { $ne: 'default' } },
{ role: { $ne: null } },
],
}).toArray(),
user.getRoles(domainId),
]);
for (const role of roles) rudocs[role._id] = [];
@@ -104,7 +109,7 @@ class DomainUserHandler extends ManageHandler {
// TODO Improve here
// eslint-disable-next-line no-await-in-loop
const ud = await user.getById(domainId, udoc.uid);
rudocs[udoc.role].push({ ...ud, role: udoc.role });
rudocs[ud.role].push(ud);
}
const rolesSelect = roles.map((role) => [role._id, role._id]);
const udict = await user.getList(domainId, uids);
+2 -2
View File
@@ -197,7 +197,7 @@ class HomeworkCreateHandler extends HomeworkHandler {
async post({
domainId, title, content, beginAtDate, beginAtTime,
penaltySinceDate, penaltySinceTime, extensionDays,
penaltyRules, pids,
penaltyRules, pids, rated = false,
}) {
let beginAt;
let penaltySince;
@@ -216,7 +216,7 @@ class HomeworkCreateHandler extends HomeworkHandler {
if (penaltySince > endAt) throw new ValidationError('extensionDays');
await this.verifyProblems(domainId, pids);
const tid = await contest.add(domainId, title, content, this.user._id,
'homework', beginAt, endAt, pids, { penaltySince, penaltyRules }, document.TYPE_HOMEWORK);
'homework', beginAt, endAt, pids, rated, { penaltySince, penaltyRules }, document.TYPE_HOMEWORK);
this.response.body = { tid };
this.response.redirect = `/homework/${tid}`;
}
+2 -2
View File
@@ -8,7 +8,7 @@ const { PermissionError } = require('../error');
class SystemHandler extends Handler {
async prepare() {
if (!this.user.priv) throw new PermissionError('???');
this.body = {
this.response.body = {
path: [
['Hydro', '/'],
['manage_main', '/manage'],
@@ -58,7 +58,7 @@ class SystemScriptHandler extends SystemHandler {
async post({ id, args }) {
// TODO Do not use console.log
await global.Hydro.script[id].run(args, console.log);
await global.Hydro.script[id].run(JSON.parse(args), console.log);
this.back();
}
}
+1 -1
View File
@@ -176,7 +176,7 @@ class ProblemSubmitHandler extends ProblemDetailHandler {
});
await Promise.all([
record.judge(domainId, rid),
user.inc(this.user._id, 'nSubmit'),
user.incDomain(domainId, this.user._id, 'nSubmit'),
]);
this.response.body = { rid };
this.response.redirect = `/r/${rid}`;
+10
View File
@@ -0,0 +1,10 @@
const crypto = require('crypto');
/**
* @param {any} content
* @return {string}
*/
global.Hydro.lib.sha1 = module.exports = (content) => {
const hash = crypto.createHash('sha1');
hash.update(content);
return hash.digest('hex');
};
+2 -2
View File
@@ -216,7 +216,7 @@ const builtinHandler = [
];
const builtinScript = [
'install', 'uninstall', 'rating', 'recalcRating',
'install', 'uninstall', 'rating', 'recalcRating', 'register',
];
async function loadAsMaster() {
@@ -385,7 +385,7 @@ async function load() {
cluster.on('online', (worker) => {
console.log(`Worker ${worker.process.pid} is online`);
});
for (let i = 0; i < numCPUs; i++) {
for (let i = 1; i < numCPUs; i++) {
cluster.fork();
}
} else {
+3 -2
View File
@@ -207,7 +207,8 @@ const RULES = {
* @returns {ObjectID} tid
*/
function add(domainId, title, content, owner, rule,
beginAt = new Date(), endAt = new Date(), pids = [], data = {}, type = document.TYPE_CONTEST) {
beginAt = new Date(), endAt = new Date(), pids = [], rated = false,
data = {}, type = document.TYPE_CONTEST) {
validator.checkTitle(title);
validator.checkContent(content);
if (!this.RULES[rule]) throw new ValidationError('rule');
@@ -217,7 +218,7 @@ function add(domainId, title, content, owner, rule,
});
this.RULES[rule].check(data);
return document.add(domainId, content, owner, type, null, null, null, {
...data, title, rule, beginAt, endAt, pids, attend: 0,
...data, title, rule, beginAt, endAt, pids, attend: 0, rated,
});
}
+16 -8
View File
@@ -29,7 +29,7 @@ class USER {
this.nSubmit = dudoc.nSubmit || 0;
this.rating = dudoc.rating || 1500;
this.perm = dudoc.perm;
this.role = dudoc.role;
this.role = dudoc.role || 'default';
}
hasPerm(p) {
@@ -39,7 +39,7 @@ class USER {
checkPassword(password) {
const h = global.Hydro.lib[`hash.${this.hashType || 'hydro'}`];
if (!h) throw new Error('Unknown hash method');
return h.check(password, this.salt(), this.hash());
return h.check(password, this.salt(), this.hash(), this);
}
}
@@ -120,6 +120,17 @@ async function inc(_id, field, n = 1) {
return udoc;
}
function setInDomain(domainId, uid, params) {
return document.setStatus(domainId, document.TYPE_DOMAIN_USER, 0, uid, params);
}
async function incDomain(domainId, uid, field, n = 1) {
const dudoc = await getInDomain(domainId, { _id: uid });
dudoc[field] = dudoc[field] + n || n;
await setInDomain(domainId, uid, { [field]: dudoc[field] });
return dudoc;
}
async function create({
uid, mail, uname, password, regip = '127.0.0.1', priv = perm.PRIV_NONE,
}) {
@@ -151,10 +162,6 @@ function getMulti(params) {
return coll.find(params);
}
function setInDomain(domainId, uid, params) {
return document.setStatus(domainId, document.TYPE_DOMAIN_USER, 0, uid, params);
}
function setMultiInDomain(domainId, query, params) {
return document.setMultiStatus(domainId, document.TYPE_DOMAIN_USER, query, params);
}
@@ -193,8 +200,8 @@ function getRole(domainId, name) {
return document.get(domainId, document.TYPE_DOMAIN_USER, name);
}
function getMultiInDomain(domainId) {
return document.getMultiStatus(domainId, document.TYPE_DOMAIN_USER);
function getMultiInDomain(domainId, query) {
return document.getMultiStatus(domainId, document.TYPE_DOMAIN_USER, query);
}
function addRole(domainId, name, permission) {
@@ -223,6 +230,7 @@ global.Hydro.model.user = module.exports = {
getByUname,
getMulti,
inc,
incDomain,
setById,
setEmail,
setPassword,
+1 -1
View File
@@ -56,7 +56,7 @@ async function run({ username, password } = {}) {
}
await Promise.all(tasks);
tasks = [
allowFail(domain.add, 'system', 0),
domain.add('system', 0, 'Hydro').catch(() => domain.edit('system', { owner: 0, name: 'Hydro' })),
collUser.updateOne({ _id: 0 }, {
$set: defaults({
_id: 0,
+9 -7
View File
@@ -14,28 +14,30 @@ async function run({ domainId, isSub = false }, report) {
}
return true;
}
const contests = await contest.getMulti(domainId).sort('endAt', -1).toArray();
const contests = await contest.getMulti(domainId, { rated: true }).sort('endAt', -1).toArray();
await report({ total: contests.length });
await user.setMultiInDomain(domainId, {}, { rating: 1500 });
const udict = {};
for (const i in contests) {
const tdoc = contests[i];
const tsdocs = await contest.getMultiStatus(domainId, { docId: tdoc.docId })
.sort(contest.RULES[tdoc.rule].statusSort).toArray();
const udict = await user.getList(domainId, tsdocs.map((tsdoc) => tsdoc.uid));
const rankedTsdocs = contest.RULES[tdoc.rule].rank(tsdocs);
const users = [];
for (const result of rankedTsdocs) {
users.push({ ...result[1], rank: result[0], old: udict[result[1].uid].rating || 1500 });
users.push({ ...result[1], rank: result[0], old: udict[result[1].uid] || 1500 });
}
const rated = rating(users);
const tasks = [];
for (const udoc of rated) {
tasks.push(user.setInDomain(domainId, udoc.uid, { rating: udoc.new }));
udict[udoc.uid] = udoc.new;
}
await Promise.all(tasks);
if (isSub) await report({ subProgress: (i + 1) / contests.length });
else await report({ current: parseInt(i) + 1 });
}
const tasks = [];
for (const uid in udict) {
tasks.push(user.setInDomain(domainId, uid, { rating: udict[uid] }));
}
await Promise.all(tasks);
return true;
}
+14
View File
@@ -0,0 +1,14 @@
const user = require('../model/user');
const system = require('../model/system');
async function run({
uname, password, mail, uid,
}) {
if (!uid) uid = await system.inc('user');
await user.create({
uid, uname, password, mail, regip: '127.0.0.1',
});
return uid;
}
global.Hydro.script.register = module.exports = { run };
+2 -2
View File
@@ -302,7 +302,7 @@ function Route(route, RouteHandler, permission = null) {
const method = ctx.method.toLowerCase();
// TODO(masnn) domainId
const args = {
...ctx.params, ...ctx.query, ...ctx.request.body, domainId: 'system',
domainId: 'system', ...ctx.params, ...ctx.query, ...ctx.request.body,
};
if (h.request.host !== await system.get('server.host')) {
args.domainId = h.request.url.split('//')[1].split(`.${await system.get('server.host')}`)[0] || '';
@@ -399,7 +399,7 @@ function Connection(prefix, RouteConnHandler) {
sock.on('connection', async (conn) => {
const h = new RouteConnHandler(conn);
try {
const args = { ...h.request.params, domainId: 'system' };
const args = { domainId: 'system', ...h.request.params };
if (args.uid) args.uid = parseInt(validator.checkUid(args.uid));
if (args.page) args.page = parseInt(args.page);