feat(routing): consolidate scheduling strategy configuration

This commit is contained in:
fawney
2026-09-03 11:05:59 +08:00
parent e8d9877b79
commit 2cb4d554aa
88 changed files with 1836 additions and 3635 deletions
@@ -0,0 +1,4 @@
ALTER TABLE routing_groups ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0;
CREATE INDEX IF NOT EXISTS routing_groups_enabled_sort_idx
ON routing_groups (enabled, sort_order, name, id);
@@ -15,6 +15,7 @@ SELECT
description,
enabled,
is_system_default,
sort_order,
config_json,
version,
created_at,
@@ -61,10 +62,12 @@ impl SqliteRoutingGroupRepository {
#[async_trait]
impl RoutingGroupReadRepository for SqliteRoutingGroupRepository {
async fn list_routing_groups(&self) -> Result<Vec<StoredRoutingGroup>, DataLayerError> {
let rows = sqlx::query(&format!("{ROUTING_GROUP_SELECT} ORDER BY name ASC, id ASC"))
.fetch_all(&self.pool)
.await
.map_sql_err()?;
let rows = sqlx::query(&format!(
"{ROUTING_GROUP_SELECT} ORDER BY enabled DESC, sort_order ASC, name ASC, id ASC"
))
.fetch_all(&self.pool)
.await
.map_sql_err()?;
rows.iter().map(map_group_row).collect()
}
@@ -168,10 +171,10 @@ impl RoutingGroupWriteRepository for SqliteRoutingGroupRepository {
sqlx::query(
r#"
INSERT INTO routing_groups (
id, name, description, enabled, is_system_default, config_json,
id, name, description, enabled, is_system_default, sort_order, config_json,
version, created_at, updated_at, published_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"#,
)
.bind(&group.id)
@@ -179,6 +182,7 @@ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
.bind(&group.description)
.bind(group.enabled)
.bind(group.is_system_default)
.bind(group.sort_order)
.bind(json_to_string(
&group.config_json,
"routing_groups.config_json",
@@ -229,6 +233,7 @@ SET name = ?,
description = ?,
enabled = ?,
is_system_default = ?,
sort_order = ?,
config_json = ?,
version = ?,
updated_at = ?,
@@ -240,6 +245,7 @@ WHERE id = ?
.bind(&group.description)
.bind(group.enabled)
.bind(group.is_system_default)
.bind(group.sort_order)
.bind(json_to_string(
&group.config_json,
"routing_groups.config_json",
@@ -438,6 +444,7 @@ fn map_group_row(row: &SqliteRow) -> Result<StoredRoutingGroup, DataLayerError>
description: row.try_get("description").map_sql_err()?,
enabled: row.try_get("enabled").map_sql_err()?,
is_system_default: row.try_get("is_system_default").map_sql_err()?,
sort_order: row.try_get("sort_order").map_sql_err()?,
config_json: json_from_string(
row.try_get("config_json").map_sql_err()?,
"routing_groups.config_json",
@@ -514,6 +521,7 @@ mod tests {
description: Some("initial".to_string()),
enabled: true,
is_system_default: true,
sort_order: 0,
config_json: json!({"allowed_models": ["gpt-*"]}),
version: 1,
created_at: 10,
@@ -790,6 +798,7 @@ SET is_default = 1,
description: None,
enabled: true,
is_system_default,
sort_order: 0,
config_json: json!({}),
version: 1,
created_at: 1,