Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/db_schema/src/source/local_site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ pub struct LocalSite {
/// This affects post and comment images, but not avatars and banners.
pub image_allow_video_uploads: bool,
pub image_upload_disabled: bool,
pub linked_instances: Option<i32>,
pub total_posts: Option<i32>,
pub total_comments: Option<i32>,
pub total_users: Option<i32>,
pub total_communities: Option<i32>,
pub user_retention_percent: Option<i32>,
pub local_post_english_percent: Option<i32>,
pub ban_rate: Option<i32>,
pub accepted_signups_rate: Option<i32>,
pub failed_signups_rate: Option<i32>,
}

#[derive(Clone, derive_new::new)]
Expand Down
10 changes: 10 additions & 0 deletions crates/db_schema_file/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,16 @@ diesel::table! {
image_max_upload_size -> Int4,
image_allow_video_uploads -> Bool,
image_upload_disabled -> Bool,
linked_instances -> Nullable<Int4>,
total_posts -> Nullable<Int4>,
total_comments -> Nullable<Int4>,
total_users -> Nullable<Int4>,
total_communities -> Nullable<Int4>,
user_retention_percent -> Nullable<Int4>,
local_post_english_percent -> Nullable<Int4>,
ban_rate -> Nullable<Int4>,
accepted_signups_rate -> Nullable<Int4>,
failed_signups_rate -> Nullable<Int4>,
}
}

Expand Down
59 changes: 58 additions & 1 deletion crates/routes/src/utils/scheduled_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use diesel::{
QueryDsl,
QueryableByName,
SelectableHelper,
dsl::{IntervalDsl, count, exists, not, update},
dsl::{IntervalDsl, count, count_star, exists, not, update},
query_builder::AsQuery,
sql_query,
sql_types::{BigInt, Integer, Timestamptz},
Expand All @@ -34,7 +34,9 @@ use lemmy_db_schema_file::schema::{
comment,
community,
community_actions,
federation_allowlist,
federation_blocklist,
federation_queue_state,
instance,
instance_actions,
local_site,
Expand Down Expand Up @@ -125,6 +127,10 @@ pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> {
.await
.inspect_err(|e| warn!("Failed to update local user count: {e}"))
.ok();
update_stats(&mut context.pool())
.await
.inspect_err(|e| warn!("Failed to update stats: {e}"))
.ok();
overwrite_deleted_posts_and_comments(&mut context.pool())
.await
.inspect_err(|e| warn!("Failed to overwrite deleted posts/comments: {e}"))
Expand Down Expand Up @@ -518,6 +524,33 @@ async fn update_local_user_count(pool: &mut DbPool<'_>) -> LemmyResult<()> {
Ok(())
}

async fn update_stats(pool: &mut DbPool<'_>) -> LemmyResult<()> {
info!("Updating the linked instance count ...");

let conn = &mut get_conn(pool).await?;
let linked_instance_count = instance::table
// omit instance representing the local site
.left_join(site::table.left_join(local_site::table))
.filter(local_site::id.is_null())
.left_join(federation_blocklist::table)
.left_join(federation_allowlist::table)
.left_join(federation_queue_state::table)
.filter(federation_blocklist::instance_id.is_null())
.select(count_star())
.first::<i64>(conn)
.await
.map(i32::try_from)??;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a method FederatedInstanceView::count, then you can reuse FederatedInstanceView::joins instead of writing the same thing again.


update(local_site::table)
.set(local_site::linked_instances.eq(linked_instance_count))
.execute(conn)
.await?;

info!("Done.");

Ok(())
}

/// Set banned to false after ban expires
async fn update_banned_when_expired(pool: &mut DbPool<'_>) -> LemmyResult<()> {
info!("Updating banned column if it expires ...");
Expand Down Expand Up @@ -790,4 +823,28 @@ mod tests {
data.delete(pool).await?;
Ok(())
}

#[tokio::test]
#[serial]
async fn test_update_stats() -> LemmyResult<()> {
let context = LemmyContext::init_test_context().await;
let pool = &mut context.pool();
// Setup local site
let data = TestData::create(pool).await?;

// insert test data
let _instance0 = Instance::read_or_create(pool, "example0.com").await?;
let _instance1 = Instance::read_or_create(pool, "example1.com").await?;

// run the query
update_stats(pool).await?;
let local_site_after = SiteView::read_local(pool).await?.local_site;
dbg!(&local_site_after);

assert_eq!(2, local_site_after.linked_instances.unwrap());

data.delete(pool).await?;
Instance::delete_all(pool).await?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- This file should undo anything in `up.sql`
ALTER TABLE local_site
DROP COLUMN linked_instances;

ALTER TABLE local_site
DROP COLUMN total_posts;

ALTER TABLE local_site
DROP COLUMN total_comments;

ALTER TABLE local_site
DROP COLUMN total_users;

ALTER TABLE local_site
DROP COLUMN total_communities;

ALTER TABLE local_site
DROP COLUMN user_retention_percent;

ALTER TABLE local_site
DROP COLUMN local_post_english_percent;

ALTER TABLE local_site
DROP COLUMN ban_rate;

ALTER TABLE local_site
DROP COLUMN accepted_signups_rate;

ALTER TABLE local_site
DROP COLUMN failed_signups_rate;

31 changes: 31 additions & 0 deletions migrations/2026-03-08-173221-0000_additional-statistics/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Your SQL goes here
ALTER TABLE local_site
ADD COLUMN linked_instances integer;

ALTER TABLE local_site
ADD COLUMN total_posts integer;

ALTER TABLE local_site
ADD COLUMN total_comments integer;
Comment thread
malsadev marked this conversation as resolved.
Outdated

ALTER TABLE local_site
ADD COLUMN total_users integer;

ALTER TABLE local_site
ADD COLUMN total_communities integer;

ALTER TABLE local_site
ADD COLUMN user_retention_percent integer;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the percentages/rates should probably be floats.


ALTER TABLE local_site
ADD COLUMN local_post_english_percent integer;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how useful this metric would be. I spose it could be useful for servers who are multi-lingual, and wanting to try to get rid of english usage.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it shouldnt be limited to a single language, but have usage percentage for every language. Maybe with a new column language.usage_percentage.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added language_usage_percent jsonb column. It will contain something like:

{
"en" : "30.10",
"und": "10.00",
"de": "59.90"
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of slamming json into sql, we really should try to avoid that. Either add a new table, or better yet just remove this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best put it in the existing language table.


ALTER TABLE local_site
ADD COLUMN ban_rate integer;

ALTER TABLE local_site
ADD COLUMN accepted_signups_rate integer;

ALTER TABLE local_site
ADD COLUMN failed_signups_rate integer;
Comment thread
malsadev marked this conversation as resolved.
Outdated