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
//! Bulk Operation Setup and Builder Patterns
//!
//! This module provides simplified, reusable patterns for setting up bulk operations,
//! reducing code duplication and complex parameter lists.

use crate::app::model::Model;
use crate::error::AppError;
use quetty_server::bulk_operations::MessageIdentifier;

use quetty_server::service_bus_manager::QueueType;
use tuirealm::terminal::TerminalAdapter;

/// Common bulk operation configuration
#[derive(Debug, Clone)]
pub struct BulkOperationConfig {
    /// Maximum number of messages allowed for operation
    pub max_count: usize,
    /// Auto-reload threshold for UI refresh
    pub auto_reload_threshold: usize,
}

impl BulkOperationConfig {
    /// Create configuration from app config
    pub fn from_app_config() -> Self {
        let config = crate::config::get_config_or_panic();
        Self {
            max_count: config.batch().max_messages_to_process(),
            auto_reload_threshold: config.batch().auto_reload_threshold(),
        }
    }
}

/// Simplified builder for bulk operations with common validation
///
/// # Safety
/// This struct uses a raw pointer to the Model to avoid lifetime complications
/// during bulk operation setup. The safety guarantees are:
///
/// 1. The pointer is only valid during the scope of the bulk operation setup
/// 2. The Model reference must outlive the BulkOperationSetup instance
/// 3. The pointer is converted back to a reference only during validation and execution
/// 4. No mutable access through the pointer - only immutable access for validation
///
/// ## Usage Pattern
/// ```ignore
/// let setup = BulkOperationSetup::new(&model, message_ids)
///     .operation_type(BulkOperationType::Delete)
///     .validate_and_build()?; // Raw pointer used only here
/// // setup is consumed, no further pointer access
/// ```
pub struct BulkOperationSetup<T: TerminalAdapter> {
    /// Raw pointer to avoid lifetime parameters - see safety documentation above
    model: *const Model<T>,
    config: BulkOperationConfig,
    message_ids: Vec<MessageIdentifier>,
    operation_type: BulkOperationType,
}

#[derive(Debug, Clone)]
pub enum BulkOperationType {
    /// Delete messages from current queue
    Delete,
    /// Resend from DLQ to main queue (with deletion)
    ResendFromDlq { delete_source: bool },
    /// Send to DLQ from main queue (with deletion)
    SendToDlq { delete_source: bool },
}

impl<T: TerminalAdapter> BulkOperationSetup<T> {
    /// Calculate global position from local page index
    fn calculate_global_position(
        page_size: usize,
        current_page: usize,
        local_index: usize,
    ) -> usize {
        current_page * page_size + local_index + 1
    }
    /// Create a new bulk operation setup
    pub fn new(model: &Model<T>, message_ids: Vec<MessageIdentifier>) -> Self {
        Self {
            model,
            config: BulkOperationConfig::from_app_config(),
            message_ids,
            operation_type: BulkOperationType::Delete, // Default
        }
    }

    /// Set the operation type
    pub fn operation_type(mut self, op_type: BulkOperationType) -> Self {
        self.operation_type = op_type;
        self
    }

    /// Validate the operation setup and return prepared context
    pub fn validate_and_build(self) -> Result<ValidatedBulkOperation<T>, AppError> {
        let model = unsafe { &*self.model };

        // Validate basic requirements
        self.validate_message_count()?;
        self.validate_queue_type_for_operation(model)?;
        self.validate_operation_size()?; // Added back validation against max_messages_to_process

        // Validate link credit usage to prevent receiver credit exhaustion
        self.validate_link_credit_limit(model)?;

        // Log validation success
        log::info!(
            "Validated bulk {:?} operation for {} messages",
            self.operation_type,
            self.message_ids.len()
        );

        Ok(ValidatedBulkOperation {
            model: self.model,
            config: self.config,
            message_ids: self.message_ids,
            operation_type: self.operation_type,
        })
    }

    /// Validate message count is within limits
    fn validate_message_count(&self) -> Result<(), AppError> {
        let count = self.message_ids.len();

        if count == 0 {
            return Err(AppError::State(
                "No messages selected for bulk operation".to_string(),
            ));
        }

        if count > self.config.max_count {
            return Err(AppError::State(format!(
                "Too many messages selected: {} (maximum: {})",
                count, self.config.max_count
            )));
        }

        Ok(())
    }

