From a9d428bdd9d9a3fb9c828636d6ce23090fdcb42e Mon Sep 17 00:00:00 2001 From: ucmao <294323976@qq.com> Date: Mon, 5 Jan 2026 21:22:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=95=B0=E6=8D=AE=E5=88=86?= =?UTF-8?q?=E6=9E=90=E6=9D=BF=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- schema.sql | 1 + src/api/admin.py | 133 +++++++++++- src/database/data_storage_manager.py | 2 + templates/admin_modern/analysis.html | 282 ++++++++++++++++++++++++++ templates/admin_modern/base.html | 4 + templates/admin_modern/dashboard.html | 42 ++-- templates/admin_modern/users.html | 2 +- templates/admin_modern/videos.html | 209 +++++++++++++------ 8 files changed, 591 insertions(+), 84 deletions(-) create mode 100644 templates/admin_modern/analysis.html diff --git a/schema.sql b/schema.sql index 1adea62..e3671ba 100644 --- a/schema.sql +++ b/schema.sql @@ -33,6 +33,7 @@ CREATE TABLE IF NOT EXISTS `parse_library` ( `video_url` text COLLATE utf8mb4_general_ci NOT NULL COMMENT '视频播放URL', `score` int(11) DEFAULT '0' COMMENT '视频评分', `is_visible` tinyint(1) DEFAULT '1' COMMENT '是否显示给前端 (0:隐藏, 1:显示)', + `last_user_id` int(11) DEFAULT NULL COMMENT '最后解析该视频的用户ID', `create_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`video_id`) diff --git a/src/api/admin.py b/src/api/admin.py index bea2505..f4974ca 100644 --- a/src/api/admin.py +++ b/src/api/admin.py @@ -1,7 +1,7 @@ from flask import Blueprint, render_template, request, session, redirect, url_for, flash, jsonify import os import json -from datetime import datetime +from datetime import datetime, timedelta from configs.general_constants import DATABASE_CONFIG from src.database.db_manager import DBManager from configs.logging_config import logger @@ -80,14 +80,25 @@ def dashboard(): cursor.execute("SELECT COUNT(*) as count FROM users") user_count = cursor.fetchone()['count'] - # 最近 10 条视频 - cursor.execute("SELECT * FROM parse_library ORDER BY create_at DESC LIMIT 10") + # 今日活跃 (DAU) + cursor.execute("SELECT COUNT(*) as count FROM users WHERE DATE(updated_at) = CURDATE()") + active_user_count = cursor.fetchone()['count'] + + # 最近 10 条视频 (关联用户信息) + cursor.execute(""" + SELECT pl.*, u.nickname as last_user_name + FROM parse_library pl + LEFT JOIN users u ON pl.last_user_id = u.user_id + ORDER BY pl.create_at DESC + LIMIT 10 + """) recent_videos = cursor.fetchall() db.disconnect() return render_template('admin_modern/dashboard.html', video_count=video_count, user_count=user_count, + active_user_count=active_user_count, recent_videos=recent_videos) @bp.route('/videos') @@ -105,8 +116,13 @@ def videos(): # 搜索 search = request.args.get('search', '') platform = request.args.get('platform', '') + visibility = request.args.get('visibility', '') + start_date = request.args.get('start_date', '') + end_date = request.args.get('end_date', '') + min_score = request.args.get('min_score', '') + max_score = request.args.get('max_score', '') - query = "SELECT * FROM parse_library WHERE 1=1" + query = "SELECT pl.*, u.nickname as last_user_name FROM parse_library pl LEFT JOIN users u ON pl.last_user_id = u.user_id WHERE 1=1" params = [] if search: @@ -115,6 +131,21 @@ def videos(): if platform: query += " AND platform = %s" params.append(platform) + if visibility != '': + query += " AND is_visible = %s" + params.append(visibility) + if start_date: + query += " AND DATE(create_at) >= %s" + params.append(start_date) + if end_date: + query += " AND DATE(create_at) <= %s" + params.append(end_date) + if min_score: + query += " AND score >= %s" + params.append(min_score) + if max_score: + query += " AND score <= %s" + params.append(max_score) # 获取总数用于分页 cursor.execute(f"SELECT COUNT(*) as count FROM ({query}) as t", params) @@ -141,6 +172,11 @@ def videos(): limit=limit, search=search, platform=platform, + visibility=visibility, + start_date=start_date, + end_date=end_date, + min_score=min_score, + max_score=max_score, sort_by=sort_by, order=order) @@ -284,6 +320,95 @@ def clear_user_records(): finally: db.disconnect() +@bp.route('/analysis') +@login_required +def analysis(): + return render_template('admin_modern/analysis.html') + +@bp.route('/api/analysis_data') +@login_required +def analysis_data(): + db = get_db() + cursor = db.conn.cursor(dictionary=True) + try: + # 1. 最近 14 天活跃解析趋势 (基于 parse_library.create_at) + cursor.execute(""" + SELECT DATE_FORMAT(create_at, '%m-%d') as date, COUNT(*) as count + FROM parse_library + WHERE create_at >= DATE_SUB(CURDATE(), INTERVAL 14 DAY) + GROUP BY DATE(create_at) + ORDER BY DATE(create_at) ASC + """) + parse_trend = cursor.fetchall() + + # 2. 最近 14 天新用户增长趋势 + cursor.execute(""" + SELECT DATE_FORMAT(created_at, '%m-%d') as date, COUNT(*) as count + FROM users + WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 14 DAY) + GROUP BY DATE(created_at) + ORDER BY DATE(created_at) ASC + """) + user_trend = cursor.fetchall() + + # 3. 平台分布 (饼图) + cursor.execute(""" + SELECT platform, COUNT(*) as count + FROM parse_library + GROUP BY platform + """) + platform_dist = cursor.fetchall() + + # 4. 用户地域分布 (Top 10 省份) + cursor.execute(""" + SELECT province, COUNT(*) as count + FROM users + WHERE province IS NOT NULL AND province != '' + GROUP BY province + ORDER BY count DESC + LIMIT 10 + """) + province_dist = cursor.fetchall() + + # 5. 总计数据 + cursor.execute("SELECT COUNT(*) as total FROM parse_library") + total_parses = cursor.fetchone()['total'] + + cursor.execute("SELECT COUNT(*) as total FROM users") + total_users = cursor.fetchone()['total'] + + # 今日解析 + cursor.execute("SELECT COUNT(*) as total FROM parse_library WHERE DATE(create_at) = CURDATE()") + today_parses = cursor.fetchone()['total'] + + # 今日新用户 + cursor.execute("SELECT COUNT(*) as total FROM users WHERE DATE(created_at) = CURDATE()") + today_new_users = cursor.fetchone()['total'] + + # 今日活跃用户 (DAU) + cursor.execute("SELECT COUNT(*) as total FROM users WHERE DATE(updated_at) = CURDATE()") + today_active_users = cursor.fetchone()['total'] + + return jsonify({ + 'success': True, + 'parse_trend': parse_trend, + 'user_trend': user_trend, + 'platform_dist': platform_dist, + 'province_dist': province_dist, + 'stats': { + 'total_parses': total_parses, + 'total_users': total_users, + 'today_parses': today_parses, + 'today_new_users': today_new_users, + 'today_active_users': today_active_users + } + }) + except Exception as e: + logger.error(f"Analysis data error: {e}") + return jsonify({'success': False, 'message': str(e)}), 500 + finally: + db.disconnect() + @bp.route('/scores') @login_required def scores(): diff --git a/src/database/data_storage_manager.py b/src/database/data_storage_manager.py index cb151fc..95c0275 100644 --- a/src/database/data_storage_manager.py +++ b/src/database/data_storage_manager.py @@ -59,6 +59,7 @@ class DataStorageManager: try: if data_dict.get('video_url'): self.update_dict.update(data_dict) + self.update_dict['last_user_id'] = user_id manager.update_table_parse_library(**self.update_dict) manager.add_video(user_id, video_id) finally: @@ -69,6 +70,7 @@ class DataStorageManager: try: if data_dict.get('video_url'): self.update_dict.update(data_dict) + self.update_dict['last_user_id'] = user_id manager.upsert_table_parse_library(**self.update_dict) manager.add_video(user_id, video_id) finally: diff --git a/templates/admin_modern/analysis.html b/templates/admin_modern/analysis.html new file mode 100644 index 0000000..51a3b6c --- /dev/null +++ b/templates/admin_modern/analysis.html @@ -0,0 +1,282 @@ +{% extends "admin_modern/base.html" %} + +{% block title %}数据统计分析 - Ucmao Admin{% endblock %} + +{% block content %} +
+

数据统计分析

+

全方位洞察系统运行状态与用户行为趋势

+
+ + +
+
+
+ +
+
+
总用户数
+
-
+
今日新增: +-
+
+
+ +
+
+ +
+
+
今日活跃 (DAU)
+
-
+
基于用户最后交互时间
+
+
+ +
+
+ +
+
+
总解析量
+
-
+
今日解析: +-
+
+
+ +
+
+ +
+
+
解析转化率
+
-
+
平均每人解析次数
+
+
+
+ + +
+
+
+

业务增长趋势

+

最近 14 天的解析量与新用户增长对比

+
+
+
+ 解析量 +
+
+ 新用户 +
+
+
+
+
+ +
+ +
+

平台使用分布

+
+
+ + +
+

用户地区活跃排行 (Top 10)

+
+
+
+ + + + +{% endblock %} + diff --git a/templates/admin_modern/base.html b/templates/admin_modern/base.html index e8aee83..51db5a6 100644 --- a/templates/admin_modern/base.html +++ b/templates/admin_modern/base.html @@ -33,6 +33,10 @@ 仪表盘概览 + + + 数据统计分析 + 视频库管理 diff --git a/templates/admin_modern/dashboard.html b/templates/admin_modern/dashboard.html index 61030d7..ad17eb1 100644 --- a/templates/admin_modern/dashboard.html +++ b/templates/admin_modern/dashboard.html @@ -10,33 +10,35 @@
-
- + -
- + -
-
-
系统运行正常
-
版本 v2.0.0 (LTS)
+ +
+
- -
+
+
今日活跃 (DAU)
+
{{ active_user_count }}
+
+
@@ -52,6 +54,7 @@ 平台 封面 标题 + 解析用户 积分 创建时间 @@ -70,16 +73,25 @@ {% endif %} -
{{ video.title or '无标题' }}
+
{{ video.title or '无标题' }}
- {{ video.score }} + + {% if video.last_user_name %} + + {{ video.last_user_name }} + + {% else %} + 系统/匿名 + {% endif %} + + +{{ video.score }} {{ video.create_at.strftime('%m-%d %H:%M') if video.create_at else '-' }} {% endfor %} {% if not recent_videos %} - 暂无数据 + 暂无数据 {% endif %} diff --git a/templates/admin_modern/users.html b/templates/admin_modern/users.html index d1bed6c..5e2a4ed 100644 --- a/templates/admin_modern/users.html +++ b/templates/admin_modern/users.html @@ -52,7 +52,7 @@
- +
{{ user.nickname or '微信用户' }}
{{ user.open_id }}
diff --git a/templates/admin_modern/videos.html b/templates/admin_modern/videos.html index 7faa057..0a27f8c 100644 --- a/templates/admin_modern/videos.html +++ b/templates/admin_modern/videos.html @@ -11,26 +11,72 @@
-
-
-
- - +
+ +
+ +
+ + +
+ + +
+ + + +
+ + +
+ + {% if search or platform or visibility or start_date or end_date or min_score or max_score %} + 重置 + {% endif %} +
+
+ + +
+
高级筛选:
+ + +
+ + + +
+ + +
+ + + 至 + +
+ + +
+ + + - + +
- - - - {% if search or platform %} - 重置 - {% endif %}
@@ -91,91 +137,107 @@ - - - - + + + - - - - + + {% for video in videos %} - - - - - + - - - {% endfor %} {% if not videos %} - + {% endif %}
+ 封面视频信息 (点击标题编辑) + 封面视频信息 (点击标题编辑)解析用户 平台 {% if sort_by == 'platform' %}{% endif %} + 积分 {% if sort_by == 'score' %}{% endif %} + 创建时间 {% if sort_by == 'create_at' %}{% endif %} 前端可见操作前端可见操作
+ + {% if video.cover_url %} -
+
- +
{% else %} -
+
{% endif %}
+
-
+
{{ video.title or '未命名视频' }} {% if session.get('admin_role') == 'admin' %} - + {% endif %}
-
{{ video.video_id }}
+
{{ video.video_id }}
- {{ video.platform }} + + {% if video.last_user_name %} + + {{ video.last_user_name }} + + {% else %} + 系统/匿名 + {% endif %} + + {{ video.platform }} +
- {{ video.score }} + {{ video.score }} {% if session.get('admin_role') == 'admin' %} - + {% endif %}
+ {{ video.create_at.strftime('%Y-%m-%d %H:%M') if video.create_at else '-' }} - + {% if video.is_visible %} + + {% else %} + + {% endif %} -
- +
+
{% if session.get('admin_role') == 'admin' %} - + {% else %} + - {% endif %}
没有找到匹配的记录
没有找到匹配的记录
@@ -187,10 +249,10 @@
共 {{ total }} 条记录
{% if page > 1 %} - 上一页 + 上一页 {% endif %} {% if total > page * limit %} - 下一页 + 下一页 {% endif %}
@@ -347,6 +409,7 @@ } function toggleVisibility(videoId, currentStatus, btn) { + if (userRole !== 'admin') return; const newStatus = currentStatus === 1 ? 0 : 1; fetch("{{ url_for('admin_modern.toggle_visibility') }}", { method: 'POST', @@ -354,12 +417,15 @@ body: JSON.stringify({ video_id: videoId, is_visible: newStatus }) }).then(r => r.json()).then(data => { if(data.success) { - btn.classList.toggle('bg-indigo-600'); - btn.classList.toggle('bg-slate-200'); - btn.querySelector('span').classList.toggle('translate-x-6'); - btn.querySelector('span').classList.toggle('translate-x-1'); + if(newStatus === 1) { + btn.className = 'px-4 py-1.5 bg-emerald-50 text-emerald-600 rounded-full text-xs font-black hover:bg-emerald-100 transition-all cursor-pointer'; + btn.textContent = '可见'; + } else { + btn.className = 'px-4 py-1.5 bg-slate-100 text-slate-400 rounded-full text-xs font-black hover:bg-slate-200 transition-all cursor-pointer'; + btn.textContent = '隐藏'; + } btn.setAttribute('onclick', `toggleVisibility('${videoId}', ${newStatus}, this)`); - showToast(newStatus ? '已设置为显示' : '已成功隐藏'); + showToast(newStatus ? '已设置为可见' : '已成功隐藏'); } }); } @@ -384,7 +450,12 @@ video_ids: selected, is_visible: status, search: urlParams.get('search') || '', - platform: urlParams.get('platform') || '' + platform: urlParams.get('platform') || '', + visibility: urlParams.get('visibility') || '', + start_date: urlParams.get('start_date') || '', + end_date: urlParams.get('end_date') || '', + min_score: urlParams.get('min_score') || '', + max_score: urlParams.get('max_score') || '' }) }).then(r => r.json()).then(data => { if(data.success) { @@ -452,7 +523,12 @@ video_ids: selected, score_delta: val, search: urlParams.get('search') || '', - platform: urlParams.get('platform') || '' + platform: urlParams.get('platform') || '', + visibility: urlParams.get('visibility') || '', + start_date: urlParams.get('start_date') || '', + end_date: urlParams.get('end_date') || '', + min_score: urlParams.get('min_score') || '', + max_score: urlParams.get('max_score') || '' }) }).then(r => r.json()).then(data => { if(data.success) { @@ -484,7 +560,12 @@ body: JSON.stringify({ video_ids: selected, search: urlParams.get('search') || '', - platform: urlParams.get('platform') || '' + platform: urlParams.get('platform') || '', + visibility: urlParams.get('visibility') || '', + start_date: urlParams.get('start_date') || '', + end_date: urlParams.get('end_date') || '', + min_score: urlParams.get('min_score') || '', + max_score: urlParams.get('max_score') || '' }) }).then(r => r.json()).then(data => { if(data.success) {