mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
refactor: move analytics into services directory
This commit is contained in:
1 parent
e43cbe26ce
commit
740cbcecb4
31 files changed
+6
-5
No files matched your search
@@ -0,0 +1,26 @@
|
||||
import type { Request, Response } from 'express'
|
||||
import Pagination from '../models/pagination'
|
||||
import { errorCommands } from '../models/commands'
|
||||
|
||||
export async function commandListController(
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
const { nextKey, order } = req.query as { nextKey?: string; order: string }
|
||||
const itemsPerPage = 100
|
||||
|
||||
try {
|
||||
const pagination = new Pagination(order, itemsPerPage, nextKey)
|
||||
const payload = await errorCommands(pagination)
|
||||
const newNextKey = payload[payload.length - 1]?._id
|
||||
|
||||
res.render('commands', {
|
||||
errorCommands: payload,
|
||||
nextKey: newNextKey,
|
||||
order: pagination.getOrder(),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
res.status(500).send('Error fetching data')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Request, Response } from 'express'
|
||||
import { userCount } from '../models/users'
|
||||
import { questList } from '../models/quests'
|
||||
|
||||
export async function homeController(req: Request, res: Response) {
|
||||
const quests = await questList()
|
||||
const [totalQuests, totalCompleted] = quests.reduce(
|
||||
([total, completed], quest) => [
|
||||
total + quest.started,
|
||||
completed + quest.completed,
|
||||
],
|
||||
[0, 0]
|
||||
)
|
||||
|
||||
const payload = {
|
||||
title: 'Linux Odyssey Admin',
|
||||
userCount: await userCount(),
|
||||
totalQuests,
|
||||
totalCompleted,
|
||||
completedRate: Math.round((totalCompleted / totalQuests) * 10000) / 100,
|
||||
}
|
||||
res.render('home', payload)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Request, Response } from 'express'
|
||||
import { questList } from '../models/quests'
|
||||
|
||||
export async function questListController(req: Request, res: Response) {
|
||||
const quests = await questList()
|
||||
quests.forEach((quest) => {
|
||||
quest.failed = quest.started - quest.completed
|
||||
})
|
||||
res.render('quests', { quests })
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Request, Response } from 'express'
|
||||
import Pagination from '../models/pagination'
|
||||
import { sessionList, sessionDetail } from '../models/sessions'
|
||||
|
||||
export async function sessionListController(req: Request, res: Response) {
|
||||
const { nextKey, order } = req.query as { nextKey: string; order: string }
|
||||
const itemsPerPage = 50
|
||||
|
||||
try {
|
||||
const pagination = new Pagination(order, itemsPerPage, nextKey)
|
||||
const sessions = await sessionList(pagination)
|
||||
const newNextKey = sessions[sessions.length - 1]?._id
|
||||
|
||||
res.render('sessions', {
|
||||
sessions,
|
||||
nextKey: newNextKey,
|
||||
order: pagination.getOrder(),
|
||||
})
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
res.status(500).send('Error fetching session data')
|
||||
}
|
||||
}
|
||||
|
||||
export async function sessionDetailController(req: Request, res: Response) {
|
||||
const { id } = req.params
|
||||
const session = await sessionDetail(id)
|
||||
res.render('sessionDetail', { session })
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { Request, Response } from 'express'
|
||||
import mongoose from 'mongoose'
|
||||
import Pagination from '../models/pagination'
|
||||
import { userList, userDetail, idToUser } from '../models/users'
|
||||
|
||||
export async function userListController(req: Request, res: Response) {
|
||||
const { nextKey, order } = req.query as { nextKey: string; order: string }
|
||||
const itemsPerPage = 50
|
||||
|
||||
try {
|
||||
const pagination = new Pagination(order, itemsPerPage, nextKey)
|
||||
const users = await userList(pagination)
|
||||
const newNextKey = users[users.length - 1]?._id
|
||||
|
||||
res.render('users', {
|
||||
users,
|
||||
nextKey: newNextKey,
|
||||
order: pagination.getOrder(),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
res.status(500).send('Error fetching user data')
|
||||
}
|
||||
}
|
||||
|
||||
export async function userDetailController(req: Request, res: Response) {
|
||||
const id = new mongoose.Types.ObjectId(req.params.id)
|
||||
const sessions = await userDetail(id)
|
||||
try {
|
||||
const user = await idToUser(id)
|
||||
res.render('usersDetail', { sessions, user })
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
res.status(500).send('Error fetching user data')
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user