File size: 5,064 Bytes
c3aa9b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node

/**
 * Generate comprehensive cache analytics report
 * Shows detailed statistics and insights about cached documentation
 */

import { CacheService } from '../services/cache-service.js';
import dotenv from 'dotenv';

dotenv.config();

const cacheService = new CacheService(process.env.PAGE_CACHE_PATH || './data/page-cache.db');

console.log('\n📊 Cache Analytics Report\n');
console.log('═══════════════════════════════════════════════════════════════\n');

try {
  await cacheService.initialize();
  
  const report = cacheService.generateAnalyticsReport();
  
  // Overall Statistics
  console.log('📈 Overall Statistics:');
  console.log(`   Total Pages: ${report.overall.total}`);
  console.log(`   Indexed Pages: ${report.overall.indexed} (${Math.round(100 * report.overall.indexed / report.overall.total)}%)`);
  console.log(`   Not Indexed: ${report.overall.total - report.overall.indexed}`);
  
  if (report.overall.oldest_check) {
    const oldestDate = new Date(report.overall.oldest_check);
    const newestDate = new Date(report.overall.newest_check);
    const avgDate = new Date(report.overall.avg_check);
    
    console.log(`   Oldest Check: ${oldestDate.toLocaleDateString()} ${oldestDate.toLocaleTimeString()}`);
    console.log(`   Newest Check: ${newestDate.toLocaleDateString()} ${newestDate.toLocaleTimeString()}`);
    console.log(`   Average Check: ${avgDate.toLocaleDateString()} ${avgDate.toLocaleTimeString()}`);
  }
  
  // By Source
  console.log('\n📚 By Source:');
  for (const source of report.bySource) {
    const percentage = Math.round(100 * source.indexed_pages / source.total_pages);
    const lastUpdate = source.last_updated ? new Date(source.last_updated).toLocaleDateString() : 'Never';
    console.log(`   ${source.source.padEnd(12)} ${source.indexed_pages}/${source.total_pages} indexed (${percentage}%) - Last: ${lastUpdate}`);
  }
  
  // Recent Activity
  console.log('\n🕐 Recent Activity (Last 24 Hours):');
  if (report.recentActivity.length === 0) {
    console.log('   No recent activity');
  } else {
    for (const activity of report.recentActivity) {
      console.log(`   ${activity.source.padEnd(12)} ${activity.count} pages updated`);
    }
  }
  
  // Stale Pages
  console.log('\n⚠️  Stale Pages (Not Checked in 7 Days):');
  if (report.stalePages.length === 0) {
    console.log('   No stale pages - excellent!');
  } else {
    let totalStale = 0;
    for (const stale of report.stalePages) {
      console.log(`   ${stale.source.padEnd(12)} ${stale.count} pages`);
      totalStale += stale.count;
    }
    console.log(`   Total: ${totalStale} pages need refresh`);
  }
  
  // Cache Efficiency
  console.log('\n💾 Cache Efficiency:');
  console.log(`   Unique Content: ${report.efficiency.unique_content}`);
  console.log(`   Total Pages: ${report.efficiency.total_pages}`);
  console.log(`   Uniqueness: ${report.efficiency.uniqueness_percent}%`);
  if (report.efficiency.uniqueness_percent < 100) {
    const duplicates = report.efficiency.total_pages - report.efficiency.unique_content;
    console.log(`   Duplicates: ${duplicates} pages share content hashes`);
  }
  
  // Database Size
  console.log('\n💿 Database Size:');
  const sizeKB = Math.round(report.databaseSize / 1024);
  const sizeMB = (report.databaseSize / (1024 * 1024)).toFixed(2);
  console.log(`   ${sizeKB} KB (${sizeMB} MB)`);
  console.log(`   Avg per page: ${Math.round(report.databaseSize / report.overall.total)} bytes`);
  
  // Recommendations
  console.log('\n💡 Recommendations:');
  const recommendations = [];
  
  if (report.stalePages.length > 0) {
    const totalStale = report.stalePages.reduce((sum, s) => sum + s.count, 0);
    recommendations.push(`- Consider refreshing ${totalStale} stale pages with: npm run index all`);
  }
  
  if (report.overall.indexed < report.overall.total) {
    const notIndexed = report.overall.total - report.overall.indexed;
    recommendations.push(`- ${notIndexed} pages are not indexed yet - run indexing to complete`);
  }
  
  if (report.efficiency.uniqueness_percent < 95) {
    recommendations.push(`- Low uniqueness (${report.efficiency.uniqueness_percent}%) may indicate duplicate content`);
  }
  
  if (sizeMB > 100) {
    recommendations.push(`- Large database (${sizeMB} MB) - consider running: npm run optimize-db`);
  }
  
  if (recommendations.length === 0) {
    console.log('   ✅ Everything looks great! Your cache is healthy.');
  } else {
    recommendations.forEach(rec => console.log(`   ${rec}`));
  }
  
  console.log('\n═══════════════════════════════════════════════════════════════\n');
  
  cacheService.close();
  
} catch (error) {
  console.error('\n❌ Error generating report:', error.message);
  console.error(error.stack);
  process.exit(1);
}