mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
core: rollback to document.count() for better performance
https://www.mongodb.com/docs/manual/reference/command/count/#mongodb-dbcommand-dbcmd.count:~:text=This%20loss%20of,a%20query%20document.
This commit is contained in:
5 files changed
+23
-18
No files matched your search
@@ -39,11 +39,11 @@ class AdminStatusHandler extends Handler {
|
||||
const record = await RecordModel.stat();
|
||||
const status = await getStatus();
|
||||
const [domainCount, userCount, problemCount, discussionCount, recordCount] = await Promise.all([
|
||||
DomainModel.coll.countDocuments(),
|
||||
UserModel.coll.countDocuments(),
|
||||
DocumentModel.coll.countDocuments({ docType: DocumentModel.TYPE_PROBLEM }),
|
||||
DocumentModel.coll.countDocuments({ docType: DocumentModel.TYPE_DISCUSSION }),
|
||||
RecordModel.coll.countDocuments(),
|
||||
DomainModel.coll.count(),
|
||||
UserModel.coll.count(),
|
||||
DocumentModel.coll.count({ docType: DocumentModel.TYPE_PROBLEM }),
|
||||
DocumentModel.coll.count({ docType: DocumentModel.TYPE_DISCUSSION }),
|
||||
RecordModel.coll.count(),
|
||||
]);
|
||||
this.response.body = {
|
||||
record,
|
||||
|
||||
@@ -12,8 +12,9 @@ async function paginate<T>(
|
||||
filter = cursor[key];
|
||||
break;
|
||||
}
|
||||
const coll = db.collection(cursor.namespace.collection as any);
|
||||
const [count, pageDocs] = await Promise.all([
|
||||
db.collection(cursor.namespace.collection as any).countDocuments(filter),
|
||||
Object.keys(filter).length ? coll.count(filter) : coll.countDocuments(filter),
|
||||
cursor.skip((page - 1) * pageSize).limit(pageSize).toArray(),
|
||||
]);
|
||||
const numPages = Math.floor((count + pageSize - 1) / pageSize);
|
||||
|
||||
@@ -986,6 +986,7 @@ export const statusText = (tdoc: Tdoc, tsdoc?: any) => (
|
||||
|
||||
global.Hydro.model.contest = {
|
||||
RULES,
|
||||
buildContestRule,
|
||||
add,
|
||||
getListStatus,
|
||||
getMultiStatus,
|
||||
|
||||
@@ -49,14 +49,17 @@ export default class RecordModel {
|
||||
|
||||
@ArgMethod
|
||||
static async stat(domainId?: string) {
|
||||
// INFO:
|
||||
// using .count() for a much better performace
|
||||
// @see https://www.mongodb.com/docs/manual/reference/command/count/
|
||||
const [d5min, d1h, day, week, month, year, total] = await Promise.all([
|
||||
RecordModel.coll.countDocuments({ _id: { $gte: Time.getObjectID(moment().add(-5, 'minutes')) }, ...domainId ? { domainId } : {} }),
|
||||
RecordModel.coll.countDocuments({ _id: { $gte: Time.getObjectID(moment().add(-1, 'hour')) }, ...domainId ? { domainId } : {} }),
|
||||
RecordModel.coll.countDocuments({ _id: { $gte: Time.getObjectID(moment().add(-1, 'day')) }, ...domainId ? { domainId } : {} }),
|
||||
RecordModel.coll.countDocuments({ _id: { $gte: Time.getObjectID(moment().add(-1, 'week')) }, ...domainId ? { domainId } : {} }),
|
||||
RecordModel.coll.countDocuments({ _id: { $gte: Time.getObjectID(moment().add(-1, 'month')) }, ...domainId ? { domainId } : {} }),
|
||||
RecordModel.coll.countDocuments({ _id: { $gte: Time.getObjectID(moment().add(-1, 'year')) }, ...domainId ? { domainId } : {} }),
|
||||
RecordModel.coll.countDocuments(domainId ? { domainId } : {}),
|
||||
RecordModel.coll.find({ _id: { $gte: Time.getObjectID(moment().add(-5, 'minutes')) }, ...domainId ? { domainId } : {} }).count(),
|
||||
RecordModel.coll.find({ _id: { $gte: Time.getObjectID(moment().add(-1, 'hour')) }, ...domainId ? { domainId } : {} }).count(),
|
||||
RecordModel.coll.find({ _id: { $gte: Time.getObjectID(moment().add(-1, 'day')) }, ...domainId ? { domainId } : {} }).count(),
|
||||
RecordModel.coll.find({ _id: { $gte: Time.getObjectID(moment().add(-1, 'week')) }, ...domainId ? { domainId } : {} }).count(),
|
||||
RecordModel.coll.find({ _id: { $gte: Time.getObjectID(moment().add(-1, 'month')) }, ...domainId ? { domainId } : {} }).count(),
|
||||
RecordModel.coll.find({ _id: { $gte: Time.getObjectID(moment().add(-1, 'year')) }, ...domainId ? { domainId } : {} }).count(),
|
||||
RecordModel.coll.find(domainId ? { domainId } : {}).count(),
|
||||
]);
|
||||
return {
|
||||
d5min, d1h, day, week, month, year, total,
|
||||
|
||||
@@ -18,11 +18,11 @@ export async function feedback(): Promise<[string, StatusUpdate]> {
|
||||
const [mid, $update, inf] = await sysinfo.update();
|
||||
const [installId, name, url] = system.getMany(['installid', 'server.name', 'server.url']);
|
||||
const [domainCount, userCount, problemCount, discussionCount, recordCount] = await Promise.all([
|
||||
domain.coll.countDocuments(),
|
||||
user.coll.countDocuments(),
|
||||
document.coll.countDocuments({ docType: document.TYPE_PROBLEM }),
|
||||
document.coll.countDocuments({ docType: document.TYPE_DISCUSSION }),
|
||||
record.coll.countDocuments(),
|
||||
domain.coll.count(),
|
||||
user.coll.count(),
|
||||
document.coll.count({ docType: document.TYPE_PROBLEM }),
|
||||
document.coll.count({ docType: document.TYPE_DISCUSSION }),
|
||||
record.coll.count(),
|
||||
]);
|
||||
const info: Record<string, any> = {
|
||||
mid: mid.toString(),
|
||||
|
||||
Reference in new issue
Block a user