quetty 0.1.9

Terminal-based Azure Service Bus queue manager with intuitive TUI interface
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
use crate::components::common::{MessageActivityMsg, Msg, PopupActivityMsg};
use crate::error::AppError;
use quetty_server::bulk_operations::BulkOperationResult;
use std::sync::mpsc::Sender;

/// Context for bulk operation completion handling
#[derive(Debug, Clone)]
pub struct BulkOperationContext {
    pub operation_type: BulkOperationType,
    pub successful_count: usize,
    pub failed_count: usize,
    pub total_count: usize,
    pub message_ids: Vec<String>,
    pub should_remove_from_state: bool,
    pub reload_threshold: usize,
    pub current_message_count: usize,
    pub selected_from_current_page: usize,
}

/// Type of bulk operation being performed
#[derive(Debug, Clone)]
pub enum BulkOperationType {
    Delete,
    Send {
        from_queue_display: String,
        to_queue_display: String,
        should_delete: bool,
    },
}

/// Strategy for handling completion of bulk operations
#[derive(Debug, Clone)]
pub enum ReloadStrategy {
    /// Force reload and show completion message after
    ForceReload { reason: String },
    /// Remove from local state and show completion message
    LocalRemoval,
    /// Only show completion message (no state changes)
    CompletionOnly,
}

/// Centralized bulk operation post-processor
pub struct BulkOperationPostProcessor;

impl BulkOperationPostProcessor {
    /// Determine the appropriate reload strategy for a bulk operation
    pub fn determine_reload_strategy(context: &BulkOperationContext) -> ReloadStrategy {
        let large_operation = context.successful_count >= context.reload_threshold;

        match &context.operation_type {
            BulkOperationType::Delete => {
                let all_current_deleted =
                    context.selected_from_current_page >= context.current_message_count;

                // Only force reload in extreme cases where UI state might be completely invalid
                if all_current_deleted && large_operation {
                    // Both conditions: all current page deleted AND large operation
                    let reason = format!(
                        "Complete current page deletion in large operation ({} messages) - ensuring UI consistency",
                        context.successful_count
                    );
                    ReloadStrategy::ForceReload { reason }
                } else if context.successful_count > 0 {
                    // For all other deletions, use smart local removal with backfill
                    ReloadStrategy::LocalRemoval
                } else {
                    ReloadStrategy::CompletionOnly
                }
            }
            BulkOperationType::Send { should_delete, .. } => {
                if *should_delete && context.successful_count > 0 {
                    // Check if this operation would leave the queue completely empty
                    // This happens when we successfully process all remaining messages
                    let queue_would_be_empty =
                        context.successful_count >= context.current_message_count;

                    if queue_would_be_empty {
                        // Force reload to fetch fresh messages from server when queue becomes empty
                        let reason = format!(
                            "Queue emptied by send-with-delete operation ({} messages) - reloading fresh messages",
                            context.successful_count
                        );
                        ReloadStrategy::ForceReload { reason }
                    } else {
                        // Use local removal with backfill for partial operations
                        ReloadStrategy::LocalRemoval
                    }
                } else {
                    ReloadStrategy::CompletionOnly
                }
            }
        }
    }

