refactor(admin): 简化统计查询逻辑,移除域名提取

移除 extractDomain 函数,直接使用 site_id 字段进行分组统计。
简化 SQL 查询,不再获取 post_slug 和 post_url 字段。
This commit is contained in:
anghunk
2026-02-09 15:58:31 +08:00
parent fea594af0d
commit 67a2777ea8

View File

@@ -12,31 +12,12 @@ type DomainCounts = StatusCounts & {
domain: string; 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 }>) => { export const getStats = async (c: Context<{ Bindings: Bindings }>) => {
try { try {
const rawSiteId = c.req.query('siteId'); const rawSiteId = c.req.query('siteId');
const siteId = rawSiteId && rawSiteId !== 'default' ? rawSiteId : null; 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[] = []; const params: any[] = [];
if (siteId) { 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<{ const { results } = await c.env.CWD_DB.prepare(sql).bind(...params).all<{
created: number; created: number;
post_slug: string;
post_url: string | null;
status: string; status: string;
site_id: string | null;
}>(); }>();
const summary: StatusCounts = { const summary: StatusCounts = {
@@ -65,10 +45,9 @@ export const getStats = async (c: Context<{ Bindings: Bindings }>) => {
const thirtyDaysAgo = now - 29 * 24 * 60 * 60 * 1000; const thirtyDaysAgo = now - 29 * 24 * 60 * 60 * 1000;
for (const row of results) { for (const row of results) {
const domain = const domainKey = row.site_id && row.site_id.trim() ? row.site_id.trim() : 'default';
extractDomain(row.post_url) || extractDomain(row.post_slug) || 'unknown';
let counts = domainMap.get(domain); let counts = domainMap.get(domainKey);
if (!counts) { if (!counts) {
counts = { counts = {
total: 0, total: 0,
@@ -76,7 +55,7 @@ export const getStats = async (c: Context<{ Bindings: Bindings }>) => {
pending: 0, pending: 0,
rejected: 0 rejected: 0
}; };
domainMap.set(domain, counts); domainMap.set(domainKey, counts);
} }
counts.total += 1; counts.total += 1;
if (row.status === 'approved') { if (row.status === 'approved') {