mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +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
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user