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'
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() {
return UserProfile.find({
$where: function () {
for (const key in this.progress) {
if (this.progress[key].completed === true) {
return true
}
}
return false
return UserProfile.aggregate([
{
$project: {
_id: 0,
progress: { $objectToArray: '$progress' },
},
},
}).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() {