gongxiangp2/scripts/analyze-missing-files-report.ts

273 lines
9.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 深入分析缺失源文件报告
*/
import { readFile, writeFile } from 'fs/promises'
interface MissingReportRow {
'素材ID': string
'素材类型': string
'产品型号(SPU)': string
'作者': string
'使用店铺': string
'场景类别': string
'平台分类': string
'品类分类': string
'渲染器': string
'源文件路径': string
'源文件名': string
'源文件大小(字节)': string
'文件是否存在': string
'创建时间': string
'更新时间': string
}
// 简单的CSV解析函数
function parseCSVLine(line: string): string[] {
const result: string[] = []
let current = ''
let inQuotes = false
for (let i = 0; i < line.length; i++) {
const char = line[i]
if (char === '"') {
if (inQuotes && line[i + 1] === '"') {
current += '"'
i++
} else {
inQuotes = !inQuotes
}
} else if (char === ',' && !inQuotes) {
result.push(current)
current = ''
} else {
current += char
}
}
result.push(current)
return result
}
async function main() {
console.log('='.repeat(60))
console.log('缺失源文件报告深度分析')
console.log('='.repeat(60))
console.log()
// 读取报告
console.log('📋 读取缺失源文件报告...')
let reportData: MissingReportRow[] = []
try {
const content = await readFile('缺失源文件报告.csv', 'utf-8')
const lines = content.split('\n').filter(line => line.trim())
if (lines.length < 2) {
throw new Error('CSV文件格式错误')
}
const headers = parseCSVLine(lines[0]).map(h => h.trim())
for (let i = 1; i < lines.length; i++) {
const values = parseCSVLine(lines[i])
const row: any = {}
headers.forEach((header, index) => {
row[header] = (values[index] || '').trim()
})
reportData.push(row as MissingReportRow)
}
console.log(` 读取到 ${reportData.length} 条记录`)
} catch (error) {
console.error('❌ 无法读取报告:', error)
process.exit(1)
}
// 分类统计
const existsOnExternal = reportData.filter(r => r['文件是否存在'] === '是(外网服务器)')
const stillMissing = reportData.filter(r => r['文件是否存在'] === '否')
console.log()
console.log('📊 总体统计:')
console.log(` 总记录数: ${reportData.length}`)
console.log(` 外网服务器存在: ${existsOnExternal.length} 个`)
console.log(` 仍然缺失: ${stillMissing.length} 个`)
console.log()
// 1. 按素材类型分析
console.log('📦 按素材类型分析:')
const typeStats: Record<string, { total: number; external: number; missing: number; totalSize: number }> = {}
for (const row of reportData) {
const type = row['素材类型'] || '未知'
if (!typeStats[type]) {
typeStats[type] = { total: 0, external: 0, missing: 0, totalSize: 0 }
}
typeStats[type].total++
const size = parseInt(row['源文件大小(字节)'] || '0', 10)
typeStats[type].totalSize += size
if (row['文件是否存在'] === '是(外网服务器)') {
typeStats[type].external++
} else {
typeStats[type].missing++
}
}
for (const [type, stats] of Object.entries(typeStats).sort((a, b) => b[1].total - a[1].total)) {
const totalGB = stats.totalSize / (1024 * 1024 * 1024)
const externalPercent = (stats.external / stats.total * 100).toFixed(1)
const missingPercent = (stats.missing / stats.total * 100).toFixed(1)
console.log(` ${type}:`)
console.log(` 总数: ${stats.total}, 外网存在: ${stats.external} (${externalPercent}%), 缺失: ${stats.missing} (${missingPercent}%)`)
console.log(` 总大小: ${totalGB.toFixed(2)} GB`)
}
console.log()
// 2. 按作者分析(缺失文件)
console.log('👤 按作者分析(仍然缺失的文件):')
const authorStats: Record<string, number> = {}
for (const row of stillMissing) {
const author = row['作者'] || '未知'
authorStats[author] = (authorStats[author] || 0) + 1
}
const topMissingAuthors = Object.entries(authorStats)
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
for (const [author, count] of topMissingAuthors) {
console.log(` ${author}: ${count} 个缺失文件`)
}
console.log()
// 3. 按店铺分析
console.log('🏪 按店铺分析:')
const shopStats: Record<string, { total: number; external: number; missing: number }> = {}
for (const row of reportData) {
const shop = row['使用店铺'] || '未知'
if (!shopStats[shop]) {
shopStats[shop] = { total: 0, external: 0, missing: 0 }
}
shopStats[shop].total++
if (row['文件是否存在'] === '是(外网服务器)') {
shopStats[shop].external++
} else {
shopStats[shop].missing++
}
}
for (const [shop, stats] of Object.entries(shopStats).sort((a, b) => b[1].missing - a[1].missing)) {
const missingPercent = (stats.missing / stats.total * 100).toFixed(1)
console.log(` ${shop}: 总数 ${stats.total}, 外网存在 ${stats.external}, 缺失 ${stats.missing} (${missingPercent}%)`)
}
console.log()
// 4. 按创建时间分析
console.log('📅 按创建时间分析(缺失文件):')
const dateStats: Record<string, number> = {}
for (const row of stillMissing) {
const dateStr = row['创建时间'] || ''
if (dateStr) {
const date = new Date(dateStr)
const yearMonth = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`
dateStats[yearMonth] = (dateStats[yearMonth] || 0) + 1
}
}
const sortedDates = Object.entries(dateStats)
.sort((a, b) => a[0].localeCompare(b[0]))
.slice(-12) // 最近12个月
for (const [date, count] of sortedDates) {
console.log(` ${date}: ${count} 个缺失文件`)
}
console.log()
// 5. 文件大小分析
console.log('📏 文件大小分析:')
const externalSizes = existsOnExternal
.map(r => parseInt(r['源文件大小(字节)'] || '0', 10))
.filter(s => s > 0)
.sort((a, b) => b - a)
const missingSizes = stillMissing
.map(r => parseInt(r['源文件大小(字节)'] || '0', 10))
.filter(s => s > 0)
.sort((a, b) => b - a)
if (externalSizes.length > 0) {
const externalTotal = externalSizes.reduce((a, b) => a + b, 0)
const externalAvg = externalTotal / externalSizes.length
const externalMax = externalSizes[0]
const externalMin = externalSizes[externalSizes.length - 1]
console.log(` 外网服务器存在:`)
console.log(` 总数: ${externalSizes.length} 个`)
console.log(` 总大小: ${(externalTotal / (1024 * 1024 * 1024)).toFixed(2)} GB`)
console.log(` 平均大小: ${(externalAvg / (1024 * 1024)).toFixed(2)} MB`)
console.log(` 最大: ${(externalMax / (1024 * 1024)).toFixed(2)} MB`)
console.log(` 最小: ${(externalMin / (1024 * 1024)).toFixed(2)} MB`)
}
if (missingSizes.length > 0) {
const missingTotal = missingSizes.reduce((a, b) => a + b, 0)
const missingAvg = missingTotal / missingSizes.length
const missingMax = missingSizes[0]
const missingMin = missingSizes[missingSizes.length - 1]
console.log(` 仍然缺失:`)
console.log(` 总数: ${missingSizes.length} 个`)
console.log(` 总大小: ${(missingTotal / (1024 * 1024 * 1024)).toFixed(2)} GB`)
console.log(` 平均大小: ${(missingAvg / (1024 * 1024)).toFixed(2)} MB`)
console.log(` 最大: ${(missingMax / (1024 * 1024)).toFixed(2)} MB`)
console.log(` 最小: ${(missingMin / (1024 * 1024)).toFixed(2)} MB`)
}
console.log()
// 6. 生成详细分析报告
console.log('💾 生成详细分析报告...')
const analysisRows: string[] = []
analysisRows.push('分析项,数值')
analysisRows.push(`总记录数,${reportData.length}`)
analysisRows.push(`外网服务器存在,${existsOnExternal.length}`)
analysisRows.push(`仍然缺失,${stillMissing.length}`)
analysisRows.push('')
analysisRows.push('按素材类型统计,总数,外网存在,缺失,总大小(GB)')
for (const [type, stats] of Object.entries(typeStats).sort((a, b) => b[1].total - a[1].total)) {
const totalGB = stats.totalSize / (1024 * 1024 * 1024)
analysisRows.push(`${type},${stats.total},${stats.external},${stats.missing},${totalGB.toFixed(2)}`)
}
analysisRows.push('')
analysisRows.push('按店铺统计(缺失文件最多的前10个),店铺,缺失数')
const topMissingShops = Object.entries(shopStats)
.sort((a, b) => b[1].missing - a[1].missing)
.slice(0, 10)
for (const [shop, stats] of topMissingShops) {
analysisRows.push(`${shop},${stats.missing}`)
}
analysisRows.push('')
analysisRows.push('按作者统计(缺失文件最多的前10个),作者,缺失数')
for (const [author, count] of topMissingAuthors) {
analysisRows.push(`${author},${count}`)
}
const outputDir = process.env.IS_IN_CONTAINER === 'true' ? '/tmp' : '.'
const outputFile = `${outputDir}/缺失源文件分析报告.csv`
await writeFile(outputFile, analysisRows.join('\n'), 'utf-8')
console.log(`✅ 分析报告已生成: ${outputFile}`)
console.log()
}
main().catch((error) => {
console.error('执行失败:', error)
process.exit(1)
})