feat: show analytics data page by page

This commit is contained in:
Chen, Tai-Lin committed 2023-12-10 03:21:30 +08:00
1 parent 9200336b49
commit ac31ca8659
9 files changed
+87 -12

No files matched your search

+23 -3
View File
@@ -1,9 +1,29 @@
/* eslint-disable object-shorthand */
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 payload = {
errorCommands: await errorCommands(),
const itemsPerPage = 250
try {
const totalCommands = await Command.countDocuments({
$and: [
{ error: { $exists: true } },
{ error: { $ne: null } },
{ error: { $ne: '' } },
],
})
const maxPages = Math.ceil(totalCommands / itemsPerPage)
const pageNumber = req.query.page || 1
const payload = {
errorCommands: await errorCommands(pageNumber, itemsPerPage),
pages: Array.from({ length: maxPages }, (_, index) => index + 1),
}
res.render('commands', payload)
} catch (error) {
res.status(500).send('Error fetching data')
}
res.render('commands', payload)
}