File size: 8,747 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env node
import { ChangeDetectionService } from '../services/change-detection-service.js';
import { CacheService } from '../services/cache-service.js';

/**
 * CLI tool for checking documentation changes
 * Usage:
 *   npm run check-changes              - Check all sources
 *   npm run check-changes k3s          - Check specific source
 *   npm run check-changes k3s rancher  - Check multiple sources
 *   npm run check-changes -- --limit 10 --days 7
 */

const SOURCES = ['suse', 'rancher', 'k3s', 'rke2', 'longhorn', 'harvester', 'neuvector', 'kubewarden'];

async function main() {
  const args = process.argv.slice(2);
  
  // Parse options
  const options = {
    limit: null,
    olderThanDays: null,
    parallel: 5,
    stats: false,
    watch: false,
    watchInterval: 24, // hours
  };

  const sourcesToCheck = [];

  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    
    if (arg === '--limit' && args[i + 1]) {
      options.limit = parseInt(args[i + 1]);
      i++;
    } else if (arg === '--days' && args[i + 1]) {
      options.olderThanDays = parseInt(args[i + 1]);
      i++;
    } else if (arg === '--parallel' && args[i + 1]) {
      options.parallel = parseInt(args[i + 1]);
      i++;
    } else if (arg === '--stats') {
      options.stats = true;
    } else if (arg === '--watch') {
      options.watch = true;
    } else if (arg === '--interval' && args[i + 1]) {
      options.watchInterval = parseInt(args[i + 1]);
      i++;
    } else if (SOURCES.includes(arg)) {
      sourcesToCheck.push(arg);
    } else if (!arg.startsWith('--')) {
      console.log(`⚠️  Unknown source: ${arg}`);
      console.log(`   Available sources: ${SOURCES.join(', ')}`);
    }
  }

  // Default to all sources if none specified
  if (sourcesToCheck.length === 0 && !options.stats) {
    sourcesToCheck.push(...SOURCES);
  }

  const cacheService = new CacheService();
  const changeDetection = new ChangeDetectionService(cacheService);

  try {
    await changeDetection.initialize();

    // Show statistics
    if (options.stats) {
      console.log('📊 Change Detection Statistics\n');
      const stats = await changeDetection.getStatistics();
      
      console.log(`Total Pages: ${stats.totalPages}`);
      console.log(`Recently Checked (last 24h): ${stats.recentlyChecked}`);
      console.log(`Never Checked: ${stats.neverChecked}\n`);
      
      console.log('📈 Staleness Distribution:');
      for (const [bucket, count] of Object.entries(stats.staleness)) {
        const pct = ((count / stats.totalPages) * 100).toFixed(1);
        console.log(`   ${bucket.padEnd(15)} ${count.toString().padStart(5)} (${pct}%)`);
      }
      
      console.log('\n📋 By Source:');
      for (const [source, data] of Object.entries(stats.bySource)) {
        console.log(`\n   ${source}:`);
        console.log(`      Total: ${data.total}`);
        console.log(`      Recently Checked: ${data.recentlyChecked}`);
        console.log(`      Never Checked: ${data.neverChecked}`);
      }
      
      return;
    }

    // Watch mode
    if (options.watch) {
      console.log(`🔄 Starting watch mode (checking every ${options.watchInterval} hours)`);
      console.log('   Press Ctrl+C to stop\n');
      
      await changeDetection.startAutoDetection({
        intervalHours: options.watchInterval,
        sources: sourcesToCheck,
        checkOptions: {
          limit: options.limit,
          olderThanDays: options.olderThanDays,
          parallel: options.parallel,
          onProgress: (current, total, result) => {
            if (result.changed) {
              console.log(`   🔄 [${current}/${total}] Changed: ${result.url}`);
            }
          },
        },
        onChangesDetected: async (results) => {
          console.log('\n⚠️  Changes detected! Consider re-indexing these sources:');
          for (const [source, data] of Object.entries(results.sources)) {
            if (data.changed.length > 0) {
              console.log(`   ${source}: ${data.changed.length} changed pages`);
            }
          }
          console.log('\nRun: npm run index <source> -- --force\n');
        },
      });

      // Keep process running
      process.on('SIGINT', () => {
        changeDetection.stopAutoDetection();
        process.exit(0);
      });

      // Prevent exit
      await new Promise(() => {});
    }

    // One-time check
    else {
      const checkOptions = {
        limit: options.limit,
        olderThanDays: options.olderThanDays,
        parallel: options.parallel,
        onProgress: (current, total, result) => {
          const status = result.changed ? '🔄' : 
                        result.status === 'error' ? '❌' : '✓';
          process.stdout.write(`\r${status} Checking: ${current}/${total} `);
          
          if (result.changed) {
            console.log(`\n   🔄 Changed: ${result.url}`);
          }
        },
      };

      let filterMsg = '';
      if (options.limit) filterMsg += ` (limit: ${options.limit})`;
      if (options.olderThanDays) filterMsg += ` (older than ${options.olderThanDays} days)`;

      console.log(`🔍 Checking for changes${filterMsg}\n`);

      const results = await changeDetection.checkAllSources(sourcesToCheck, checkOptions);

      // Print summary
      console.log('\n\n' + '='.repeat(60));
      console.log('📊 Change Detection Summary');
      console.log('='.repeat(60) + '\n');

      for (const [source, data] of Object.entries(results.sources)) {
        console.log(`📋 ${source.toUpperCase()}`);
        console.log(`   Total Checked: ${data.totalChecked}`);
        console.log(`   ✓ Unchanged: ${data.unchanged.length}`);
        console.log(`   🔄 Changed: ${data.changed.length}`);
        console.log(`   ❌ Errors: ${data.errors.length}`);
        console.log(`   ➕ New: ${data.new.length}`);
        
        if (data.changed.length > 0) {
          console.log(`\n   Changed URLs:`);
          data.changed.slice(0, 5).forEach(item => {
            console.log(`      - ${item.url}`);
          });
          if (data.changed.length > 5) {
            console.log(`      ... and ${data.changed.length - 5} more`);
          }
        }
        console.log();
      }

      console.log('='.repeat(60));
      console.log('📈 Overall Summary');
      console.log('='.repeat(60));
      console.log(`   ✓ Unchanged: ${results.summary.totalUnchanged}`);
      console.log(`   🔄 Changed: ${results.summary.totalChanged}`);
      console.log(`   ❌ Errors: ${results.summary.totalErrors}`);
      console.log(`   ➕ New: ${results.summary.totalNew}`);
      console.log('='.repeat(60) + '\n');

      if (results.summary.totalChanged > 0) {
        console.log('⚠️  Changes detected! To re-index changed sources, run:');
        for (const source of Object.keys(results.sources)) {
          if (results.sources[source].changed.length > 0) {
            console.log(`   npm run index ${source} -- --force`);
          }
        }
        console.log();
      } else {
        console.log('✅ All checked pages are up to date!\n');
      }
    }

  } catch (error) {
    console.error('❌ Error:', error.message);
    console.error(error.stack);
    process.exit(1);
  } finally {
    if (cacheService.db) {
      cacheService.close();
    }
  }
}

