From 6bfc4087723ea2323d1a7bbedb80e03fe6a545b9 Mon Sep 17 00:00:00 2001 From: chenpei Date: Sat, 4 Jul 2026 13:19:33 +0800 Subject: [PATCH] feat: show traffic purpose labels --- components/admin/PublicTrafficTab.tsx | 71 ++++- lib/__tests__/public-traffic-stats.test.ts | 44 ++- lib/public-traffic-stats.ts | 344 +++++++++++++++++++++ 3 files changed, 451 insertions(+), 8 deletions(-) diff --git a/components/admin/PublicTrafficTab.tsx b/components/admin/PublicTrafficTab.tsx index b22507e0..db2fb2f5 100644 --- a/components/admin/PublicTrafficTab.tsx +++ b/components/admin/PublicTrafficTab.tsx @@ -36,9 +36,22 @@ interface PublicTrafficStats { totalRequests: number statusCounts: Record }> + byPurpose: Array<{ + key: string + label: string + category: string + detail: string + responseBytes: number + requestBytes: number | null + requestBytesKnownCount: number + totalRequests: number + }> topPaths: Array<{ path: string method: string + purposeLabel: string + purposeCategory: string + purposeDetail: string responseBytes: number totalRequests: number }> @@ -306,12 +319,49 @@ export default function PublicTrafficTab({ showToast }: PublicTrafficTabProps) {
-

流量最高路径

+

流量用途汇总

- + + + + + + + + {stats.byPurpose.slice(0, 12).map((item) => ( + + + + + + + ))} + {stats.byPurpose.length === 0 && ( + + + + )} + +
路径用途类别响应流量请求次数
+
{item.label}
+
{item.detail}
+
{item.category}{formatBytes(item.responseBytes)}{formatNumber(item.totalRequests)}
+ 当前范围没有用途数据。 +
+
+
+ +
+

流量最高路径明细

+
+ + + + + @@ -320,15 +370,22 @@ export default function PublicTrafficTab({ showToast }: PublicTrafficTabProps) { {stats.topPaths.map((item) => ( - - - - + + + + + ))} {stats.topPaths.length === 0 && ( - diff --git a/lib/__tests__/public-traffic-stats.test.ts b/lib/__tests__/public-traffic-stats.test.ts index d3a45ef3..b8e9c8fd 100644 --- a/lib/__tests__/public-traffic-stats.test.ts +++ b/lib/__tests__/public-traffic-stats.test.ts @@ -1,7 +1,12 @@ import { mkdtempSync, rmSync, writeFileSync } from 'fs' import os from 'os' import path from 'path' -import { getPublicTrafficStats, normalizeTrafficRange, parsePublicTrafficLogLine } from '@/lib/public-traffic-stats' +import { + classifyPublicTrafficPurpose, + getPublicTrafficStats, + normalizeTrafficRange, + parsePublicTrafficLogLine, +} from '@/lib/public-traffic-stats' describe('public traffic stats', () => { test('parses standard nginx access log response bytes', () => { @@ -68,6 +73,13 @@ describe('public traffic stats', () => { totalRequests: 1, }), ]) + expect(stats.byPurpose).toEqual([ + expect.objectContaining({ + key: 'static-asset', + label: '图片直链访问', + category: '资源文件', + }), + ]) } finally { if (previousPaths === undefined) { delete process.env.PUBLIC_TRAFFIC_LOG_PATHS @@ -77,4 +89,34 @@ describe('public traffic stats', () => { rmSync(tempDir, { recursive: true, force: true }) } }) + + test('classifies technical paths into human-readable business purposes', () => { + expect(classifyPublicTrafficPurpose('sc.jiansun.vip', '/api/materials/download', 'GET')).toEqual( + expect.objectContaining({ + key: 'material-download', + label: '素材下载/打包下载', + }), + ) + + expect(classifyPublicTrafficPurpose('cp.jiansun.vip', '/api/intake/non-integrated', 'GET')).toEqual( + expect.objectContaining({ + key: 'product-intake-non-integrated', + label: '产品设计平台未集成素材列表', + }), + ) + + expect( + classifyPublicTrafficPurpose( + 's3.jiansun.vip', + '/gongxiangpan/uploads/direct/d685fa51-d8eb-42ea-b471-4a91269a7a90/1783137399966-p0j9wv.jpg', + 'GET', + ), + ).toEqual( + expect.objectContaining({ + key: 'direct-upload-asset', + label: '图片直链访问', + category: '直传资源', + }), + ) + }) }) diff --git a/lib/public-traffic-stats.ts b/lib/public-traffic-stats.ts index 79b35f0d..dc9ef850 100644 --- a/lib/public-traffic-stats.ts +++ b/lib/public-traffic-stats.ts @@ -26,6 +26,13 @@ export interface PublicTrafficLogEntry { requestBytes: number | null } +export interface PublicTrafficPurpose { + key: string + label: string + category: string + detail: string +} + export interface PublicTrafficStats { generatedAt: string available: boolean @@ -58,9 +65,22 @@ export interface PublicTrafficStats { totalRequests: number statusCounts: Record }> + byPurpose: Array<{ + key: string + label: string + category: string + detail: string + responseBytes: number + requestBytes: number | null + requestBytesKnownCount: number + totalRequests: number + }> topPaths: Array<{ path: string method: string + purposeLabel: string + purposeCategory: string + purposeDetail: string responseBytes: number totalRequests: number }> @@ -195,6 +215,303 @@ function parseNumber(value: unknown): number | null { return null } +function safeDecodePath(value: string): string { + try { + return decodeURIComponent(value) + } catch { + return value + } +} + +function isAssetPath(pathValue: string): boolean { + return /\.(?:avif|webp|png|jpe?g|gif|svg|mp4|webm|mp3|wav|zip|rar|7z|pdf|psd|ai|cdr)$/i.test(pathValue) +} + +function assetTypeLabel(pathValue: string): string { + if (/\.(?:avif|webp|png|jpe?g|gif|svg)$/i.test(pathValue)) return '图片直链访问' + if (/\.(?:mp4|webm)$/i.test(pathValue)) return '视频直链访问' + if (/\.(?:mp3|wav)$/i.test(pathValue)) return '音频直链访问' + if (/\.(?:zip|rar|7z)$/i.test(pathValue)) return '压缩包直链访问' + if (/\.(?:pdf|psd|ai|cdr)$/i.test(pathValue)) return '设计文件直链访问' + return '资源文件直链访问' +} + +function fileNameFromPath(pathValue: string): string { + const rawName = pathValue.split('/').filter(Boolean).pop() || pathValue + return safeDecodePath(rawName) +} + +export function classifyPublicTrafficPurpose(domain: string, pathValue: string, method = 'GET'): PublicTrafficPurpose { + const pathLower = pathValue.toLowerCase() + const domainLower = domain.toLowerCase() + const methodUpper = method.toUpperCase() + + if (pathLower === '/api/materials/download') { + return { + key: 'material-download', + label: '素材下载/打包下载', + category: '素材库', + detail: '用户下载素材原文件、压缩包或分享页下载时产生的流量。', + } + } + + if (pathLower === '/api/external/download') { + return { + key: 'external-material-download', + label: '外部平台素材下载', + category: '外部接口', + detail: '脉象、产品平台等外部系统通过下载 token 拉取素材文件。', + } + } + + if (pathLower.startsWith('/api/external/')) { + return { + key: 'external-api', + label: '外部平台对接接口', + category: '外部接口', + detail: '外部系统查询素材、SPU、SKU、条码或图片信息。', + } + } + + if (domainLower === 'cp.jiansun.vip') { + if (pathLower === '/api/intake/non-integrated') { + return { + key: 'product-intake-non-integrated', + label: '产品设计平台未集成素材列表', + category: '产品设计平台', + detail: '产品设计平台查看未集成素材或待处理录入内容。', + } + } + if (pathLower === '/api/intake/product-patents') { + return { + key: 'product-intake-submit', + label: '产品设计平台资料提交', + category: '产品设计平台', + detail: '产品设计平台提交产品图片、专利或录入资料。', + } + } + if (pathLower === '/api/storage/object') { + return { + key: 'product-storage-object', + label: '产品设计平台取图跳转', + category: '产品设计平台', + detail: '产品设计平台请求对象文件地址,通常后续由存储或 CDN 承接真实图片流量。', + } + } + if (pathLower.startsWith('/api/intake/')) { + return { + key: 'product-intake-api', + label: '产品设计平台录入接口', + category: '产品设计平台', + detail: '产品设计平台录入、查询或处理产品资料的接口请求。', + } + } + if (pathLower.startsWith('/api/')) { + return { + key: 'product-platform-api', + label: '产品设计平台业务接口', + category: '产品设计平台', + detail: '产品设计平台页面调用的业务接口。', + } + } + return { + key: 'product-platform-page', + label: '产品设计平台页面访问', + category: '产品设计平台', + detail: '用户打开产品设计平台页面或加载前端静态资源。', + } + } + + if (pathLower.startsWith('/gongxiangpan/public-display/white-background/')) { + return { + key: 'white-background-preview', + label: '标准模型白底图预览', + category: '资源直链', + detail: '白底图等标准模型展示图片从资源入口直接返回。', + } + } + + if (pathLower.startsWith('/gongxiangpan/public-display/task-detail/')) { + return { + key: 'task-detail-preview', + label: '任务详情素材预览图', + category: '资源直链', + detail: '素材详情、主图、SKU 图、详情图等预览资源直链访问。', + } + } + + if (pathLower.startsWith('/gongxiangpan/public-display/materials/')) { + return { + key: 'material-preview', + label: '素材预览图访问', + category: '资源直链', + detail: '素材库列表、详情或分享页展示预览图。', + } + } + + if (pathLower.startsWith('/gongxiangpan/uploads/direct/')) { + return { + key: 'direct-upload-asset', + label: assetTypeLabel(pathValue), + category: '直传资源', + detail: `直传上传目录里的资源文件:${fileNameFromPath(pathValue)}`, + } + } + + if (pathLower.startsWith('/gongxiangpan/uploads/')) { + return { + key: 'legacy-upload-asset', + label: assetTypeLabel(pathValue), + category: '历史上传资源', + detail: `历史上传目录里的资源文件:${fileNameFromPath(pathValue)}`, + } + } + + if (pathLower.startsWith('/gongxiangpan/product-package/')) { + return { + key: 'product-package-asset', + label: '产品包资源访问', + category: '资源直链', + detail: '产品包、原始文件或成套资源通过资源入口直接访问。', + } + } + + if (pathLower.startsWith('/gongxiangpan/') || domainLower === 's3.jiansun.vip') { + return { + key: 'storage-asset', + label: isAssetPath(pathValue) ? assetTypeLabel(pathValue) : '资源存储入口访问', + category: '资源直链', + detail: isAssetPath(pathValue) + ? `资源入口直接返回文件:${fileNameFromPath(pathValue)}` + : '资源入口上的对象存储、探测或目录类请求。', + } + } + + if (pathLower.startsWith('/api/materials/upload/')) { + return { + key: 'material-upload', + label: methodUpper === 'GET' ? '上传状态查询' : '素材上传接口', + category: '素材库', + detail: '素材上传分片、合并、刷新或上传状态相关请求。', + } + } + + if (pathLower === '/api/materials') { + return { + key: 'material-list-search', + label: '素材列表/搜索接口', + category: '素材库', + detail: '首页、素材列表或筛选搜索素材时加载的数据。', + } + } + + if (pathLower.startsWith('/api/materials/')) { + return { + key: 'material-detail-api', + label: '素材详情/管理接口', + category: '素材库', + detail: '素材详情、SKU、资产排序、恢复、收藏、点赞等接口请求。', + } + } + + if (pathLower === '/api/asset-center/substrates') { + return { + key: 'asset-center-substrates', + label: '资产中心基底素材列表', + category: '素材库', + detail: '资产中心加载基底素材、贴图或可选素材列表。', + } + } + + if (pathLower.startsWith('/api/asset-center/')) { + return { + key: 'asset-center-api', + label: '资产中心接口', + category: '素材库', + detail: '资产中心页面的数据查询或操作接口。', + } + } + + if (pathLower === '/api/rankings') { + return { + key: 'home-ranking', + label: '首页排行榜接口', + category: '素材库', + detail: '首页展示热门、排行或推荐素材数据。', + } + } + + if (pathLower.startsWith('/api/auth/') || pathLower.startsWith('/api/dingtalk/') || pathLower === '/dingtalk-callback') { + return { + key: 'login-auth', + label: '登录/钉钉认证', + category: '账号', + detail: '登录状态、钉钉回调、会话检查等账号认证请求。', + } + } + + if (pathLower.startsWith('/api/performance-survey') || pathLower.startsWith('/performance-survey')) { + return { + key: 'performance-survey', + label: '考核调研功能', + category: '素材库', + detail: '考核调研页面、列表、提交和结果查看相关请求。', + } + } + + if (pathLower.startsWith('/admin') || pathLower.startsWith('/api/admin/')) { + return { + key: 'admin-console', + label: '后台管理功能', + category: '后台', + detail: '管理员后台页面和管理接口请求。', + } + } + + if (pathLower.startsWith('/share/')) { + return { + key: 'share-page', + label: '素材分享页', + category: '素材库', + detail: '外部或内部用户打开素材分享页面。', + } + } + + if (pathLower.startsWith('/_next/') || pathLower === '/favicon.ico' || pathLower === '/api/favicon') { + return { + key: 'frontend-static', + label: '前端静态资源', + category: '页面加载', + detail: '页面 JS、CSS、图标等前端基础资源。', + } + } + + if (isAssetPath(pathValue)) { + return { + key: 'static-asset', + label: assetTypeLabel(pathValue), + category: '资源文件', + detail: `直接访问的文件:${fileNameFromPath(pathValue)}`, + } + } + + if (pathLower.startsWith('/api/')) { + return { + key: 'other-api', + label: '其他业务接口', + category: '其他', + detail: '尚未细分到具体模块的接口请求。', + } + } + + return { + key: 'other-page', + label: '其他页面访问', + category: '其他', + detail: '尚未细分到具体模块的页面或资源请求。', + } +} + function parseJsonLogLine(line: string, fallbackDomain: string): PublicTrafficLogEntry | null { try { const raw = JSON.parse(line) as Record @@ -326,12 +643,37 @@ function addEntry(stats: PublicTrafficStats, uniqueIps: Set, entry: Publ domain.requestBytesKnownCount += 1 } + const purpose = classifyPublicTrafficPurpose(entry.domain, entry.path, entry.method) + let purposeStat = stats.byPurpose.find((item) => item.key === purpose.key) + if (!purposeStat) { + purposeStat = { + key: purpose.key, + label: purpose.label, + category: purpose.category, + detail: purpose.detail, + responseBytes: 0, + requestBytes: null, + requestBytesKnownCount: 0, + totalRequests: 0, + } + stats.byPurpose.push(purposeStat) + } + purposeStat.responseBytes += entry.responseBytes + purposeStat.totalRequests += 1 + if (entry.requestBytes !== null) { + purposeStat.requestBytes = (purposeStat.requestBytes || 0) + entry.requestBytes + purposeStat.requestBytesKnownCount += 1 + } + const pathKey = `${entry.method} ${entry.path}` let topPath = stats.topPaths.find((item) => `${item.method} ${item.path}` === pathKey) if (!topPath) { topPath = { method: entry.method, path: entry.path, + purposeLabel: purpose.label, + purposeCategory: purpose.category, + purposeDetail: purpose.detail, responseBytes: 0, totalRequests: 0, } @@ -411,6 +753,7 @@ export async function getPublicTrafficStats(range: PublicTrafficRange): Promise< }, byDay: [], byDomain: [], + byPurpose: [], topPaths: [], source: { logPaths, @@ -442,6 +785,7 @@ export async function getPublicTrafficStats(range: PublicTrafficRange): Promise< stats.source.requestBytesAvailable = stats.summary.requestBytesKnownCount > 0 stats.byDay.sort((a, b) => a.date.localeCompare(b.date)) stats.byDomain.sort((a, b) => b.responseBytes - a.responseBytes) + stats.byPurpose.sort((a, b) => b.responseBytes - a.responseBytes) stats.topPaths.sort((a, b) => b.responseBytes - a.responseBytes) stats.topPaths = stats.topPaths.slice(0, 20)
用途和原始路径类别 方法 响应流量 请求次数
{item.path}{item.method}{formatBytes(item.responseBytes)}{formatNumber(item.totalRequests)} +
{item.purposeLabel}
+
{item.purposeDetail}
+
+ {item.path} +
+
{item.purposeCategory}{item.method}{formatBytes(item.responseBytes)}{formatNumber(item.totalRequests)}
+ 当前范围没有路径数据。