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' 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 payload = { const itemsPerPage = 250
errorCommands: await errorCommands(),
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)
} }
+16 -3
View File
@@ -1,8 +1,21 @@
import { 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 sessions = await sessionList() const pageNumber = req.query.page || 1
res.render('sessions', { sessions }) const itemsPerPage = 100
try {
const totalSessions = await sessionCount()
const maxPages = Math.ceil(totalSessions / itemsPerPage)
const sessions = await sessionList(pageNumber, itemsPerPage)
const pages = Array.from({ length: maxPages }, (_, index) => index + 1)
res.render('sessions', { sessions, pages })
} catch (error) {
res.status(500).send('Error fetching session data')
}
} }
export async function sessionDetailController(req, res) { export async function sessionDetailController(req, res) {
+17 -3
View File
@@ -1,6 +1,20 @@
import { userList } from '../models/users.js' import { userList, userCount } from '../models/users.js'
// eslint-disable-next-line import/prefer-default-export
export async function userListController(req, res) { export async function userListController(req, res) {
res.render('users', { users: await userList() }) const pageNumber = req.query.page || 1
const itemsPerPage = 50
try {
const totalUsers = await userCount()
const maxPages = Math.ceil(totalUsers / itemsPerPage)
const users = await userList(pageNumber, itemsPerPage)
res.render('users', {
users,
maxPages: Array.from({ length: maxPages }, (_, index) => index + 1),
})
} catch (error) {
res.status(500).send('Error fetching user data')
}
} }
+5 -1
View File
@@ -1,7 +1,9 @@
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() { export async function errorCommands(pageNumber, itemsPerPage) {
const skipAmount = (pageNumber - 1) * itemsPerPage
const commands = await Command.find({ const commands = await Command.find({
$and: [ $and: [
{ error: { $exists: true } }, { error: { $exists: true } },
@@ -13,6 +15,8 @@ export async function errorCommands() {
path: 'session', path: 'session',
populate: { path: 'user' }, populate: { path: 'user' },
}) })
.skip(skipAmount)
.limit(itemsPerPage)
.exec() .exec()
return commands.map(({ command, error, createdAt, session }) => { return commands.map(({ command, error, createdAt, session }) => {
const { user, quest } = session const { user, quest } = session
+5 -1
View File
@@ -30,7 +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() { export async function sessionList(pageNumber, itemsPerPage) {
const skipAmount = (pageNumber - 1) * itemsPerPage
const sessions = await Session.aggregate([ const sessions = await Session.aggregate([
{ {
$lookup: { $lookup: {
@@ -50,6 +52,8 @@ export async function sessionList() {
}, },
}, },
]) ])
.skip(skipAmount)
.limit(itemsPerPage)
return sessions.map( return sessions.map(
({ ({
_id, _id,
+6 -1
View File
@@ -11,7 +11,9 @@ export function userCount() {
return User.find().count() return User.find().count()
} }
export async function userList() { export async function userList(pageNumber, itemsPerPage) {
const skipAmount = (pageNumber - 1) * itemsPerPage
const users = await Session.aggregate([ const users = await Session.aggregate([
{ {
$group: { $group: {
@@ -30,7 +32,10 @@ export async function userList() {
}, },
{ $unwind: '$userData' }, { $unwind: '$userData' },
{ $sort: { 'userData.createdAt': 1 } }, { $sort: { 'userData.createdAt': 1 } },
{ $skip: skipAmount },
{ $limit: itemsPerPage },
]) ])
return users.map(({ userData, count, lastActivityAt }) => { return users.map(({ userData, count, lastActivityAt }) => {
const { username, email, createdAt } = userData const { username, email, createdAt } = userData
return { return {
@@ -22,3 +22,8 @@
{{/each}} {{/each}}
</table> </table>
</div> </div>
<div class='pagination'>
{{#each pages}}
<a href='/commands?page={{this}}'>{{this}}</a>
{{/each}}
</div>
@@ -25,3 +25,8 @@
{{/each}} {{/each}}
</table> </table>
</div> </div>
<div class='pagination'>
{{#each pages}}
<a href='/sessions?page={{this}}'>{{this}}</a>
{{/each}}
</div>
@@ -21,3 +21,8 @@
{{/each}} {{/each}}
</table> </table>
</div> </div>
<div class='pagination'>
{{#each maxPages}}
<a href='/users?page={{this}}'>{{this}}</a>
{{/each}}
</div>