heliosdb-nano 3.22.3

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
//! A/B Testing Metrics - Analytics and Statistics
//!
//! Collects and reports metrics for A/B testing experiments.

use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Branch-level metrics
#[derive(Debug, Clone, Default)]
pub struct BranchMetrics {
    /// Branch name
    pub branch: String,
    /// Total queries
    pub query_count: u64,
    /// Successful queries
    pub success_count: u64,
    /// Failed queries
    pub failure_count: u64,
    /// Total latency (ms) - for computing average
    pub total_latency_ms: f64,
    /// Min latency (ms)
    pub min_latency_ms: f64,
    /// Max latency (ms)
    pub max_latency_ms: f64,
    /// Custom events: event_name -> (count, total_value)
    pub events: HashMap<String, (u64, f64)>,
    /// First query timestamp
    pub first_query: Option<chrono::DateTime<chrono::Utc>>,
    /// Last query timestamp
    pub last_query: Option<chrono::DateTime<chrono::Utc>>,
}

impl BranchMetrics {
    /// Create new branch metrics
    pub fn new(branch: impl Into<String>) -> Self {
        Self {
            branch: branch.into(),
            min_latency_ms: f64::MAX,
            max_latency_ms: f64::MIN,
            ..Default::default()
        }
    }

    /// Record a query
    pub fn record_query(&mut self, latency_ms: f64, success: bool) {
        self.query_count += 1;
        self.total_latency_ms += latency_ms;

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

        if latency_ms < self.min_latency_ms {
            self.min_latency_ms = latency_ms;
        }
        if latency_ms > self.max_latency_ms {
            self.max_latency_ms = latency_ms;
        }

        let now = chrono::Utc::now();
        if self.first_query.is_none() {
            self.first_query = Some(now);
        }
        self.last_query = Some(now);
    }

    /// Record a custom event
    pub fn record_event(&mut self, event_name: &str, value: f64) {
        let entry = self.events.entry(event_name.to_string()).or_default();
        entry.0 += 1;
        entry.1 += value;
    }

    /// Get average latency
    pub fn avg_latency_ms(&self) -> f64 {
        if self.query_count == 0 {
            0.0
        } else {
            self.total_latency_ms / self.query_count as f64
        }
    }

    /// Get success rate
    pub fn success_rate(&self) -> f64 {
        if self.query_count == 0 {
            0.0
        } else {
            self.success_count as f64 / self.query_count as f64
        }
    }

    /// Get error rate
    pub fn error_rate(&self) -> f64 {
        if self.query_count == 0 {
            0.0
        } else {
            self.failure_count as f64 / self.query_count as f64
        }
    }

    /// Get event average
    pub fn event_average(&self, event_name: &str) -> f64 {
        if let Some((count, total)) = self.events.get(event_name) {
            if *count == 0 {
                0.0
            } else {
                *total / *count as f64
            }
        } else {
            0.0
        }
    }
}

/// Experiment-level metrics
#[derive(Debug, Clone)]
pub struct ExperimentMetrics {
    /// Experiment name
    pub experiment: String,
    /// Per-branch metrics
    pub branches: HashMap<String, BranchMetrics>,
    /// Total queries across all branches
    pub total_queries: u64,
    /// Experiment start time (first query)
    pub started_at: Option<chrono::DateTime<chrono::Utc>>,
    /// Last activity
    pub last_activity: Option<chrono::DateTime<chrono::Utc>>,
}

impl ExperimentMetrics {
    /// Create new experiment metrics
    pub fn new(experiment: impl Into<String>) -> Self {
        Self {
            experiment: experiment.into(),
            branches: HashMap::new(),
            total_queries: 0,
            started_at: None,
            last_activity: None,
        }
    }

    /// Get or create branch metrics
    pub fn get_branch_mut(&mut self, branch: &str) -> &mut BranchMetrics {
        self.branches
            .entry(branch.to_string())
            .or_insert_with(|| BranchMetrics::new(branch))
    }

