feat: update admin config import and runtime handling

This commit is contained in:
fawney19
2026-05-25 15:23:02 +08:00
parent 505d9fd8bc
commit ae138ddb56
16 changed files with 1095 additions and 162 deletions
@@ -577,7 +577,7 @@ impl GlobalModelWriteRepository for InMemoryGlobalModelReadRepository {
record.config.clone(),
0,
0,
0,
record.usage_count.unwrap_or(0),
Some(1_711_000_000),
Some(1_711_000_000),
)?;
@@ -606,6 +606,9 @@ impl GlobalModelWriteRepository for InMemoryGlobalModelReadRepository {
existing.default_tiered_pricing = record.default_tiered_pricing.clone();
existing.supported_capabilities = record.supported_capabilities.clone();
existing.config = record.config.clone();
if let Some(usage_count) = record.usage_count {
existing.usage_count = usage_count;
}
existing.updated_at_unix_secs = Some(1_711_000_100);
}
self.get_admin_global_model_by_id(&record.id).await
@@ -336,6 +336,8 @@ WHERE provider_id = ?
record: &CreateAdminGlobalModelRecord,
) -> Result<Option<StoredAdminGlobalModel>, DataLayerError> {
let now = current_unix_secs();
let usage_count =
optional_admin_global_model_usage_count_i64(record.usage_count)?.unwrap_or_default();
sqlx::query(
r#"
INSERT INTO global_models (
@@ -351,7 +353,7 @@ INSERT INTO global_models (
created_at,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"#,
)
.bind(&record.id)
@@ -367,6 +369,7 @@ VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)
&record.supported_capabilities,
"global_models.supported_capabilities",
)?)
.bind(usage_count)
.bind(optional_json_to_string(
&record.config,
"global_models.config",
@@ -385,6 +388,7 @@ VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)
record: &UpdateAdminGlobalModelRecord,
) -> Result<Option<StoredAdminGlobalModel>, DataLayerError> {
let now = current_unix_secs();
let usage_count = optional_admin_global_model_usage_count_i64(record.usage_count)?;
let updated = sqlx::query(
r#"
UPDATE global_models
@@ -395,6 +399,7 @@ SET
default_tiered_pricing = ?,
supported_capabilities = ?,
config = ?,
usage_count = COALESCE(?, usage_count),
updated_at = ?
WHERE id = ?
"#,
@@ -414,6 +419,7 @@ WHERE id = ?
&record.config,
"global_models.config",
)?)
.bind(usage_count)
.bind(now as i64)
.bind(&record.id)
.execute(&self.pool)
@@ -880,6 +886,20 @@ fn map_active_global_model_row(
)
}
fn optional_admin_global_model_usage_count_i64(
value: Option<u64>,
) -> Result<Option<i64>, DataLayerError> {
value
.map(|value| {
i64::try_from(value).map_err(|_| {
DataLayerError::InvalidInput(
"global_models.usage_count exceeds i64 range".to_string(),
)
})
})
.transpose()
}
#[cfg(test)]
mod tests {
use super::MysqlGlobalModelReadRepository;
@@ -664,6 +664,8 @@ RETURNING id
&self,
record: &CreateAdminGlobalModelRecord,
) -> Result<Option<StoredAdminGlobalModel>, DataLayerError> {
let usage_count =
optional_admin_global_model_usage_count_i64(record.usage_count)?.unwrap_or_default();
let inserted = sqlx::query(
r#"
INSERT INTO global_models (
@@ -690,7 +692,7 @@ RETURNING id
.bind(record.default_price_per_request)
.bind(record.default_tiered_pricing.clone())
.bind(record.supported_capabilities.clone())
.bind(0_i32)
.bind(usage_count)
.bind(record.config.clone())
.fetch_optional(&self.pool)
.await
@@ -707,6 +709,7 @@ RETURNING id
&self,
record: &UpdateAdminGlobalModelRecord,
) -> Result<Option<StoredAdminGlobalModel>, DataLayerError> {
let usage_count = optional_admin_global_model_usage_count_i64(record.usage_count)?;
let updated = sqlx::query(
r#"
UPDATE global_models
@@ -717,6 +720,7 @@ SET
default_tiered_pricing = $5,
supported_capabilities = $6,
config = $7,
usage_count = COALESCE($8, usage_count),
updated_at = NOW()
WHERE id = $1
RETURNING id
@@ -729,6 +733,7 @@ RETURNING id
.bind(record.default_tiered_pricing.clone())
.bind(record.supported_capabilities.clone())
.bind(record.config.clone())
.bind(usage_count)
.fetch_optional(&self.pool)
.await
.map_postgres_err()?;
@@ -1203,6 +1208,20 @@ fn map_provider_active_global_model_row(
)
}
fn optional_admin_global_model_usage_count_i64(
value: Option<u64>,
) -> Result<Option<i64>, DataLayerError> {
value
.map(|value| {
i64::try_from(value).map_err(|_| {
DataLayerError::InvalidInput(
"global_models.usage_count exceeds i64 range".to_string(),
)
})
})
.transpose()
}
#[cfg(test)]
mod tests {
use super::{
@@ -321,6 +321,8 @@ WHERE provider_id = ?
record: &CreateAdminGlobalModelRecord,
) -> Result<Option<StoredAdminGlobalModel>, DataLayerError> {
let now = current_unix_secs();
let usage_count =
optional_admin_global_model_usage_count_i64(record.usage_count)?.unwrap_or_default();
sqlx::query(
r#"
INSERT INTO global_models (
@@ -336,7 +338,7 @@ INSERT INTO global_models (
created_at,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"#,
)
.bind(&record.id)
@@ -352,6 +354,7 @@ VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)
&record.supported_capabilities,
"global_models.supported_capabilities",
)?)
.bind(usage_count)
.bind(optional_json_to_string(
&record.config,
"global_models.config",
@@ -370,6 +373,7 @@ VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)
record: &UpdateAdminGlobalModelRecord,
) -> Result<Option<StoredAdminGlobalModel>, DataLayerError> {
let now = current_unix_secs();
let usage_count = optional_admin_global_model_usage_count_i64(record.usage_count)?;
let updated = sqlx::query(
r#"
UPDATE global_models
@@ -380,6 +384,7 @@ SET
default_tiered_pricing = ?,
supported_capabilities = ?,
config = ?,
usage_count = COALESCE(?, usage_count),
updated_at = ?
WHERE id = ?
"#,
@@ -399,6 +404,7 @@ WHERE id = ?
&record.config,
"global_models.config",
)?)
.bind(usage_count)
.bind(now as i64)
.bind(&record.id)
.execute(&self.pool)
@@ -1124,6 +1130,20 @@ fn map_active_global_model_row(
)
}
fn optional_admin_global_model_usage_count_i64(
value: Option<u64>,
) -> Result<Option<i64>, DataLayerError> {
value
.map(|value| {
i64::try_from(value).map_err(|_| {
DataLayerError::InvalidInput(
"global_models.usage_count exceeds i64 range".to_string(),
)
})
})
.transpose()
}
#[cfg(test)]
mod tests {
use super::SqliteGlobalModelReadRepository;