    /// Validate operation size is within processing limits (max_messages_to_process)
    fn validate_operation_size(&self) -> Result<(), AppError> {
        let config = crate::config::get_config_or_panic();
        let max_messages_to_process = config.batch().max_messages_to_process();

        if self.message_ids.len() > max_messages_to_process {
            return Err(AppError::State(format!(
                "Operation size {} exceeds maximum allowed processing limit of {}",
                self.message_ids.len(),
                max_messages_to_process
            )));
        }
        Ok(())
    }

    /// Validate queue type matches operation requirements
    fn validate_queue_type_for_operation(&self, model: &Model<T>) -> Result<(), AppError> {
        let current_type = model.queue_manager.queue_state.current_queue_type.clone();
        let required_type = self.get_required_queue_type();

        if current_type != required_type {
            let current_name = queue_type_display_name(current_type);
            let required_name = queue_type_display_name(required_type);

            return Err(AppError::State(format!(
                "Operation not allowed: currently in {current_name} but operation requires {required_name}"
            )));
        }

        // Validate that we have messages loaded to prevent operations on stale cache
        let messages = model.queue_manager.queue_state.messages.as_ref();
        if messages.is_none() || messages.map(|m| m.is_empty()).unwrap_or(true) {
            return Err(AppError::State(
                "No messages available for operation. Please wait for messages to load after queue switch.".to_string()
            ));
        }

        Ok(())
    }

    /// Get required queue type for the operation
    fn get_required_queue_type(&self) -> QueueType {
        match self.operation_type {
            BulkOperationType::Delete => {
                // Delete can work from either queue type
                // We'll use the current queue type (no restriction)
                unsafe { &*self.model }
                    .queue_manager
                    .queue_state
                    .current_queue_type
                    .clone()
            }
            BulkOperationType::ResendFromDlq { .. } => QueueType::DeadLetter,
            BulkOperationType::SendToDlq { .. } => QueueType::Main,
        }
    }

    /// Ensure the operation will not exceed Service Bus link credit (2048)
    fn validate_link_credit_limit(&self, model: &Model<T>) -> Result<(), AppError> {
        /// Azure Service Bus link credit limit - maximum number of messages
        /// that can be locked by a receiver at once
        const LINK_CREDIT_LIMIT: usize = 2048;

        // For single message operations, check if the position itself exceeds the limit
        if self.message_ids.len() == 1 {
            // Get the current UI index to calculate global position
            if let Ok(tuirealm::State::One(tuirealm::StateValue::Usize(selected_index))) = model
                .app
                .state(&crate::components::common::ComponentId::Messages)
            {
                let page_size = crate::config::get_config_or_panic().max_messages() as usize;
                let current_page = model.queue_state().message_pagination.current_page;
                let global_position =
                    Self::calculate_global_position(page_size, current_page, selected_index);

                if global_position > LINK_CREDIT_LIMIT {
                    return Err(AppError::State(format!(
                        "Cannot process message at position {global_position}, which exceeds the Azure Service Bus link-credit limit of {LINK_CREDIT_LIMIT}.\n\nTo fix this:\n• Process messages from earlier pages\n• Use filtering to reduce the message set"
                    )));
                }

                log::debug!(
                    "Link credit validation passed: single_message_position={global_position}, limit={LINK_CREDIT_LIMIT}"
                );
            }
        } else {
            // For multiple selections, use the gap calculation method
            let gap_sum = model.queue_state().bulk_selection.calculate_gap_sum();

            if gap_sum > LINK_CREDIT_LIMIT {
                return Err(AppError::State(format!(
                    "Operation would require processing {gap_sum} non-selected messages between your selections, which exceeds the Azure Service Bus link-credit limit of {LINK_CREDIT_LIMIT}. This would cause the bulk operation to get stuck.\n\nTo fix this:\n• Select messages that are closer together (reduce gaps between selections)\n• Split into multiple smaller operations\n• Select contiguous ranges of messages"
                )));
            }

            log::debug!(
                "Link credit validation passed: gap_sum={}, selected_count={}, limit={}",
                gap_sum,
                self.message_ids.len(),
                LINK_CREDIT_LIMIT
            );
        }

        Ok(())
    }
}

