fix: improved performance by truncating not displayed text

This commit is contained in:
Per Stark
2025-09-22 15:37:55 +02:00
parent f592eb7200
commit 903585bfef
5 changed files with 52 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ pub mod html_state;
pub mod middlewares;
pub mod router_factory;
pub mod routes;
pub mod utils;
use axum::{extract::FromRef, Router};
use axum_session::{Session, SessionStore};

View File

@@ -17,6 +17,7 @@ use crate::{
auth_middleware::RequireUser,
response_middleware::{HtmlError, TemplateResponse},
},
utils::text_content_preview::truncate_text_contents,
};
#[derive(Serialize)]
@@ -58,6 +59,8 @@ pub async fn show_content_page(
User::get_text_contents(&user.id, &state.db).await?
};
let text_contents = truncate_text_contents(text_contents);
let conversation_archive = User::get_user_conversations(&user.id, &state.db).await?;
let data = ContentPageData {
user,
@@ -117,7 +120,8 @@ pub async fn patch_text_content(
TextContent::patch(&id, &form.context, &form.category, &form.text, &state.db).await?;
if target.as_deref() == Some("latest_content_section") {
let text_contents = User::get_latest_text_contents(&user.id, &state.db).await?;
let text_contents =
truncate_text_contents(User::get_latest_text_contents(&user.id, &state.db).await?);
return Ok(TemplateResponse::new_template(
"dashboard/recent_content.html",
@@ -128,7 +132,7 @@ pub async fn patch_text_content(
));
}
let text_contents = User::get_text_contents(&user.id, &state.db).await?;
let text_contents = truncate_text_contents(User::get_text_contents(&user.id, &state.db).await?);
let categories = User::get_user_categories(&user.id, &state.db).await?;
let conversation_archive = User::get_user_conversations(&user.id, &state.db).await?;
@@ -166,7 +170,7 @@ pub async fn delete_text_content(
state.db.delete_item::<TextContent>(&id).await?;
// Get updated content, categories and return the refreshed list
let text_contents = User::get_text_contents(&user.id, &state.db).await?;
let text_contents = truncate_text_contents(User::get_text_contents(&user.id, &state.db).await?);
let categories = User::get_user_categories(&user.id, &state.db).await?;
let conversation_archive = User::get_user_conversations(&user.id, &state.db).await?;
@@ -205,7 +209,8 @@ pub async fn show_recent_content(
State(state): State<HtmlState>,
RequireUser(user): RequireUser,
) -> Result<impl IntoResponse, HtmlError> {
let text_contents = User::get_latest_text_contents(&user.id, &state.db).await?;
let text_contents =
truncate_text_contents(User::get_latest_text_contents(&user.id, &state.db).await?);
Ok(TemplateResponse::new_template(
"dashboard/recent_content.html",

View File

@@ -9,10 +9,12 @@ use serde::Serialize;
use tokio::join;
use crate::{
html_state::HtmlState,
middlewares::{
auth_middleware::RequireUser,
response_middleware::{HtmlError, TemplateResponse},
},
utils::text_content_preview::truncate_text_contents,
AuthSessionType,
};
use common::storage::store;
@@ -26,8 +28,6 @@ use common::{
},
};
use crate::html_state::HtmlState;
#[derive(Serialize)]
pub struct IndexPageData {
user: Option<User>,
@@ -52,6 +52,8 @@ pub async fn index_handler(
User::get_unfinished_ingestion_tasks(&user.id, &state.db)
)?;
let text_contents = truncate_text_contents(text_contents);
Ok(TemplateResponse::new_template(
"dashboard/base.html",
IndexPageData {
@@ -94,7 +96,8 @@ pub async fn delete_text_content(
);
// Render updated content
let latest_text_contents = User::get_latest_text_contents(&user.id, &state.db).await?;
let latest_text_contents =
truncate_text_contents(User::get_latest_text_contents(&user.id, &state.db).await?);
Ok(TemplateResponse::new_partial(
"index/signed_in/recent_content.html",

View File

@@ -0,0 +1 @@
pub mod text_content_preview;

View File

@@ -0,0 +1,35 @@
use common::storage::types::text_content::TextContent;
const TEXT_PREVIEW_LENGTH: usize = 50;
fn maybe_truncate(value: &str) -> Option<String> {
let mut char_count = 0;
for (idx, _) in value.char_indices() {
if char_count == TEXT_PREVIEW_LENGTH {
return Some(value[..idx].to_string());
}
char_count += 1;
}
None
}
pub fn truncate_text_content(mut content: TextContent) -> TextContent {
if let Some(truncated) = maybe_truncate(&content.text) {
content.text = truncated;
}
if let Some(context) = content.context.as_mut() {
if let Some(truncated) = maybe_truncate(context) {
*context = truncated;
}
}
content
}
pub fn truncate_text_contents(contents: Vec<TextContent>) -> Vec<TextContent> {
contents.into_iter().map(truncate_text_content).collect()
}