// Show help
if (process.argv.includes('--help') || process.argv.includes('-h')) {
  console.log(`
📋 Change Detection Tool

Check documentation sources for updates and changes.

Usage:
  npm run check-changes [sources...] [options]

Sources:
  suse, rancher, k3s, rke2, longhorn, harvester, neuvector, kubewarden
  (if no source specified, checks all sources)

Options:
  --limit <n>        Check only first N pages from each source
  --days <n>         Only check pages last checked more than N days ago
  --parallel <n>     Number of parallel checks (default: 5)
  --stats           Show change detection statistics instead of checking
  --watch           Run continuously and check on interval
  --interval <hrs>  Interval in hours for watch mode (default: 24)

Examples:
  # Check all sources
  npm run check-changes

  # Check specific source
  npm run check-changes k3s

  # Check multiple sources
  npm run check-changes k3s rancher

  # Check only first 10 pages from each source
  npm run check-changes -- --limit 10

  # Check pages not checked in last 7 days
  npm run check-changes -- --days 7

  # Show statistics
  npm run check-changes -- --stats

  # Run in watch mode (check every 24 hours)
  npm run check-changes -- --watch

  # Watch mode with custom interval (every 6 hours)
  npm run check-changes -- --watch --interval 6
`);
  process.exit(0);
}

main();