Skip to content

Commit f827b19

Browse files
author
Matthijs van Otterdijk
authored
Add a statistics endpoint (#20)
1 parent a104386 commit f827b19

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/server.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ enum ResourceSpec {
9797
commit: String,
9898
threshold: f32,
9999
},
100+
GetStatistics,
100101
}
101102

102103
#[derive(Debug, Error)]
@@ -149,6 +150,7 @@ fn uri_to_spec(uri: &Uri) -> Result<ResourceSpec, SpecParseError> {
149150
static ref RE_SEARCH: Regex = Regex::new(r"^/search(/?)$").unwrap();
150151
static ref RE_SIMILAR: Regex = Regex::new(r"^/similar(/?)$").unwrap();
151152
static ref RE_DUPLICATES: Regex = Regex::new(r"^/duplicates(/?)$").unwrap();
153+
static ref RE_STATISTICS: Regex = Regex::new(r"^/statistics$").unwrap();
152154
}
153155
let path = uri.path();
154156

@@ -239,6 +241,8 @@ fn uri_to_spec(uri: &Uri) -> Result<ResourceSpec, SpecParseError> {
239241
}
240242
_ => Err(SpecParseError::NoCommitIdOrDomain),
241243
}
244+
} else if RE_STATISTICS.is_match(path) {
245+
Ok(ResourceSpec::GetStatistics)
242246
} else {
243247
Err(SpecParseError::UnknownPath)
244248
}
@@ -659,6 +663,11 @@ impl Service {
659663
let result = self.get_similar_documents(domain, commit, id, count).await;
660664
string_response_or_error(result)
661665
}
666+
Ok(ResourceSpec::GetStatistics) => {
667+
let statistics = self.vector_store.statistics();
668+
let json_string = serde_json::to_string_pretty(&statistics).map_err(|e| e.into());
669+
json_response_or_error(json_string)
670+
}
662671
Ok(_) => todo!(),
663672
Err(e) => Ok(Response::builder()
664673
.status(StatusCode::NOT_FOUND)
@@ -801,6 +810,21 @@ fn string_response_or_error(
801810
}
802811
}
803812

813+
fn json_response_or_error(
814+
result: Result<String, ResponseError>,
815+
) -> Result<Response<Body>, Infallible> {
816+
match result {
817+
Ok(task_id) => Ok(Response::builder()
818+
.header("Content-Type", "application/json")
819+
.body(task_id.into())
820+
.unwrap()),
821+
Err(e) => Ok(Response::builder()
822+
.status(400)
823+
.body(e.to_string().into())
824+
.unwrap()),
825+
}
826+
}
827+
804828
#[derive(Debug, Error)]
805829
enum AssignIndexError {
806830
#[error("io error: {0}")]

src/vectors.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::sync::atomic::{self, AtomicUsize};
1212
use std::sync::{Arc, Condvar, Mutex, RwLock, Weak};
1313

1414
use lru::LruCache;
15+
use serde::Serialize;
1516
use urlencoding::encode;
1617

1718
use crate::vecmath::{Embedding, EmbeddingBytes, EMBEDDING_BYTE_LENGTH, EMBEDDING_LENGTH};
@@ -413,7 +414,7 @@ impl PageArena {
413414
}
414415
}
415416

416-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
417+
#[derive(Serialize, Debug, Clone, Copy, PartialEq, Eq)]
417418
pub struct VectorStoreStatistics {
418419
free: usize,
419420
loading: usize,

0 commit comments

Comments
 (0)