/// Validated bulk operation ready for execution
///
/// # Safety
/// Contains a validated raw pointer from BulkOperationSetup. The same safety
/// guarantees apply - the Model reference must outlive this struct's usage.
/// Access is controlled through safe methods that convert the pointer to references.
pub struct ValidatedBulkOperation<T: TerminalAdapter> {
    /// Raw pointer inherited from BulkOperationSetup - see safety documentation
    model: *const Model<T>,
    config: BulkOperationConfig,
    message_ids: Vec<MessageIdentifier>,
    operation_type: BulkOperationType,
}

impl<T: TerminalAdapter> ValidatedBulkOperation<T> {
    /// Get the message IDs for this operation
    pub fn message_ids(&self) -> &[MessageIdentifier] {
        &self.message_ids
    }

    /// Get the model reference (unsafe but controlled)
    pub fn model(&self) -> &Model<T> {
        unsafe { &*self.model }
    }

    /// Get loading message for this operation
    pub fn get_loading_message(&self) -> String {
        let count = self.message_ids.len();
        match &self.operation_type {
            BulkOperationType::Delete => format!("Deleting {count} messages..."),
            BulkOperationType::ResendFromDlq {
                delete_source: true,
            } => {
                format!("Bulk resending {count} messages from DLQ to main queue...")
            }
            BulkOperationType::ResendFromDlq {
                delete_source: false,
            } => {
                format!("Bulk copying {count} messages from DLQ to main queue (keeping in DLQ)...")
            }
            BulkOperationType::SendToDlq {
                delete_source: true,
            } => {
                format!("Bulk moving {count} messages from main queue to DLQ...")
            }
            BulkOperationType::SendToDlq {
                delete_source: false,
            } => {
                format!("Bulk copying {count} messages from main queue to DLQ...")
            }
        }
    }

    /// Get target queue name for send operations
    pub fn get_target_queue(&self) -> Result<String, AppError> {
        let model = self.model();
        let current_queue_name = model
            .queue_state()
            .current_queue_name
            .as_ref()
            .ok_or_else(|| AppError::State("No current queue name available".to_string()))?;

        match &self.operation_type {
            BulkOperationType::Delete => Err(AppError::State(
                "Delete operations don't have target queues".to_string(),
            )),
            BulkOperationType::ResendFromDlq { .. } => {
                // Remove DLQ suffix to get main queue name
                if current_queue_name.ends_with("/$deadletterqueue") {
                    Ok(current_queue_name
                        .strip_suffix("/$deadletterqueue")
                        .unwrap_or(current_queue_name)
                        .to_string())
                } else {
                    Ok(current_queue_name.clone())
                }
            }
            BulkOperationType::SendToDlq { .. } => {
                // Add DLQ suffix to current queue name
                Ok(format!("{current_queue_name}/$deadletterqueue"))
            }
        }
    }

    /// Get display names for from/to queues
    pub fn get_queue_display_names(&self) -> (String, String) {
        match &self.operation_type {
            BulkOperationType::Delete => ("Current".to_string(), "Deleted".to_string()),
            BulkOperationType::ResendFromDlq { .. } => ("DLQ".to_string(), "Main".to_string()),
            BulkOperationType::SendToDlq { .. } => ("Main".to_string(), "DLQ".to_string()),
        }
    }

    /// Check if this operation should delete from source
    pub fn should_delete_source(&self) -> bool {
        match &self.operation_type {
            BulkOperationType::Delete => true, // Delete operations always delete
            BulkOperationType::ResendFromDlq { delete_source } => *delete_source,
            BulkOperationType::SendToDlq { delete_source } => *delete_source,
        }
    }

