()
| 2890 | // 使用统计 |
| 2891 | // ===================================================================== |
| 2892 | async function refreshUsageStats() { |
| 2893 | const loading = document.getElementById('usageLoading'); |
| 2894 | const list = document.getElementById('usageList'); |
| 2895 | |
| 2896 | try { |
| 2897 | loading.style.display = 'block'; |
| 2898 | list.innerHTML = ''; |
| 2899 | |
| 2900 | const [statsResponse, aggregatedResponse] = await Promise.all([ |
| 2901 | fetch('./usage/stats', { headers: getAuthHeaders() }), |
| 2902 | fetch('./usage/aggregated', { headers: getAuthHeaders() }) |
| 2903 | ]); |
| 2904 | |
| 2905 | if (statsResponse.status === 401 || aggregatedResponse.status === 401) { |
| 2906 | showStatus('认证失败,请重新登录', 'error'); |
| 2907 | setTimeout(() => location.reload(), 1500); |
| 2908 | return; |
| 2909 | } |
| 2910 | |
| 2911 | const statsData = await statsResponse.json(); |
| 2912 | const aggregatedData = await aggregatedResponse.json(); |
| 2913 | |
| 2914 | if (statsResponse.ok && aggregatedResponse.ok) { |
| 2915 | AppState.usageStatsData = statsData.success ? statsData.data : statsData; |
| 2916 | |
| 2917 | const aggData = aggregatedData.success ? aggregatedData.data : aggregatedData; |
| 2918 | document.getElementById('totalApiCalls').textContent = aggData.total_calls_24h || 0; |
| 2919 | document.getElementById('totalFiles').textContent = aggData.total_files || 0; |
| 2920 | document.getElementById('avgCallsPerFile').textContent = (aggData.avg_calls_per_file || 0).toFixed(1); |
| 2921 | |
| 2922 | renderUsageList(); |
| 2923 | |
| 2924 | showStatus(`已加载 ${aggData.total_files || Object.keys(AppState.usageStatsData).length} 个文件的使用统计`, 'success'); |
| 2925 | } else { |
| 2926 | const errorMsg = statsData.detail || aggregatedData.detail || '加载使用统计失败'; |
| 2927 | showStatus(`错误: ${errorMsg}`, 'error'); |
| 2928 | } |
| 2929 | } catch (error) { |
| 2930 | showStatus(`网络错误: ${error.message}`, 'error'); |
| 2931 | } finally { |
| 2932 | loading.style.display = 'none'; |
| 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | function renderUsageList() { |
| 2937 | const list = document.getElementById('usageList'); |
no test coverage detected