Skip to main content

velesdb_core/collection/
diagnostics.rs

1//! Collection health diagnostics for embedded SDK usage.
2//!
3//! Provides [`CollectionDiagnostics`] and [`IndexHealth`] to let developers
4//! inspect a collection's readiness without relying on the REST server.
5
6use super::types::Collection;
7use super::{GraphCollection, MetadataCollection, VectorCollection};
8
9/// Health status of a collection's search index.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum IndexHealth {
12    /// Index is populated and ready for search.
13    Healthy,
14    /// Index needs to be rebuilt (e.g., after corruption or schema change).
15    NeedsRebuild(String),
16    /// Index is empty (no data ingested yet).
17    Empty,
18}
19
20/// Diagnostic snapshot of a collection's state.
21///
22/// Returned by [`VectorCollection::diagnostics()`],
23/// [`GraphCollection::diagnostics()`],
24/// [`MetadataCollection::diagnostics()`],
25/// and [`Database::collection_diagnostics()`](crate::Database::collection_diagnostics).
26#[derive(Debug, Clone)]
27pub struct CollectionDiagnostics {
28    /// Whether the collection contains at least one vector/point.
29    pub has_vectors: bool,
30    /// Whether the collection is ready to serve search queries.
31    pub search_ready: bool,
32    /// Whether a valid dimension is configured (> 0 for vector collections).
33    pub dimension_configured: bool,
34    /// Total number of points in the collection.
35    pub point_count: usize,
36    /// Health status of the primary search index.
37    pub index_health: IndexHealth,
38}
39
40impl CollectionDiagnostics {
41    /// Builds diagnostics from a `Collection` instance.
42    pub(crate) fn from_collection(coll: &Collection) -> Self {
43        let config = coll.config();
44        let point_count = config.point_count;
45        let has_vectors = point_count > 0;
46        let dimension_configured = config.dimension > 0;
47        let search_ready = has_vectors && dimension_configured;
48        let index_health = if point_count == 0 {
49            IndexHealth::Empty
50        } else {
51            IndexHealth::Healthy
52        };
53
54        Self {
55            has_vectors,
56            search_ready,
57            dimension_configured,
58            point_count,
59            index_health,
60        }
61    }
62
63    /// Builds diagnostics for a metadata-only collection.
64    pub(crate) fn from_metadata(coll: &Collection) -> Self {
65        let point_count = coll.len();
66
67        Self {
68            has_vectors: point_count > 0,
69            search_ready: false,
70            dimension_configured: false,
71            point_count,
72            index_health: if point_count == 0 {
73                IndexHealth::Empty
74            } else {
75                IndexHealth::Healthy
76            },
77        }
78    }
79}
80
81impl VectorCollection {
82    /// Returns diagnostic information about this collection.
83    #[must_use]
84    pub fn diagnostics(&self) -> CollectionDiagnostics {
85        CollectionDiagnostics::from_collection(&self.inner)
86    }
87}
88
89impl GraphCollection {
90    /// Returns diagnostic information about this collection.
91    #[must_use]
92    pub fn diagnostics(&self) -> CollectionDiagnostics {
93        CollectionDiagnostics::from_collection(&self.inner)
94    }
95}
96
97impl MetadataCollection {
98    /// Returns diagnostic information about this collection.
99    #[must_use]
100    pub fn diagnostics(&self) -> CollectionDiagnostics {
101        CollectionDiagnostics::from_metadata(&self.inner)
102    }
103}