quetty-server 0.1.7

Core Azure Service Bus client library for Quetty terminal application
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
//! Types and data structures for bulk operations.
//!
//! This module defines the core types used throughout the bulk operations system,
//! including result tracking, message identification, configuration, and operation contexts.

use crate::consumer::Consumer;
use azservicebus::ServiceBusClient;
use azservicebus::core::BasicRetryPolicy;
use serde::Deserialize;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;

/// Result of a bulk operation with detailed statistics and error tracking.
///
/// Provides comprehensive information about the outcome of bulk operations,
/// including success counts, failure details, and lists of processed messages.
/// Used to report operation results to callers and for UI feedback.
///
/// # Examples
///
/// ```no_run
/// use quetty_server::bulk_operations::BulkOperationResult;
///
/// let mut result = BulkOperationResult::new(100);
/// result.add_success();
/// result.add_failure("Connection timeout".to_string());
///
/// if result.is_complete_success() {
///     println!("All operations completed successfully");
/// } else {
///     println!("Partial success: {} of {} succeeded",
///              result.successful, result.total_requested);
/// }
/// ```
#[derive(Debug, Clone)]
pub struct BulkOperationResult {
    /// Total number of operations requested
    pub total_requested: usize,
    /// Number of operations that completed successfully
    pub successful: usize,
    /// Number of operations that failed
    pub failed: usize,
    /// Number of target items that were not found
    pub not_found: usize,
    /// Detailed error messages for failed operations
    pub error_details: Vec<String>,
    /// Identifiers of messages that were processed successfully
    pub successful_message_ids: Vec<MessageIdentifier>,
}

impl BulkOperationResult {
    /// Creates a new BulkOperationResult for the specified number of operations.
    ///
    /// # Arguments
    ///
    /// * `total_requested` - The total number of operations that will be attempted
    ///
    /// # Returns
    ///
    /// A new result tracker with zero counts and empty collections
    pub fn new(total_requested: usize) -> Self {
        Self {
            total_requested,
            successful: 0,
            failed: 0,
            not_found: 0,
            error_details: Vec::new(),
            successful_message_ids: Vec::new(),
        }
    }

    pub fn add_success(&mut self) {
        self.successful += 1;
    }

    pub fn add_failure(&mut self, error: String) {
        self.failed += 1;
        self.error_details.push(error);
    }

    pub fn add_successful_message(&mut self, message_id: MessageIdentifier) {
        self.successful += 1;
        self.successful_message_ids.push(message_id.clone());
        log::debug!(
            "SUCCESS COUNT: Incremented to {} (added message: {})",
            self.successful,
            message_id.id
        );
    }

    pub fn add_not_found(&mut self) {
        self.not_found += 1;
    }

    /// Checks if all requested operations completed successfully.
    ///
    /// # Returns
    ///
    /// `true` if all operations succeeded with no failures or missing items
    pub fn is_complete_success(&self) -> bool {
        self.successful == self.total_requested && self.failed == 0 && self.not_found == 0
    }
}

/// Identifier for targeting specific messages in bulk operations.
///
/// Combines message ID and sequence number for precise message targeting.
/// The sequence number is used for optimization during bulk scanning operations
/// to minimize the number of messages that need to be processed.
///
/// # Examples
///
/// ```no_run
/// use quetty_server::bulk_operations::MessageIdentifier;
///
/// let msg_id = MessageIdentifier::new("msg-123".to_string(), 4567);
/// println!("Message: {} at sequence {}", msg_id.id, msg_id.sequence);
///
/// let composite = msg_id.composite_key();
/// println!("Composite key: {}", composite);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MessageIdentifier {
    /// The unique message identifier
    pub id: String,
    /// The message sequence number for optimization
    pub sequence: i64,
}

impl std::fmt::Display for MessageIdentifier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.id)
    }
}

impl MessageIdentifier {
    /// Creates a new MessageIdentifier with the specified ID and sequence.
    ///
    /// # Arguments
    ///
    /// * `id` - The unique message identifier
    /// * `sequence` - The message sequence number
    ///
    /// # Returns
    ///
    /// A new MessageIdentifier instance
    pub fn new(id: String, sequence: i64) -> Self {
        Self { id, sequence }
    }

    pub fn from_message(message: &crate::model::MessageModel) -> Self {
        Self {
            id: message.id.clone(),
            sequence: message.sequence,
        }
    }

    pub fn from_string(id: String) -> Self {
        Self { id, sequence: 0 }
    }

