Skip to main content

moltendb_core/handlers/
process_analytics.rs

1#![allow(dead_code)]
2use serde_json::{Value, json};
3use crate::validation;
4use crate::{engine, analytics};
5
6/// Handle an analytics query request.
7///
8/// Parses the payload as an AnalyticsQuery and executes it against the database.
9/// Returns the result value and execution metadata (time, rows scanned).
10///
11/// Note: this function exists but is not currently wired to an HTTP route.
12/// It is available for future use or direct calls from other handlers.
13pub fn process_analytics(db: &engine::Db, payload: &Value, max_body_size: usize) -> Value {
14    // Validate the request structure first.
15    if let Err(e) = validation::validate_request(payload, max_body_size) {
16        return json!({ "error": e.to_string() });
17    }
18
19    // Deserialize the payload into a strongly-typed AnalyticsQuery struct.
20    let query: analytics::AnalyticsQuery = match serde_json::from_value(payload.clone()) {
21        Ok(q)  => q,
22        Err(e) => return json!({ "error": format!("Invalid analytics query: {}", e) }),
23    };
24
25    // Execute the analytics query (COUNT, SUM, AVG, etc.) against the database.
26    let result = match analytics::execute_query(db, &query) {
27        Ok(res) => res,
28        Err(e)  => return json!({ "error": format!("Analytics execution failed: {}", e) }),
29    };
30
31    // Return the result along with execution metadata for performance monitoring.
32    json!({
33        "result": result.result,
34        "metadata": {
35            "execution_time_ms": result.metadata.execution_time_ms,
36            "rows_scanned": result.metadata.rows_scanned
37        }
38    })
39}