kaccy-ai 0.2.0

AI-powered intelligence for Kaccy Protocol - forecasting, optimization, and insights
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
//! Performance profiling utilities for AI operations
//!
//! This module provides tools for monitoring, measuring, and optimizing
//! AI service performance including latency, cost, and resource usage.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};

/// Performance metrics for a single operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationMetrics {
    /// Operation name/identifier
    pub operation: String,
    /// Duration of the operation
    pub duration: Duration,
    /// Number of tokens used (if applicable)
    pub tokens_used: Option<u32>,
    /// Estimated cost in USD (if applicable)
    pub estimated_cost: Option<f64>,
    /// Whether the operation succeeded
    pub success: bool,
    /// Error message (if failed)
    pub error: Option<String>,
    /// Timestamp when the operation completed
    pub timestamp: std::time::SystemTime,
}

impl OperationMetrics {
    /// Create new operation metrics
    #[must_use]
    pub fn new(operation: String, duration: Duration, success: bool) -> Self {
        Self {
            operation,
            duration,
            tokens_used: None,
            estimated_cost: None,
            success,
            error: None,
            timestamp: std::time::SystemTime::now(),
        }
    }

    /// Set token usage
    #[must_use]
    pub fn with_tokens(mut self, tokens: u32) -> Self {
        self.tokens_used = Some(tokens);
        self
    }

    /// Set estimated cost
    #[must_use]
    pub fn with_cost(mut self, cost: f64) -> Self {
        self.estimated_cost = Some(cost);
        self
    }

    /// Set error message
    #[must_use]
    pub fn with_error(mut self, error: String) -> Self {
        self.error = Some(error);
        self
    }
}

/// Aggregated statistics for an operation type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationStats {
    /// Total number of operations
    pub total_count: u64,
    /// Number of successful operations
    pub success_count: u64,
    /// Number of failed operations
    pub failure_count: u64,
    /// Average duration
    pub avg_duration: Duration,
    /// Minimum duration
    pub min_duration: Duration,
    /// Maximum duration
    pub max_duration: Duration,
    /// Total tokens used
    pub total_tokens: u64,
    /// Total estimated cost
    pub total_cost: f64,
    /// Success rate (0.0 to 1.0)
    pub success_rate: f64,
}

impl Default for OperationStats {
    fn default() -> Self {
        Self {
            total_count: 0,
            success_count: 0,
            failure_count: 0,
            avg_duration: Duration::ZERO,
            min_duration: Duration::MAX,
            max_duration: Duration::ZERO,
            total_tokens: 0,
            total_cost: 0.0,
            success_rate: 0.0,
        }
    }
}

impl OperationStats {
    /// Update stats with a new operation metric
    fn update(&mut self, metric: &OperationMetrics) {
        self.total_count += 1;

        if metric.success {
            self.success_count += 1;
        } else {
            self.failure_count += 1;
        }

        // Update duration stats
        if metric.duration < self.min_duration {
            self.min_duration = metric.duration;
        }
        if metric.duration > self.max_duration {
            self.max_duration = metric.duration;
        }

        // Update average (incremental mean)
        let total_ms = self.avg_duration.as_millis() as u64 * (self.total_count - 1)
            + metric.duration.as_millis() as u64;
        self.avg_duration = Duration::from_millis(total_ms / self.total_count);

        // Update tokens and cost
        if let Some(tokens) = metric.tokens_used {
            self.total_tokens += u64::from(tokens);
        }
        if let Some(cost) = metric.estimated_cost {
            self.total_cost += cost;
        }

        // Update success rate
        self.success_rate = self.success_count as f64 / self.total_count as f64;
    }

    /// Get average cost per operation
    #[must_use]
    pub fn avg_cost(&self) -> f64 {
        if self.total_count == 0 {
            0.0
        } else {
            self.total_cost / self.total_count as f64
        }
    }

    /// Get average tokens per operation
    #[must_use]
    pub fn avg_tokens(&self) -> f64 {
        if self.total_count == 0 {
            0.0
        } else {
            self.total_tokens as f64 / self.total_count as f64
        }
    }
}

/// Performance profiler for tracking AI operations
pub struct PerformanceProfiler {
    /// All recorded metrics
    metrics: Arc<Mutex<Vec<OperationMetrics>>>,
    /// Aggregated statistics per operation type
    stats: Arc<Mutex<HashMap<String, OperationStats>>>,
    /// Whether profiling is enabled
    enabled: bool,
}

impl Default for PerformanceProfiler {
    fn default() -> Self {
        Self::new()
    }
}

impl PerformanceProfiler {
    /// Create a new performance profiler
    #[must_use]
    pub fn new() -> Self {
        Self {
            metrics: Arc::new(Mutex::new(Vec::new())),
            stats: Arc::new(Mutex::new(HashMap::new())),
            enabled: true,
        }
    }

    /// Enable profiling
    pub fn enable(&mut self) {
        self.enabled = true;
    }

    /// Disable profiling
    pub fn disable(&mut self) {
        self.enabled = false;
    }