    /// Creates a composite key for exact matching.
    ///
    /// Combines the message ID and sequence number into a single string
    /// that can be used for precise identification in hash maps or other
    /// data structures that require string keys.
    ///
    /// # Returns
    ///
    /// A string in the format "id:sequence"
    pub fn composite_key(&self) -> String {
        format!("{}:{}", self.id, self.sequence)
    }
}

impl From<String> for MessageIdentifier {
    fn from(id: String) -> Self {
        Self::from_string(id)
    }
}

impl From<&str> for MessageIdentifier {
    fn from(id: &str) -> Self {
        Self::from_string(id.to_string())
    }
}

impl From<MessageIdentifier> for String {
    fn from(val: MessageIdentifier) -> Self {
        val.id
    }
}

impl PartialEq<String> for MessageIdentifier {
    fn eq(&self, other: &String) -> bool {
        &self.id == other
    }
}

impl PartialEq<MessageIdentifier> for String {
    fn eq(&self, other: &MessageIdentifier) -> bool {
        self == &other.id
    }
}

/// Configuration for bulk operation batching and limits.
///
/// Controls various aspects of bulk operations including batch sizes,
/// timeouts, processing limits, and UI behavior. Provides sensible defaults
/// for all configuration values.
///
/// # Examples
///
/// ```no_run
/// use quetty_server::bulk_operations::BatchConfig;
///
/// // Use default configuration
/// let config = BatchConfig::default();
///
/// // Create custom configuration
/// let config = BatchConfig::new(100, 600);
///
/// // Access configuration values
/// println!("Max batch size: {}", config.max_batch_size());
/// println!("Timeout: {}s", config.operation_timeout_secs());
/// ```
#[derive(Debug, Deserialize, Default, Clone)]
pub struct BatchConfig {
    /// Maximum batch size for bulk operations (default: 200)
    max_batch_size: Option<u32>,
    /// Timeout for bulk operations in seconds (default: 300)
    operation_timeout_secs: Option<u64>,
    /// Chunk size for bulk processing operations (default: 200, same as max_batch_size)
    bulk_chunk_size: Option<usize>,
    /// Processing time limit for bulk operations in seconds (default: 30)
    bulk_processing_time_secs: Option<u64>,
    /// Timeout for lock operations in seconds (default: 10)
    lock_timeout_secs: Option<u64>,
    /// Maximum messages to process in bulk operations (default: 10,000)
    max_messages_to_process: Option<usize>,
    /// Auto-reload threshold for UI refresh after bulk operations (default: 50)
    auto_reload_threshold: Option<usize>,
    /// Timeout for individual receive message operations in seconds (default: 5)
    receive_timeout_secs: Option<u64>,
}

impl BatchConfig {
    /// Creates a new BatchConfig with specified batch size and timeout.
    ///
    /// Other configuration values will use their defaults when accessed.
    ///
    /// # Arguments
    ///
    /// * `max_batch_size` - Maximum number of messages per batch
    /// * `operation_timeout_secs` - Timeout for bulk operations in seconds
    ///
    /// # Returns
    ///
    /// A new BatchConfig with the specified values
    pub fn new(max_batch_size: u32, operation_timeout_secs: u64) -> Self {
        Self {
            max_batch_size: Some(max_batch_size),
            operation_timeout_secs: Some(operation_timeout_secs),
            bulk_chunk_size: None,
            bulk_processing_time_secs: None,
            lock_timeout_secs: None,
            max_messages_to_process: None,
            auto_reload_threshold: None,
            receive_timeout_secs: None,
        }
    }

    /// Get the maximum batch size for bulk operations
    pub fn max_batch_size(&self) -> u32 {
        self.max_batch_size.unwrap_or(500)
    }

    /// Get the timeout for bulk operations
    pub fn operation_timeout_secs(&self) -> u64 {
        self.operation_timeout_secs.unwrap_or(600)
    }

    /// Get the chunk size for bulk processing operations
    pub fn bulk_chunk_size(&self) -> usize {
        self.bulk_chunk_size.unwrap_or(500)
    }

    /// Get the processing time limit for bulk operations in seconds
    pub fn bulk_processing_time_secs(&self) -> u64 {
        self.bulk_processing_time_secs.unwrap_or(300)
    }

    /// Get the timeout for lock operations in seconds
    pub fn lock_timeout_secs(&self) -> u64 {
        self.lock_timeout_secs.unwrap_or(10)
    }

    /// Get the maximum messages to process in bulk operations
    pub fn max_messages_to_process(&self) -> usize {
        self.max_messages_to_process.unwrap_or(10_000)
    }

