feat: finish errorCommands pagination

This commit is contained in:
Chen, Tai-Lin committed 2023-12-21 16:31:34 +08:00
1 parent 8c634c8115
commit bc0055781e
6 files changed
+59 -41

No files matched your search

+33 -18
View File
@@ -1,15 +1,16 @@
import { Command } from '@linux-odyssey/models'
import mongoose from 'mongoose'
import { Command } from '@linux-odyssey/models'
// eslint-disable-next-line import/prefer-default-export
export async function errorCommands({ nextKey, itemsPerPage }) {
const key = new mongoose.Types.ObjectId(nextKey)
const matchStage = nextKey ? { _id: { $lt: key } } : {}
export async function errorCommands({ nextKey, itemsPerPage, order }) {
let matchStage = {}
if (nextKey) {
const key = new mongoose.Types.ObjectId(nextKey)
matchStage = { _id: order.matchStage(key) }
}
console.log(matchStage, itemsPerPage, nextKey, order)
const commands = await Command.aggregate([
{
$match: matchStage,
},
{ $match: matchStage },
{
$match: {
$and: [
@@ -17,20 +18,34 @@ export async function errorCommands({ nextKey, itemsPerPage }) {
{ error: { $ne: null } },
{ error: { $ne: '' } },
],
}
}
},
},
{
$lookup: {
from: 'sessions', // 替換成 'session' 對應的集合名稱
localField: 'session', // 替換成 'session' 對應的字段名稱
foreignField: '_id',
as: 'session',
},
},
{ $unwind: '$session' },
{
$lookup: {
from: 'users', // 替換成 'user' 對應的集合名稱
localField: 'session.user', // 替換成 'user' 對應的字段名稱
foreignField: '_id',
as: 'session.user',
},
},
{ $unwind: '$session.user' },
{ $sort: { _id: order.order() } },
{ $limit: itemsPerPage },
])
.populate({
path: 'session',
populate: { path: 'user' },
})
.skip(skipAmount)
.limit(itemsPerPage)
.exec()
return commands.map(({ command, error, createdAt, session }) => {
return commands.map(({ _id, command, error, createdAt, session }) => {
const { user, quest } = session
return {
_id,
command: command.slice(0, 20),
error: error.slice(0, 50),
createdAt: createdAt?.toLocaleString(),
+8 -6
View File
@@ -1,5 +1,5 @@
import { Command, Session } from '@linux-odyssey/models'
import mongoose from 'mongoose'
import { Command, Session } from '@linux-odyssey/models'
export function sessionCount() {
return Session.find().count()
@@ -31,10 +31,12 @@ function formatTime(time) {
return `${hours}h ${minutes % 60}m ${seconds % 60}s`
}
export async function sessionList({ nextKey, itemsPerPage }) {
const key = new mongoose.Types.ObjectId(nextKey)
const matchStage = nextKey ? { _id: { $lt: key } } : {}
export async function sessionList({ nextKey, itemsPerPage, order }) {
let matchStage = {}
if (nextKey) {
const key = new mongoose.Types.ObjectId(nextKey)
matchStage = { user: order.matchStage(key) }
}
const sessions = await Session.aggregate([
{
$match: matchStage,
@@ -56,7 +58,7 @@ export async function sessionList({ nextKey, itemsPerPage }) {
as: 'commands',
},
},
{ $sort: { _id: -1 } },
{ $sort: { _id: order.order() } },
{ $limit: itemsPerPage },
])
+1 -4
View File
@@ -13,7 +13,6 @@ export function userCount() {
}
export async function userList({ nextKey, itemsPerPage, order }) {
console.log(order)
let matchStage = {}
if (nextKey) {
const key = new mongoose.Types.ObjectId(nextKey)
@@ -21,9 +20,7 @@ export async function userList({ nextKey, itemsPerPage, order }) {
}
console.log(matchStage)
const users = await Session.aggregate([
{
$match: matchStage,
},
{ $match: matchStage },
{
$group: {
_id: '$user',