mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
wip: models/commands analytics
This commit is contained in:
1 parent
ceb9416fab
commit
8c634c8115
7 files changed
+47
-35
No files matched your search
@@ -4,25 +4,26 @@ 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 itemsPerPage = 250
|
const itemsPerPage = 250
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const totalCommands = await Command.countDocuments({
|
// const totalCommands = await Command.countDocuments({
|
||||||
$and: [
|
// $and: [
|
||||||
{ error: { $exists: true } },
|
// { error: { $exists: true } },
|
||||||
{ error: { $ne: null } },
|
// { error: { $ne: null } },
|
||||||
{ error: { $ne: '' } },
|
// { error: { $ne: '' } },
|
||||||
],
|
// ],
|
||||||
})
|
// })
|
||||||
|
|
||||||
const maxPages = Math.ceil(totalCommands / itemsPerPage)
|
// const maxPages = Math.ceil(totalCommands / itemsPerPage)
|
||||||
|
|
||||||
const pageNumber = req.query.page || 1
|
// const pageNumber = req.query
|
||||||
const payload = {
|
|
||||||
errorCommands: await errorCommands(pageNumber, itemsPerPage),
|
const payload = await errorCommands({ nextKey, itemsPerPage })
|
||||||
pages: Array.from({ length: maxPages }, (_, index) => index + 1),
|
const newNextKey = payload[payload.length - 1]?._id
|
||||||
}
|
|
||||||
res.render('commands', payload)
|
res.render('commands', { payload, nextKey: newNextKey })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).send('Error fetching data')
|
res.status(500).send('Error fetching data')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
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 pageNumber = req.query.page || 1
|
const { nextKey } = req.query
|
||||||
const itemsPerPage = 100
|
const itemsPerPage = 100
|
||||||
|
|
||||||
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(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, nextKey: newNextKey })
|
||||||
|
|
||||||
res.render('sessions', { sessions, pages })
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).send('Error fetching session data')
|
res.status(500).send('Error fetching session data')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,26 @@
|
|||||||
import { Command } from '@linux-odyssey/models'
|
import { Command } from '@linux-odyssey/models'
|
||||||
|
import mongoose from 'mongoose'
|
||||||
|
|
||||||
// eslint-disable-next-line import/prefer-default-export
|
// eslint-disable-next-line import/prefer-default-export
|
||||||
export async function errorCommands(pageNumber, itemsPerPage) {
|
export async function errorCommands({ nextKey, itemsPerPage }) {
|
||||||
const skipAmount = (pageNumber - 1) * itemsPerPage
|
const key = new mongoose.Types.ObjectId(nextKey)
|
||||||
|
const matchStage = nextKey ? { _id: { $lt: key } } : {}
|
||||||
|
|
||||||
const commands = await Command.find({
|
const commands = await Command.aggregate([
|
||||||
|
{
|
||||||
|
$match: matchStage,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
$and: [
|
$and: [
|
||||||
{ error: { $exists: true } },
|
{ error: { $exists: true } },
|
||||||
{ error: { $ne: null } },
|
{ error: { $ne: null } },
|
||||||
{ error: { $ne: '' } },
|
{ error: { $ne: '' } },
|
||||||
],
|
],
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
{ $limit: itemsPerPage },
|
||||||
|
])
|
||||||
.populate({
|
.populate({
|
||||||
path: 'session',
|
path: 'session',
|
||||||
populate: { path: 'user' },
|
populate: { path: 'user' },
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Command, Session } from '@linux-odyssey/models'
|
import { Command, Session } from '@linux-odyssey/models'
|
||||||
|
import mongoose from 'mongoose'
|
||||||
|
|
||||||
export function sessionCount() {
|
export function sessionCount() {
|
||||||
return Session.find().count()
|
return Session.find().count()
|
||||||
@@ -30,10 +31,14 @@ 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(pageNumber, itemsPerPage) {
|
export async function sessionList({ nextKey, itemsPerPage }) {
|
||||||
const skipAmount = (pageNumber - 1) * itemsPerPage
|
const key = new mongoose.Types.ObjectId(nextKey)
|
||||||
|
const matchStage = nextKey ? { _id: { $lt: key } } : {}
|
||||||
|
|
||||||
const sessions = await Session.aggregate([
|
const sessions = await Session.aggregate([
|
||||||
|
{
|
||||||
|
$match: matchStage,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: 'users',
|
from: 'users',
|
||||||
@@ -51,9 +56,10 @@ export async function sessionList(pageNumber, itemsPerPage) {
|
|||||||
as: 'commands',
|
as: 'commands',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{ $sort: { _id: -1 } },
|
||||||
|
{ $limit: itemsPerPage },
|
||||||
])
|
])
|
||||||
.skip(skipAmount)
|
|
||||||
.limit(itemsPerPage)
|
|
||||||
return sessions.map(
|
return sessions.map(
|
||||||
({
|
({
|
||||||
_id,
|
_id,
|
||||||
|
|||||||
@@ -23,7 +23,5 @@
|
|||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
<div class='pagination'>
|
<div class='pagination'>
|
||||||
{{#each pages}}
|
<a href='/commands?nextKey={{nextKey}}'>Next</a>
|
||||||
<a href='/commands?page={{this}}'>{{this}}</a>
|
|
||||||
{{/each}}
|
|
||||||
</div>
|
</div>
|
||||||
@@ -26,7 +26,5 @@
|
|||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
<div class='pagination'>
|
<div class='pagination'>
|
||||||
{{#each pages}}
|
<a href='/sessions?nextKey={{nextKey}}'>Next</a>
|
||||||
<a href='/sessions?page={{this}}'>{{this}}</a>
|
|
||||||
{{/each}}
|
|
||||||
</div>
|
</div>
|
||||||
Whitespace-only changes.
Reference in new issue
Block a user