oxirs-vec 0.2.4

Vector index abstractions for semantic similarity and AI-augmented querying
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
//! Billing and usage metering for multi-tenancy

use crate::multi_tenancy::types::{MultiTenancyError, MultiTenancyResult, TenantOperation};
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

/// Billing period for charges
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BillingPeriod {
    Hourly,
    Daily,
    Monthly,
    Annual,
}

impl BillingPeriod {
    /// Get duration in seconds
    pub fn duration_secs(&self) -> i64 {
        match self {
            Self::Hourly => 3600,
            Self::Daily => 86400,
            Self::Monthly => 2592000, // 30 days
            Self::Annual => 31536000, // 365 days
        }
    }
}

/// Pricing model for billing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PricingModel {
    /// Pay per request
    PerRequest {
        /// Cost per request
        cost_per_request: f64,
    },
    /// Pay per vector stored
    PerVector {
        /// Cost per 1000 vectors per month
        cost_per_1k_vectors: f64,
    },
    /// Pay per storage GB
    PerStorage {
        /// Cost per GB per month
        cost_per_gb: f64,
    },
    /// Pay per compute unit
    PerComputeUnit {
        /// Cost per compute unit (query complexity weighted)
        cost_per_unit: f64,
    },
    /// Flat subscription
    Subscription {
        /// Monthly subscription fee
        monthly_fee: f64,
        /// Included requests
        included_requests: u64,
        /// Overage cost per request
        overage_cost: f64,
    },
    /// Custom pricing
    Custom {
        /// Base fee
        base_fee: f64,
        /// Operation costs
        operation_costs: HashMap<String, f64>,
    },
}

impl PricingModel {
    /// Calculate cost for an operation
    pub fn calculate_cost(&self, operation: TenantOperation, count: u64) -> f64 {
        match self {
            Self::PerRequest { cost_per_request } => *cost_per_request * count as f64,
            Self::PerComputeUnit { cost_per_unit } => {
                *cost_per_unit * operation.default_cost_weight() * count as f64
            }
            Self::Custom {
                operation_costs, ..
            } => {
                let op_cost = operation_costs
                    .get(operation.name())
                    .copied()
                    .unwrap_or(0.01);
                op_cost * count as f64
            }
            _ => 0.0, // Other models calculated differently
        }
    }
}

/// Usage record for billing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UsageRecord {
    /// Tenant ID
    pub tenant_id: String,
    /// Operation type
    pub operation: TenantOperation,
    /// Number of operations
    pub count: u64,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
    /// Cost (computed)
    pub cost: f64,
    /// Metadata
    pub metadata: HashMap<String, String>,
}

impl UsageRecord {
    /// Create new usage record
    pub fn new(tenant_id: impl Into<String>, operation: TenantOperation, count: u64) -> Self {
        Self {
            tenant_id: tenant_id.into(),
            operation,
            count,
            timestamp: Utc::now(),
            cost: 0.0,
            metadata: HashMap::new(),
        }
    }

    /// Calculate cost using pricing model
    pub fn calculate_cost(&mut self, pricing: &PricingModel) {
        self.cost = pricing.calculate_cost(self.operation, self.count);
    }
}

/// Billing metrics for a tenant
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BillingMetrics {
    /// Tenant ID
    pub tenant_id: String,

    /// Current billing period start
    pub period_start: DateTime<Utc>,

    /// Current billing period end
    pub period_end: DateTime<Utc>,

    /// Total cost for current period
    pub total_cost: f64,

    /// Total requests in period
    pub total_requests: u64,

    /// Average request cost
    pub avg_request_cost: f64,

    /// Cost by operation type
    pub cost_by_operation: HashMap<String, f64>,

    /// Request count by operation
    pub requests_by_operation: HashMap<String, u64>,

    /// Peak daily cost
    pub peak_daily_cost: f64,

    /// Estimated monthly cost (projected)
    pub estimated_monthly_cost: f64,
}

impl BillingMetrics {
    /// Create new billing metrics
    pub fn new(tenant_id: impl Into<String>, period: BillingPeriod) -> Self {
        let now = Utc::now();
        let period_end = now + Duration::seconds(period.duration_secs());

        Self {
            tenant_id: tenant_id.into(),
            period_start: now,
            period_end,
            total_cost: 0.0,
            total_requests: 0,
            avg_request_cost: 0.0,
            cost_by_operation: HashMap::new(),
            requests_by_operation: HashMap::new(),
            peak_daily_cost: 0.0,
            estimated_monthly_cost: 0.0,
        }
    }

