Skip to main content

kalamdb_commons/
api_models.rs

1use serde::{Deserialize, Serialize};
2
3use crate::websocket_messages::SubscriptionOptions;
4
5/// Health check response from the server.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct HealthCheckResponse {
8    /// Health status (e.g. "healthy").
9    pub status: String,
10    /// Server version.
11    #[serde(default)]
12    pub version: String,
13    /// API version (e.g. "v1").
14    pub api_version: String,
15    /// Server build date.
16    #[serde(default)]
17    pub build_date: Option<String>,
18}
19
20/// Per-node health details from the cluster health endpoint.
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct ClusterNodeHealth {
23    pub node_id: u64,
24    pub role: String,
25    pub status: String,
26    pub api_addr: String,
27    pub is_self: bool,
28    pub is_leader: bool,
29    pub replication_lag: Option<u64>,
30    pub catchup_progress_pct: Option<u8>,
31    pub hostname: Option<String>,
32    pub memory_usage_mb: Option<u64>,
33    pub cpu_usage_percent: Option<f32>,
34    pub uptime_seconds: Option<u64>,
35    pub uptime_human: Option<String>,
36}
37
38/// Cluster health response from the server.
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct ClusterHealthResponse {
41    pub status: String,
42    #[serde(default)]
43    pub version: String,
44    #[serde(default)]
45    pub build_date: String,
46    pub is_cluster_mode: bool,
47    #[serde(default)]
48    pub cluster_id: String,
49    pub node_id: u64,
50    pub is_leader: bool,
51    pub total_groups: u32,
52    pub groups_leading: u32,
53    pub current_term: u64,
54    pub last_applied: Option<u64>,
55    pub millis_since_quorum_ack: Option<u64>,
56    #[serde(default)]
57    pub nodes: Vec<ClusterNodeHealth>,
58}
59
60/// Execution status enum shared by API and SDK SQL responses.
61#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
62#[serde(rename_all = "lowercase")]
63pub enum ResponseStatus {
64    Success,
65    Error,
66}
67
68impl std::fmt::Display for ResponseStatus {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        match self {
71            ResponseStatus::Success => write!(f, "success"),
72            ResponseStatus::Error => write!(f, "error"),
73        }
74    }
75}
76
77/// Status values returned by `SUBSCRIBE TO` over the SQL HTTP API.
78#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
79#[serde(rename_all = "snake_case")]
80pub enum SqlSubscriptionStatus {
81    SubscriptionRequired,
82    Active,
83}
84
85impl SqlSubscriptionStatus {
86    pub fn as_str(self) -> &'static str {
87        match self {
88            SqlSubscriptionStatus::SubscriptionRequired => "subscription_required",
89            SqlSubscriptionStatus::Active => "active",
90        }
91    }
92}
93
94impl std::fmt::Display for SqlSubscriptionStatus {
95    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96        write!(f, "{}", self.as_str())
97    }
98}
99
100/// Nested subscription metadata returned by `SUBSCRIBE TO` over SQL HTTP.
101#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
102pub struct SqlSubscriptionDescriptor {
103    pub id: String,
104    pub sql: String,
105    #[serde(default, skip_serializing_if = "Option::is_none")]
106    pub options: Option<SubscriptionOptions>,
107}
108
109impl SqlSubscriptionDescriptor {
110    pub fn new(id: impl Into<String>, sql: impl Into<String>) -> Self {
111        Self {
112            id: id.into(),
113            sql: sql.into(),
114            options: None,
115        }
116    }
117}
118
119/// Single-row payload returned for `SUBSCRIBE TO` via `/v1/api/sql`.
120#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
121pub struct SqlSubscriptionRow {
122    pub status: SqlSubscriptionStatus,
123    pub ws_url: String,
124    pub subscription: SqlSubscriptionDescriptor,
125    pub message: String,
126}
127
128impl SqlSubscriptionRow {
129    pub fn new(
130        subscription_id: impl Into<String>,
131        ws_url: impl Into<String>,
132        sql: impl Into<String>,
133        message: impl Into<String>,
134    ) -> Self {
135        Self {
136            status: SqlSubscriptionStatus::SubscriptionRequired,
137            ws_url: ws_url.into(),
138            subscription: SqlSubscriptionDescriptor::new(subscription_id, sql),
139            message: message.into(),
140        }
141    }
142}