From 11e03d1bd7106d968a12e2be062dcca5602a55c2 Mon Sep 17 00:00:00 2001 From: seaHi Date: Fri, 25 Sep 2026 11:38:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E7=AE=A1=E7=90=86=E9=9D=A2=E6=9D=BF=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E6=B5=81=E9=87=8F=E7=BB=9F=E8=AE=A1=E5=92=8C=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E6=B6=88=E8=80=97=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/admin/AnalyticsPanel.svelte | 148 +++++++++++++----- src/routes/admin/settings/+page.server.ts | 18 +-- src/routes/admin/settings/+page.svelte | 2 +- 3 files changed, 113 insertions(+), 55 deletions(-) diff --git a/src/lib/components/admin/AnalyticsPanel.svelte b/src/lib/components/admin/AnalyticsPanel.svelte index 82e7aac..eadbf1d 100644 --- a/src/lib/components/admin/AnalyticsPanel.svelte +++ b/src/lib/components/admin/AnalyticsPanel.svelte @@ -1,33 +1,60 @@
-
-
-
文章总数
-
{dbStats.posts}
-
-
-
页面总数
-
{dbStats.pages}
-
-
-
分类 / 标签
-
{dbStats.categories} / {dbStats.tags}
-
-
- {#if loading}
@@ -151,18 +169,50 @@
{error}
- {:else if !analyticsData || analyticsData.length === 0} + {:else if !trafficData || trafficData.length === 0}
没有获取到有效的流量数据,请确保 CF_API_TOKEN 和 CF_ZONE_ID 配置正确,且网站已有访问。
{:else} +
Cloudflare 资源消耗 (近30天)
+ {#if missingEnvVars} +
+ 💡 提示:如需查看 D1 数据库读写和 R2 存储用量,请在系统环境变量中补充 CF_ACCOUNT_ID, CF_D1_ID 和 CF_R2_BUCKET。 +
+ {/if}
-
总请求数 (30天)
+
D1 数据库读取行数
+
{d1Data.length > 0 ? formatNumber(d1RowsRead) : '-'}
+
+
+
D1 数据库写入行数
+
{d1Data.length > 0 ? formatNumber(d1RowsWritten) : '-'}
+
+
+
R2 存储空间用量
+
{r2Data.length > 0 ? formatBytes(r2PayloadSize) : '-'}
+
+
+ +
网站流量统计 (近30天)
+
+
+
页面浏览量 (PV)
+
{totalPV.toLocaleString()}
+
+
+
独立访客 (UV)
+
{totalUV.toLocaleString()}
+
+
+
总请求数
{totalRequests.toLocaleString()}
-
总下发流量 (30天)
+
总下发流量
{formatBytes(totalBytes)}
@@ -175,7 +225,7 @@
-

流量与缓存趋势 (近30天)

+

流量趋势 (近30天)

@@ -190,6 +240,30 @@ gap: 24px; } + .section-title { + font-size: 1.2rem; + font-weight: 600; + color: var(--text-color); + margin-bottom: -10px; + } + + .info-alert { + padding: 12px 16px; + background: rgba(33, 150, 243, 0.1); + border-left: 4px solid #2196f3; + border-radius: 4px; + font-size: 0.9rem; + color: var(--text-color); + margin-bottom: 5px; + } + + .info-alert code { + background: rgba(0, 0, 0, 0.05); + padding: 2px 6px; + border-radius: 3px; + font-family: monospace; + } + .stats-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); diff --git a/src/routes/admin/settings/+page.server.ts b/src/routes/admin/settings/+page.server.ts index a09d1ab..4761f3d 100644 --- a/src/routes/admin/settings/+page.server.ts +++ b/src/routes/admin/settings/+page.server.ts @@ -8,29 +8,14 @@ export const load: PageServerLoad = async ({ locals, platform }) => { const db = platform?.env?.DB; const settings: Record = {}; - let dbStats = { posts: 0, pages: 0, tags: 0, categories: 0 }; if (db) { try { const { results } = await db.prepare('SELECT key, value FROM settings').all(); results.forEach((row: any) => { settings[row.key] = row.value; }); - - const [postsRes, pagesRes, tagsRes, categoriesRes] = await db.batch([ - db.prepare('SELECT COUNT(*) as count FROM posts'), - db.prepare('SELECT COUNT(*) as count FROM pages'), - db.prepare('SELECT COUNT(*) as count FROM tags'), - db.prepare('SELECT COUNT(*) as count FROM categories') - ]); - - dbStats = { - posts: (postsRes.results[0] as any).count || 0, - pages: (pagesRes.results[0] as any).count || 0, - tags: (tagsRes.results[0] as any).count || 0, - categories: (categoriesRes.results[0] as any).count || 0 - }; } catch (err: any) { - console.error('Failed to load settings or stats', err); + console.error('Failed to load settings', err); } } @@ -38,7 +23,6 @@ export const load: PageServerLoad = async ({ locals, platform }) => { user: locals.user, platformEnv: db ? 'Cloudflare D1 (Connected)' : 'Local Mock', settings, - dbStats, analytics: fetchAnalytics(platform) // Streaming promise }; }; diff --git a/src/routes/admin/settings/+page.svelte b/src/routes/admin/settings/+page.svelte index 204a380..1153c98 100644 --- a/src/routes/admin/settings/+page.svelte +++ b/src/routes/admin/settings/+page.svelte @@ -321,7 +321,7 @@
{:else if activeTab === 'analytics'} - + {/if}