    /// Handle bulk operation completion with appropriate reload strategy
    pub fn handle_completion(
        context: &BulkOperationContext,
        tx_to_main: &Sender<Msg>,
        error_reporter: &crate::error::ErrorReporter,
    ) -> Result<(), AppError> {
        let strategy = Self::determine_reload_strategy(context);

        log::info!(
            "Processing bulk operation completion: type={:?}, strategy={:?}",
            context.operation_type,
            strategy
        );

        match strategy {
            ReloadStrategy::ForceReload { reason } => {
                log::info!("Forcing message reload: {reason}");

                // Send reload first
                if let Err(e) = tx_to_main.send(Msg::MessageActivity(
                    MessageActivityMsg::ForceReloadMessages,
                )) {
                    error_reporter.report_send_error("force reload message", &e);
                    return Err(AppError::Component(e.to_string()));
                }

                // Refresh queue statistics after bulk operation
                if let Err(e) = tx_to_main.send(Msg::MessageActivity(
                    MessageActivityMsg::RefreshQueueStatistics,
                )) {
                    error_reporter.report_send_error("refresh queue statistics", &e);
                    // Don't fail the operation if statistics refresh fails
                }

                // Send completion message after reload
                Self::send_completion_message(context, tx_to_main, error_reporter)?;
            }
            ReloadStrategy::LocalRemoval => {
                // Remove from local state first - this preserves existing messages that weren't deleted
                if context.should_remove_from_state && !context.message_ids.is_empty() {
                    log::info!(
                        "Smart local removal: removing {} messages from state while preserving others",
                        context.message_ids.len()
                    );

                    if let Err(e) = tx_to_main.send(Msg::MessageActivity(
                        MessageActivityMsg::BulkRemoveMessagesFromState(
                            context.message_ids.clone(),
                        ),
                    )) {
                        error_reporter.report_send_error("remove messages from state", &e);
                        return Err(AppError::Component(e.to_string()));
                    }
                }

                // Refresh queue statistics after bulk operation
                log::info!("Refreshing queue statistics after smart local removal");
                if let Err(e) = tx_to_main.send(Msg::MessageActivity(
                    MessageActivityMsg::RefreshQueueStatistics,
                )) {
                    error_reporter.report_send_error("refresh queue statistics", &e);
                    // Don't fail the operation if statistics refresh fails
                }

                // Send completion message after local removal
                Self::send_completion_message(context, tx_to_main, error_reporter)?;
            }
            ReloadStrategy::CompletionOnly => {
                // Refresh queue statistics after bulk operation
                if let Err(e) = tx_to_main.send(Msg::MessageActivity(
                    MessageActivityMsg::RefreshQueueStatistics,
                )) {
                    error_reporter.report_send_error("refresh queue statistics", &e);
                    // Don't fail the operation if statistics refresh fails
                }

                // Only send completion message
                Self::send_completion_message(context, tx_to_main, error_reporter)?;
            }
        }

        // After operations that remove messages from the main queue (delete or send-with-delete), ensure selections are cleared
        if matches!(context.operation_type, BulkOperationType::Delete)
            || matches!(
                context.operation_type,
                BulkOperationType::Send {
                    should_delete: true,
                    ..
                }
            )
        {
            if let Err(e) =
                tx_to_main.send(Msg::MessageActivity(MessageActivityMsg::ClearAllSelections))
            {
                error_reporter.report_send_error("clear selections", &e);
            }
        }

        Ok(())
    }

