196 lines
4.3 KiB
TypeScript
Executable File
196 lines
4.3 KiB
TypeScript
Executable File
/**
|
|
* Stats API routes
|
|
*/
|
|
import express, { type Request, type Response } from 'express';
|
|
import { getDatabase } from '../config/database.js';
|
|
|
|
const router = express.Router();
|
|
|
|
/**
|
|
* Get overall statistics
|
|
*/
|
|
router.get('/overall', async (req: Request, res: Response) => {
|
|
try {
|
|
const db = await getDatabase();
|
|
|
|
// Get total videos count
|
|
const videosResult = await db.get('SELECT COUNT(*) as total FROM videos');
|
|
const totalVideos = videosResult?.total || 0;
|
|
|
|
// Get total users count
|
|
const usersResult = await db.get('SELECT COUNT(*) as total FROM users');
|
|
const totalUsers = usersResult?.total || 0;
|
|
|
|
// Get total views (sum of all video views)
|
|
const viewsResult = await db.get('SELECT SUM(views) as total FROM videos');
|
|
const totalViews = viewsResult?.total || 0;
|
|
|
|
// Get videos uploaded today
|
|
const todayStart = new Date();
|
|
todayStart.setHours(0, 0, 0, 0);
|
|
const todayResult = await db.get(
|
|
'SELECT COUNT(*) as total FROM videos WHERE created_at >= ?',
|
|
[todayStart.toISOString()]
|
|
);
|
|
const todayVideos = todayResult?.total || 0;
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
totalVideos,
|
|
totalUsers,
|
|
totalViews,
|
|
todayVideos
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('获取统计数据失败:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: '获取统计数据失败'
|
|
});
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Get video statistics by category
|
|
*/
|
|
router.get('/videos/category', async (req: Request, res: Response) => {
|
|
try {
|
|
const db = await getDatabase();
|
|
|
|
const result = await db.all(`
|
|
SELECT
|
|
category,
|
|
COUNT(*) as count,
|
|
SUM(views) as totalViews
|
|
FROM videos
|
|
GROUP BY category
|
|
ORDER BY count DESC
|
|
`);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: result || []
|
|
});
|
|
} catch (error) {
|
|
console.error('获取分类统计失败:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: '获取分类统计失败'
|
|
});
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Get recent activity stats
|
|
*/
|
|
router.get('/activity', async (req: Request, res: Response) => {
|
|
try {
|
|
const db = await getDatabase();
|
|
|
|
// Get videos uploaded in last 7 days
|
|
const weekAgo = new Date();
|
|
weekAgo.setDate(weekAgo.getDate() - 7);
|
|
|
|
const result = await db.all(`
|
|
SELECT
|
|
DATE(created_at) as date,
|
|
COUNT(*) as count
|
|
FROM videos
|
|
WHERE created_at >= ?
|
|
GROUP BY DATE(created_at)
|
|
ORDER BY date DESC
|
|
`, [weekAgo.toISOString()]);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: result || []
|
|
});
|
|
} catch (error) {
|
|
console.error('获取活动统计失败:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: '获取活动统计失败'
|
|
});
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Get latest videos
|
|
*/
|
|
router.get('/latest', async (req: Request, res: Response) => {
|
|
try {
|
|
const db = await getDatabase();
|
|
const limit = parseInt(req.query.limit as string) || 10;
|
|
|
|
const result = await db.all(`
|
|
SELECT
|
|
id,
|
|
title,
|
|
description,
|
|
cover_url,
|
|
video_url,
|
|
duration,
|
|
views,
|
|
likes,
|
|
category,
|
|
status,
|
|
user_id,
|
|
created_at,
|
|
updated_at
|
|
FROM videos
|
|
WHERE status = 'published'
|
|
ORDER BY created_at DESC
|
|
LIMIT ?
|
|
`, [limit]);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: result || []
|
|
});
|
|
} catch (error) {
|
|
console.error('获取最新视频失败:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: '获取最新视频失败'
|
|
});
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Get recent users
|
|
*/
|
|
router.get('/recent-users', async (req: Request, res: Response) => {
|
|
try {
|
|
const db = await getDatabase();
|
|
const limit = parseInt(req.query.limit as string) || 10;
|
|
|
|
const result = await db.all(`
|
|
SELECT
|
|
id,
|
|
username,
|
|
email,
|
|
avatar,
|
|
role,
|
|
created_at,
|
|
updated_at
|
|
FROM users
|
|
ORDER BY created_at DESC
|
|
LIMIT ?
|
|
`, [limit]);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: result || []
|
|
});
|
|
} catch (error) {
|
|
console.error('获取最近用户失败:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: '获取最近用户失败'
|
|
});
|
|
}
|
|
});
|
|
|
|
export default router; |