move delarations to @hydrooj/common

This commit is contained in:
undefined committed 2025-02-09 15:17:10 +00:00
1 parent 100ccf718f
commit 4a33816a37
49 files changed
+496 -465

No files matched your search

+4 -7
View File
@@ -5,13 +5,12 @@ import { omit } from 'lodash';
import { ObjectId } from 'mongodb';
import PQueue from 'p-queue';
import sanitize from 'sanitize-filename';
import { JudgeResultBody, ProblemConfigFile, TestCase } from '@hydrooj/common';
import { Context } from '../context';
import {
FileLimitExceededError, ForbiddenError, ProblemIsReferencedError, ValidationError,
} from '../error';
import {
JudgeResultBody, ProblemConfigFile, RecordDoc, Task, TestCase,
} from '../interface';
import { RecordDoc, Task } from '../interface';
import { Logger } from '../logger';
import * as builtin from '../model/builtin';
import { PERM, STATUS } from '../model/builtin';
@@ -73,11 +72,10 @@ function processPayload(body: Partial<JudgeResultBody>) {
}
export async function next(body: Partial<JudgeResultBody>) {
body.rid = new ObjectId(body.rid); // TODO remove this
const {
$set, $push, $unset, $inc,
} = processPayload(body);
const rdoc = await record.update(body.domainId, body.rid, $set, $push, $unset, $inc);
const rdoc = await record.update(body.domainId, new ObjectId(body.rid), $set, $push, $unset, $inc);
if (rdoc) bus.broadcast('record/change', rdoc, $set, $push, body);
return rdoc;
}
@@ -139,12 +137,11 @@ bus.on('record/judge', async (rdoc, updated, pdoc) => {
});
export async function end(body: Partial<JudgeResultBody>) {
body.rid = new ObjectId(body.rid); // TODO remove this
const { $set, $push } = processPayload(body);
const $unset: any = { progress: '' };
$set.judgeAt = new Date();
$set.judger = body.judger ?? 1;
const rdoc = await record.update(body.domainId, body.rid, $set, $push, $unset);
const rdoc = await record.update(body.domainId, new ObjectId(body.rid), $set, $push, $unset);
if (rdoc) bus.broadcast('record/change', rdoc, null, null, body); // trigger a full update
await postJudge(rdoc);
return rdoc;
+2 -2
View File
@@ -115,7 +115,7 @@ class SystemScriptHandler extends SystemHandler {
const time = new Date().getTime() - start;
judge.end({
domainId,
rid,
rid: rid.toHexString(),
status: STATUS.STATUS_ACCEPTED,
message: inspect(ret, false, 10, true),
judger: 1,
@@ -128,7 +128,7 @@ class SystemScriptHandler extends SystemHandler {
logger.error(err);
judge.end({
domainId,
rid,
rid: rid.toHexString(),
status: STATUS.STATUS_SYSTEM_ERROR,
message: `${err.message} \n${(err as any).params || []} \n${err.stack} `,
judger: 1,
+17 -160
View File
@@ -4,11 +4,16 @@ import { CredentialDeviceType } from '@simplewebauthn/types';
import type fs from 'fs';
import type { Dictionary, NumericDictionary } from 'lodash';
import type { Binary, FindCursor, ObjectId } from 'mongodb';
import type {
FileInfo, RecordPayload,
} from '@hydrooj/common/types';
import type { Context } from './context';
import type { DocStatusType } from './model/document';
import type { ProblemDoc } from './model/problem';
import type { Handler } from './service/server';
export * from '@hydrooj/common/types';
type document = typeof import('./model/document');
export interface System {
@@ -142,76 +147,6 @@ export interface BaseUser {
}
export type BaseUserDict = Record<number, BaseUser>;
export interface FileInfo {
/** storage path */
_id: string,
/** filename */
name: string,
/** file size (in bytes) */
size: number,
etag: string,
lastModified: Date,
}
export interface TestCaseConfig {
input: string;
output: string;
time?: string;
memory?: string;
score?: number;
}
export enum ProblemType {
Default = 'default',
SubmitAnswer = 'submit_answer',
Interactive = 'interactive',
Communication = 'communication',
Objective = 'objective',
Remote = 'remote_judge',
}
export enum SubtaskType {
min = 'min',
max = 'max',
sum = 'sum',
}
export interface SubtaskConfig {
time?: string;
memory?: string;
score?: number;
if?: number[];
id?: number;
type?: SubtaskType;
cases?: TestCaseConfig[];
}
export interface ProblemConfigFile {
type?: ProblemType;
subType?: string;
target?: string;
score?: number;
time?: string;
memory?: string;
filename?: string;
checker_type?: string;
checker?: string;
interactor?: string;
manager?: string;
num_processes?: number;
user_extra_files?: string[];
judge_extra_files?: string[];
detail?: boolean;
answers?: Record<string, [string | string[], number]>;
redirect?: string;
cases?: TestCaseConfig[];
subtasks?: SubtaskConfig[];
langs?: string[];
validator?: string;
time_limit_rate?: Record<string, number>;
memory_limit_rate?: Record<string, number>;
}
export interface ProblemConfig {
redirect?: [string, string];
count: number;
@@ -284,46 +219,11 @@ export interface ProblemStatusDoc extends StatusDocBase {
star?: boolean;
}
export interface TestCase {
id?: number;
subtaskId?: number;
score?: number;
time: number;
memory: number;
status: number;
message: string;
}
export interface RecordDoc {
export type RecordDoc = {
[K in keyof RecordPayload]: K extends 'hackTarget' | 'contest' ? ObjectId : RecordPayload[K];
} & {
_id: ObjectId;
domainId: string;
pid: number;
uid: number;
lang: string;
code: string;
score: number;
memory: number;
time: number;
judgeTexts: (string | JudgeMessage)[];
compilerTexts: string[];
testCases: Required<TestCase>[];
rejudged: boolean;
source?: string;
/** judge uid */
judger: number;
judgeAt: Date;
status: number;
progress?: number;
/** pretest */
input?: string;
/** hack target rid */
hackTarget?: ObjectId;
/** 0 if pretest&script */
contest?: ObjectId;
files?: Record<string, string>
subtasks?: Record<number, SubtaskResult>;
}
};
export interface RecordStatDoc {
_id: ObjectId;
@@ -335,24 +235,6 @@ export interface RecordStatDoc {
length: number;
lang: string;
}
export interface JudgeMeta {
problemOwner: number;
hackRejudge?: string;
rejudge?: boolean;
// FIXME stricter types
type?: string;
}
export interface JudgeRequest extends Omit<RecordDoc, '_id' | 'testCases'> {
priority: number;
type: 'judge' | 'generate';
rid: ObjectId;
config: ProblemConfigFile;
meta: JudgeMeta;
data: FileInfo[];
source: string;
trusted: boolean;
}
export interface ScoreboardNode {
type: 'string' | 'rank' | 'user' | 'email' | 'record' | 'records' | 'problem' | 'solved' | 'time' | 'total_score';
@@ -569,39 +451,6 @@ export interface Script {
validate: any,
}
export interface JudgeMessage {
message: string;
params?: string[];
stack?: string;
}
export interface SubtaskResult {
type: SubtaskType;
score: number;
status: number;
}
export interface JudgeResultBody {
key: string;
domainId: string;
rid: ObjectId;
judger?: number;
progress?: number;
addProgress?: number;
case?: TestCase;
cases?: TestCase[];
status?: number;
score?: number;
/** in miliseconds */
time?: number;
/** in kilobytes */
memory?: number;
message?: string | JudgeMessage;
compilerText?: string;
nop?: boolean;
subtasks?: Record<number, SubtaskResult>;
}
export interface Task {
_id: ObjectId;
type: string;
@@ -683,6 +532,13 @@ export interface ContestBalloonDoc {
sentAt?: Date;
}
export interface LockDoc {
_id: ObjectId;
key: string;
lockAt: Date;
daemonId: string;
}
declare module './service/db' {
interface Collections {
'blacklist': BlacklistDoc;
@@ -712,6 +568,7 @@ declare module './service/db' {
'opcount': OpCountDoc;
'schedule': Schedule;
'contest.balloon': ContestBalloonDoc;
'lock': LockDoc;
}
}
+5 -3
View File
@@ -1,7 +1,9 @@
import { load } from 'js-yaml';
import readYamlCases from '@hydrooj/utils/lib/cases';
import type { ProblemConfig, ProblemConfigFile } from '../interface';
import { normalizeSubtasks, parseMemoryMB, parseTimeMS } from '../utils';
import {
normalizeSubtasks, ProblemConfigFile, readYamlCases,
} from '@hydrooj/common';
import { parseMemoryMB, parseTimeMS } from '@hydrooj/utils';
import type { ProblemConfig } from '../interface';
export async function parseConfig(config: string | ProblemConfigFile = {}) {
const cfg: ProblemConfigFile = typeof config === 'string'
+2 -2
View File
@@ -2,9 +2,9 @@ import {
getScoreColor, STATUS, STATUS_CODES, STATUS_SHORT_TEXTS,
STATUS_TEXTS, USER_GENDER_FEMALE, USER_GENDER_ICONS, USER_GENDER_MALE,
USER_GENDER_OTHER, USER_GENDER_RANGE, USER_GENDERS,
} from '@hydrooj/utils/lib/status';
} from '@hydrooj/common';
export * from '@hydrooj/utils/lib/status';
export * from '@hydrooj/common/status';
export const PERM = {
PERM_NONE: 0n,
+2 -1
View File
@@ -8,11 +8,12 @@ import yaml from 'js-yaml';
import { pick } from 'lodash';
import { Filter, ObjectId } from 'mongodb';
import type { Readable } from 'stream';
import { ProblemConfigFile } from '@hydrooj/common';
import { Logger, size, streamToBuffer } from '@hydrooj/utils/lib/utils';
import { Context } from '../context';
import { FileUploadError, ProblemNotFoundError, ValidationError } from '../error';
import type {
Document, ProblemConfigFile, ProblemDict, ProblemStatusDoc, User,
Document, ProblemDict, ProblemStatusDoc, User,
} from '../interface';
import { parseConfig } from '../lib/testdataConfig';
import * as bus from '../service/bus';
+2 -3
View File
@@ -5,11 +5,10 @@ import {
Filter, FindOptions, MatchKeysAndValues,
ObjectId, OnlyFieldsOfType, PushOperator, UpdateFilter,
} from 'mongodb';
import { ProblemConfigFile } from '@hydrooj/common';
import { Context } from '../context';
import { ProblemNotFoundError } from '../error';
import {
JudgeMeta, ProblemConfigFile, RecordDoc,
} from '../interface';
import { JudgeMeta, RecordDoc } from '../interface';
import db from '../service/db';
import { MaybeArray, NumberKeys } from '../typeutils';
import { ArgMethod, buildProjection, Time } from '../utils';
+2 -2
View File
@@ -4,8 +4,8 @@ import * as cordis from 'cordis';
import yaml from 'js-yaml';
import { Dictionary } from 'lodash';
import moment from 'moment-timezone';
import { LangConfig, parseLang } from '@hydrooj/utils/lib/lang';
import { retry } from '@hydrooj/utils/lib/utils';
import { LangConfig, parseLang } from '@hydrooj/common';
import { retry } from '@hydrooj/utils';
import { Context, Service } from '../context';
import { Setting as _Setting } from '../interface';
import { Logger } from '../logger';