mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
refactor: add Pagination class
This commit is contained in:
1 parent
19cd10ab7b
commit
68db16419d
9 files changed
+99
-89
No files matched your search
@@ -1,23 +0,0 @@
|
|||||||
/* eslint-disable class-methods-use-this */
|
|
||||||
// eslint-disable-next-line max-classes-per-file
|
|
||||||
class Order {}
|
|
||||||
|
|
||||||
export class Asc extends Order {
|
|
||||||
matchStage(nextKey) {
|
|
||||||
return { $gt: nextKey }
|
|
||||||
}
|
|
||||||
|
|
||||||
order() {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Desc extends Order {
|
|
||||||
matchStage(nextKey) {
|
|
||||||
return { $lt: nextKey }
|
|
||||||
}
|
|
||||||
|
|
||||||
order() {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
/* eslint-disable object-shorthand */
|
/* eslint-disable object-shorthand */
|
||||||
import { Desc } from '../constants.js'
|
import { Desc } from '../models/pagination.js'
|
||||||
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
|
||||||
@@ -9,23 +8,7 @@ export async function commandListController(req, res) {
|
|||||||
const itemsPerPage = 100
|
const itemsPerPage = 100
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// const totalCommands = await Command.countDocuments({
|
const payload = await errorCommands(new Desc(itemsPerPage, nextKey))
|
||||||
// $and: [
|
|
||||||
// { error: { $exists: true } },
|
|
||||||
// { error: { $ne: null } },
|
|
||||||
// { error: { $ne: '' } },
|
|
||||||
// ],
|
|
||||||
// })
|
|
||||||
|
|
||||||
// const maxPages = Math.ceil(totalCommands / itemsPerPage)
|
|
||||||
|
|
||||||
// const pageNumber = req.query
|
|
||||||
|
|
||||||
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', { errorCommands: payload, nextKey: newNextKey })
|
res.render('commands', { errorCommands: payload, nextKey: newNextKey })
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Desc } from '../constants.js'
|
import { Desc } from '../models/pagination.js'
|
||||||
import { sessionCount, sessionList, sessionDetail } from '../models/sessions.js'
|
import { 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
|
||||||
@@ -9,11 +9,7 @@ export async function sessionListController(req, res) {
|
|||||||
// const totalSessions = await sessionCount()
|
// const totalSessions = await sessionCount()
|
||||||
// const maxPages = Math.ceil(totalSessions / itemsPerPage)
|
// const maxPages = Math.ceil(totalSessions / itemsPerPage)
|
||||||
|
|
||||||
const sessions = await sessionList({
|
const sessions = await sessionList(new Desc(itemsPerPage, nextKey))
|
||||||
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 })
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
import { Desc } from '../constants.js'
|
import { Desc } from '../models/pagination.js'
|
||||||
import { userList, userCount } from '../models/users.js'
|
import { userList } from '../models/users.js'
|
||||||
|
|
||||||
export async function userListController(req, res) {
|
export async function userListController(req, res) {
|
||||||
const { nextKey } = req.query
|
const { nextKey } = req.query
|
||||||
const itemsPerPage = 50
|
const itemsPerPage = 50
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const users = await userList({
|
const users = await userList(new Desc(itemsPerPage, nextKey))
|
||||||
nextKey,
|
|
||||||
itemsPerPage,
|
|
||||||
order: new Desc(),
|
|
||||||
})
|
|
||||||
const newNextKey = users[users.length - 1]?._id
|
const newNextKey = users[users.length - 1]?._id
|
||||||
|
|
||||||
res.render('users', { users, nextKey: newNextKey })
|
res.render('users', { users, nextKey: newNextKey })
|
||||||
|
|||||||
@@ -1,16 +1,9 @@
|
|||||||
import mongoose from 'mongoose'
|
|
||||||
import { Command } from '@linux-odyssey/models'
|
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, order }) {
|
export async function errorCommands(pagination) {
|
||||||
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([
|
const commands = await Command.aggregate([
|
||||||
{ $match: matchStage },
|
pagination.matchStage('_id'),
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$and: [
|
$and: [
|
||||||
@@ -38,8 +31,8 @@ export async function errorCommands({ nextKey, itemsPerPage, order }) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ $unwind: '$session.user' },
|
{ $unwind: '$session.user' },
|
||||||
{ $sort: { _id: order.order() } },
|
pagination.order('_id'),
|
||||||
{ $limit: itemsPerPage },
|
pagination.limit(),
|
||||||
])
|
])
|
||||||
|
|
||||||
return commands.map(({ _id, command, error, createdAt, session }) => {
|
return commands.map(({ _id, command, error, createdAt, session }) => {
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/* eslint-disable lines-between-class-members */
|
||||||
|
/* eslint-disable class-methods-use-this */
|
||||||
|
// eslint-disable-next-line max-classes-per-file
|
||||||
|
import mongoose from 'mongoose'
|
||||||
|
|
||||||
|
class Pagination {
|
||||||
|
nextKey = null
|
||||||
|
itemsPerPage = 50
|
||||||
|
|
||||||
|
constructor(itemsPerPage = 50, nextKey = null) {
|
||||||
|
this.itemsPerPage = itemsPerPage
|
||||||
|
if (nextKey) {
|
||||||
|
this.nextKey = new mongoose.Types.ObjectId(nextKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
limit() {
|
||||||
|
return { $limit: this.itemsPerPage }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Asc extends Pagination {
|
||||||
|
matchStage(field) {
|
||||||
|
const stage = this.nextKey ? { [field]: { $gt: this.nextKey } } : {}
|
||||||
|
return { $match: stage }
|
||||||
|
}
|
||||||
|
|
||||||
|
order(field) {
|
||||||
|
return { $sort: { [field]: 1 } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Desc extends Pagination {
|
||||||
|
matchStage(field) {
|
||||||
|
const stage = this.nextKey ? { [field]: { $lt: this.nextKey } } : {}
|
||||||
|
return { $match: stage }
|
||||||
|
}
|
||||||
|
|
||||||
|
order(field) {
|
||||||
|
return { $sort: { [field]: -1 } }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import mongoose from 'mongoose'
|
|
||||||
import { Command, Session } from '@linux-odyssey/models'
|
import { Command, Session } from '@linux-odyssey/models'
|
||||||
|
|
||||||
export function sessionCount() {
|
export function sessionCount() {
|
||||||
@@ -31,16 +30,9 @@ 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, order }) {
|
export async function sessionList(pagination) {
|
||||||
let matchStage = {}
|
|
||||||
if (nextKey) {
|
|
||||||
const key = new mongoose.Types.ObjectId(nextKey)
|
|
||||||
matchStage = { _id: order.matchStage(key) }
|
|
||||||
}
|
|
||||||
const sessions = await Session.aggregate([
|
const sessions = await Session.aggregate([
|
||||||
{
|
pagination.matchStage('_id'),
|
||||||
$match: matchStage,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: 'users',
|
from: 'users',
|
||||||
@@ -58,8 +50,8 @@ export async function sessionList({ nextKey, itemsPerPage, order }) {
|
|||||||
as: 'commands',
|
as: 'commands',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ $sort: { _id: order.order() } },
|
pagination.order('_id'),
|
||||||
{ $limit: itemsPerPage },
|
pagination.limit(),
|
||||||
])
|
])
|
||||||
|
|
||||||
return sessions.map(
|
return sessions.map(
|
||||||
|
|||||||
@@ -12,15 +12,9 @@ export function userCount() {
|
|||||||
return User.find().count()
|
return User.find().count()
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function userList({ nextKey, itemsPerPage, order }) {
|
export async function userList(pagination) {
|
||||||
let matchStage = {}
|
|
||||||
if (nextKey) {
|
|
||||||
const key = new mongoose.Types.ObjectId(nextKey)
|
|
||||||
matchStage = { user: order.matchStage(key) }
|
|
||||||
}
|
|
||||||
console.log(matchStage)
|
|
||||||
const users = await Session.aggregate([
|
const users = await Session.aggregate([
|
||||||
{ $match: matchStage },
|
pagination.matchStage('user'),
|
||||||
{
|
{
|
||||||
$group: {
|
$group: {
|
||||||
_id: '$user',
|
_id: '$user',
|
||||||
@@ -37,8 +31,8 @@ export async function userList({ nextKey, itemsPerPage, order }) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ $unwind: '$userData' },
|
{ $unwind: '$userData' },
|
||||||
{ $sort: { 'userData._id': order.order() } },
|
pagination.order('userData._id'),
|
||||||
{ $limit: itemsPerPage },
|
pagination.limit(),
|
||||||
])
|
])
|
||||||
|
|
||||||
return users.map(({ userData, count, lastActivityAt }) => {
|
return users.map(({ userData, count, lastActivityAt }) => {
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import mongoose from 'mongoose'
|
||||||
|
import { Desc } from '../src/models/pagination.js'
|
||||||
|
|
||||||
|
describe('Pagination', () => {
|
||||||
|
it('should return correct limit', async () => {
|
||||||
|
const pagination = new Desc(10)
|
||||||
|
|
||||||
|
expect(pagination.limit()).toMatchObject({ $limit: 10 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return correct order', async () => {
|
||||||
|
const pagination = new Desc(10)
|
||||||
|
|
||||||
|
expect(pagination.order('_id')).toMatchObject({ $sort: { _id: -1 } })
|
||||||
|
expect(pagination.order('time')).toMatchObject({ $sort: { time: -1 } })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return correct match stage for empty nextKey', async () => {
|
||||||
|
const pagination = new Desc(10)
|
||||||
|
|
||||||
|
expect(pagination.matchStage('_id')).toMatchObject({ $match: {} })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return correct match stage for empty nextKey', async () => {
|
||||||
|
const nextKeyStr = '656c7fea28a4c087bfcefe12'
|
||||||
|
const pagination = new Desc(10, nextKeyStr)
|
||||||
|
const nextKey = new mongoose.Types.ObjectId(nextKeyStr)
|
||||||
|
|
||||||
|
expect(pagination.matchStage('_id')).toMatchObject({
|
||||||
|
$match: {
|
||||||
|
_id: {
|
||||||
|
$lt: nextKey,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in new issue
Block a user