fix: analytics completed rate

This commit is contained in:
Chen, Tai-Lin committed 2023-12-11 01:09:06 +08:00
1 parent ac31ca8659
commit a578d4f910
1 file changed
+48 -10
+48 -10
View File
@@ -3,20 +3,58 @@
import { UserProfile } from '@linux-odyssey/models' import { UserProfile } from '@linux-odyssey/models'
export function totalQuests() { export function totalQuests() {
return UserProfile.find().count() return UserProfile.aggregate([
{
$project: {
_id: 0,
progress: { $objectToArray: '$progress' },
},
},
{
$unwind: '$progress',
},
{
$group: {
_id: null,
totalCount: { $sum: 1 },
},
},
]).then((results) => {
if (results.length === 0) {
throw new Error('No documents found')
}
return results[0].totalCount
})
} }
export function totalCompleted() { export function totalCompleted() {
return UserProfile.find({ return UserProfile.aggregate([
$where: function () { {
for (const key in this.progress) { $project: {
if (this.progress[key].completed === true) { _id: 0,
return true progress: { $objectToArray: '$progress' },
} },
}
return false
}, },
}).count() {
$unwind: '$progress',
},
{
$match: {
'progress.v.completed': true,
},
},
{
$group: {
_id: null,
totalCount: { $sum: 1 },
},
},
]).then((results) => {
if (results.length === 0) {
throw new Error('No documents found')
}
return results[0].totalCount
})
} }
export function userProfileList() { export function userProfileList() {