    /// Check if profiling is enabled
    #[must_use]
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Record an operation metric
    pub fn record(&self, metric: OperationMetrics) {
        if !self.enabled {
            return;
        }

        // Update aggregated stats
        if let Ok(mut stats) = self.stats.lock() {
            let operation_stats = stats
                .entry(metric.operation.clone())
                .or_insert_with(OperationStats::default);
            operation_stats.update(&metric);
        }

        // Store the metric
        if let Ok(mut metrics) = self.metrics.lock() {
            metrics.push(metric);
        }
    }

    /// Get statistics for a specific operation
    #[must_use]
    pub fn get_stats(&self, operation: &str) -> Option<OperationStats> {
        self.stats
            .lock()
            .ok()
            .and_then(|stats| stats.get(operation).cloned())
    }

    /// Get statistics for all operations
    #[must_use]
    pub fn get_all_stats(&self) -> HashMap<String, OperationStats> {
        self.stats
            .lock()
            .ok()
            .map(|s| s.clone())
            .unwrap_or_default()
    }

    /// Get all recorded metrics
    #[must_use]
    pub fn get_all_metrics(&self) -> Vec<OperationMetrics> {
        self.metrics
            .lock()
            .ok()
            .map(|m| m.clone())
            .unwrap_or_default()
    }

    /// Clear all recorded data
    pub fn clear(&self) {
        if let Ok(mut metrics) = self.metrics.lock() {
            metrics.clear();
        }
        if let Ok(mut stats) = self.stats.lock() {
            stats.clear();
        }
    }

    /// Get total operations count
    #[must_use]
    pub fn total_operations(&self) -> u64 {
        self.stats
            .lock()
            .ok()
            .map_or(0, |s| s.values().map(|stat| stat.total_count).sum())
    }

    /// Get total cost across all operations
    #[must_use]
    pub fn total_cost(&self) -> f64 {
        self.stats
            .lock()
            .ok()
            .map_or(0.0, |s| s.values().map(|stat| stat.total_cost).sum())
    }

    /// Get total tokens used across all operations
    #[must_use]
    pub fn total_tokens(&self) -> u64 {
        self.stats
            .lock()
            .ok()
            .map_or(0, |s| s.values().map(|stat| stat.total_tokens).sum())
    }

    /// Generate a performance report
    #[must_use]
    pub fn generate_report(&self) -> PerformanceReport {
        let stats = self.get_all_stats();
        let total_ops = self.total_operations();
        let total_cost = self.total_cost();
        let total_tokens = self.total_tokens();

        let overall_success_rate = if total_ops > 0 {
            stats.values().map(|s| s.success_count).sum::<u64>() as f64 / total_ops as f64
        } else {
            0.0
        };

        PerformanceReport {
            total_operations: total_ops,
            total_cost,
            total_tokens,
            overall_success_rate,
            operation_stats: stats,
        }
    }
}

/// Complete performance report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceReport {
    /// Total number of operations across all types
    pub total_operations: u64,
    /// Total cost across all operations
    pub total_cost: f64,
    /// Total tokens used across all operations
    pub total_tokens: u64,
    /// Overall success rate
    pub overall_success_rate: f64,
    /// Statistics per operation type
    pub operation_stats: HashMap<String, OperationStats>,
}

impl PerformanceReport {
    /// Print a human-readable report
    pub fn print(&self) {
        println!("=== Performance Report ===");
        println!("Total Operations: {}", self.total_operations);
        println!("Total Cost: ${:.4}", self.total_cost);
        println!("Total Tokens: {}", self.total_tokens);
        println!(
            "Overall Success Rate: {:.2}%",
            self.overall_success_rate * 100.0
        );
        println!("\nPer-Operation Statistics:");

        for (operation, stats) in &self.operation_stats {
            println!("\n  {operation}:");
            println!("    Count: {}", stats.total_count);
            println!("    Success Rate: {:.2}%", stats.success_rate * 100.0);
            println!("    Avg Duration: {:?}", stats.avg_duration);
            println!("    Min Duration: {:?}", stats.min_duration);
            println!("    Max Duration: {:?}", stats.max_duration);
            println!("    Avg Cost: ${:.6}", stats.avg_cost());
            println!("    Avg Tokens: {:.1}", stats.avg_tokens());
        }
    }
}

/// Scoped profiler for automatic timing
pub struct ScopedProfiler {
    operation: String,
    start: Instant,
    profiler: Arc<PerformanceProfiler>,
    tokens: Option<u32>,
    cost: Option<f64>,
}

impl ScopedProfiler {
    /// Create a new scoped profiler
    #[must_use]
    pub fn new(operation: String, profiler: Arc<PerformanceProfiler>) -> Self {
        Self {
            operation,
            start: Instant::now(),
            profiler,
            tokens: None,
            cost: None,
        }
    }

    /// Set token usage
    pub fn set_tokens(&mut self, tokens: u32) {
        self.tokens = Some(tokens);
    }

    /// Set cost
    pub fn set_cost(&mut self, cost: f64) {
        self.cost = Some(cost);
    }

