use schemars::JsonSchema;
use serde::Serialize;
use serde_json::Value;
use crate::tenants::{CompactionStatus, SnapshotInfo};
pub fn body<T: Serialize>(value: &T) -> Value {
serde_json::to_value(value).expect("response types serialize")
}
#[derive(Serialize, JsonSchema)]
pub struct Added {
pub added: usize,
}
#[derive(Serialize, JsonSchema)]
pub struct Deleted {
pub deleted: bool,
}
#[derive(Serialize, JsonSchema)]
pub struct Match {
pub id: String,
pub text: String,
pub metadata: Value,
pub score: f64,
}
#[derive(Serialize, JsonSchema)]
pub struct Matches {
pub matches: Vec<Match>,
}
#[derive(Serialize, JsonSchema)]
pub struct SearchHit {
pub id: String,
pub score: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<Value>,
}
#[derive(Serialize, JsonSchema)]
pub struct SearchHits {
pub hits: Vec<SearchHit>,
}
#[derive(Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct Maintained {
pub document_index_trained: bool,
pub vector_index: VectorIndexBody,
}
#[derive(Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct VectorIndexBody {
pub rows: usize,
pub graph: bool,
pub current: bool,
pub stale_mutations: u64,
pub rebuild_recommended: bool,
}
impl From<writerslogic_warm::core::documents::VectorIndexStatus> for VectorIndexBody {
fn from(status: writerslogic_warm::core::documents::VectorIndexStatus) -> Self {
Self {
rows: status.rows,
graph: status.graph,
current: status.current,
stale_mutations: status.stale_mutations,
rebuild_recommended: status.rebuild_recommended,
}
}
}
#[derive(Serialize, JsonSchema)]
pub struct LineError {
pub line: u64,
pub message: String,
}
#[derive(Serialize, JsonSchema)]
pub struct StreamResult {
pub added: u64,
pub rejected: u64,
pub errors: Vec<LineError>,
pub errors_truncated: bool,
}
#[derive(Serialize, JsonSchema)]
pub struct ListedDocument {
pub id: String,
pub metadata: Value,
}
#[derive(Serialize, JsonSchema)]
pub struct DocumentPage {
pub documents: Vec<ListedDocument>,
pub next_cursor: Option<String>,
pub count: usize,
}
#[derive(Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct Stats {
pub tenant: String,
pub documents: usize,
pub vectors: usize,
pub document_index_trained: bool,
pub vector_index: VectorIndexBody,
pub used_bytes: f64,
pub capacity_bytes: f64,
}
#[derive(Serialize, JsonSchema)]
pub struct Snapshot {
pub id: String,
pub bytes: u64,
pub created_at: String,
}
impl From<&SnapshotInfo> for Snapshot {
fn from(s: &SnapshotInfo) -> Self {
Self {
id: s.id.clone(),
bytes: s.bytes,
created_at: s.created_at.clone(),
}
}
}
#[derive(Serialize, JsonSchema)]
pub struct Snapshots {
pub snapshots: Vec<Snapshot>,
}
#[derive(Serialize, JsonSchema)]
pub struct Restored {
pub restored: bool,
pub previous_snapshot: String,
}
#[derive(Serialize, JsonSchema)]
pub struct CompactionView {
pub running: bool,
pub runs: u64,
pub failures: u64,
pub last_trigger: Option<String>,
pub last_started_at: Option<String>,
pub last_duration_ms: Option<u64>,
pub last_error: Option<String>,
}
impl From<&CompactionStatus> for CompactionView {
fn from(c: &CompactionStatus) -> Self {
Self {
running: c.running,
runs: c.runs,
failures: c.failures,
last_trigger: c.last_trigger.map(str::to_string),
last_started_at: c.last_started_at.clone(),
last_duration_ms: c.last_duration_ms,
last_error: c.last_error.map(str::to_string),
}
}
}
#[derive(Serialize, JsonSchema)]
pub struct CompactionStatusBody {
pub scheduled_interval_seconds: u64,
pub status: CompactionView,
}
#[derive(Serialize, JsonSchema)]
pub struct CompactionRun {
pub status: CompactionView,
}
#[derive(Serialize, JsonSchema)]
pub struct KeysReloaded {
pub keys: usize,
}
#[derive(Serialize, JsonSchema)]
pub struct TlsReloaded {
pub reloaded: bool,
}
#[derive(Serialize, JsonSchema)]
pub struct Tenants {
pub tenants: Vec<String>,
pub open: usize,
}
#[derive(Serialize, JsonSchema)]
pub struct Health {
pub status: String,
}
#[derive(Serialize, JsonSchema)]
pub struct Problem {
#[serde(rename = "type")]
pub kind: String,
pub title: String,
pub status: u16,
pub detail: String,
pub code: String,
pub instance: Option<String>,
pub request_id: Option<String>,
pub error: LegacyError,
}
#[derive(Serialize, JsonSchema)]
pub struct LegacyError {
pub code: String,
pub message: String,
}