@arela/uploader 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,474 @@
1
+ import cliProgress from 'cli-progress';
2
+
3
+ import logger from '../services/LoggingService.js';
4
+ import ScanApiService from '../services/ScanApiService.js';
5
+
6
+ import appConfig from '../config/config.js';
7
+
8
+ /**
9
+ * Propagate Command
10
+ * Propagates arela_path from detected pedimentos to related files in the same directory
11
+ * Optimized for large datasets with batch processing and progress tracking
12
+ */
13
+ export class PropagateCommand {
14
+ constructor(options = {}) {
15
+ this.options = {
16
+ batchSize: parseInt(options.batchSize) || 50, // Process 50 pedimentos at a time
17
+ showStats: options.showStats || false,
18
+ api: options.api || 'default',
19
+ };
20
+
21
+ this.scanApiService = null;
22
+ this.tableName = null;
23
+ this.stats = {
24
+ startTime: Date.now(),
25
+ pedimentosProcessed: 0,
26
+ filesUpdated: 0,
27
+ filesFailed: 0,
28
+ directoriesProcessed: 0,
29
+ };
30
+ }
31
+
32
+ /**
33
+ * Main execution method
34
+ */
35
+ async execute() {
36
+ try {
37
+ console.log('šŸ”„ Starting arela propagate command\n');
38
+
39
+ // Step 1: Validate configuration
40
+ await this.#validateConfiguration();
41
+
42
+ // Step 2: Initialize API service
43
+ this.scanApiService = new ScanApiService();
44
+
45
+ // Step 3: Fetch all tables for this instance
46
+ const scanConfig = appConfig.getScanConfig();
47
+ const tables = await this.scanApiService.getInstanceTables(
48
+ scanConfig.companySlug,
49
+ scanConfig.serverId,
50
+ scanConfig.basePathLabel,
51
+ );
52
+
53
+ if (tables.length === 0) {
54
+ console.error(
55
+ '\nāŒ No tables found for this instance. Run "arela scan" first.\n',
56
+ );
57
+ process.exit(1);
58
+ }
59
+
60
+ console.log(
61
+ `šŸ“‹ Found ${tables.length} table${tables.length === 1 ? '' : 's'} to process:`,
62
+ );
63
+ for (const table of tables) {
64
+ console.log(` - ${table.tableName}`);
65
+ }
66
+ console.log();
67
+
68
+ // Step 4: Process each table
69
+ let totalStats = {
70
+ pedimentosProcessed: 0,
71
+ filesUpdated: 0,
72
+ filesFailed: 0,
73
+ directoriesProcessed: 0,
74
+ };
75
+
76
+ for (const table of tables) {
77
+ console.log(`\nšŸ”„ Processing table: ${table.tableName}\n`);
78
+ this.tableName = table.tableName;
79
+
80
+ // Process this table
81
+ const stats = await this.#processTable();
82
+
83
+ totalStats.pedimentosProcessed += stats.pedimentosProcessed;
84
+ totalStats.filesUpdated += stats.filesUpdated;
85
+ totalStats.filesFailed += stats.filesFailed;
86
+ totalStats.directoriesProcessed += stats.directoriesProcessed;
87
+ }
88
+
89
+ // Show combined results
90
+ const duration = ((Date.now() - this.stats.startTime) / 1000).toFixed(2);
91
+ const filesPerSec =
92
+ totalStats.filesUpdated > 0
93
+ ? (totalStats.filesUpdated / parseFloat(duration)).toFixed(1)
94
+ : 0;
95
+
96
+ console.log('\\nāœ… Propagation Complete!\\n');
97
+ console.log(`šŸ“Š Total Results:`);
98
+ console.log(` Tables Processed: ${tables.length}`);
99
+ console.log(
100
+ ` Pedimentos Processed: ${totalStats.pedimentosProcessed.toLocaleString()}`,
101
+ );
102
+ console.log(
103
+ ` Files Updated: ${totalStats.filesUpdated.toLocaleString()}`,
104
+ );
105
+ console.log(
106
+ ` Files Failed: ${totalStats.filesFailed.toLocaleString()}`,
107
+ );
108
+ console.log(
109
+ ` Directories Processed: ${totalStats.directoriesProcessed.toLocaleString()}`,
110
+ );
111
+ console.log(` Duration: ${duration}s`);
112
+ console.log(` Speed: ${filesPerSec} files/sec\\n`);
113
+ } catch (error) {
114
+ logger.error('Propagation command failed:', error);
115
+ console.error(`\\nāŒ Error: ${error.message}\\n`);
116
+ if (process.env.VERBOSE || process.env.DEBUG) {
117
+ console.error('Stack trace:', error.stack);
118
+ }
119
+ process.exit(1);
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Process a single table
125
+ * @private
126
+ * @returns {Promise<Object>} Statistics for this table
127
+ */
128
+ async #processTable() {
129
+ const tableStats = {
130
+ pedimentosProcessed: 0,
131
+ filesUpdated: 0,
132
+ filesFailed: 0,
133
+ directoriesProcessed: 0,
134
+ };
135
+
136
+ // Show initial statistics
137
+ const initialStats = await this.#showInitialStats();
138
+
139
+ // Mark files needing propagation (if we have pedimento sources)
140
+ if (initialStats.pedimentoSources > 0) {
141
+ await this.#markFilesForPropagation();
142
+ } else {
143
+ console.log(' ā„¹ļø No pedimento sources found. Skipping.\\n');
144
+ return tableStats;
145
+ }
146
+
147
+ // Check if there are files to propagate
148
+ const statsAfterMarking = await this.scanApiService.getPropagationStats(
149
+ this.tableName,
150
+ );
151
+ if (statsAfterMarking.pending === 0) {
152
+ console.log(
153
+ ' ā„¹ļø All files already have arela_path. Nothing to propagate.\\n',
154
+ );
155
+ return tableStats;
156
+ }
157
+
158
+ console.log(
159
+ ` šŸš€ Found ${statsAfterMarking.pending.toLocaleString()} files ready for propagation.\\n`,
160
+ );
161
+
162
+ // Process pedimentos and propagate arela_path
163
+ const stats = await this.#processPropagation();
164
+ Object.assign(tableStats, stats);
165
+
166
+ // Show final statistics for this table
167
+ await this.#showFinalStats();
168
+
169
+ return tableStats;
170
+ }
171
+
172
+ /**
173
+ * Validate scan configuration
174
+ * @private
175
+ */
176
+ async #validateConfiguration() {
177
+ logger.debug('Validating scan configuration...');
178
+
179
+ // Set API target
180
+ appConfig.setApiTarget(this.options.api);
181
+
182
+ // Validate scan config (same as scan/identify commands)
183
+ // Note: validateScanConfig() throws on error, doesn't return errors array
184
+ try {
185
+ appConfig.validateScanConfig();
186
+ } catch (error) {
187
+ console.error(`\\nāŒ ${error.message}\\n`);
188
+ throw new Error('Invalid scan configuration');
189
+ }
190
+
191
+ console.log(`šŸŽÆ API Target: ${this.options.api}`);
192
+ console.log(`šŸ“¦ Batch Size: ${this.options.batchSize}\\n`);
193
+
194
+ logger.debug('Configuration validated');
195
+ }
196
+
197
+ /**
198
+ * Show initial propagation statistics
199
+ * @private
200
+ */
201
+ async #showInitialStats() {
202
+ try {
203
+ const stats = await this.scanApiService.getPropagationStats(
204
+ this.tableName,
205
+ );
206
+
207
+ console.log('šŸ“ˆ Initial Status:');
208
+ console.log(` Total Files: ${stats.totalFiles.toLocaleString()}`);
209
+ console.log(
210
+ ` With arela_path: ${stats.withArelaPath.toLocaleString()}`,
211
+ );
212
+ console.log(
213
+ ` Pedimento Sources: ${stats.pedimentoSources.toLocaleString()}`,
214
+ );
215
+ console.log(` Errors: ${stats.errors.toLocaleString()}\n`);
216
+
217
+ return stats;
218
+ } catch (error) {
219
+ logger.error('Failed to fetch initial stats:', error);
220
+ throw new Error(`Failed to fetch propagation stats: ${error.message}`);
221
+ }
222
+ }
223
+
224
+ /**
225
+ * Mark files that need propagation
226
+ * This is a preparation step that flags files for efficient processing
227
+ * @private
228
+ */
229
+ async #markFilesForPropagation() {
230
+ try {
231
+ console.log('šŸ·ļø Marking files needing propagation...');
232
+ const result = await this.scanApiService.markFilesNeedingPropagation(
233
+ this.tableName,
234
+ );
235
+ console.log(`āœ“ Marked ${result.markedCount.toLocaleString()} files\n`);
236
+ } catch (error) {
237
+ logger.error('Failed to mark files:', error);
238
+ throw new Error(`Failed to mark files: ${error.message}`);
239
+ }
240
+ }
241
+
242
+ /**
243
+ * Process propagation in batches
244
+ * Fetches pedimentos and propagates their arela_path to files in the same directory
245
+ * @private
246
+ */
247
+ async #processPropagation() {
248
+ console.log('šŸš€ Processing propagation...\n');
249
+
250
+ // First, get the total count of pedimento sources
251
+ const initialStats = await this.scanApiService.getPropagationStats(
252
+ this.tableName,
253
+ );
254
+ const totalPedimentos = initialStats.pedimentoSources;
255
+
256
+ if (totalPedimentos === 0) {
257
+ console.log('ā„¹ļø No pedimento sources found.\n');
258
+ return;
259
+ }
260
+
261
+ let offset = 0;
262
+ let hasMore = true;
263
+ let processedCount = 0;
264
+
265
+ // Create progress bar with actual total
266
+ const progressBar = new cliProgress.SingleBar(
267
+ {
268
+ format:
269
+ 'šŸ“„ Propagating |{bar}| {percentage}% | {value}/{total} directories | {speed} files/sec | {filesUpdated} files updated',
270
+ barCompleteChar: '\u2588',
271
+ barIncompleteChar: '\u2591',
272
+ hideCursor: true,
273
+ clearOnComplete: false,
274
+ stopOnComplete: true,
275
+ },
276
+ cliProgress.Presets.shades_classic,
277
+ );
278
+
279
+ const startTime = Date.now();
280
+ let filesUpdated = 0;
281
+
282
+ // Start progress bar with actual total
283
+ progressBar.start(totalPedimentos, 0, {
284
+ speed: '0',
285
+ filesUpdated: 0,
286
+ });
287
+
288
+ try {
289
+ while (hasMore) {
290
+ // Fetch batch of pedimento sources
291
+ const pedimentos = await this.scanApiService.fetchPedimentoSources(
292
+ this.tableName,
293
+ offset,
294
+ this.options.batchSize,
295
+ );
296
+
297
+ // Validate response
298
+ if (!pedimentos || !Array.isArray(pedimentos)) {
299
+ logger.error(
300
+ 'Invalid response from fetchPedimentoSources:',
301
+ pedimentos,
302
+ );
303
+ throw new Error('API returned invalid data format (expected array)');
304
+ }
305
+
306
+ if (pedimentos.length === 0) {
307
+ hasMore = false;
308
+ break;
309
+ }
310
+
311
+ // Process each pedimento's directory
312
+ for (const pedimento of pedimentos) {
313
+ const {
314
+ id,
315
+ directory_path,
316
+ arela_path,
317
+ rfc,
318
+ detected_pedimento_year,
319
+ } = pedimento;
320
+
321
+ // Fetch files in the same directory
322
+ const files =
323
+ await this.scanApiService.fetchFilesNeedingPropagationByDirectory(
324
+ this.tableName,
325
+ directory_path,
326
+ );
327
+
328
+ // Validate response
329
+ if (!files || !Array.isArray(files)) {
330
+ logger.error(
331
+ `Invalid response for directory ${directory_path}:`,
332
+ files,
333
+ );
334
+ this.stats.filesFailed++;
335
+ continue;
336
+ }
337
+
338
+ if (files.length > 0) {
339
+ // Prepare batch update
340
+ const updates = files.map((file) => ({
341
+ id: file.id,
342
+ arelaPath: arela_path,
343
+ rfc: rfc,
344
+ detectedPedimentoYear: detected_pedimento_year,
345
+ propagatedFromId: id,
346
+ propagationError: null,
347
+ }));
348
+
349
+ // Send batch update to API
350
+ try {
351
+ const result = await this.scanApiService.batchUpdatePropagation(
352
+ this.tableName,
353
+ updates,
354
+ );
355
+
356
+ filesUpdated += result.updated;
357
+ this.stats.filesUpdated += result.updated;
358
+ this.stats.filesFailed += result.errors;
359
+ } catch (error) {
360
+ logger.error(
361
+ `Failed to update files in directory ${directory_path}:`,
362
+ error,
363
+ );
364
+ this.stats.filesFailed += files.length;
365
+ }
366
+ }
367
+
368
+ this.stats.directoriesProcessed++;
369
+ this.stats.pedimentosProcessed++;
370
+ processedCount++;
371
+
372
+ // Update progress bar
373
+ const elapsed = (Date.now() - startTime) / 1000;
374
+ const speed = elapsed > 0 ? Math.round(filesUpdated / elapsed) : 0;
375
+
376
+ progressBar.update(processedCount, {
377
+ speed: speed.toString(),
378
+ filesUpdated: filesUpdated.toLocaleString(),
379
+ });
380
+ }
381
+
382
+ // Move to next batch
383
+ offset += pedimentos.length;
384
+
385
+ // Check if we got fewer results than requested (indicates last batch)
386
+ if (pedimentos.length < this.options.batchSize) {
387
+ hasMore = false;
388
+ }
389
+ }
390
+ } finally {
391
+ progressBar.stop();
392
+ }
393
+
394
+ const duration = ((Date.now() - startTime) / 1000).toFixed(2);
395
+ const speed =
396
+ duration > 0 ? Math.round(filesUpdated / parseFloat(duration)) : 0;
397
+
398
+ console.log('\n šŸ“Š Results:');
399
+ console.log(
400
+ ` Pedimentos Processed: ${this.stats.pedimentosProcessed.toLocaleString()}`,
401
+ );
402
+ console.log(
403
+ ` Directories Processed: ${this.stats.directoriesProcessed.toLocaleString()}`,
404
+ );
405
+ console.log(
406
+ ` Files Updated: ${this.stats.filesUpdated.toLocaleString()}`,
407
+ );
408
+ console.log(` Errors: ${this.stats.filesFailed.toLocaleString()}`);
409
+ console.log(` Duration: ${duration}s`);
410
+ console.log(` Speed: ${speed} files/sec\n`);
411
+
412
+ return {
413
+ pedimentosProcessed: this.stats.pedimentosProcessed,
414
+ filesUpdated: this.stats.filesUpdated,
415
+ filesFailed: this.stats.filesFailed,
416
+ directoriesProcessed: this.stats.directoriesProcessed,
417
+ };
418
+ }
419
+
420
+ /**
421
+ * Show final propagation statistics
422
+ * @private
423
+ */
424
+ async #showFinalStats() {
425
+ try {
426
+ const stats = await this.scanApiService.getPropagationStats(
427
+ this.tableName,
428
+ );
429
+
430
+ console.log('šŸ“ˆ Final Status:');
431
+ console.log(` Total Files: ${stats.totalFiles.toLocaleString()}`);
432
+ console.log(
433
+ ` With arela_path: ${stats.withArelaPath.toLocaleString()}`,
434
+ );
435
+ console.log(
436
+ ` Needs Propagation: ${stats.needsPropagation.toLocaleString()}`,
437
+ );
438
+ console.log(` Pending: ${stats.pending.toLocaleString()}`);
439
+ console.log(` Errors: ${stats.errors.toLocaleString()}`);
440
+
441
+ if (stats.maxAttemptsReached > 0) {
442
+ console.log(
443
+ `\nāš ļø ${stats.maxAttemptsReached} files reached max propagation attempts.`,
444
+ );
445
+ console.log(
446
+ ' Run with increased max_propagation_attempts if needed, or review propagation errors.',
447
+ );
448
+ }
449
+
450
+ if (this.options.showStats) {
451
+ const duration = ((Date.now() - this.stats.startTime) / 1000).toFixed(
452
+ 2,
453
+ );
454
+ console.log('\nšŸ’» Performance Stats:');
455
+ console.log(` Total Duration: ${duration}s`);
456
+ console.log(` Memory Used: ${this.#getMemoryUsage()}`);
457
+ }
458
+ } catch (error) {
459
+ logger.error('Failed to fetch final stats:', error);
460
+ // Don't throw - command was successful even if we can't fetch final stats
461
+ }
462
+ }
463
+
464
+ /**
465
+ * Get formatted memory usage
466
+ * @private
467
+ */
468
+ #getMemoryUsage() {
469
+ const used = process.memoryUsage();
470
+ return `${Math.round(used.heapUsed / 1024 / 1024)}MB`;
471
+ }
472
+ }
473
+
474
+ export default PropagateCommand;