use crate::client::RestClient;
use crate::error::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricResponse {
pub interval: String,
pub timestamps: Vec<i64>,
pub values: Vec<Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Shard {
pub uid: String,
pub bdb_uid: u32,
pub node_uid: String,
pub role: String,
pub status: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub slots: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub used_memory: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub backup_progress: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub import_progress: Option<f64>,
pub all_nodes: Option<Vec<u32>>,
pub assigned_slots: Option<String>,
pub client_cert_subject_validation_type: Option<String>,
pub redis_info: Option<Value>,
pub roles: Option<Vec<String>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShardStats {
pub uid: String,
pub intervals: Vec<StatsInterval>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatsInterval {
pub interval: String,
pub timestamps: Vec<i64>,
pub values: Vec<Value>,
}
pub struct ShardHandler {
client: RestClient,
}
impl ShardHandler {
pub fn new(client: RestClient) -> Self {
ShardHandler { client }
}
pub async fn list(&self) -> Result<Vec<Shard>> {
self.client.get("/v1/shards").await
}
pub async fn get(&self, uid: &str) -> Result<Shard> {
self.client.get(&format!("/v1/shards/{}", uid)).await
}
pub async fn stats(&self, uid: &str) -> Result<ShardStats> {
self.client.get(&format!("/v1/shards/{}/stats", uid)).await
}
pub async fn stats_metric(&self, uid: &str, metric: &str) -> Result<MetricResponse> {
self.client
.get(&format!("/v1/shards/{}/stats/{}", uid, metric))
.await
}
pub async fn list_by_database(&self, bdb_uid: u32) -> Result<Vec<Shard>> {
self.client
.get(&format!("/v1/bdbs/{}/shards", bdb_uid))
.await
}
pub async fn list_by_node(&self, node_uid: u32) -> Result<Vec<Shard>> {
self.client
.get(&format!("/v1/nodes/{}/shards", node_uid))
.await
}
pub async fn failover_all(&self, body: ShardActionRequest) -> Result<Action> {
self.client.post("/v1/shards/actions/failover", &body).await
}
pub async fn migrate_all(&self, body: ShardActionRequest) -> Result<Action> {
self.client.post("/v1/shards/actions/migrate", &body).await
}
pub async fn failover(&self, uid: &str, body: ShardActionRequest) -> Result<Action> {
self.client
.post(&format!("/v1/shards/{}/actions/failover", uid), &body)
.await
}
pub async fn migrate(&self, uid: &str, body: ShardActionRequest) -> Result<Action> {
self.client
.post(&format!("/v1/shards/{}/actions/migrate", uid), &body)
.await
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShardActionRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub shard_uids: Option<Vec<String>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Action {
pub action_uid: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<String>,
}