mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 09:46:28 +08:00
core: optimize storageUsage calc
This commit is contained in:
1 parent
0af7299c31
commit
82419e3ce2
1 file changed
+16
-22
@@ -4,35 +4,29 @@ import { STATUS } from '../model/builtin';
|
|||||||
import storage from '../model/storage';
|
import storage from '../model/storage';
|
||||||
|
|
||||||
export async function run(_, report) {
|
export async function run(_, report) {
|
||||||
const cursor = storage.coll.find({ path: { $regex: /^problem\//i } }).sort({ path: 1 });
|
let totalProblemSize = 0;
|
||||||
const count = await cursor.count();
|
const m = await storage.coll.aggregate([
|
||||||
report({ message: `Total ${count} files` });
|
{ $match: { path: { $regex: /^problem\//i } } },
|
||||||
let current = '';
|
{ $addFields: { domainId: { $arrayElemAt: [{ $split: ['$path', '/'] }, 1] } } },
|
||||||
let memory = 0;
|
{ $group: { _id: '$domainId', size: { $sum: '$size' }, count: { $sum: 1 } } },
|
||||||
let start = new Date().getTime();
|
{ $match: { size: { $gt: 10 * 1024 * 1024 } } },
|
||||||
// eslint-disable-next-line no-constant-condition
|
{ $sort: { size: -1 } },
|
||||||
while (true) {
|
]).toArray();
|
||||||
const doc = await cursor.next();
|
for (let i = 0; i < m.length; i++) {
|
||||||
const id = doc?.path.split('problem/')[1].split('/')[0]!;
|
const message = m[i]._id;
|
||||||
if (!current) current = id;
|
|
||||||
if (!doc || current !== id) {
|
|
||||||
const end = new Date().getTime();
|
|
||||||
report({
|
report({
|
||||||
case: {
|
case: {
|
||||||
message: current,
|
id: i + 1,
|
||||||
memory: Math.floor(memory / 102.4) / 10,
|
message,
|
||||||
time: end - start,
|
memory: Math.floor(m[i].size / 102.4) / 10,
|
||||||
|
time: 0,
|
||||||
status: STATUS.STATUS_ACCEPTED,
|
status: STATUS.STATUS_ACCEPTED,
|
||||||
score: 0,
|
score: 0,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
start = end;
|
totalProblemSize += m[i].size;
|
||||||
memory = 0;
|
|
||||||
current = id;
|
|
||||||
if (!doc) break;
|
|
||||||
}
|
|
||||||
memory += doc.size || 0;
|
|
||||||
}
|
}
|
||||||
|
report({ message: `Problem total ${totalProblemSize / 1024 / 1024} MB` });
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user