    /// Complete with success
    pub fn complete_success(self) {
        self.complete(true, None);
    }

    /// Complete with error
    pub fn complete_error(self, error: String) {
        self.complete(false, Some(error));
    }

    /// Internal completion
    fn complete(self, success: bool, error: Option<String>) {
        let duration = self.start.elapsed();
        let mut metric = OperationMetrics::new(self.operation.clone(), duration, success);

        if let Some(tokens) = self.tokens {
            metric = metric.with_tokens(tokens);
        }
        if let Some(cost) = self.cost {
            metric = metric.with_cost(cost);
        }
        if let Some(err) = error {
            metric = metric.with_error(err);
        }

        self.profiler.record(metric);
    }
}

impl Drop for ScopedProfiler {
    fn drop(&mut self) {
        // Auto-complete as success if not explicitly completed
        let duration = self.start.elapsed();
        let mut metric = OperationMetrics::new(self.operation.clone(), duration, true);

        if let Some(tokens) = self.tokens {
            metric = metric.with_tokens(tokens);
        }
        if let Some(cost) = self.cost {
            metric = metric.with_cost(cost);
        }

        self.profiler.record(metric);
    }
}

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

    #[test]
    fn test_operation_metrics_creation() {
        let metric = OperationMetrics::new("test_op".to_string(), Duration::from_millis(100), true)
            .with_tokens(500)
            .with_cost(0.01);

        assert_eq!(metric.operation, "test_op");
        assert_eq!(metric.duration, Duration::from_millis(100));
        assert!(metric.success);
        assert_eq!(metric.tokens_used, Some(500));
        assert_eq!(metric.estimated_cost, Some(0.01));
    }

    #[test]
    fn test_operation_stats_update() {
        let mut stats = OperationStats::default();

        let metric1 = OperationMetrics::new("test".to_string(), Duration::from_millis(100), true)
            .with_tokens(500)
            .with_cost(0.01);

        let metric2 = OperationMetrics::new("test".to_string(), Duration::from_millis(200), true)
            .with_tokens(600)
            .with_cost(0.02);

        stats.update(&metric1);
        stats.update(&metric2);

        assert_eq!(stats.total_count, 2);
        assert_eq!(stats.success_count, 2);
        assert_eq!(stats.total_tokens, 1100);
        assert_eq!(stats.total_cost, 0.03);
        assert_eq!(stats.success_rate, 1.0);
    }

    #[test]
    fn test_profiler_record() {
        let profiler = PerformanceProfiler::new();

        let metric = OperationMetrics::new("eval".to_string(), Duration::from_millis(100), true)
            .with_tokens(500)
            .with_cost(0.01);

        profiler.record(metric);

        assert_eq!(profiler.total_operations(), 1);
        assert_eq!(profiler.total_tokens(), 500);
        assert_eq!(profiler.total_cost(), 0.01);
    }

    #[test]
    fn test_profiler_stats() {
        let profiler = PerformanceProfiler::new();

        profiler.record(OperationMetrics::new(
            "op1".to_string(),
            Duration::from_millis(100),
            true,
        ));
        profiler.record(OperationMetrics::new(
            "op1".to_string(),
            Duration::from_millis(200),
            true,
        ));
        profiler.record(OperationMetrics::new(
            "op2".to_string(),
            Duration::from_millis(150),
            true,
        ));

        let stats = profiler.get_stats("op1").unwrap();
        assert_eq!(stats.total_count, 2);
        assert_eq!(stats.success_count, 2);

        assert_eq!(profiler.total_operations(), 3);
    }

    #[test]
    fn test_profiler_clear() {
        let profiler = PerformanceProfiler::new();

        profiler.record(OperationMetrics::new(
            "test".to_string(),
            Duration::from_millis(100),
            true,
        ));

        assert_eq!(profiler.total_operations(), 1);

        profiler.clear();

        assert_eq!(profiler.total_operations(), 0);
    }

    #[test]
    fn test_performance_report() {
        let profiler = PerformanceProfiler::new();

        profiler.record(
            OperationMetrics::new("eval".to_string(), Duration::from_millis(100), true)
                .with_tokens(500)
                .with_cost(0.01),
        );

        let report = profiler.generate_report();

        assert_eq!(report.total_operations, 1);
        assert_eq!(report.total_tokens, 500);
        assert_eq!(report.total_cost, 0.01);
        assert_eq!(report.overall_success_rate, 1.0);
    }

    #[test]
    fn test_profiler_enable_disable() {
        let mut profiler = PerformanceProfiler::new();

        profiler.disable();
        assert!(!profiler.is_enabled());

        profiler.record(OperationMetrics::new(
            "test".to_string(),
            Duration::from_millis(100),
            true,
        ));

        assert_eq!(profiler.total_operations(), 0);

        profiler.enable();
        assert!(profiler.is_enabled());

        profiler.record(OperationMetrics::new(
            "test".to_string(),
            Duration::from_millis(100),
            true,
        ));

        assert_eq!(profiler.total_operations(), 1);
    }
}