mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-09-02 19:23:49 +00:00
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
This commit is contained in:
parent
3922f0f325
commit
8cf14f407f
3 changed files with 34 additions and 20 deletions
|
@ -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.
|
||||
|
|
|
@ -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"] }
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue