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

@@ -1,11 +1,12 @@
/* eslint-disable object-shorthand */
import { Desc } from '../constants.js'
import { Command } from '@linux-odyssey/models'
import { errorCommands } from '../models/commands.js'
// eslint-disable-next-line import/prefer-default-export
export async function commandListController(req, res) {
const { nextKey } = req.query
const itemsPerPage = 250
const itemsPerPage = 100
try {
// const totalCommands = await Command.countDocuments({
@@ -20,10 +21,14 @@ export async function commandListController(req, res) {
// const pageNumber = req.query
const payload = await errorCommands({ nextKey, itemsPerPage })
const payload = await errorCommands({
nextKey,
itemsPerPage,
order: new Desc(),
})
const newNextKey = payload[payload.length - 1]?._id
res.render('commands', { payload, nextKey: newNextKey })
res.render('commands', { errorCommands: payload, nextKey: newNextKey })
} catch (error) {
res.status(500).send('Error fetching data')
}
@@ -1,18 +1,24 @@
import { Desc } from '../constants.js'
import { sessionCount, sessionList, sessionDetail } from '../models/sessions.js'
export async function sessionListController(req, res) {
const { nextKey } = req.query
const itemsPerPage = 100
const itemsPerPage = 10
try {
// const totalSessions = await sessionCount()
// const maxPages = Math.ceil(totalSessions / itemsPerPage)
const sessions = await sessionList({ nextKey, itemsPerPage })
const sessions = await sessionList({
nextKey,
itemsPerPage,
order: new Desc(),
})
const newNextKey = sessions[sessions.length - 1]?._id
res.render('sessions', { sessions, nextKey: newNextKey })
} catch (error) {
console.log(error)
res.status(500).send('Error fetching session data')
}
}
+1 -8
View File
@@ -6,9 +6,6 @@ export async function userListController(req, res) {
const itemsPerPage = 50
try {
// const totalUsers = await userCount()
// const maxPages = Math.ceil(totalUsers / itemsPerPage)
const users = await userList({
nextKey,
itemsPerPage,
@@ -16,11 +13,7 @@ export async function userListController(req, res) {
})
const newNextKey = users[users.length - 1]?._id
res.render('users', {
users,
nextKey: newNextKey,
// maxPages: Array.from({ length: maxPages }, (_, index) => index + 1),
})
res.render('users', { users, nextKey: newNextKey })
} catch (error) {
console.error(error)
res.status(500).send('Error fetching user data')
+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',