From 8cf14f407fba600b2f09c8fb37aa08719a4b1295 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Fri, 4 Jul 2025 07:31:13 +0000 Subject: [PATCH] Move local site user count updates to scheduled task (#5822) * Move local site stats updates to scheduled task (fixes #5816, fixes #3213) * only user count via scheduled task * join to local site --- .../replaceable_schema/triggers.sql | 16 ---------- crates/routes/Cargo.toml | 8 +++-- crates/routes/src/utils/scheduled_tasks.rs | 30 +++++++++++++++++-- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/crates/db_schema_file/replaceable_schema/triggers.sql b/crates/db_schema_file/replaceable_schema/triggers.sql index 72102ca86..6b1efae4c 100644 --- a/crates/db_schema_file/replaceable_schema/triggers.sql +++ b/crates/db_schema_file/replaceable_schema/triggers.sql @@ -254,22 +254,6 @@ WHERE RETURN NULL; END; $$); -CALL r.create_triggers ('local_user', $$ -BEGIN - UPDATE - local_site AS a - SET - users = a.users + diff.users - FROM ( - SELECT - coalesce(sum(count_diff), 0) AS users - FROM select_old_and_new_rows AS old_and_new_rows - WHERE (local_user).accepted_application) AS diff -WHERE - diff.users != 0; -RETURN NULL; -END; -$$); -- Count subscribers for communities. -- subscribers should be updated only when a local community is followed by a local or remote person. -- subscribers_local should be updated only when a local person follows a local or remote community. diff --git a/crates/routes/Cargo.toml b/crates/routes/Cargo.toml index 5e995e693..e5e652bf1 100644 --- a/crates/routes/Cargo.toml +++ b/crates/routes/Cargo.toml @@ -16,6 +16,10 @@ doctest = false [lints] workspace = true +# dummy to make `./scripts/test.sh lemmy_routes` work +[features] +full = [] + [dependencies] lemmy_db_views_community = { workspace = true, features = ["full"] } lemmy_db_views_post = { workspace = true, features = ["full"] } @@ -28,8 +32,8 @@ lemmy_db_views_person_content_combined = { workspace = true, features = [ ] } lemmy_db_views_site = { workspace = true, features = ["full"] } lemmy_utils = { workspace = true, features = ["full"] } -lemmy_db_schema = { workspace = true } -lemmy_api_utils = { workspace = true } +lemmy_db_schema = { workspace = true, features = ["full"] } +lemmy_api_utils = { workspace = true, features = ["full"] } lemmy_db_schema_file = { workspace = true } activitypub_federation = { workspace = true } actix-web = { workspace = true, features = ["cookies"] } diff --git a/crates/routes/src/utils/scheduled_tasks.rs b/crates/routes/src/utils/scheduled_tasks.rs index 1c0ae0e81..8739b48cf 100644 --- a/crates/routes/src/utils/scheduled_tasks.rs +++ b/crates/routes/src/utils/scheduled_tasks.rs @@ -3,7 +3,7 @@ use activitypub_federation::config::Data; use chrono::{DateTime, TimeZone, Utc}; use clokwerk::{AsyncScheduler, TimeUnits as CTimeUnits}; use diesel::{ - dsl::{exists, not, IntervalDsl}, + dsl::{count, exists, not, update, IntervalDsl}, query_builder::AsQuery, sql_query, sql_types::{Integer, Timestamptz}, @@ -37,10 +37,13 @@ use lemmy_db_schema_file::schema::{ federation_blocklist, instance, instance_actions, + local_site, + local_user, person, post, received_activity, sent_activity, + site, }; use lemmy_db_views_site::SiteView; use lemmy_utils::error::{LemmyErrorType, LemmyResult}; @@ -330,7 +333,7 @@ async fn overwrite_deleted_posts_and_comments(pool: &mut DbPool<'_>) -> LemmyRes Ok(()) } -/// Re-calculate the site and community active counts every 12 hours +/// Re-calculate the site, community active counts and local user count async fn active_counts(pool: &mut DbPool<'_>) -> LemmyResult<()> { info!("Updating active site and community aggregates ..."); @@ -359,6 +362,29 @@ async fn active_counts(pool: &mut DbPool<'_>) -> LemmyResult<()> { .execute(&mut conn) .await?; + let mut conn = get_conn(pool).await?; + + let user_count: i64 = local_user::table + .inner_join( + person::table.left_join( + instance_actions::table + .inner_join(instance::table.inner_join(site::table.inner_join(local_site::table))), + ), + ) + // only count approved users + .filter(local_user::accepted_application) + // ignore banned and deleted accounts + .filter(instance_actions::received_ban_at.is_null()) + .filter(not(person::deleted)) + .select(count(local_user::id)) + .get_result(&mut conn) + .await?; + + update(local_site::table) + .set((local_site::users.eq(user_count),)) + .execute(&mut conn) + .await?; + info!("Done."); Ok(()) }