mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-19 16:15:04 +00:00
Use single connector with list TTS endpoint
This commit is contained in:
1 parent
db5af2c1f6
commit
1da020abd4
3 files changed
+63
-34
No files matched your search
+18
-4
@@ -1,7 +1,13 @@
|
||||
// NB: Incrementally getting rid of build warnings...
|
||||
#![forbid(unused_imports)]
|
||||
#![forbid(unused_mut)]
|
||||
#![forbid(unused_variables)]
|
||||
|
||||
use anyhow::anyhow;
|
||||
use container_common::anyhow_result::AnyhowResult;
|
||||
use log::warn;
|
||||
use sqlx::MySqlPool;
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::{MySql, MySqlPool};
|
||||
use std::collections::{HashSet, HashMap};
|
||||
|
||||
/// Map of model_token => vec<category tokens>
|
||||
@@ -14,7 +20,15 @@ pub struct TtsModelCategoryMap {
|
||||
pub async fn fetch_and_build_tts_model_category_map(
|
||||
mysql_pool: &MySqlPool
|
||||
) -> AnyhowResult<TtsModelCategoryMap> {
|
||||
let assignments = list_tts_model_category_assignments(mysql_pool).await?;
|
||||
let mut connection = mysql_pool.acquire().await?;
|
||||
fetch_and_build_tts_model_category_map_with_connection(&mut connection).await
|
||||
}
|
||||
|
||||
/// Fetch a map of every model to all of its categories.
|
||||
pub async fn fetch_and_build_tts_model_category_map_with_connection(
|
||||
mysql_connection: &mut PoolConnection<MySql>
|
||||
) -> AnyhowResult<TtsModelCategoryMap> {
|
||||
let assignments = list_tts_model_category_assignments(mysql_connection).await?;
|
||||
|
||||
let mut map : HashMap<String, HashSet<String>> = HashMap::new();
|
||||
|
||||
@@ -39,7 +53,7 @@ pub struct CategoryAssignment {
|
||||
pub category_token: String,
|
||||
}
|
||||
|
||||
async fn list_tts_model_category_assignments(mysql_pool: &MySqlPool) -> AnyhowResult<Vec<CategoryAssignment>> {
|
||||
async fn list_tts_model_category_assignments(mysql_connection: &mut PoolConnection<MySql>) -> AnyhowResult<Vec<CategoryAssignment>> {
|
||||
let maybe_results = sqlx::query_as!(
|
||||
CategoryAssignment,
|
||||
r#"
|
||||
@@ -59,7 +73,7 @@ WHERE
|
||||
|
||||
"#,
|
||||
)
|
||||
.fetch_all(mysql_pool)
|
||||
.fetch_all(mysql_connection)
|
||||
.await;
|
||||
|
||||
match maybe_results {
|
||||
|
||||
@@ -3,7 +3,8 @@ use chrono::{DateTime, Utc};
|
||||
use container_common::anyhow_result::AnyhowResult;
|
||||
use crate::helpers::boolean_converters::i8_to_bool;
|
||||
use log::{warn, info};
|
||||
use sqlx::MySqlPool;
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::{MySql, MySqlPool};
|
||||
|
||||
// FIXME: This is the old style of query scoping and shouldn't be copied.
|
||||
|
||||
@@ -41,14 +42,23 @@ pub async fn list_tts_models(
|
||||
scope_creator_username: Option<&str>,
|
||||
require_mod_approved: bool
|
||||
) -> AnyhowResult<Vec<TtsModelRecordForList>> {
|
||||
let mut connection = mysql_pool.acquire().await?;
|
||||
list_tts_models_with_connection(&mut connection, scope_creator_username, require_mod_approved).await
|
||||
}
|
||||
|
||||
pub async fn list_tts_models_with_connection(
|
||||
mysql_connection: &mut PoolConnection<MySql>,
|
||||
scope_creator_username: Option<&str>,
|
||||
require_mod_approved: bool
|
||||
) -> AnyhowResult<Vec<TtsModelRecordForList>> {
|
||||
|
||||
let maybe_models = match scope_creator_username {
|
||||
Some(username) => {
|
||||
list_tts_models_creator_scoped(mysql_pool, username, require_mod_approved)
|
||||
list_tts_models_creator_scoped(mysql_connection, username, require_mod_approved)
|
||||
.await
|
||||
},
|
||||
None => {
|
||||
list_tts_models_for_all_creators(mysql_pool, require_mod_approved)
|
||||
list_tts_models_for_all_creators(mysql_connection, require_mod_approved)
|
||||
.await
|
||||
},
|
||||
};
|
||||
@@ -92,7 +102,7 @@ pub async fn list_tts_models(
|
||||
}
|
||||
|
||||
async fn list_tts_models_for_all_creators(
|
||||
mysql_pool: &MySqlPool,
|
||||
mysql_connection: &mut PoolConnection<MySql>,
|
||||
allow_mod_disabled: bool
|
||||
) -> AnyhowResult<Vec<InternalRawTtsModelRecordForList>> {
|
||||
// TODO: There has to be a better way.
|
||||
@@ -126,7 +136,7 @@ WHERE
|
||||
AND tts.user_deleted_at IS NULL
|
||||
AND tts.mod_deleted_at IS NULL
|
||||
"#)
|
||||
.fetch_all(mysql_pool)
|
||||
.fetch_all(mysql_connection)
|
||||
.await?
|
||||
} else {
|
||||
info!("listing tts models for everyone; all");
|
||||
@@ -156,7 +166,7 @@ WHERE
|
||||
tts.user_deleted_at IS NULL
|
||||
AND tts.mod_deleted_at IS NULL
|
||||
"#)
|
||||
.fetch_all(mysql_pool)
|
||||
.fetch_all(mysql_connection)
|
||||
.await?
|
||||
};
|
||||
|
||||
@@ -164,7 +174,7 @@ WHERE
|
||||
}
|
||||
|
||||
async fn list_tts_models_creator_scoped(
|
||||
mysql_pool: &MySqlPool,
|
||||
mysql_connection: &mut PoolConnection<MySql>,
|
||||
scope_creator_username: &str,
|
||||
allow_mod_disabled: bool
|
||||
) -> AnyhowResult<Vec<InternalRawTtsModelRecordForList>> {
|
||||
@@ -202,7 +212,7 @@ WHERE
|
||||
AND tts.mod_deleted_at IS NULL
|
||||
"#,
|
||||
scope_creator_username)
|
||||
.fetch_all(mysql_pool)
|
||||
.fetch_all(mysql_connection)
|
||||
.await?
|
||||
} else {
|
||||
info!("listing tts models for user; all");
|
||||
@@ -235,7 +245,7 @@ WHERE
|
||||
AND tts.mod_deleted_at IS NULL
|
||||
"#,
|
||||
scope_creator_username)
|
||||
.fetch_all(mysql_pool)
|
||||
.fetch_all(mysql_connection)
|
||||
.await?
|
||||
};
|
||||
|
||||
@@ -266,4 +276,3 @@ struct InternalRawTtsModelRecordForList {
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
@@ -1,23 +1,21 @@
|
||||
use actix_http::http::header;
|
||||
use actix_web::HttpResponseBuilder;
|
||||
use actix_web::cookie::Cookie;
|
||||
// NB: Incrementally getting rid of build warnings...
|
||||
#![forbid(unused_imports)]
|
||||
#![forbid(unused_mut)]
|
||||
#![forbid(unused_variables)]
|
||||
|
||||
use actix_web::error::ResponseError;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::{Responder, web, HttpResponse, error, HttpRequest, HttpMessage};
|
||||
use actix_web::{web, HttpResponse, HttpRequest};
|
||||
use chrono::{DateTime, Utc};
|
||||
use container_common::anyhow_result::AnyhowResult;
|
||||
use crate::http_server::web_utils::ip_address::get_request_ip;
|
||||
use crate::http_server::web_utils::response_error_helpers::to_simple_json_error;
|
||||
use crate::server_state::ServerState;
|
||||
use database_queries::queries::tts::tts_category_assignments::fetch_and_build_tts_model_category_map::fetch_and_build_tts_model_category_map;
|
||||
use database_queries::queries::tts::tts_models::list_tts_models::list_tts_models;
|
||||
use database_queries::queries::tts::tts_category_assignments::fetch_and_build_tts_model_category_map::fetch_and_build_tts_model_category_map_with_connection;
|
||||
use database_queries::queries::tts::tts_models::list_tts_models::list_tts_models_with_connection;
|
||||
use lexical_sort::natural_lexical_cmp;
|
||||
use log::{info, warn, log, error};
|
||||
use regex::Regex;
|
||||
use sqlx::MySqlPool;
|
||||
use sqlx::error::DatabaseError;
|
||||
use sqlx::error::Error::Database;
|
||||
use sqlx::mysql::MySqlDatabaseError;
|
||||
use log::{info, warn, error};
|
||||
use sqlx::MySql;
|
||||
use sqlx::pool::PoolConnection;
|
||||
use std::collections::HashSet;
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
@@ -80,7 +78,7 @@ impl fmt::Display for ListTtsModelsError {
|
||||
}
|
||||
|
||||
pub async fn list_tts_models_handler(
|
||||
http_request: HttpRequest,
|
||||
_http_request: HttpRequest,
|
||||
server_state: web::Data<Arc<ServerState>>) -> Result<HttpResponse, ListTtsModelsError>
|
||||
{
|
||||
let maybe_models = server_state.caches.voice_list.copy_without_bump_if_unexpired()
|
||||
@@ -96,7 +94,15 @@ pub async fn list_tts_models_handler(
|
||||
},
|
||||
None => {
|
||||
info!("Populating TTS models from database");
|
||||
let models = get_all_models(&server_state.mysql_pool)
|
||||
|
||||
let mut mysql_connection = server_state.mysql_pool.acquire()
|
||||
.await
|
||||
.map_err(|e| {
|
||||
warn!("Could not acquire DB pool: {:?}", e);
|
||||
ListTtsModelsError::ServerError
|
||||
})?;
|
||||
|
||||
let models = get_all_models(&mut mysql_connection)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
error!("Error querying database: {:?}", e);
|
||||
@@ -118,22 +124,22 @@ pub async fn list_tts_models_handler(
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
.map_err(|e| ListTtsModelsError::ServerError)?;
|
||||
.map_err(|_e| ListTtsModelsError::ServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body))
|
||||
}
|
||||
|
||||
async fn get_all_models(mysql_pool: &MySqlPool) -> AnyhowResult<Vec<TtsModelRecordForResponse>> {
|
||||
let mut models = list_tts_models(
|
||||
mysql_pool,
|
||||
async fn get_all_models(mysql_connection: &mut PoolConnection<MySql>) -> AnyhowResult<Vec<TtsModelRecordForResponse>> {
|
||||
let mut models = list_tts_models_with_connection(
|
||||
mysql_connection,
|
||||
None,
|
||||
false
|
||||
).await?;
|
||||
|
||||
let model_categories_map
|
||||
= fetch_and_build_tts_model_category_map(mysql_pool).await?;
|
||||
= fetch_and_build_tts_model_category_map_with_connection(mysql_connection).await?;
|
||||
|
||||
// Make the list nice for human readers.
|
||||
models.sort_by(|a, b|
|
||||
|
||||
Reference in new issue
Block a user