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