axonml-server 0.6.2

REST API server for AxonML Machine Learning Framework
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
//! Database Module — Aegis-DB Client and Multi-Model Data Access
//!
//! Top-level database module for the AxonML server, providing a unified async
//! client for the Aegis-DB backend. Exposes three data-access paradigms through
//! a single `Database` struct:
//!
//! - **SQL queries** via `query()` / `query_with_params()` / `execute()` with
//!   `QueryRequest` / `QueryResponse` request/response types.
//! - **Key-value store** via `kv_set()` / `kv_get()` / `kv_delete()`.
//! - **Document store** via `doc_insert()` / `doc_get()` / `doc_update()` /
//!   `doc_delete()` / `doc_query()` / `doc_find_one()`, with `DocumentQuery`
//!   for filtered/sorted/paginated lookups.
//! - **Time series** via `ts_write_one()` / `ts_query()`, using `DataPoint`,
//!   `TimeSeriesQuery`, and `TimeSeriesAggregation`.
//!
//! Security: `validate_db_host()` restricts connections to loopback and
//! RFC-1918 private addresses, preventing SSRF. Authentication uses a
//! bearer-token flow stored in an `Arc<RwLock<Option<String>>>`.
//!
//! Re-exports sub-modules: `datasets`, `models`, `notebooks`, `runs`,
//! `schema`, and `users`.
//!
//! # File
//! `crates/axonml-server/src/db/mod.rs`
//!
//! # Author
//! Andrew Jewell Sr. — AutomataNexus LLC
//! ORCID: 0009-0005-2158-7060
//!
//! # Updated
//! April 16, 2026 11:15 PM EST
//!
//! # Disclaimer
//! Use at own risk. This software is provided "as is", without warranty of any
//! kind, express or implied. The author and AutomataNexus shall not be held
//! liable for any damages arising from the use of this software.

// =============================================================================
// Sub-Module Declarations
// =============================================================================

pub mod datasets;
pub mod models;
pub mod notebooks;
pub mod runs;
pub mod schema;
pub mod users;

// =============================================================================
// Imports
// =============================================================================

use crate::config::AegisConfig;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::RwLock;
use url::Url;

// =============================================================================
// Error Types
// =============================================================================