    /// Get branch metrics
    pub fn get_branch(&self, branch: &str) -> Option<&BranchMetrics> {
        self.branches.get(branch)
    }

    /// Record a query for a branch
    pub fn record_query(&mut self, branch: &str, latency_ms: f64, success: bool) {
        let metrics = self.get_branch_mut(branch);
        metrics.record_query(latency_ms, success);

        self.total_queries += 1;

        let now = chrono::Utc::now();
        if self.started_at.is_none() {
            self.started_at = Some(now);
        }
        self.last_activity = Some(now);
    }

    /// Record a custom event for a branch
    pub fn record_event(&mut self, branch: &str, event_name: &str, value: f64) {
        let metrics = self.get_branch_mut(branch);
        metrics.record_event(event_name, value);
    }

    /// Compare branches (simple statistical comparison)
    pub fn compare(&self, branch_a: &str, branch_b: &str) -> Option<BranchComparison> {
        let a = self.get_branch(branch_a)?;
        let b = self.get_branch(branch_b)?;

        Some(BranchComparison {
            branch_a: branch_a.to_string(),
            branch_b: branch_b.to_string(),
            sample_size_a: a.query_count,
            sample_size_b: b.query_count,
            success_rate_a: a.success_rate(),
            success_rate_b: b.success_rate(),
            success_rate_diff: b.success_rate() - a.success_rate(),
            avg_latency_a: a.avg_latency_ms(),
            avg_latency_b: b.avg_latency_ms(),
            latency_diff: b.avg_latency_ms() - a.avg_latency_ms(),
            // Statistical significance would require more complex calculation
            is_significant: false,
            confidence: 0.0,
        })
    }

