mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-09-04 23:19:17 +08:00
feat: 集成数据库配置管理并添加错误日志查看器
主要变更:
1. **数据库集成**:
* 引入 MySQL 数据库支持,使用 SQLAlchemy 和 `databases` 库持久化存储应用程序设置。
* 添加了 `app/database` 目录,包含数据库连接、模型和初始化逻辑。
* 更新 `requirements.txt` 添加数据库相关依赖 (`pymysql`, `sqlalchemy`, `aiomysql`, `databases`, `python-dotenv`)。
2. **配置管理重构**:
* 重构 `ConfigService` (`app/service/config/config_service.py`),使其从数据库加载和保存设置,并支持从 `.env` 文件同步初始配置到数据库。
* 修改 `Settings` 模型 (`app/config/config.py`) 以包含数据库连接信息,并添加了从数据库加载/同步配置的逻辑。
* 配置相关的路由 (`app/router/config_routes.py`) 更新为异步,并调用新的 `ConfigService` 方法。
* `KeyManager` (`app/service/key/key_manager.py`) 现在可以在配置更新后重置和重新初始化。
3. **错误日志查看器**:
* 新增 `/logs` 页面 (`app/templates/error_logs.html`) 用于展示应用程序错误日志。
* 添加了相应的路由 (`app/router/log_routes.py`)、静态资源 (`app/static/css/error_logs.css`, `app/static/js/error_logs.js`) 和日志记录器 (`app/log/logger.py`)。
* 在配置页面和密钥管理页面的导航栏中添加了指向日志页面的链接。
4. **异步操作**:
* 将配置服务和相关路由转换为异步 (`async def`) 以支持异步数据库操作。
5. **其他**:
* 更新了应用程序初始化逻辑 (`app/core/application.py`, `app/core/initialization.py`) 以包含数据库连接的建立和关闭。
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
/* 错误日志页面样式 */
|
||||
|
||||
/* 全局样式 */
|
||||
body {
|
||||
font-family: 'Roboto', sans-serif;
|
||||
background-color: #f8f9fa;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #764ba2;
|
||||
font-weight: 700;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
/* 导航栏样式 */
|
||||
.nav-tabs {
|
||||
display: flex;
|
||||
margin-bottom: 20px;
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.tab-link {
|
||||
padding: 10px 20px;
|
||||
margin-right: 5px;
|
||||
color: #555;
|
||||
text-decoration: none;
|
||||
border-radius: 5px;
|
||||
transition: all 0.3s ease;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.tab-link i {
|
||||
margin-right: 5px;
|
||||
}
|
||||
|
||||
.tab-link:hover {
|
||||
background-color: #f0f0f0;
|
||||
color: #764ba2;
|
||||
}
|
||||
|
||||
.tab-link.active {
|
||||
background-color: #764ba2;
|
||||
color: white;
|
||||
}
|
||||
|
||||
/* 卡片样式 */
|
||||
.card {
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||
margin-bottom: 20px;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.card-header {
|
||||
background-color: #f8f9fa;
|
||||
border-bottom: 1px solid #eee;
|
||||
padding: 15px 20px;
|
||||
border-radius: 10px 10px 0 0 !important;
|
||||
}
|
||||
|
||||
.card-body {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.card-footer {
|
||||
background-color: #f8f9fa;
|
||||
border-top: 1px solid #eee;
|
||||
padding: 15px 20px;
|
||||
border-radius: 0 0 10px 10px !important;
|
||||
}
|
||||
|
||||
/* 表格样式 */
|
||||
.table {
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.table th {
|
||||
background-color: #f8f9fa;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
/* 错误日志内容截断 */
|
||||
.error-log-content {
|
||||
max-width: 300px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
/* 模态框样式 */
|
||||
.modal-dialog {
|
||||
max-width: 800px;
|
||||
}
|
||||
|
||||
pre {
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
/* 分页样式 */
|
||||
.pagination .page-item.active .page-link {
|
||||
background-color: #0d6efd;
|
||||
border-color: #0d6efd;
|
||||
}
|
||||
|
||||
.pagination .page-link {
|
||||
color: #0d6efd;
|
||||
}
|
||||
|
||||
/* 按钮样式 */
|
||||
.btn-view-details {
|
||||
padding: 0.25rem 0.5rem;
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
|
||||
/* 加载指示器样式 */
|
||||
#loadingIndicator .spinner-border {
|
||||
width: 3rem;
|
||||
height: 3rem;
|
||||
}
|
||||
|
||||
/* 刷新按钮样式 */
|
||||
.refresh-btn {
|
||||
position: fixed;
|
||||
top: 20px;
|
||||
right: 20px;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border-radius: 50%;
|
||||
background-color: #764ba2;
|
||||
color: white;
|
||||
border: none;
|
||||
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
cursor: pointer;
|
||||
z-index: 1000;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.refresh-btn:hover {
|
||||
background-color: #5d3b82;
|
||||
transform: scale(1.1);
|
||||
}
|
||||
|
||||
.refresh-btn i {
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.rotating {
|
||||
animation: rotate 1s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes rotate {
|
||||
from { transform: rotate(0deg); }
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
/* 滚动按钮样式 */
|
||||
.scroll-buttons {
|
||||
position: fixed;
|
||||
bottom: 20px;
|
||||
right: 20px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.scroll-btn {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border-radius: 50%;
|
||||
background-color: #764ba2;
|
||||
color: white;
|
||||
border: none;
|
||||
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.scroll-btn:hover {
|
||||
background-color: #5d3b82;
|
||||
transform: scale(1.1);
|
||||
}
|
||||
|
||||
/* 版权信息样式 */
|
||||
.copyright {
|
||||
text-align: center;
|
||||
margin-top: 30px;
|
||||
padding: 20px 0;
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
border-top: 1px solid #eee;
|
||||
}
|
||||
|
||||
.copyright a {
|
||||
color: #764ba2;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.copyright a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.copyright img {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
border-radius: 50%;
|
||||
vertical-align: middle;
|
||||
margin-right: 5px;
|
||||
}
|
||||
|
||||
/* 复制状态提示 */
|
||||
#copyStatus {
|
||||
position: fixed;
|
||||
bottom: 20px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
background-color: rgba(0, 0, 0, 0.7);
|
||||
color: white;
|
||||
padding: 10px 20px;
|
||||
border-radius: 5px;
|
||||
z-index: 1000;
|
||||
opacity: 0;
|
||||
transition: opacity 0.3s ease;
|
||||
}
|
||||
|
||||
#copyStatus.show {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
/* 响应式调整 */
|
||||
@media (max-width: 768px) {
|
||||
.d-flex.justify-content-between {
|
||||
flex-direction: column;
|
||||
align-items: flex-start !important;
|
||||
}
|
||||
|
||||
.d-flex.justify-content-between > div {
|
||||
margin-top: 1rem;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.card-header .d-flex {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.card-header .input-group {
|
||||
margin-bottom: 0.5rem;
|
||||
margin-right: 0 !important;
|
||||
}
|
||||
|
||||
#refreshBtn {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
// 错误日志页面JavaScript
|
||||
|
||||
// 页面滚动功能
|
||||
function scrollToTop() {
|
||||
window.scrollTo({
|
||||
top: 0,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
}
|
||||
|
||||
function scrollToBottom() {
|
||||
window.scrollTo({
|
||||
top: document.body.scrollHeight,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
}
|
||||
|
||||
// 刷新页面功能
|
||||
function refreshPage(button) {
|
||||
if (button) {
|
||||
button.classList.add('rotating');
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
window.location.reload();
|
||||
}, 500);
|
||||
}
|
||||
|
||||
// 全局变量
|
||||
let currentPage = 1;
|
||||
let pageSize = 20;
|
||||
let totalPages = 1;
|
||||
let errorLogs = [];
|
||||
|
||||
// 页面加载完成后执行
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
// 初始化页面大小选择器
|
||||
const pageSizeSelector = document.getElementById('pageSize');
|
||||
pageSizeSelector.value = pageSize;
|
||||
pageSizeSelector.addEventListener('change', function() {
|
||||
pageSize = parseInt(this.value);
|
||||
currentPage = 1; // 重置到第一页
|
||||
loadErrorLogs();
|
||||
});
|
||||
|
||||
// 初始化刷新按钮
|
||||
document.getElementById('refreshBtn').addEventListener('click', function() {
|
||||
loadErrorLogs();
|
||||
});
|
||||
|
||||
// 加载错误日志数据
|
||||
loadErrorLogs();
|
||||
});
|
||||
|
||||
// 加载错误日志数据
|
||||
function loadErrorLogs() {
|
||||
showLoading(true);
|
||||
showError(false);
|
||||
showNoData(false);
|
||||
|
||||
const offset = (currentPage - 1) * pageSize;
|
||||
|
||||
fetch(`/api/logs/errors?limit=${pageSize}&offset=${offset}`)
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('网络响应异常');
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
errorLogs = data;
|
||||
renderErrorLogs();
|
||||
showLoading(false);
|
||||
|
||||
if (errorLogs.length === 0) {
|
||||
showNoData(true);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('获取错误日志失败:', error);
|
||||
showLoading(false);
|
||||
showError(true);
|
||||
});
|
||||
}
|
||||
|
||||
// 渲染错误日志表格
|
||||
function renderErrorLogs() {
|
||||
const tableBody = document.getElementById('errorLogsTable');
|
||||
tableBody.innerHTML = '';
|
||||
|
||||
errorLogs.forEach(log => {
|
||||
const row = document.createElement('tr');
|
||||
|
||||
// 格式化日期
|
||||
const requestTime = new Date(log.request_time);
|
||||
const formattedTime = requestTime.toLocaleString('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit'
|
||||
});
|
||||
|
||||
// 截断错误日志内容
|
||||
const errorLogContent = log.error_log ? log.error_log.substring(0, 100) + (log.error_log.length > 100 ? '...' : '') : '无';
|
||||
|
||||
row.innerHTML = `
|
||||
<td>${log.id}</td>
|
||||
<td>${log.gemini_key || '无'}</td>
|
||||
<td class="error-log-content">${errorLogContent}</td>
|
||||
<td>${formattedTime}</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-primary btn-view-details" data-log-id="${log.id}">
|
||||
查看详情
|
||||
</button>
|
||||
</td>
|
||||
`;
|
||||
|
||||
tableBody.appendChild(row);
|
||||
});
|
||||
|
||||
// 添加详情按钮事件监听
|
||||
document.querySelectorAll('.btn-view-details').forEach(button => {
|
||||
button.addEventListener('click', function() {
|
||||
const logId = parseInt(this.getAttribute('data-log-id'));
|
||||
showLogDetails(logId);
|
||||
});
|
||||
});
|
||||
|
||||
// 更新分页
|
||||
updatePagination();
|
||||
}
|
||||
|
||||
// 显示错误日志详情
|
||||
function showLogDetails(logId) {
|
||||
const log = errorLogs.find(log => log.id === logId);
|
||||
if (!log) return;
|
||||
|
||||
// 格式化日期
|
||||
const requestTime = new Date(log.request_time);
|
||||
const formattedTime = requestTime.toLocaleString('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit'
|
||||
});
|
||||
|
||||
// 格式化请求消息
|
||||
let formattedRequestMsg = '';
|
||||
if (log.request_msg) {
|
||||
try {
|
||||
if (typeof log.request_msg === 'string') {
|
||||
formattedRequestMsg = log.request_msg;
|
||||
} else {
|
||||
formattedRequestMsg = JSON.stringify(log.request_msg, null, 2);
|
||||
}
|
||||
} catch (e) {
|
||||
formattedRequestMsg = String(log.request_msg);
|
||||
}
|
||||
} else {
|
||||
formattedRequestMsg = '无';
|
||||
}
|
||||
|
||||
// 填充模态框内容
|
||||
document.getElementById('modalGeminiKey').textContent = log.gemini_key || '无';
|
||||
document.getElementById('modalErrorLog').textContent = log.error_log || '无';
|
||||
document.getElementById('modalRequestMsg').textContent = formattedRequestMsg;
|
||||
document.getElementById('modalRequestTime').textContent = formattedTime;
|
||||
|
||||
// 显示模态框
|
||||
const modal = new bootstrap.Modal(document.getElementById('logDetailModal'));
|
||||
modal.show();
|
||||
}
|
||||
|
||||
// 更新分页控件
|
||||
function updatePagination() {
|
||||
const paginationElement = document.getElementById('pagination');
|
||||
paginationElement.innerHTML = '';
|
||||
|
||||
// 计算总页数
|
||||
const totalCount = errorLogs.length;
|
||||
totalPages = Math.max(1, Math.ceil(totalCount / pageSize));
|
||||
|
||||
// 上一页按钮
|
||||
const prevItem = document.createElement('li');
|
||||
prevItem.className = `page-item ${currentPage === 1 ? 'disabled' : ''}`;
|
||||
prevItem.innerHTML = `<a class="page-link" href="#" aria-label="上一页"><span aria-hidden="true">«</span></a>`;
|
||||
prevItem.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
if (currentPage > 1) {
|
||||
currentPage--;
|
||||
loadErrorLogs();
|
||||
}
|
||||
});
|
||||
paginationElement.appendChild(prevItem);
|
||||
|
||||
// 页码按钮
|
||||
for (let i = 1; i <= totalPages; i++) {
|
||||
const pageItem = document.createElement('li');
|
||||
pageItem.className = `page-item ${i === currentPage ? 'active' : ''}`;
|
||||
pageItem.innerHTML = `<a class="page-link" href="#">${i}</a>`;
|
||||
pageItem.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
currentPage = i;
|
||||
loadErrorLogs();
|
||||
});
|
||||
paginationElement.appendChild(pageItem);
|
||||
}
|
||||
|
||||
// 下一页按钮
|
||||
const nextItem = document.createElement('li');
|
||||
nextItem.className = `page-item ${currentPage === totalPages ? 'disabled' : ''}`;
|
||||
nextItem.innerHTML = `<a class="page-link" href="#" aria-label="下一页"><span aria-hidden="true">»</span></a>`;
|
||||
nextItem.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
if (currentPage < totalPages) {
|
||||
currentPage++;
|
||||
loadErrorLogs();
|
||||
}
|
||||
});
|
||||
paginationElement.appendChild(nextItem);
|
||||
}
|
||||
|
||||
// 显示/隐藏加载指示器
|
||||
function showLoading(show) {
|
||||
const loadingIndicator = document.getElementById('loadingIndicator');
|
||||
if (show) {
|
||||
loadingIndicator.classList.remove('d-none');
|
||||
} else {
|
||||
loadingIndicator.classList.add('d-none');
|
||||
}
|
||||
}
|
||||
|
||||
// 显示/隐藏错误消息
|
||||
function showError(show) {
|
||||
const errorMessage = document.getElementById('errorMessage');
|
||||
if (show) {
|
||||
errorMessage.classList.remove('d-none');
|
||||
} else {
|
||||
errorMessage.classList.add('d-none');
|
||||
}
|
||||
}
|
||||
|
||||
// 显示/隐藏无数据消息
|
||||
function showNoData(show) {
|
||||
const noDataMessage = document.getElementById('noDataMessage');
|
||||
if (show) {
|
||||
noDataMessage.classList.remove('d-none');
|
||||
} else {
|
||||
noDataMessage.classList.add('d-none');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user