wip: models/commands analytics

This commit is contained in:
Chen, Tai-Lin committed 2023-12-20 15:05:10 +08:00
1 parent ceb9416fab
commit 8c634c8115
7 files changed
+52 -40

No files matched your search

+15 -14
View File
@@ -4,25 +4,26 @@ 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
try {
const totalCommands = await Command.countDocuments({
$and: [
{ error: { $exists: true } },
{ error: { $ne: null } },
{ error: { $ne: '' } },
],
})
// const totalCommands = await Command.countDocuments({
// $and: [
// { error: { $exists: true } },
// { error: { $ne: null } },
// { error: { $ne: '' } },
// ],
// })
const maxPages = Math.ceil(totalCommands / itemsPerPage)
// 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)
// const pageNumber = req.query
const payload = await errorCommands({ nextKey, itemsPerPage })
const newNextKey = payload[payload.length - 1]?._id
res.render('commands', { payload, nextKey: newNextKey })
} catch (error) {
res.status(500).send('Error fetching data')
}
@@ -1,18 +1,17 @@
import { sessionCount, sessionList, sessionDetail } from '../models/sessions.js'
export async function sessionListController(req, res) {
const pageNumber = req.query.page || 1
const { nextKey } = req.query
const itemsPerPage = 100
try {
const totalSessions = await sessionCount()
const maxPages = Math.ceil(totalSessions / itemsPerPage)
// const totalSessions = await sessionCount()
// const maxPages = Math.ceil(totalSessions / itemsPerPage)
const sessions = await sessionList(pageNumber, itemsPerPage)
const sessions = await sessionList({ nextKey, itemsPerPage })
const newNextKey = sessions[sessions.length - 1]?._id
const pages = Array.from({ length: maxPages }, (_, index) => index + 1)
res.render('sessions', { sessions, pages })
res.render('sessions', { sessions, nextKey: newNextKey })
} catch (error) {
res.status(500).send('Error fetching session data')
}
+19 -9
View File
@@ -1,16 +1,26 @@
import { Command } from '@linux-odyssey/models'
import mongoose from 'mongoose'
// eslint-disable-next-line import/prefer-default-export
export async function errorCommands(pageNumber, itemsPerPage) {
const skipAmount = (pageNumber - 1) * itemsPerPage
export async function errorCommands({ nextKey, itemsPerPage }) {
const key = new mongoose.Types.ObjectId(nextKey)
const matchStage = nextKey ? { _id: { $lt: key } } : {}
const commands = await Command.find({
$and: [
{ error: { $exists: true } },
{ error: { $ne: null } },
{ error: { $ne: '' } },
],
})
const commands = await Command.aggregate([
{
$match: matchStage,
},
{
$match: {
$and: [
{ error: { $exists: true } },
{ error: { $ne: null } },
{ error: { $ne: '' } },
],
}
}
{ $limit: itemsPerPage },
])
.populate({
path: 'session',
populate: { path: 'user' },
+10 -4
View File
@@ -1,4 +1,5 @@
import { Command, Session } from '@linux-odyssey/models'
import mongoose from 'mongoose'
export function sessionCount() {
return Session.find().count()
@@ -30,10 +31,14 @@ function formatTime(time) {
return `${hours}h ${minutes % 60}m ${seconds % 60}s`
}
export async function sessionList(pageNumber, itemsPerPage) {
const skipAmount = (pageNumber - 1) * itemsPerPage
export async function sessionList({ nextKey, itemsPerPage }) {
const key = new mongoose.Types.ObjectId(nextKey)
const matchStage = nextKey ? { _id: { $lt: key } } : {}
const sessions = await Session.aggregate([
{
$match: matchStage,
},
{
$lookup: {
from: 'users',
@@ -51,9 +56,10 @@ export async function sessionList(pageNumber, itemsPerPage) {
as: 'commands',
},
},
{ $sort: { _id: -1 } },
{ $limit: itemsPerPage },
])
.skip(skipAmount)
.limit(itemsPerPage)
return sessions.map(
({
_id,
+1 -3
View File
@@ -23,7 +23,5 @@
</table>
</div>
<div class='pagination'>
{{#each pages}}
<a href='/commands?page={{this}}'>{{this}}</a>
{{/each}}
<a href='/commands?nextKey={{nextKey}}'>Next</a>
</div>
+1 -3
View File
@@ -26,7 +26,5 @@
</table>
</div>
<div class='pagination'>
{{#each pages}}
<a href='/sessions?page={{this}}'>{{this}}</a>
{{/each}}
<a href='/sessions?nextKey={{nextKey}}'>Next</a>
</div>
Whitespace-only changes.