#[derive(Error, Debug)]
pub enum DbError {
    #[error("Connection failed: {0}")]
    ConnectionFailed(String),
    #[error("Query failed: {0}")]
    QueryFailed(String),
    #[error("Not found: {0}")]
    NotFound(String),
    #[error("Already exists: {0}")]
    AlreadyExists(String),
    #[error("Invalid data: {0}")]
    InvalidData(String),
    #[error("HTTP error: {0}")]
    HttpError(#[from] reqwest::Error),
    #[error("JSON error: {0}")]
    JsonError(#[from] serde_json::Error),
}

// =============================================================================
// Core Types — Database Connection
// =============================================================================

/// Database connection wrapper for Aegis-DB
#[derive(Clone)]
pub struct Database {
    client: Client,
    base_url: String,
    auth: Option<(String, String)>,
    token: Arc<RwLock<Option<String>>>,
}

// =============================================================================
// Types — SQL Query Request / Response
// =============================================================================

/// Query request body
#[derive(Debug, Serialize)]
struct QueryRequest {
    query: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    params: Option<Vec<Value>>,
}

/// Query response from Aegis-DB
#[derive(Debug, Deserialize)]
pub struct QueryResponse {
    pub rows: Vec<Value>,
    #[serde(default)]
    pub affected_rows: u64,
}

// =============================================================================
// Types — Key-Value Store
// =============================================================================

/// KV get response
#[derive(Debug, Deserialize)]
struct KvGetResponse {
    value: Option<Value>,
}

// =============================================================================
// Types — Document Store
// =============================================================================

/// Document insert response
#[derive(Debug, Deserialize)]
struct DocumentInsertResponse {
    #[serde(default)]
    success: bool,
    #[serde(default)]
    id: Option<String>,
}

/// Document get response
#[derive(Debug, Deserialize)]
struct DocumentGetResponse {
    #[serde(default)]
    data: Option<Value>,
}

/// Document query response
#[derive(Debug, Deserialize)]
struct DocumentQueryResponse {
    #[serde(default)]
    documents: Vec<Value>,
}

/// Document query request
#[derive(Debug, Serialize, Default)]
pub struct DocumentQuery {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub skip: Option<u32>,
}

// =============================================================================
// Types — Time Series
// =============================================================================

/// Time series data point
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DataPoint {
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub value: f64,
    #[serde(default)]
    pub tags: std::collections::HashMap<String, String>,
}

/// Time series aggregation options
#[derive(Debug, Serialize, Default)]
pub struct TimeSeriesAggregation {
    #[serde(rename = "type")]
    pub agg_type: String,
    pub interval: String,
    pub function: String,
}

/// Time series query request
#[derive(Debug, Serialize, Default)]
pub struct TimeSeriesQuery {
    pub metric: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start: Option<chrono::DateTime<chrono::Utc>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end: Option<chrono::DateTime<chrono::Utc>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<std::collections::HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aggregation: Option<TimeSeriesAggregation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
}

/// Time series query response
#[derive(Debug, Deserialize)]
struct TimeSeriesResponse {
    #[serde(default)]
    points: Vec<DataPoint>,
}

// =============================================================================
// Security — Host Validation
// =============================================================================

/// SECURITY: Validate that a database host is a loopback or private network address.
/// Prevents SSRF by ensuring we only connect to internal database services.
fn validate_db_host(host: &str, port: u16) -> Result<(), DbError> {
    let is_allowed = host == "localhost"
        || host == "127.0.0.1"
        || host == "::1"
        || host == "[::1]"
        || host.starts_with("10.")
        || host.starts_with("172.")
        || host.starts_with("192.168.");

    if !is_allowed {
        return Err(DbError::ConnectionFailed(format!(
            "Only loopback/private network database hosts allowed, got '{}'",
            host
        )));
    }

    if port == 0 {
        return Err(DbError::ConnectionFailed(
            "Invalid database port".to_string(),
        ));
    }

    Ok(())
}

// =============================================================================
// Implementation — Database
// =============================================================================

impl Database {
    // -------------------------------------------------------------------------
    // Connection and Authentication
    // -------------------------------------------------------------------------

    /// Create a new database connection.
    /// SECURITY: Only allows connections to loopback/private network addresses.
    pub async fn new(config: &AegisConfig) -> Result<Self, DbError> {
        // SECURITY: Validate the database host to prevent SSRF
        validate_db_host(&config.host, config.port)?;

        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .map_err(|e| DbError::ConnectionFailed(e.to_string()))?;

        let base_url = format!("http://{}:{}", config.host, config.port);
        let auth = Some((config.username.clone(), config.password.clone()));

        let db = Self {
            client,
            base_url,
            auth,
            token: Arc::new(RwLock::new(None)),
        };

        // Test connection and authenticate
        db.authenticate().await?;

        Ok(db)
    }

    /// Authenticate with Aegis-DB
    async fn authenticate(&self) -> Result<(), DbError> {
        if let Some((username, password)) = &self.auth {
            let resp = self
                .client
                .post(self.build_url("/api/v1/auth/login")?)
                .json(&serde_json::json!({
                    "username": username,
                    "password": password
                }))
                .send()
                .await?;

            if resp.status().is_success() {
                let body: Value = resp.json().await?;
                if let Some(token) = body.get("token").and_then(|t| t.as_str()) {
                    let mut lock = self.token.write().await;
                    *lock = Some(token.to_string());
                }
            }
        }
        Ok(())
    }

    /// Get authorization header
    async fn auth_header(&self) -> Option<String> {
        let lock = self.token.read().await;
        lock.as_ref().map(|t| format!("Bearer {}", t))
    }

    /// SECURITY: Build a URL from the pre-validated base_url and a relative path.
    /// base_url is validated in new() to only allow private/loopback addresses.
    fn build_url(&self, path: &str) -> Result<Url, DbError> {
        let base = Url::parse(&self.base_url)
            .map_err(|e| DbError::ConnectionFailed(format!("Invalid base URL: {}", e)))?;
        base.join(path)
            .map_err(|e| DbError::ConnectionFailed(format!("Invalid URL path '{}': {}", path, e)))
    }

    // -------------------------------------------------------------------------
    // SQL Query Operations
    // -------------------------------------------------------------------------

    /// Execute a query and return results
    pub async fn query(&self, sql: &str) -> Result<QueryResponse, DbError> {
        self.query_with_params(sql, vec![]).await
    }

    /// Execute a query with parameters
    pub async fn query_with_params(
        &self,
        sql: &str,
        params: Vec<Value>,
    ) -> Result<QueryResponse, DbError> {
        let mut request = self
            .client
            .post(self.build_url("/api/v1/query")?)
            .json(&QueryRequest {
                query: sql.to_string(),
                params: if params.is_empty() {
                    None
                } else {
                    Some(params)
                },
            });

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        if !resp.status().is_success() {
            let error = resp
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DbError::QueryFailed(error));
        }

        let result: QueryResponse = resp.json().await?;
        Ok(result)
    }

    /// Execute a statement (INSERT, UPDATE, DELETE)
    #[allow(dead_code)]
    pub async fn execute(&self, sql: &str) -> Result<u64, DbError> {
        self.execute_with_params(sql, vec![]).await
    }

    /// Execute a statement with parameters
    #[allow(dead_code)]
    pub async fn execute_with_params(&self, sql: &str, params: Vec<Value>) -> Result<u64, DbError> {
        let result = self.query_with_params(sql, params).await?;
        Ok(result.affected_rows)
    }

    // -------------------------------------------------------------------------
    // Key-Value Store Operations
    // -------------------------------------------------------------------------

    /// Set a key-value pair
    pub async fn kv_set(&self, key: &str, value: Value) -> Result<(), DbError> {
        let mut request =
            self.client
                .post(self.build_url("/api/v1/kv/keys")?)
                .json(&serde_json::json!({
                    "key": key,
                    "value": value
                }));

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        if !resp.status().is_success() {
            let error = resp
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DbError::QueryFailed(error));
        }

        Ok(())
    }

