From 67a2777ea8fca17d3be28ef88bb777455b419ea6 Mon Sep 17 00:00:00 2001 From: anghunk Date: Mon, 9 Feb 2026 15:58:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor(admin):=20=E7=AE=80=E5=8C=96=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E6=9F=A5=E8=AF=A2=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=9F=9F=E5=90=8D=E6=8F=90=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除 extractDomain 函数,直接使用 site_id 字段进行分组统计。 简化 SQL 查询,不再获取 post_slug 和 post_url 字段。 --- cwd-api/src/api/admin/getStats.ts | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/cwd-api/src/api/admin/getStats.ts b/cwd-api/src/api/admin/getStats.ts index 79120b1..a0f23a5 100644 --- a/cwd-api/src/api/admin/getStats.ts +++ b/cwd-api/src/api/admin/getStats.ts @@ -12,31 +12,12 @@ type DomainCounts = StatusCounts & { domain: string; }; -function extractDomain(source: string | null | undefined): string | null { - if (!source) { - return null; - } - const value = source.trim(); - if (!value) { - return null; - } - if (!/^https?:\/\//i.test(value)) { - return null; - } - try { - const url = new URL(value); - return url.hostname.toLowerCase(); - } catch { - return null; - } -} - export const getStats = async (c: Context<{ Bindings: Bindings }>) => { try { const rawSiteId = c.req.query('siteId'); const siteId = rawSiteId && rawSiteId !== 'default' ? rawSiteId : null; - let sql = 'SELECT created, post_slug, post_url, status FROM Comment'; + let sql = 'SELECT created, status, site_id FROM Comment'; const params: any[] = []; if (siteId) { @@ -46,9 +27,8 @@ export const getStats = async (c: Context<{ Bindings: Bindings }>) => { const { results } = await c.env.CWD_DB.prepare(sql).bind(...params).all<{ created: number; - post_slug: string; - post_url: string | null; status: string; + site_id: string | null; }>(); const summary: StatusCounts = { @@ -65,10 +45,9 @@ export const getStats = async (c: Context<{ Bindings: Bindings }>) => { const thirtyDaysAgo = now - 29 * 24 * 60 * 60 * 1000; for (const row of results) { - const domain = - extractDomain(row.post_url) || extractDomain(row.post_slug) || 'unknown'; + const domainKey = row.site_id && row.site_id.trim() ? row.site_id.trim() : 'default'; - let counts = domainMap.get(domain); + let counts = domainMap.get(domainKey); if (!counts) { counts = { total: 0, @@ -76,7 +55,7 @@ export const getStats = async (c: Context<{ Bindings: Bindings }>) => { pending: 0, rejected: 0 }; - domainMap.set(domain, counts); + domainMap.set(domainKey, counts); } counts.total += 1; if (row.status === 'approved') {