    /// Shared: Format detailed result message for bulk operations (delete, send with delete)
    pub fn format_bulk_operation_result_message(
        operation: &str,
        queue_name: &str,
        successful_count: usize,
        failed_count: usize,
        not_found_count: usize,
        total_count: usize,
        is_delete: bool,
    ) -> String {
        if successful_count == 0 {
            if failed_count > 0 {
                format!(
                    "❌ Bulk {operation} failed: No messages were processed from {queue_name}\n\n\
                    📊 Results:\n\
                    • ❌ Failed: {failed_count} messages\n\
                    • ⚠️  Not found: {not_found_count} messages\n\
                    • 📦 Total requested: {total_count}\n\n\
                    💡 Messages may have been already processed, moved, or deleted by another process."
                )
            } else {
                let unavailable_hint = if is_delete {
                    format!(
                        "💡 The {not_found_count} messages you selected were not available for deletion.\n\
                        This typically happens when:\n\
                        • Messages were processed by another consumer\n\
                        • Messages were moved or deleted by another process\n\
                        • Selected messages are only visible in preview but not available for consumption\n\n\
                        🔄 Try refreshing the queue to see the current available messages."
                    )
                } else {
                    format!(
                        "💡 The {not_found_count} messages you selected were not available for moving.\n\
                        This typically happens when:\n\
                        • Messages were processed by another consumer\n\
                        • Messages were moved or deleted by another process\n\
                        • Selected messages are only visible in preview but not available for consumption\n\n\
                        🔄 Try refreshing the queue to see the current available messages."
                    )
                };
                format!(
                    "⚠️  No messages were processed from {queue_name}

📊 Results:
• ⚠️  Not found: {not_found_count} messages
• 📦 Total requested: {total_count}

{unavailable_hint}"
                )
            }
        } else if failed_count > 0 || not_found_count > 0 {
            // Partial success
            format!(
                "⚠️ Bulk {operation} operation completed with mixed results

{queue_name}


                📊 Results:

                • ✅ Successfully processed: {successful_count} messages

                • ❌ Failed: {failed_count} messages

                • ⚠️  Not found: {not_found_count} messages

                • 📦 Total requested: {total_count}



                💡 Some messages may have been processed by another process during the operation."
            )
        } else {
            // Complete success
            let operation_word = if is_delete { "move" } else { "copy" };
            let past_tense = if is_delete { "moved" } else { "copied" };

            // Convert arrow representation to 'to' wording for the processed line
            let queue_wording = if queue_name.contains('') {
                queue_name.replace('', "to")
            } else {
                queue_name.to_string()
            };

            format!(
                "✅ Bulk {op} operation completed successfully!\n\n{count} message{plural} processed from {queue_wording}\n\nAll messages {past_tense} successfully",
                op = operation_word,
                count = successful_count,
                plural = if successful_count == 1 { "" } else { "s" },
                queue_wording = queue_wording,
                past_tense = past_tense,
            )
        }
    }

    /// Send the appropriate completion message for the operation type
    fn send_completion_message(
        context: &BulkOperationContext,
        tx_to_main: &Sender<Msg>,
        error_reporter: &crate::error::ErrorReporter,
    ) -> Result<(), AppError> {
        match &context.operation_type {
            BulkOperationType::Delete => {
                if let Err(e) = tx_to_main.send(Msg::MessageActivity(
                    MessageActivityMsg::BulkDeleteCompleted {
                        successful_count: context.successful_count,
                        failed_count: context.failed_count,
                        total_count: context.total_count,
                    },
                )) {
                    error_reporter.report_send_error("bulk delete completion message", &e);
                    return Err(AppError::Component(e.to_string()));
                }
            }
            BulkOperationType::Send {
                from_queue_display,
                to_queue_display,
                should_delete,
            } => {
                // Use detailed message if should_delete (move), else fallback to old summary
                let not_found_count = context
                    .total_count
                    .saturating_sub(context.successful_count + context.failed_count);
                let queue_name_combined = format!("{from_queue_display}{to_queue_display}");
                let operation = if *should_delete { "move" } else { "copy" };
                let is_delete = *should_delete;
                let message = Self::format_bulk_operation_result_message(
                    operation,
                    &queue_name_combined,
                    context.successful_count,
                    context.failed_count,
                    not_found_count,
                    context.total_count,
                    is_delete,
                );
                if let Err(e) =
                    tx_to_main.send(Msg::PopupActivity(PopupActivityMsg::ShowSuccess(message)))
                {
                    error_reporter.report_send_error("success popup message", &e);
                    return Err(AppError::Component(e.to_string()));
                }
            }
        }
        Ok(())
    }

    /// Create context from bulk operation result for delete operations
    pub fn create_delete_context(
        result: &BulkOperationResult,
        message_ids: Vec<String>,
        reload_threshold: usize,
        current_message_count: usize,
        selected_from_current_page: usize,
    ) -> BulkOperationContext {
        BulkOperationContext {
            operation_type: BulkOperationType::Delete,
            successful_count: result.successful,
            failed_count: result.failed,
            total_count: message_ids.len(),
            message_ids,
            should_remove_from_state: true,
            reload_threshold,
            current_message_count,
            selected_from_current_page,
        }
    }

