strings not used internally11

This commit is contained in:
Priec
2026-09-07 10:25:14 +02:00
parent 042993fafc
commit 082b77d35a
10 changed files with 147 additions and 31 deletions

View File

@@ -2,6 +2,7 @@
//! responses into the view models the templates render.
use axum::http::HeaderMap;
use common::money::MoneyRounding;
use crate::{
AppState,
@@ -60,7 +61,7 @@ pub(crate) async fn load_catalog(
.await
.map_err(|error| LoadError::Backend(error.message().to_string()))?
.into_inner();
Ok(catalog_view(&catalog))
catalog_view(&catalog)
}
pub(crate) async fn run_query(
@@ -141,15 +142,15 @@ fn value_output(value: crate::analytics::AnalyticsValue) -> serde_json::Value {
}
}
fn catalog_view(catalog: &GetAnalyticsCatalogResponse) -> CatalogView {
CatalogView {
tables: catalog.tables.iter().map(catalog_table_view).collect(),
llm_context: build_llm_context(catalog),
}
fn catalog_view(catalog: &GetAnalyticsCatalogResponse) -> Result<CatalogView, LoadError> {
Ok(CatalogView {
tables: catalog.tables.iter().map(catalog_table_view).collect::<Result<_, _>>()?,
llm_context: build_llm_context(catalog)?,
})
}
fn catalog_table_view(table: &AnalyticsTable) -> CatalogTableView {
CatalogTableView {
fn catalog_table_view(table: &AnalyticsTable) -> Result<CatalogTableView, LoadError> {
Ok(CatalogTableView {
name: table.name.clone(),
starter_query: format!("SELECT *\nFROM {}\nLIMIT 100;", quote_identifier(&table.name)),
columns: table
@@ -160,19 +161,21 @@ fn catalog_table_view(table: &AnalyticsTable) -> CatalogTableView {
if column.is_system {
details.push_str(", system");
}
if !column.rounding.is_empty() {
details.push_str(&format!(", rounding {}", column.rounding));
let rounding = MoneyRounding::try_from(column.rounding)
.map_err(|error| LoadError::Backend(error.to_string()))?;
if !column.is_system {
details.push_str(&format!(", rounding {}", rounding.as_storage_str()));
}
if !column.currency.is_empty() {
details.push_str(&format!(", currency {}", column.currency));
}
CatalogColumnView {
Ok::<_, LoadError>(CatalogColumnView {
name: column.name.clone(),
insert_text: quote_identifier(&column.name),
details,
}
})
})
.collect(),
.collect::<Result<_, _>>()?,
links: table
.links
.iter()
@@ -182,10 +185,10 @@ fn catalog_table_view(table: &AnalyticsTable) -> CatalogTableView {
required: link.required,
})
.collect(),
}
})
}
fn build_llm_context(catalog: &GetAnalyticsCatalogResponse) -> String {
fn build_llm_context(catalog: &GetAnalyticsCatalogResponse) -> Result<String, LoadError> {
let mut text = format!(
"Write one read-only PostgreSQL SELECT query for the komp_ac analytics API.\n\
Profile: {}\n\n\
@@ -212,8 +215,10 @@ AVAILABLE ANALYTICS SCHEMA\n",
if column.is_system {
text.push_str(" [system]");
}
if !column.rounding.is_empty() {
text.push_str(&format!(" [rounding: {}]", column.rounding));
let rounding = MoneyRounding::try_from(column.rounding)
.map_err(|error| LoadError::Backend(error.to_string()))?;
if !column.is_system {
text.push_str(&format!(" [rounding: {}]", rounding.as_storage_str()));
}
if !column.currency.is_empty() {
text.push_str(&format!(" [currency: {}]", column.currency));
@@ -232,7 +237,7 @@ AVAILABLE ANALYTICS SCHEMA\n",
}
}
}
text
Ok(text)
}
fn quote_identifier(identifier: &str) -> String {