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
+57 -39

No files matched your search

@@ -1,11 +1,12 @@
/* eslint-disable object-shorthand */ /* eslint-disable object-shorthand */
import { Desc } from '../constants.js'
import { Command } from '@linux-odyssey/models' import { Command } from '@linux-odyssey/models'
import { errorCommands } from '../models/commands.js' import { errorCommands } from '../models/commands.js'
// eslint-disable-next-line import/prefer-default-export // eslint-disable-next-line import/prefer-default-export
export async function commandListController(req, res) { export async function commandListController(req, res) {
const { nextKey } = req.query const { nextKey } = req.query
const itemsPerPage = 250 const itemsPerPage = 100
try { try {
// const totalCommands = await Command.countDocuments({ // const totalCommands = await Command.countDocuments({
@@ -20,10 +21,14 @@ export async function commandListController(req, res) {
// const pageNumber = req.query // 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 const newNextKey = payload[payload.length - 1]?._id
res.render('commands', { payload, nextKey: newNextKey }) res.render('commands', { errorCommands: payload, nextKey: newNextKey })
} catch (error) { } catch (error) {
res.status(500).send('Error fetching data') res.status(500).send('Error fetching data')
} }
@@ -1,18 +1,24 @@
import { Desc } from '../constants.js'
import { sessionCount, sessionList, sessionDetail } from '../models/sessions.js' import { sessionCount, sessionList, sessionDetail } from '../models/sessions.js'
export async function sessionListController(req, res) { export async function sessionListController(req, res) {
const { nextKey } = req.query const { nextKey } = req.query
const itemsPerPage = 100 const itemsPerPage = 10
try { try {
// const totalSessions = await sessionCount() // const totalSessions = await sessionCount()
// const maxPages = Math.ceil(totalSessions / itemsPerPage) // 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 const newNextKey = sessions[sessions.length - 1]?._id
res.render('sessions', { sessions, nextKey: newNextKey }) res.render('sessions', { sessions, nextKey: newNextKey })
} catch (error) { } catch (error) {
console.log(error)
res.status(500).send('Error fetching session data') 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 const itemsPerPage = 50
try { try {
// const totalUsers = await userCount()
// const maxPages = Math.ceil(totalUsers / itemsPerPage)
const users = await userList({ const users = await userList({
nextKey, nextKey,
itemsPerPage, itemsPerPage,
@@ -16,11 +13,7 @@ export async function userListController(req, res) {
}) })
const newNextKey = users[users.length - 1]?._id const newNextKey = users[users.length - 1]?._id
res.render('users', { res.render('users', { users, nextKey: newNextKey })
users,
nextKey: newNextKey,
// maxPages: Array.from({ length: maxPages }, (_, index) => index + 1),
})
} catch (error) { } catch (error) {
console.error(error) console.error(error)
res.status(500).send('Error fetching user data') res.status(500).send('Error fetching user data')
+32 -17
View File
@@ -1,15 +1,16 @@
import { Command } from '@linux-odyssey/models'
import mongoose from 'mongoose' import mongoose from 'mongoose'
import { Command } from '@linux-odyssey/models'
// eslint-disable-next-line import/prefer-default-export // eslint-disable-next-line import/prefer-default-export
export async function errorCommands({ nextKey, itemsPerPage }) { export async function errorCommands({ nextKey, itemsPerPage, order }) {
let matchStage = {}
if (nextKey) {
const key = new mongoose.Types.ObjectId(nextKey) const key = new mongoose.Types.ObjectId(nextKey)
const matchStage = nextKey ? { _id: { $lt: key } } : {} matchStage = { _id: order.matchStage(key) }
}
console.log(matchStage, itemsPerPage, nextKey, order)
const commands = await Command.aggregate([ const commands = await Command.aggregate([
{ { $match: matchStage },
$match: matchStage,
},
{ {
$match: { $match: {
$and: [ $and: [
@@ -17,20 +18,34 @@ export async function errorCommands({ nextKey, itemsPerPage }) {
{ error: { $ne: null } }, { error: { $ne: null } },
{ error: { $ne: '' } }, { 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 }, { $limit: itemsPerPage },
]) ])
.populate({
path: 'session', return commands.map(({ _id, command, error, createdAt, session }) => {
populate: { path: 'user' },
})
.skip(skipAmount)
.limit(itemsPerPage)
.exec()
return commands.map(({ command, error, createdAt, session }) => {
const { user, quest } = session const { user, quest } = session
return { return {
_id,
command: command.slice(0, 20), command: command.slice(0, 20),
error: error.slice(0, 50), error: error.slice(0, 50),
createdAt: createdAt?.toLocaleString(), createdAt: createdAt?.toLocaleString(),
+7 -5
View File
@@ -1,5 +1,5 @@
import { Command, Session } from '@linux-odyssey/models'
import mongoose from 'mongoose' import mongoose from 'mongoose'
import { Command, Session } from '@linux-odyssey/models'
export function sessionCount() { export function sessionCount() {
return Session.find().count() return Session.find().count()
@@ -31,10 +31,12 @@ function formatTime(time) {
return `${hours}h ${minutes % 60}m ${seconds % 60}s` return `${hours}h ${minutes % 60}m ${seconds % 60}s`
} }
export async function sessionList({ nextKey, itemsPerPage }) { export async function sessionList({ nextKey, itemsPerPage, order }) {
let matchStage = {}
if (nextKey) {
const key = new mongoose.Types.ObjectId(nextKey) const key = new mongoose.Types.ObjectId(nextKey)
const matchStage = nextKey ? { _id: { $lt: key } } : {} matchStage = { user: order.matchStage(key) }
}
const sessions = await Session.aggregate([ const sessions = await Session.aggregate([
{ {
$match: matchStage, $match: matchStage,
@@ -56,7 +58,7 @@ export async function sessionList({ nextKey, itemsPerPage }) {
as: 'commands', as: 'commands',
}, },
}, },
{ $sort: { _id: -1 } }, { $sort: { _id: order.order() } },
{ $limit: itemsPerPage }, { $limit: itemsPerPage },
]) ])
+1 -4
View File
@@ -13,7 +13,6 @@ export function userCount() {
} }
export async function userList({ nextKey, itemsPerPage, order }) { export async function userList({ nextKey, itemsPerPage, order }) {
console.log(order)
let matchStage = {} let matchStage = {}
if (nextKey) { if (nextKey) {
const key = new mongoose.Types.ObjectId(nextKey) const key = new mongoose.Types.ObjectId(nextKey)
@@ -21,9 +20,7 @@ export async function userList({ nextKey, itemsPerPage, order }) {
} }
console.log(matchStage) console.log(matchStage)
const users = await Session.aggregate([ const users = await Session.aggregate([
{ { $match: matchStage },
$match: matchStage,
},
{ {
$group: { $group: {
_id: '$user', _id: '$user',