    /// Record usage
    pub fn record_usage(&mut self, record: &UsageRecord) {
        self.total_cost += record.cost;
        self.total_requests += record.count;

        let op_name = record.operation.name().to_string();
        *self.cost_by_operation.entry(op_name.clone()).or_insert(0.0) += record.cost;
        *self.requests_by_operation.entry(op_name).or_insert(0) += record.count;

        // Update average
        if self.total_requests > 0 {
            self.avg_request_cost = self.total_cost / self.total_requests as f64;
        }

        // Update estimated monthly cost
        let elapsed_secs = (Utc::now() - self.period_start).num_seconds() as f64;
        if elapsed_secs > 0.0 {
            let monthly_secs = 2592000.0; // 30 days
            self.estimated_monthly_cost = self.total_cost * (monthly_secs / elapsed_secs);
        }
    }

    /// Reset for new billing period
    pub fn reset(&mut self, period: BillingPeriod) {
        self.period_start = Utc::now();
        self.period_end = self.period_start + Duration::seconds(period.duration_secs());
        self.total_cost = 0.0;
        self.total_requests = 0;
        self.avg_request_cost = 0.0;
        self.cost_by_operation.clear();
        self.requests_by_operation.clear();
    }
}

/// Billing engine for multi-tenancy
pub struct BillingEngine {
    /// Pricing models by tenant
    pricing: Arc<Mutex<HashMap<String, PricingModel>>>,

    /// Usage records
    usage_history: Arc<Mutex<Vec<UsageRecord>>>,

    /// Current billing metrics by tenant
    metrics: Arc<Mutex<HashMap<String, BillingMetrics>>>,

    /// Billing period
    period: BillingPeriod,
}

impl BillingEngine {
    /// Create new billing engine
    pub fn new(period: BillingPeriod) -> Self {
        Self {
            pricing: Arc::new(Mutex::new(HashMap::new())),
            usage_history: Arc::new(Mutex::new(Vec::new())),
            metrics: Arc::new(Mutex::new(HashMap::new())),
            period,
        }
    }

    /// Set pricing model for tenant
    pub fn set_pricing(
        &self,
        tenant_id: impl Into<String>,
        pricing: PricingModel,
    ) -> MultiTenancyResult<()> {
        let tenant_id = tenant_id.into();

        self.pricing
            .lock()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .insert(tenant_id.clone(), pricing);

        // Initialize metrics
        self.metrics
            .lock()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .entry(tenant_id.clone())
            .or_insert_with(|| BillingMetrics::new(tenant_id, self.period));

        Ok(())
    }

    /// Record usage for tenant
    pub fn record_usage(
        &self,
        tenant_id: &str,
        operation: TenantOperation,
        count: u64,
    ) -> MultiTenancyResult<f64> {
        let mut record = UsageRecord::new(tenant_id, operation, count);

        // Calculate cost
        let pricing = self
            .pricing
            .lock()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .get(tenant_id)
            .cloned()
            .ok_or_else(|| MultiTenancyError::BillingError {
                message: format!("No pricing model for tenant: {}", tenant_id),
            })?;

        record.calculate_cost(&pricing);
        let cost = record.cost;

        // Update metrics
        let mut metrics = self
            .metrics
            .lock()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?;

        metrics
            .entry(tenant_id.to_string())
            .or_insert_with(|| BillingMetrics::new(tenant_id, self.period))
            .record_usage(&record);

        // Store record
        self.usage_history
            .lock()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .push(record);

        Ok(cost)
    }

    /// Get billing metrics for tenant
    pub fn get_metrics(&self, tenant_id: &str) -> MultiTenancyResult<BillingMetrics> {
        self.metrics
            .lock()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .get(tenant_id)
            .cloned()
            .ok_or_else(|| MultiTenancyError::TenantNotFound {
                tenant_id: tenant_id.to_string(),
            })
    }

    /// Get usage history for tenant
    pub fn get_usage_history(
        &self,
        tenant_id: &str,
        start: DateTime<Utc>,
        end: DateTime<Utc>,
    ) -> MultiTenancyResult<Vec<UsageRecord>> {
        let history = self
            .usage_history
            .lock()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?;

        Ok(history
            .iter()
            .filter(|r| r.tenant_id == tenant_id && r.timestamp >= start && r.timestamp <= end)
            .cloned()
            .collect())
    }