    /// Calculate context for post-processing
    pub fn calculate_post_processing_context(&self) -> BulkOperationContext {
        let model = self.model();

        // Get current page messages
        let current_page_messages = model
            .queue_state()
            .message_pagination
            .get_current_page_messages(crate::config::get_current_page_size());
        let current_message_count = current_page_messages.len();

        let selected_from_current_page = self
            .message_ids
            .iter()
            .filter(|msg_id| {
                current_page_messages
                    .iter()
                    .any(|current_msg| current_msg.id == **msg_id)
            })
            .count();

        log::debug!(
            "calculate_post_processing_context: current_message_count={}, selected_from_current_page={}, total_selected={}",
            current_message_count,
            selected_from_current_page,
            self.message_ids.len()
        );

        // Use actual highest selected index if available, otherwise get current message index
        let max_position = if let Some(highest_index) = model
            .queue_state()
            .bulk_selection
            .get_highest_selected_position()
        {
            // get_highest_selected_position returns 1-based position
            highest_index
        } else if self.message_ids.len() == 1 {
            // Single message operation - calculate global index properly
            if let Ok(tuirealm::State::One(tuirealm::StateValue::Usize(selected_index))) = model
                .app
                .state(&crate::components::common::ComponentId::Messages)
            {
                // Calculate global index
                let page_size = crate::config::get_config_or_panic().max_messages() as usize;
                let current_page = model.queue_state().message_pagination.current_page;
                BulkOperationSetup::<T>::calculate_global_position(
                    page_size,
                    current_page,
                    selected_index,
                )
            } else {
                // Fallback to page-based estimation
                let page_size = crate::config::get_config_or_panic().max_messages() as usize;
                let current_page = model.queue_state().message_pagination.current_page;
                self.calculate_max_position(model, page_size, current_page)
            }
        } else {
            // Fallback to the old calculation method
            let page_size = crate::config::get_config_or_panic().max_messages() as usize;
            let current_page = model.queue_state().message_pagination.current_page;
            self.calculate_max_position(model, page_size, current_page)
        };

        BulkOperationContext {
            auto_reload_threshold: self.config.auto_reload_threshold,
            current_message_count,
            selected_from_current_page,
            max_position,
        }
    }

    /// Calculate estimated maximum position of selected messages
    fn calculate_max_position(
        &self,
        model: &Model<T>,
        page_size: usize,
        current_page: usize,
    ) -> usize {
        let all_loaded_messages = &model.queue_state().message_pagination.all_loaded_messages;

        // Find the highest position among selected messages in loaded data
        let mut max_loaded_position = 0;
        for (index, loaded_msg) in all_loaded_messages.iter().enumerate() {
            if self
                .message_ids
                .iter()
                .any(|msg_id| msg_id.id == loaded_msg.id)
            {
                max_loaded_position = std::cmp::max(max_loaded_position, index + 1);
            }
        }

        // If we found positions in loaded data, use that
        if max_loaded_position > 0 {
            log::info!(
                "Found selected messages in loaded data, highest position: {max_loaded_position}"
            );
            return max_loaded_position;
        }

        // Fallback: estimate based on current page
        // If user is on page 30 selecting messages, those messages are likely around position 30 * page_size
        let page_based_estimate = (current_page + 1) * page_size;

        log::info!(
            "Using page-based estimation: page {} * page_size {} = estimated position {}",
            current_page + 1,
            page_size,
            page_based_estimate
        );

        page_based_estimate
    }
}

/// Context information for bulk operation post-processing
#[derive(Debug, Clone)]
pub struct BulkOperationContext {
    pub auto_reload_threshold: usize,
    pub current_message_count: usize,
    pub selected_from_current_page: usize,
    pub max_position: usize,
}

/// Get human-readable queue type name
pub fn queue_type_display_name(queue_type: QueueType) -> &'static str {
    match queue_type {
        QueueType::Main => "main queue",
        QueueType::DeadLetter => "dead letter queue",
    }
}

/// Common validation patterns extracted as a trait
pub trait BulkOperationValidation<T: TerminalAdapter> {
    /// Quick validation for empty message lists
    fn validate_not_empty(message_ids: &[MessageIdentifier]) -> Result<(), AppError> {
        if message_ids.is_empty() {
            log::warn!("No messages provided for bulk operation");
            return Err(AppError::State(
                "No messages selected for bulk operation".to_string(),
            ));
        }
        Ok(())
    }

    /// Log operation warning about message order
    fn log_message_order_warning(message_count: usize, operation_name: &str) {
        log::warn!(
            "Bulk {operation_name} for {message_count} messages may affect message order. Messages may not be processed in their original sequence."
        );
    }
}

// Implement the validation trait for any TerminalAdapter type
impl<T: TerminalAdapter> BulkOperationValidation<T> for Model<T> {}