mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
utils: 提取公共模块
This commit is contained in:
1 parent
64fec8a407
commit
5cc63bcb22
16 files changed
+356
-427
No files matched your search
@@ -14,19 +14,6 @@ import { STATUS_COMPILE_ERROR, STATUS_SYSTEM_ERROR } from '../status';
|
||||
import readCases from '../cases';
|
||||
import judge from '../judge';
|
||||
|
||||
function formatStr(content: string, ...args) {
|
||||
let result = content;
|
||||
if (args.length > 0) {
|
||||
for (const key in args) {
|
||||
if (args[key] !== undefined) {
|
||||
const reg = new RegExp(`(\\{${key}\\})`, 'g');
|
||||
result = result.replace(reg, args[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
class JudgeTask {
|
||||
stat: Record<string, Date>;
|
||||
session: any;
|
||||
@@ -131,14 +118,14 @@ class JudgeTask {
|
||||
delete data.message;
|
||||
}
|
||||
if (data.judge_text && typeof data.judge_text !== 'string') {
|
||||
data.judge_text = formatStr(data.judge_text.message, ...data.judge_text.params);
|
||||
data.judge_text = data.judge_text.message.format(data.judge_text.params);
|
||||
}
|
||||
if (data.case?.message !== undefined) {
|
||||
data.case.judge_text = data.case.message;
|
||||
delete data.case.message;
|
||||
}
|
||||
if (data.case?.judge_text && typeof data.case?.judge_text !== 'string') {
|
||||
data.case.judge_text = formatStr(data.case.judge_text.message, ...data.case.judge_text.params);
|
||||
data.case.judge_text = data.case.judge_text.message.format(data.case.judge_text.params);
|
||||
}
|
||||
data.key = 'next';
|
||||
data.tag = this.tag;
|
||||
|
||||
@@ -8,6 +8,7 @@ import fs from 'fs-extra';
|
||||
import { noop } from 'lodash';
|
||||
import { Logger } from 'hydrooj/dist/logger';
|
||||
import * as monitor from 'hydrooj/dist/service/monitor';
|
||||
import * as sysinfo from './sysinfo';
|
||||
import * as tmpfs from './tmpfs';
|
||||
import { FormatError, CompileError, SystemError } from './error';
|
||||
import { STATUS_COMPILE_ERROR, STATUS_SYSTEM_ERROR } from './status';
|
||||
@@ -36,7 +37,6 @@ async function postInit() {
|
||||
// Only start a single daemon
|
||||
if (!global.Hydro.isFirstWorker) return;
|
||||
const judge = require('./judge');
|
||||
const sysinfo = require('./sysinfo');
|
||||
|
||||
const { task, system } = global.Hydro.model;
|
||||
const { storage } = global.Hydro.service;
|
||||
|
||||
@@ -1,23 +1,12 @@
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import systeminformation from 'systeminformation';
|
||||
import { noop } from 'lodash';
|
||||
import { get as _get } from '@hydrooj/utils/lib/sysinfo';
|
||||
import { judge } from './judge/run';
|
||||
import * as tmpfs from './tmpfs';
|
||||
import { getConfig } from './config';
|
||||
|
||||
function size(s: number, base = 1) {
|
||||
s *= base;
|
||||
const unit = 1024;
|
||||
const unitNames = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
||||
for (const unitName of unitNames) {
|
||||
if (s < unit) return `${Math.round(s * 10) / 10} ${unitName}`;
|
||||
s /= unit;
|
||||
}
|
||||
return `${Math.round(s * unit)} ${unitNames[unitNames.length - 1]}`;
|
||||
}
|
||||
|
||||
const cache: any = {};
|
||||
export { update } from '@hydrooj/utils/lib/sysinfo';
|
||||
|
||||
async function stackSize() {
|
||||
let output = '';
|
||||
@@ -35,8 +24,8 @@ int main(){
|
||||
main();
|
||||
}`,
|
||||
config: {
|
||||
time: 3000,
|
||||
memory: 256,
|
||||
time: '3s',
|
||||
memory: '256m',
|
||||
},
|
||||
stat: {},
|
||||
clean: [],
|
||||
@@ -61,38 +50,10 @@ int main(){
|
||||
}
|
||||
|
||||
export async function get() {
|
||||
const [
|
||||
Cpu, Memory, OsInfo,
|
||||
CurrentLoad, CpuFlags, CpuTemp,
|
||||
Battery, stack,
|
||||
] = await Promise.all([
|
||||
systeminformation.cpu(),
|
||||
systeminformation.mem(),
|
||||
systeminformation.osInfo(),
|
||||
systeminformation.currentLoad(),
|
||||
systeminformation.cpuFlags(),
|
||||
systeminformation.cpuTemperature(),
|
||||
systeminformation.battery(),
|
||||
stackSize(),
|
||||
]);
|
||||
const cpu = `${Cpu.manufacturer} ${Cpu.brand}`;
|
||||
const memory = `${size(Memory.active)}/${size(Memory.total)}`;
|
||||
const osinfo = `${OsInfo.distro} ${OsInfo.release} ${OsInfo.codename} ${OsInfo.kernel} ${OsInfo.arch}`;
|
||||
const load = `${CurrentLoad.avgLoad}`;
|
||||
const flags = CpuFlags;
|
||||
let battery;
|
||||
if (!Battery.hasBattery) battery = 'No battery';
|
||||
else battery = `${Battery.type} ${Battery.model} ${Battery.percent}%${Battery.isCharging ? ' Charging' : ''}`;
|
||||
const mid = OsInfo.serial;
|
||||
cache.cpu = cpu;
|
||||
cache.osinfo = osinfo;
|
||||
cache.flags = flags;
|
||||
cache.mid = mid;
|
||||
cache.stack = stack;
|
||||
global.reqCount = 0;
|
||||
return {
|
||||
mid, cpu, memory, osinfo, load, flags, CpuTemp, battery, stack, reqCount: 0,
|
||||
};
|
||||
const info = await _get();
|
||||
// @ts-ignore
|
||||
info.stack = await stackSize();
|
||||
return info;
|
||||
}
|
||||
|
||||
declare global {
|
||||
@@ -102,31 +63,3 @@ declare global {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function update() {
|
||||
const [Memory, CurrentLoad, CpuTemp, Battery] = await Promise.all([
|
||||
systeminformation.mem(),
|
||||
systeminformation.currentLoad(),
|
||||
systeminformation.cpuTemperature(),
|
||||
systeminformation.battery(),
|
||||
]);
|
||||
const {
|
||||
mid, cpu, osinfo, flags, stack,
|
||||
} = cache;
|
||||
const memory = `${size(Memory.active)}/${size(Memory.total)}`;
|
||||
const load = `${CurrentLoad.avgLoad}`;
|
||||
let battery;
|
||||
if (!Battery.hasBattery) battery = 'No battery';
|
||||
else battery = `${Battery.type} ${Battery.model} ${Battery.percent}%${Battery.isCharging ? ' Charging' : ''}`;
|
||||
const reqCount = global.reqCount;
|
||||
global.reqCount = 0;
|
||||
return [
|
||||
mid,
|
||||
{
|
||||
memory, load, battery, CpuTemp, reqCount,
|
||||
},
|
||||
{
|
||||
mid, cpu, memory, osinfo, load, flags, battery, CpuTemp, stack, reqCount,
|
||||
},
|
||||
];
|
||||
}
|
||||
@@ -5,34 +5,9 @@ import _ from 'lodash';
|
||||
import { EventEmitter } from 'events';
|
||||
import { FormatError } from './error';
|
||||
|
||||
const TIME_RE = /^([0-9]+(?:\.[0-9]*)?)([mu]?)s?$/i;
|
||||
const TIME_UNITS = { '': 1000, m: 1, u: 0.001 };
|
||||
const MEMORY_RE = /^([0-9]+(?:\.[0-9]*)?)([kmg])b?$/i;
|
||||
const MEMORY_UNITS = { k: 1 / 1024, m: 1, g: 1024 };
|
||||
const EMPTY_STR = /^[ \r\n\t]*$/i;
|
||||
|
||||
export const cmd = parse;
|
||||
export function noop() { }
|
||||
|
||||
export function parseTimeMS(val: string | number) {
|
||||
if (typeof val === 'number') return val;
|
||||
const match = TIME_RE.exec(val);
|
||||
if (!match) throw new FormatError('Error parsing time: {0}', [val]);
|
||||
return Math.floor(parseFloat(match[1]) * TIME_UNITS[match[2]]);
|
||||
}
|
||||
|
||||
export function parseMemoryMB(val: string | number) {
|
||||
if (typeof val === 'number') return val;
|
||||
const match = MEMORY_RE.exec(val);
|
||||
if (!match) throw new FormatError('Error parsing memory: {0}', [val]);
|
||||
return Math.floor(parseFloat(match[1]) * MEMORY_UNITS[match[2]]);
|
||||
}
|
||||
|
||||
export function sleep(timeout: number) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, timeout);
|
||||
});
|
||||
}
|
||||
|
||||
export function parseFilename(filePath: string) {
|
||||
const t = filePath.split('/');
|
||||
@@ -112,3 +87,5 @@ export function ensureFile(folder: string) {
|
||||
return f;
|
||||
};
|
||||
}
|
||||
|
||||
export * from '@hydrooj/utils/lib/utils';
|
||||
Reference in new issue
Block a user