use serde_json::{Map, Value as JsonValue};
use crate::control::security::identity::AuthenticatedIdentity;
use crate::control::server::response_shape::types::ShapedRows;
use crate::control::state::SharedState;
use super::super::super::result::{DdlError, DdlResult};
pub fn show_topics(
state: &SharedState,
identity: &AuthenticatedIdentity,
) -> Result<Vec<DdlResult>, DdlError> {
let tenant_id = identity.tenant_id.as_u64();
let columns = vec![
"name".to_string(),
"retention_secs".to_string(),
"buffered_events".to_string(),
"owner".to_string(),
];
let topics = state.ep_topic_registry.list_for_tenant(tenant_id);
let mut rows = Vec::with_capacity(topics.len());
for t in &topics {
let buffer_key = format!("topic:{}", t.name);
let buffered = state
.cdc_router
.get_buffer(tenant_id, &buffer_key)
.map(|b| b.len())
.unwrap_or(0);
let mut row = Map::new();
row.insert("name".to_string(), JsonValue::String(t.name.clone()));
row.insert(
"retention_secs".to_string(),
JsonValue::String(t.retention.max_age_secs.to_string()),
);
row.insert(
"buffered_events".to_string(),
JsonValue::String(buffered.to_string()),
);
row.insert("owner".to_string(), JsonValue::String(t.owner.clone()));
rows.push(row);
}
let column_types = ShapedRows::text_types(columns.len());
Ok(vec![DdlResult::Rows(ShapedRows {
columns,
column_types,
rows,
notice: None,
})])
}