    /// Create context from bulk operation result for send operations
    #[allow(clippy::too_many_arguments)]
    pub fn create_send_context(
        result: &BulkOperationResult,
        message_ids_to_remove: Vec<String>,
        reload_threshold: usize,
        from_queue_display: String,
        to_queue_display: String,
        should_delete: bool,
        current_message_count: usize,
        selected_from_current_page: usize,
    ) -> BulkOperationContext {
        BulkOperationContext {
            operation_type: BulkOperationType::Send {
                from_queue_display,
                to_queue_display,
                should_delete,
            },
            successful_count: result.successful,
            failed_count: result.failed,
            total_count: result.total_requested,
            message_ids: message_ids_to_remove,
            should_remove_from_state: should_delete,
            reload_threshold,
            current_message_count,
            selected_from_current_page,
        }
    }

    /// Extract message IDs that were successfully processed for removal from local state
    pub fn extract_successfully_processed_message_ids(
        bulk_data: &crate::app::updates::messages::bulk_execution::task_manager::BulkSendData,
        successful_count: usize,
    ) -> Vec<String> {
        use crate::app::updates::messages::bulk_execution::task_manager::BulkSendData;

        match bulk_data {
            BulkSendData::MessageIds(message_ids) => {
                // Take up to the successful count from the original message IDs
                // Note: This assumes the bulk operation processes messages in order
                // For more precise tracking, we would need the actual IDs from the operation result
                message_ids
                    .iter()
                    .take(successful_count)
                    .map(|id| id.id.clone())
                    .collect()
            }
            BulkSendData::MessageData(messages_data) => {
                // Extract message IDs from the message data
                messages_data
                    .iter()
                    .take(successful_count)
                    .map(|(id, _)| id.id.clone())
                    .collect()
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_delete_strategy_large_operation_with_all_current_deleted() {
        // This represents the only case where ForceReload should be used for deletes:
        // Large operation AND all current page messages deleted
        let context = BulkOperationContext {
            operation_type: BulkOperationType::Delete,
            successful_count: 100,
            failed_count: 0,
            total_count: 100,
            message_ids: vec![],
            should_remove_from_state: true,
            reload_threshold: 50,
            current_message_count: 20,
            selected_from_current_page: 20, // All current messages deleted
        };

        match BulkOperationPostProcessor::determine_reload_strategy(&context) {
            ReloadStrategy::ForceReload { reason } => {
                assert!(reason.contains("Complete current page deletion in large operation"));
            }
            _ => panic!(
                "Expected ForceReload strategy for large operation that deletes all current page messages"
            ),
        }
    }

    #[test]
    fn test_delete_strategy_large_operation_partial_current() {
        // Large operation but not all current page deleted -> LocalRemoval (smart)
        let context = BulkOperationContext {
            operation_type: BulkOperationType::Delete,
            successful_count: 100,
            failed_count: 0,
            total_count: 100,
            message_ids: vec![],
            should_remove_from_state: true,
            reload_threshold: 50,
            current_message_count: 20,
            selected_from_current_page: 10, // Only partial current page deleted
        };

        match BulkOperationPostProcessor::determine_reload_strategy(&context) {
            ReloadStrategy::LocalRemoval => {}
            _ => panic!(
                "Expected LocalRemoval strategy for large operation that preserves some current page messages"
            ),
        }
    }

    #[test]
    fn test_delete_strategy_small_operation_all_current_deleted() {
        // Small operation but all current deleted -> LocalRemoval (smart)
        let context = BulkOperationContext {
            operation_type: BulkOperationType::Delete,
            successful_count: 5,
            failed_count: 0,
            total_count: 5,
            message_ids: vec![],
            should_remove_from_state: true,
            reload_threshold: 50,
            current_message_count: 5,
            selected_from_current_page: 5, // All current deleted but small operation
        };

        match BulkOperationPostProcessor::determine_reload_strategy(&context) {
            ReloadStrategy::LocalRemoval => {}
            _ => panic!(
                "Expected LocalRemoval strategy for small operation (even if all current deleted)"
            ),
        }
    }

    #[test]
    fn test_delete_strategy_small_local_removal() {
        // Typical small delete operation -> LocalRemoval
        let context = BulkOperationContext {
            operation_type: BulkOperationType::Delete,
            successful_count: 3,
            failed_count: 0,
            total_count: 3,
            message_ids: vec!["1".to_string(), "2".to_string(), "3".to_string()],
            should_remove_from_state: true,
            reload_threshold: 50,
            current_message_count: 20,
            selected_from_current_page: 3,
        };

        match BulkOperationPostProcessor::determine_reload_strategy(&context) {
            ReloadStrategy::LocalRemoval => {}
            _ => panic!("Expected LocalRemoval strategy for typical small operation"),
        }
    }

    #[test]
    fn test_send_strategy_large_move() {
        let context = BulkOperationContext {
            operation_type: BulkOperationType::Send {
                from_queue_display: "Main".to_string(),
                to_queue_display: "DLQ".to_string(),
                should_delete: true,
            },
            successful_count: 2000,
            failed_count: 0,
            total_count: 2000,
            message_ids: vec![],
            should_remove_from_state: true,
            reload_threshold: 50,
            current_message_count: 1000, // Less than successful_count, so queue will be emptied
            selected_from_current_page: 1000,
        };

        match BulkOperationPostProcessor::determine_reload_strategy(&context) {
            ReloadStrategy::ForceReload { reason } => {
                assert!(reason.contains("Queue emptied by send-with-delete operation"));
            }
            _ => {
                panic!("Expected ForceReload strategy for large send operation that empties queue")
            }
        }
    }

    #[test]
    fn test_send_strategy_copy_only() {
        let context = BulkOperationContext {
            operation_type: BulkOperationType::Send {
                from_queue_display: "Main".to_string(),
                to_queue_display: "Other".to_string(),
                should_delete: false, // Copy operation
            },
            successful_count: 50,
            failed_count: 0,
            total_count: 50,
            message_ids: vec![],
            should_remove_from_state: false,
            reload_threshold: 10,
            current_message_count: 1000,
            selected_from_current_page: 50,
        };

        match BulkOperationPostProcessor::determine_reload_strategy(&context) {
            ReloadStrategy::CompletionOnly => {}
            _ => panic!("Expected CompletionOnly strategy for copy operation"),
        }
    }

    #[test]
    fn test_send_strategy_local_removal() {
        // This represents your scenario: 2000 messages deleted from mixed pages (not all current page)
        let context = BulkOperationContext {
            operation_type: BulkOperationType::Send {
                from_queue_display: "Main".to_string(),
                to_queue_display: "DLQ".to_string(),
                should_delete: true,
            },
            successful_count: 2000,
            failed_count: 0,
            total_count: 2000,
            message_ids: vec![],
            should_remove_from_state: true,
            reload_threshold: 50,
            current_message_count: 3000, // 3000 total messages loaded
            selected_from_current_page: 1000, // Only 1000 from current page (not all current page deleted)
        };

        match BulkOperationPostProcessor::determine_reload_strategy(&context) {
            ReloadStrategy::LocalRemoval => {}
            _ => panic!(
                "Expected LocalRemoval strategy for large send operation that doesn't empty the queue"
            ),
        }
    }

    #[test]
    fn test_send_strategy_force_reload_when_queue_emptied() {
        // Test the new logic: when send-with-delete empties the entire queue, force reload
        let context = BulkOperationContext {
            operation_type: BulkOperationType::Send {
                from_queue_display: "Main".to_string(),
                to_queue_display: "DLQ".to_string(),
                should_delete: true,
            },
            successful_count: 100,
            failed_count: 0,
            total_count: 100,
            message_ids: vec![],
            should_remove_from_state: true,
            reload_threshold: 50,
            current_message_count: 100, // All messages would be processed
            selected_from_current_page: 100,
        };

        match BulkOperationPostProcessor::determine_reload_strategy(&context) {
            ReloadStrategy::ForceReload { reason } => {
                assert!(reason.contains("Queue emptied by send-with-delete operation"));
            }
            _ => panic!(
                "Expected ForceReload strategy when send-with-delete empties the entire queue"
            ),
        }
    }
}