core: add storage calc tool

This commit is contained in:
undefined committed 2021-08-09 16:12:23 +08:00
1 parent ceea8b5bdd
commit 63bf550311
3 files changed
+43 -1

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.32.12",
"version": "2.32.13",
"bin": "bin/hydrooj.js",
"main": "src/loader",
"module": "src/loader",
+1
View File
@@ -31,6 +31,7 @@ export const builtinHandler = [
export const builtinScript = [
'rating', 'problemStat', 'blacklist', 'deleteUser', 'updateFilelist',
'storageUsage',
];
function getFiles(folder: string, base = ''): string[] {
@@ -0,0 +1,41 @@
/* eslint-disable no-await-in-loop */
import { STATUS } from '../model/builtin';
import storage from '../model/storage';
export const description = 'Calculate storage usage';
export async function run(_: {}, report: Function) {
const cursor = storage.coll.find({ path: { $regex: /^problem\//i } }).sort({ path: 1 });
const count = await cursor.count();
report({ message: `Total ${count} files` });
let current: string;
let memory = 0;
let start = new Date().getTime();
// eslint-disable-next-line no-constant-condition
while (true) {
const doc = await cursor.next();
const id = doc?.path.split('problem/')[1].split('/')[0]!;
if (!current) current = id;
if (!doc || current !== id) {
const end = new Date().getTime();
report({
case: {
message: current,
memory: Math.floor(memory / 102.4) / 10,
time: end - start,
status: STATUS.STATUS_ACCEPTED,
score: 0,
},
});
start = end;
memory = 0;
current = id;
if (!doc) break;
}
memory += doc.size || 0;
}
}
export const validate = {};
global.Hydro.script.storageUsage = { run, description, validate };