    /// Get a key-value pair
    pub async fn kv_get(&self, key: &str) -> Result<Option<Value>, DbError> {
        let mut request = self
            .client
            .get(self.build_url(&format!("/api/v1/kv/keys/{}", key))?);

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        if resp.status().as_u16() == 404 {
            return Ok(None);
        }

        if !resp.status().is_success() {
            let error = resp
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DbError::QueryFailed(error));
        }

        let result: KvGetResponse = resp.json().await?;
        Ok(result.value)
    }

    /// Delete a key-value pair
    pub async fn kv_delete(&self, key: &str) -> Result<(), DbError> {
        let mut request = self
            .client
            .delete(self.build_url(&format!("/api/v1/kv/keys/{}", key))?);

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        if !resp.status().is_success() && resp.status().as_u16() != 404 {
            let error = resp
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DbError::QueryFailed(error));
        }

        Ok(())
    }

    // -------------------------------------------------------------------------
    // Document Store Operations
    // -------------------------------------------------------------------------

    /// Create a document collection
    pub async fn create_collection(&self, name: &str) -> Result<(), DbError> {
        let mut request = self
            .client
            .post(self.build_url("/api/v1/documents/collections")?)
            .json(&serde_json::json!({ "name": name }));

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        // 409 Conflict or "already exists" is fine
        if resp.status().as_u16() == 409 {
            return Ok(());
        }

        let body = resp.text().await.unwrap_or_default();
        if body.contains("already exists") {
            return Ok(());
        }

        Ok(())
    }

    /// Insert a document into a collection
    pub async fn doc_insert(
        &self,
        collection: &str,
        id: Option<&str>,
        data: Value,
    ) -> Result<String, DbError> {
        let mut request = self
            .client
            .post(self.build_url(&format!(
                "/api/v1/documents/collections/{}/documents",
                collection
            ))?)
            .json(&serde_json::json!({
                "id": id,
                "document": data
            }));

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        if !resp.status().is_success() {
            let error = resp
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DbError::QueryFailed(error));
        }

        let result: DocumentInsertResponse = resp.json().await?;

        // Check success flag if returned
        if !result.success && result.id.is_none() {
            return Err(DbError::InvalidData("Document insert failed".to_string()));
        }

        result
            .id
            .ok_or_else(|| DbError::InvalidData("No document ID returned".to_string()))
    }

    /// Get a document by ID
    pub async fn doc_get(&self, collection: &str, id: &str) -> Result<Option<Value>, DbError> {
        let mut request = self.client.get(self.build_url(&format!(
            "/api/v1/documents/collections/{}/documents/{}",
            collection, id
        ))?);

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        if resp.status().as_u16() == 404 {
            return Ok(None);
        }

        if !resp.status().is_success() {
            let error = resp
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DbError::QueryFailed(error));
        }

        let result: DocumentGetResponse = resp.json().await?;
        Ok(result.data)
    }

    /// Update a document by ID
    pub async fn doc_update(&self, collection: &str, id: &str, data: Value) -> Result<(), DbError> {
        let mut request = self
            .client
            .put(self.build_url(&format!(
                "/api/v1/documents/collections/{}/documents/{}",
                collection, id
            ))?)
            .json(&serde_json::json!({ "document": data }));

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        if resp.status().as_u16() == 404 {
            return Err(DbError::NotFound(format!("Document {} not found", id)));
        }

        if !resp.status().is_success() {
            let error = resp
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DbError::QueryFailed(error));
        }

        Ok(())
    }

    /// Delete a document by ID
    pub async fn doc_delete(&self, collection: &str, id: &str) -> Result<(), DbError> {
        let mut request = self.client.delete(self.build_url(&format!(
            "/api/v1/documents/collections/{}/documents/{}",
            collection, id
        ))?);

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        if !resp.status().is_success() && resp.status().as_u16() != 404 {
            let error = resp
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DbError::QueryFailed(error));
        }

        Ok(())
    }

    /// Query documents in a collection with filter
    pub async fn doc_query(
        &self,
        collection: &str,
        query: DocumentQuery,
    ) -> Result<Vec<Value>, DbError> {
        let mut request = self
            .client
            .post(self.build_url(&format!(
                "/api/v1/documents/collections/{}/query",
                collection
            ))?)
            .json(&query);

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        if !resp.status().is_success() {
            let error = resp
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DbError::QueryFailed(error));
        }

        let result: DocumentQueryResponse = resp.json().await?;

        // Extract the "data" field from each document wrapper
        let documents: Vec<Value> = result
            .documents
            .into_iter()
            .filter_map(|doc| doc.get("data").cloned())
            .collect();

        Ok(documents)
    }

    /// Find one document matching a filter
    pub async fn doc_find_one(
        &self,
        collection: &str,
        filter: Value,
    ) -> Result<Option<Value>, DbError> {
        let query = DocumentQuery {
            filter: Some(filter),
            limit: Some(1),
            ..Default::default()
        };

        let docs = self.doc_query(collection, query).await?;
        Ok(docs.into_iter().next())
    }

    // -------------------------------------------------------------------------
    // Time Series Operations
    // -------------------------------------------------------------------------

    /// Write a single time series data point
    pub async fn ts_write_one(
        &self,
        metric: &str,
        value: f64,
        tags: std::collections::HashMap<String, String>,
    ) -> Result<(), DbError> {
        let point = DataPoint {
            timestamp: chrono::Utc::now(),
            value,
            tags,
        };

        let mut request = self
            .client
            .post(self.build_url("/api/v1/timeseries/write")?)
            .json(&serde_json::json!({
                "metric": metric,
                "points": [point]
            }));

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        if !resp.status().is_success() {
            let error = resp
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DbError::QueryFailed(error));
        }

        Ok(())
    }

    /// Query time series data
    pub async fn ts_query(&self, query: TimeSeriesQuery) -> Result<Vec<DataPoint>, DbError> {
        let mut request = self
            .client
            .post(self.build_url("/api/v1/timeseries/query")?)
            .json(&query);

        if let Some(auth) = self.auth_header().await {
            request = request.header("Authorization", auth);
        }

        let resp = request.send().await?;

        if !resp.status().is_success() {
            let error = resp
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DbError::QueryFailed(error));
        }

        let result: TimeSeriesResponse = resp.json().await?;
        Ok(result.points)
    }

    // -------------------------------------------------------------------------
    // Health Check
    // -------------------------------------------------------------------------

    /// Check if database is healthy
    pub async fn health_check(&self) -> bool {
        let url = match self.build_url("/health") {
            Ok(u) => u,
            Err(_) => return false,
        };
        match self.client.get(url).send().await {
            Ok(resp) => resp.status().is_success(),
            Err(_) => false,
        }
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_query_request_serialization() {
        let req = QueryRequest {
            query: "SELECT * FROM users".to_string(),
            params: None,
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("SELECT * FROM users"));
        assert!(!json.contains("params"));
    }

    #[test]
    fn test_query_request_with_params() {
        let req = QueryRequest {
            query: "SELECT * FROM users WHERE id = $1".to_string(),
            params: Some(vec![serde_json::json!("user-123")]),
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("params"));
    }
}