    /// Reset billing period for tenant
    pub fn reset_period(&self, tenant_id: &str) -> MultiTenancyResult<()> {
        let mut metrics = self
            .metrics
            .lock()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?;

        metrics
            .get_mut(tenant_id)
            .ok_or_else(|| MultiTenancyError::TenantNotFound {
                tenant_id: tenant_id.to_string(),
            })?
            .reset(self.period);

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
    use super::*;

    #[test]
    fn test_billing_period() {
        assert_eq!(BillingPeriod::Hourly.duration_secs(), 3600);
        assert_eq!(BillingPeriod::Daily.duration_secs(), 86400);
        assert_eq!(BillingPeriod::Monthly.duration_secs(), 2592000);
    }

    #[test]
    fn test_pricing_models() {
        let model = PricingModel::PerRequest {
            cost_per_request: 0.01,
        };
        assert_eq!(
            model.calculate_cost(TenantOperation::VectorSearch, 100),
            1.0
        );

        let model = PricingModel::PerComputeUnit { cost_per_unit: 0.1 };
        let cost = model.calculate_cost(TenantOperation::IndexBuild, 1);
        assert!(cost > 0.0); // Should be weighted by operation complexity
    }

    #[test]
    fn test_usage_record() {
        let mut record = UsageRecord::new("tenant1", TenantOperation::VectorSearch, 100);
        assert_eq!(record.count, 100);
        assert_eq!(record.cost, 0.0);

        let pricing = PricingModel::PerRequest {
            cost_per_request: 0.01,
        };
        record.calculate_cost(&pricing);
        assert_eq!(record.cost, 1.0);
    }

    #[test]
    fn test_billing_metrics() {
        let mut metrics = BillingMetrics::new("tenant1", BillingPeriod::Daily);
        assert_eq!(metrics.total_cost, 0.0);
        assert_eq!(metrics.total_requests, 0);

        let mut record = UsageRecord::new("tenant1", TenantOperation::VectorSearch, 100);
        record.cost = 1.0;
        metrics.record_usage(&record);

        assert_eq!(metrics.total_cost, 1.0);
        assert_eq!(metrics.total_requests, 100);
        assert!((metrics.avg_request_cost - 0.01).abs() < 0.001);
    }

    #[test]
    fn test_billing_engine() -> Result<()> {
        let engine = BillingEngine::new(BillingPeriod::Daily);

        // Set pricing
        let pricing = PricingModel::PerRequest {
            cost_per_request: 0.01,
        };
        engine.set_pricing("tenant1", pricing)?;

        // Record usage
        let cost = engine.record_usage("tenant1", TenantOperation::VectorSearch, 100)?;
        assert_eq!(cost, 1.0);

        // Get metrics
        let metrics = engine.get_metrics("tenant1")?;
        assert_eq!(metrics.total_cost, 1.0);
        assert_eq!(metrics.total_requests, 100);

        // Record more usage
        engine.record_usage("tenant1", TenantOperation::VectorInsert, 50)?;

        let metrics = engine.get_metrics("tenant1")?;
        assert_eq!(metrics.total_cost, 1.5);
        assert_eq!(metrics.total_requests, 150);
        Ok(())
    }

    #[test]
    fn test_usage_history() -> Result<()> {
        let engine = BillingEngine::new(BillingPeriod::Daily);

        let pricing = PricingModel::PerRequest {
            cost_per_request: 0.01,
        };
        engine.set_pricing("tenant1", pricing)?;

        // Record some usage
        engine.record_usage("tenant1", TenantOperation::VectorSearch, 100)?;
        engine.record_usage("tenant1", TenantOperation::VectorInsert, 50)?;

        // Get history
        let start = Utc::now() - Duration::hours(1);
        let end = Utc::now() + Duration::hours(1);
        let history = engine.get_usage_history("tenant1", start, end)?;

        assert_eq!(history.len(), 2);
        assert_eq!(history[0].count, 100);
        assert_eq!(history[1].count, 50);
        Ok(())
    }

    #[test]
    fn test_subscription_pricing() {
        let pricing = PricingModel::Subscription {
            monthly_fee: 100.0,
            included_requests: 10000,
            overage_cost: 0.02,
        };

        // Subscription pricing is handled differently, just test structure
        match pricing {
            PricingModel::Subscription {
                monthly_fee,
                included_requests,
                overage_cost,
            } => {
                assert_eq!(monthly_fee, 100.0);
                assert_eq!(included_requests, 10000);
                assert_eq!(overage_cost, 0.02);
            }
            _ => panic!("Expected subscription pricing"),
        }
    }
}