Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions tools/aimdb-mcp/src/tools/buffer_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tracing::debug;
struct GetBufferMetricsParams {
/// Endpoint of the AimDB instance: a `scheme://` URL or bare path (falls back to AIMDB_CONNECT env).
endpoint: Option<String>,
/// Substring matched against record names (e.g. `"Temperature"`).
/// Substring matched against record keys and names (e.g. `"Temperature"`).
record_key: String,
}

Expand All @@ -23,7 +23,7 @@ struct ResetBufferMetricsParams {
endpoint: Option<String>,
}

/// Returns live buffer metrics for records whose name contains `record_key`.
/// Returns live buffer metrics for records whose key or name contains `record_key`.
///
/// Session-agnostic: works in any phase.
pub async fn get_buffer_metrics(args: Option<Value>) -> McpResult<Value> {
Expand All @@ -38,7 +38,9 @@ pub async fn get_buffer_metrics(args: Option<Value>) -> McpResult<Value> {

let matching: Vec<_> = raw
.into_iter()
.filter(|r| r.name.contains(&params.record_key))
.filter(|r| {
r.record_key.contains(&params.record_key) || r.name.contains(&params.record_key)
})
.collect();

if matching.is_empty() {
Expand Down
47 changes: 47 additions & 0 deletions tools/aimdb-mcp/tests/buffer_metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Buffer metrics look up records by their configured keys.

use std::sync::Arc;

use aimdb_core::buffer::BufferCfg;
use aimdb_core::remote::AimxConfig;
use aimdb_core::AimDbBuilder;
use aimdb_mcp::{connection::ConnectionPool, tools};
use aimdb_tokio_adapter::{TokioAdapter, TokioRecordRegistrarExt};
use aimdb_uds_connector::UdsServer;
use serde::{Deserialize, Serialize};
use serde_json::json;

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Reading {
value: u64,
}

#[tokio::test]
async fn get_buffer_metrics_finds_the_configured_record_key() {
let dir = tempfile::tempdir().unwrap();
let sock = dir.path().join("aimdb.sock");
let endpoint = sock.to_str().unwrap();
let config = AimxConfig::uds_default().socket_path(endpoint);
let mut builder = AimDbBuilder::new()
.runtime(Arc::new(TokioAdapter))
.with_connector(UdsServer::from_config(config));
for key in ["sensor.indoor", "sensor.outdoor"] {
builder.configure::<Reading>(key, |reg| {
reg.buffer(BufferCfg::SingleLatest).with_remote_access();
});
}
let (_db, runner) = builder.build().await.expect("build db");
tokio::spawn(runner.run());
tools::init_connection_pool(ConnectionPool::new());

let result = tools::get_buffer_metrics(Some(json!({
"endpoint": endpoint,
"record_key": "sensor.indoor",
})))
.await
.expect("get buffer metrics");
assert_eq!(result["found"], true);
let records = result["records"].as_array().expect("matching records");
assert_eq!(records.len(), 1);
assert_eq!(records[0]["record_key"], "sensor.indoor");
}