feat: Add Brain Monitor dashboard and live API stats

This commit is contained in:
Riz Ashraf committed 2026-09-08 15:09:44 +01:00
1 parent 3239e32866
commit 18fd05bb11
10 files changed
+672 -80

No files matched your search

+36 -6
View File
@@ -1,7 +1,7 @@
use tantivy::schema::*;
use tantivy::{doc, Index, IndexWriter, IndexReader, ReloadPolicy};
use std::sync::Mutex;
use crate::models::{Entity, Task, StickyNote, Adr, Snippet};
use crate::models::{Entity, Task, Adr, Snippet};
pub struct MemoryIndex {
index: Index,
@@ -71,21 +71,51 @@ impl MemoryIndex {
Ok(())
}
pub fn search(&self, query: &str, namespace: Option<&str>) -> tantivy::Result<Vec<String>> {
pub fn search(&self, query: &str, namespace: Option<&str>) -> tantivy::Result<Vec<(String, String)>> {
let searcher = self.reader.searcher();
let query_parser = tantivy::query::QueryParser::for_index(&self.index, vec![self.title_field, self.body_field]);
let q = query_parser.parse_query(query)?;
let top_docs = searcher.search(&q, &tantivy::collector::TopDocs::with_limit(50))?;
let top_docs = searcher.search(&q, &tantivy::collector::TopDocs::with_limit(50).order_by_score())?;
let mut results = Vec::new();
for (_score, doc_address) in top_docs {
let retrieved_doc = searcher.doc::<tantivy::TantivyDocument>(doc_address)?;
if let Some(val) = retrieved_doc.get_first(self.title_field) {
if let Some(t) = val.as_str() {
results.push(t.to_string());
let id = retrieved_doc.get_first(self.id_field).and_then(|v| v.as_str()).unwrap_or("").to_string();
let doc_type = retrieved_doc.get_first(self.type_field).and_then(|v| v.as_str()).unwrap_or("").to_string();
let doc_ns = retrieved_doc.get_first(self.namespace_field).and_then(|v| v.as_str()).unwrap_or("");
if let Some(ns) = namespace {
if doc_ns != ns && doc_ns != "global" {
continue;
}
}
results.push((id, doc_type));
}
Ok(results)
}
pub fn index_snippet(&self, s: &Snippet) -> tantivy::Result<()> {
let mut writer = self.writer.lock().unwrap();
writer.add_document(doc!(
self.id_field => s.name.clone(),
self.title_field => s.name.clone(),
self.body_field => format!("{} {}", s.language, s.description),
self.type_field => "snippet",
self.namespace_field => "global"
))?;
writer.commit()?;
Ok(())
}
pub fn index_adr(&self, a: &Adr) -> tantivy::Result<()> {
let mut writer = self.writer.lock().unwrap();
writer.add_document(doc!(
self.id_field => a.id.clone(),
self.title_field => a.title.clone(),
self.body_field => format!("{} {} {}", a.context, a.decision, a.consequence),
self.type_field => "adr",
self.namespace_field => "global"
))?;
writer.commit()?;
Ok(())
}
}