    /// Get summary statistics
    pub fn summary(&self) -> ExperimentSummary {
        let branch_count = self.branches.len();
        let total_queries = self.total_queries;

        let (best_branch, best_success_rate) = self.branches
            .iter()
            .max_by(|(_, a), (_, b)| {
                a.success_rate().partial_cmp(&b.success_rate()).unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|(name, metrics)| (Some(name.clone()), metrics.success_rate()))
            .unwrap_or((None, 0.0));

        let avg_latency = if total_queries == 0 {
            0.0
        } else {
            self.branches.values().map(|b| b.total_latency_ms).sum::<f64>() / total_queries as f64
        };

        ExperimentSummary {
            experiment: self.experiment.clone(),
            branch_count,
            total_queries,
            best_branch,
            best_success_rate,
            avg_latency_ms: avg_latency,
            duration: self.started_at.map(|start| {
                chrono::Utc::now().signed_duration_since(start)
            }),
        }
    }
}

/// Comparison between two branches
#[derive(Debug, Clone)]
pub struct BranchComparison {
    /// First branch name
    pub branch_a: String,
    /// Second branch name
    pub branch_b: String,
    /// Sample size for branch A
    pub sample_size_a: u64,
    /// Sample size for branch B
    pub sample_size_b: u64,
    /// Success rate for branch A
    pub success_rate_a: f64,
    /// Success rate for branch B
    pub success_rate_b: f64,
    /// Difference in success rate (B - A)
    pub success_rate_diff: f64,
    /// Average latency for branch A
    pub avg_latency_a: f64,
    /// Average latency for branch B
    pub avg_latency_b: f64,
    /// Difference in latency (B - A)
    pub latency_diff: f64,
    /// Is the difference statistically significant
    pub is_significant: bool,
    /// Confidence level (if significant)
    pub confidence: f64,
}

/// Experiment summary
#[derive(Debug, Clone)]
pub struct ExperimentSummary {
    /// Experiment name
    pub experiment: String,
    /// Number of branches
    pub branch_count: usize,
    /// Total queries
    pub total_queries: u64,
    /// Best performing branch
    pub best_branch: Option<String>,
    /// Best success rate
    pub best_success_rate: f64,
    /// Average latency
    pub avg_latency_ms: f64,
    /// Experiment duration
    pub duration: Option<chrono::Duration>,
}

/// A/B Metrics Manager
pub struct ABMetrics {
    /// Per-experiment metrics
    experiments: Arc<RwLock<HashMap<String, ExperimentMetrics>>>,
}

impl ABMetrics {
    /// Create a new metrics manager
    pub fn new() -> Self {
        Self {
            experiments: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Record a query
    pub async fn record_query(
        &self,
        experiment: &str,
        branch: &str,
        latency_ms: f64,
        success: bool,
    ) {
        let mut experiments = self.experiments.write().await;
        let metrics = experiments
            .entry(experiment.to_string())
            .or_insert_with(|| ExperimentMetrics::new(experiment));

        metrics.record_query(branch, latency_ms, success);
    }

    /// Record a custom event
    pub async fn record_event(
        &self,
        experiment: &str,
        branch: &str,
        event_name: &str,
        value: f64,
    ) {
        let mut experiments = self.experiments.write().await;
        let metrics = experiments
            .entry(experiment.to_string())
            .or_insert_with(|| ExperimentMetrics::new(experiment));

        metrics.record_event(branch, event_name, value);
    }

    /// Get experiment metrics
    pub async fn get_experiment_metrics(&self, experiment: &str) -> Option<ExperimentMetrics> {
        self.experiments.read().await.get(experiment).cloned()
    }

    /// Get branch metrics
    pub async fn get_branch_metrics(
        &self,
        experiment: &str,
        branch: &str,
    ) -> Option<BranchMetrics> {
        self.experiments
            .read()
            .await
            .get(experiment)
            .and_then(|e| e.get_branch(branch).cloned())
    }

    /// Compare branches
    pub async fn compare_branches(
        &self,
        experiment: &str,
        branch_a: &str,
        branch_b: &str,
    ) -> Option<BranchComparison> {
        self.experiments
            .read()
            .await
            .get(experiment)
            .and_then(|e| e.compare(branch_a, branch_b))
    }

    /// Get experiment summary
    pub async fn get_summary(&self, experiment: &str) -> Option<ExperimentSummary> {
        self.experiments
            .read()
            .await
            .get(experiment)
            .map(|e| e.summary())
    }

    /// List all experiments with summaries
    pub async fn all_summaries(&self) -> Vec<ExperimentSummary> {
        self.experiments
            .read()
            .await
            .values()
            .map(|e| e.summary())
            .collect()
    }

    /// Clear metrics for an experiment
    pub async fn clear_experiment(&self, experiment: &str) {
        self.experiments.write().await.remove(experiment);
    }

    /// Clear all metrics
    pub async fn clear_all(&self) {
        self.experiments.write().await.clear();
    }

    /// Get overall statistics
    pub async fn stats(&self) -> MetricsStats {
        let experiments = self.experiments.read().await;
        let total_queries: u64 = experiments.values().map(|e| e.total_queries).sum();
        let total_branches: usize = experiments.values().map(|e| e.branches.len()).sum();

        MetricsStats {
            experiments_tracked: experiments.len(),
            total_branches_tracked: total_branches,
            total_queries_recorded: total_queries,
        }
    }
}

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

/// Overall metrics statistics
#[derive(Debug, Clone)]
pub struct MetricsStats {
    /// Number of experiments being tracked
    pub experiments_tracked: usize,
    /// Total branches across all experiments
    pub total_branches_tracked: usize,
    /// Total queries recorded
    pub total_queries_recorded: u64,
}

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

    #[test]
    fn test_branch_metrics_new() {
        let metrics = BranchMetrics::new("test");
        assert_eq!(metrics.branch, "test");
        assert_eq!(metrics.query_count, 0);
    }

    #[test]
    fn test_branch_metrics_record() {
        let mut metrics = BranchMetrics::new("test");

        metrics.record_query(10.0, true);
        metrics.record_query(20.0, true);
        metrics.record_query(30.0, false);

        assert_eq!(metrics.query_count, 3);
        assert_eq!(metrics.success_count, 2);
        assert_eq!(metrics.failure_count, 1);
        assert_eq!(metrics.min_latency_ms, 10.0);
        assert_eq!(metrics.max_latency_ms, 30.0);
        assert_eq!(metrics.avg_latency_ms(), 20.0);
        assert!((metrics.success_rate() - 0.666).abs() < 0.01);
    }

    #[test]
    fn test_branch_metrics_events() {
        let mut metrics = BranchMetrics::new("test");

        metrics.record_event("conversion", 1.0);
        metrics.record_event("conversion", 1.0);
        metrics.record_event("revenue", 100.0);
        metrics.record_event("revenue", 200.0);

        assert_eq!(metrics.event_average("conversion"), 1.0);
        assert_eq!(metrics.event_average("revenue"), 150.0);
        assert_eq!(metrics.event_average("nonexistent"), 0.0);
    }

    #[test]
    fn test_experiment_metrics() {
        let mut metrics = ExperimentMetrics::new("test_exp");

        metrics.record_query("control", 10.0, true);
        metrics.record_query("control", 15.0, true);
        metrics.record_query("treatment", 8.0, true);
        metrics.record_query("treatment", 12.0, false);

        assert_eq!(metrics.total_queries, 4);
        assert_eq!(metrics.branches.len(), 2);

        let control = metrics.get_branch("control").unwrap();
        assert_eq!(control.query_count, 2);
        assert_eq!(control.success_rate(), 1.0);

        let treatment = metrics.get_branch("treatment").unwrap();
        assert_eq!(treatment.query_count, 2);
        assert_eq!(treatment.success_rate(), 0.5);
    }

    #[test]
    fn test_branch_comparison() {
        let mut metrics = ExperimentMetrics::new("test_exp");

        metrics.record_query("control", 100.0, true);
        metrics.record_query("control", 100.0, false);
        metrics.record_query("treatment", 80.0, true);
        metrics.record_query("treatment", 80.0, true);

        let comparison = metrics.compare("control", "treatment").unwrap();

        assert_eq!(comparison.success_rate_a, 0.5);
        assert_eq!(comparison.success_rate_b, 1.0);
        assert_eq!(comparison.success_rate_diff, 0.5);
        assert_eq!(comparison.avg_latency_a, 100.0);
        assert_eq!(comparison.avg_latency_b, 80.0);
    }

    #[test]
    fn test_experiment_summary() {
        let mut metrics = ExperimentMetrics::new("test_exp");

        metrics.record_query("control", 10.0, true);
        metrics.record_query("treatment", 8.0, true);
        metrics.record_query("treatment", 8.0, true);

        let summary = metrics.summary();

        assert_eq!(summary.experiment, "test_exp");
        assert_eq!(summary.branch_count, 2);
        assert_eq!(summary.total_queries, 3);
        assert_eq!(summary.best_branch, Some("treatment".to_string()));
    }

    #[tokio::test]
    async fn test_ab_metrics_manager() {
        let manager = ABMetrics::new();

        manager.record_query("exp1", "control", 10.0, true).await;
        manager.record_query("exp1", "treatment", 8.0, true).await;
        manager.record_event("exp1", "control", "conversion", 1.0).await;

        let metrics = manager.get_experiment_metrics("exp1").await.unwrap();
        assert_eq!(metrics.total_queries, 2);

        let branch = manager.get_branch_metrics("exp1", "control").await.unwrap();
        assert_eq!(branch.query_count, 1);

        let stats = manager.stats().await;
        assert_eq!(stats.experiments_tracked, 1);
        assert_eq!(stats.total_queries_recorded, 2);
    }

    #[tokio::test]
    async fn test_clear_metrics() {
        let manager = ABMetrics::new();

        manager.record_query("exp1", "control", 10.0, true).await;
        manager.record_query("exp2", "control", 10.0, true).await;

        manager.clear_experiment("exp1").await;
        assert!(manager.get_experiment_metrics("exp1").await.is_none());
        assert!(manager.get_experiment_metrics("exp2").await.is_some());

        manager.clear_all().await;
        assert!(manager.get_experiment_metrics("exp2").await.is_none());
    }
}