Files
cursor-api/static/logs.html
2024-12-30 23:41:04 +08:00

382 lines
9.4 KiB
HTML

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>请求日志查看</title>
<!-- 引入共享样式 -->
<link rel="stylesheet" href="/static/shared-styles.css">
<script src="/static/shared.js"></script>
<style>
/* 日志页面特定样式 */
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-bottom: var(--spacing);
}
.stat-card {
background: var(--card-background);
padding: 15px;
border-radius: var(--border-radius);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.stat-card h4 {
margin: 0 0 8px 0;
color: var(--primary-color);
}
.stat-value {
font-size: 24px;
font-weight: 500;
}
.refresh-container {
display: flex;
justify-content: space-between;
align-items: center;
}
.auto-refresh {
display: flex;
align-items: center;
gap: 8px;
}
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
overflow-y: auto;
}
.modal-content {
background-color: var(--card-background);
margin: 5% auto;
padding: 20px;
border-radius: var(--border-radius);
width: 90%;
max-width: 800px;
max-height: 80vh;
overflow-y: auto;
}
.close {
float: right;
cursor: pointer;
font-size: 28px;
}
.info-button {
background: var(--primary-color);
color: white;
border: none;
padding: 4px 8px;
border-radius: 4px;
cursor: pointer;
}
.message-table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
.message-table th,
.message-table td {
border: 1px solid var(--border-color);
padding: 12px;
vertical-align: top;
}
.message-table td {
word-break: break-word;
}
.message-table td:nth-child(2) {
max-width: 600px;
}
.message-table td:first-child {
width: 80px;
white-space: nowrap;
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
}
.modal-header h3 {
margin: 0;
}
.close {
font-size: 24px;
font-weight: bold;
cursor: pointer;
padding: 5px 10px;
}
.close:hover {
color: var(--primary-color);
}
.usage-progress-container {
margin-top: 20px;
width: 100%;
height: 20px;
background-color: #f0f0f0;
border-radius: 10px;
overflow: hidden;
}
.usage-progress-bar {
height: 100%;
width: 0%;
transition: width 0.3s ease;
background: linear-gradient(to right,
#4caf50 0%,
/* 绿色 */
#8bc34a 25%,
/* 浅绿 */
#ffeb3b 50%,
/* 黄色 */
#ff9800 75%,
/* 橙色 */
#f44336 100%
/* 红色 */
);
}
</style>
</head>
<body>
<h1>请求日志查看</h1>
<div class="container">
<div class="form-group">
<label>认证令牌:</label>
<input type="password" id="authToken" placeholder="输入 AUTH_TOKEN">
</div>
<div class="refresh-container">
<div class="button-group">
<button onclick="fetchLogs()">刷新日志</button>
</div>
<div class="auto-refresh">
<input type="checkbox" id="autoRefresh" checked>
<label for="autoRefresh">自动刷新 (60秒)</label>
</div>
</div>
</div>
<div class="container">
<div class="stats-grid">
<div class="stat-card">
<h4>总请求数</h4>
<div id="totalRequests" class="stat-value">-</div>
</div>
<div class="stat-card">
<h4>活跃请求数</h4>
<div id="activeRequests" class="stat-value">-</div>
</div>
<div class="stat-card">
<h4>最后更新</h4>
<div id="lastUpdate" class="stat-value">-</div>
</div>
</div>
<div class="table-container">
<table id="logsTable">
<thead>
<tr>
<th>时间</th>
<th>模型</th>
<th>Token信息</th>
<th>Prompt</th>
<th>流式响应</th>
<th>状态</th>
<th>错误信息</th>
</tr>
</thead>
<tbody id="logsBody"></tbody>
</table>
</div>
</div>
<div id="message"></div>
<!-- 添加弹窗组件 -->
<div id="tokenModal" class="modal">
<div class="modal-content">
<span class="close">&times;</span>
<h3>Token 详细信息</h3>
<table>
<tr>
<td>Token:</td>
<td id="modalToken"></td>
</tr>
<tr>
<td>校验和:</td>
<td id="modalChecksum"></td>
</tr>
<tr>
<td>别名:</td>
<td id="modalAlias"></td>
</tr>
<tr>
<td>使用情况:</td>
<td id="modalUsage"></td>
</tr>
</table>
<!-- 添加进度条容器 -->
<div class="usage-progress-container">
<div id="modalUsageBar" class="usage-progress-bar"></div>
</div>
</div>
</div>
<div id="promptModal" class="modal">
<div class="modal-content">
<div class="modal-header">
<h3>对话内容</h3>
<span class="close">&times;</span>
</div>
<div id="promptContent"></div>
</div>
</div>
<script>
let refreshInterval;
function updateStats(data) {
document.getElementById('totalRequests').textContent = data.total;
document.getElementById('activeRequests').textContent = data.active || 0;
document.getElementById('lastUpdate').textContent =
new Date(data.timestamp).toLocaleTimeString();
}
function showTokenModal(tokenInfo) {
const modal = document.getElementById('tokenModal');
document.getElementById('modalToken').textContent = tokenInfo.token || '-';
document.getElementById('modalChecksum').textContent = tokenInfo.checksum || '-';
document.getElementById('modalAlias').textContent = tokenInfo.alias || '-';
// 获取进度条容器
const progressContainer = document.querySelector('.usage-progress-container');
// 处理使用情况和进度条
if (tokenInfo.usage) {
const current = tokenInfo.usage.fast_requests;
const max = tokenInfo.usage.max_fast_requests;
const percentage = (current / max * 100).toFixed(1);
document.getElementById('modalUsage').textContent =
`${current}/${max} (${percentage}%)`;
// 显示进度条容器
progressContainer.style.display = 'block';
// 更新进度条
const progressBar = document.getElementById('modalUsageBar');
progressBar.style.width = `${percentage}%`;
} else {
document.getElementById('modalUsage').textContent = '-';
// 隐藏进度条容器
progressContainer.style.display = 'none';
}
modal.style.display = 'block';
}
function updateTable(data) {
const tbody = document.getElementById('logsBody');
updateStats(data);
tbody.innerHTML = data.logs.map(log => `
<tr>
<td>${new Date(log.timestamp).toLocaleString()}</td>
<td>${log.model}</td>
<td>
<button class="info-button" onclick='showTokenModal(${JSON.stringify(log.token_info)})'>
查看详情
</button>
</td>
<td>
${log.prompt ?
`<button class="info-button" onclick="showPromptModal(decodeURIComponent('${encodeURIComponent(log.prompt).replace(/'/g, "\\'")}'))">
查看对话
</button>` :
'-'
}
</td>
<td>${log.stream ? '是' : '否'}</td>
<td>${log.status}</td>
<td>${log.error || '-'}</td>
</tr>
`).join('');
}
async function fetchLogs() {
const data = await makeAuthenticatedRequest('/logs');
if (data) {
updateTable(data);
showGlobalMessage('日志获取成功');
}
}
// 自动刷新控制
document.getElementById('autoRefresh').addEventListener('change', function (e) {
if (e.target.checked) {
refreshInterval = setInterval(fetchLogs, 60000);
} else {
clearInterval(refreshInterval);
}
});
// 页面加载完成后自动获取日志
document.addEventListener('DOMContentLoaded', () => {
const authToken = getAuthToken();
if (authToken) {
document.getElementById('authToken').value = authToken;
fetchLogs();
}
// 启动自动刷新
refreshInterval = setInterval(fetchLogs, 60000);
});
// 初始化 token 处理
initializeTokenHandling('authToken');
// 添加清理逻辑
window.addEventListener('beforeunload', () => {
if (refreshInterval) {
clearInterval(refreshInterval);
}
});
// 添加模态框关闭逻辑
document.querySelectorAll('.modal .close').forEach(closeBtn => {
closeBtn.onclick = function () {
this.closest('.modal').style.display = 'none';
}
});
window.onclick = function (event) {
if (event.target.classList.contains('modal')) {
event.target.style.display = 'none';
}
}
</script>
</body>
</html>