    /// Get the threshold for triggering auto-reload after bulk operations
    pub fn auto_reload_threshold(&self) -> usize {
        self.auto_reload_threshold.unwrap_or(50)
    }

    /// Get the timeout for individual receive message operations in seconds
    pub fn receive_timeout_secs(&self) -> u64 {
        self.receive_timeout_secs.unwrap_or(5)
    }
}

/// Context for Service Bus operations containing shared resources
#[derive(Debug, Clone)]
pub struct ServiceBusOperationContext {
    pub consumer: Arc<Mutex<Consumer>>,
    pub service_bus_client: Arc<Mutex<ServiceBusClient<BasicRetryPolicy>>>,
    pub main_queue_name: String,
    pub cancel_token: CancellationToken,
}

impl ServiceBusOperationContext {
    /// Create a new ServiceBusOperationContext
    pub fn new(
        consumer: Arc<Mutex<Consumer>>,
        service_bus_client: Arc<Mutex<ServiceBusClient<BasicRetryPolicy>>>,
        main_queue_name: String,
    ) -> Self {
        Self {
            consumer,
            service_bus_client,
            main_queue_name,
            cancel_token: CancellationToken::new(),
        }
    }
}

/// Parameters for bulk send operations
#[derive(Debug, Clone)]
pub struct BulkSendParams {
    pub target_queue: String,
    pub should_delete: bool,
    pub message_identifiers: Vec<MessageIdentifier>,
    pub messages_data: Option<Vec<(MessageIdentifier, Vec<u8>)>>, // For peek-based operations
    pub max_position: usize,                                      // For dynamic processing limits
}

impl BulkSendParams {
    /// Create parameters for operations that retrieve messages from the queue
    pub fn with_retrieval(
        target_queue: String,
        should_delete: bool,
        message_identifiers: Vec<MessageIdentifier>,
        max_position: usize,
    ) -> Self {
        Self {
            target_queue,
            should_delete,
            message_identifiers,
            messages_data: None,
            max_position,
        }
    }

    /// Create parameters for operations with pre-fetched message data
    pub fn with_message_data(
        target_queue: String,
        should_delete: bool,
        messages_data: Vec<(MessageIdentifier, Vec<u8>)>,
        max_position: usize,
    ) -> Self {
        // Extract identifiers from the data
        let message_identifiers = messages_data.iter().map(|(id, _)| id.clone()).collect();

        Self {
            target_queue,
            should_delete,
            message_identifiers,
            messages_data: Some(messages_data),
            max_position,
        }
    }

    /// Create parameters with max position for better processing limits
    pub fn with_max_position(
        target_queue: String,
        should_delete: bool,
        message_identifiers: Vec<MessageIdentifier>,
        max_position: usize,
    ) -> Self {
        Self {
            target_queue,
            should_delete,
            message_identifiers,
            messages_data: None,
            max_position,
        }
    }
}

/// Queue operation type determination
#[derive(Debug, Clone)]
pub enum QueueOperationType {
    /// Send to regular queue (copy message content)
    SendToQueue,
    /// Send to dead letter queue (use dead_letter_message operation)
    SendToDLQ,
}

impl QueueOperationType {
    /// Determine operation type based on target queue name
    pub fn from_queue_name(queue_name: &str) -> Self {
        if queue_name.ends_with("/$deadletterqueue") {
            Self::SendToDLQ
        } else {
            Self::SendToQueue
        }
    }
}

/// Bulk operation context containing shared resources
#[derive(Debug, Clone)]
pub struct BulkOperationContext {
    pub consumer: Arc<Mutex<crate::consumer::Consumer>>,
    pub cancel_token: CancellationToken,
    /// Name of the queue this operation is targeting (used for deferred message persistence)
    pub queue_name: String,
}

/// Parameters for process_target_messages method
pub struct ProcessTargetMessagesParams<'a> {
    pub messages: Vec<azservicebus::ServiceBusReceivedMessage>,
    pub context: &'a BulkOperationContext,
    pub params: &'a BulkSendParams,
    pub target_map: &'a HashMap<String, MessageIdentifier>,
    pub result: &'a mut BulkOperationResult,
}

impl<'a> ProcessTargetMessagesParams<'a> {
    pub fn new(
        messages: Vec<azservicebus::ServiceBusReceivedMessage>,
        context: &'a BulkOperationContext,
        params: &'a BulkSendParams,
        target_map: &'a HashMap<String, MessageIdentifier>,
        result: &'a mut BulkOperationResult,
    ) -> Self {
        Self {
            messages,
            context,
            params,
            target_map,
            result,
        }
    }
}