Merge pull request #1349 from LemmyNet/site_counts_local

Report only local counts in site_view.
This commit is contained in:
Dessalines 2021-01-14 16:10:26 -05:00 committed by GitHub
commit 66102fb2d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 125 additions and 72 deletions

View file

@ -1,13 +1,21 @@
-- Site aggregates -- Site aggregates
drop table site_aggregates; drop table site_aggregates;
drop trigger site_aggregates_site on site; drop trigger site_aggregates_site on site;
drop trigger site_aggregates_user on user_; drop trigger site_aggregates_user_insert on user_;
drop trigger site_aggregates_post on post; drop trigger site_aggregates_user_delete on user_;
drop trigger site_aggregates_comment on comment; drop trigger site_aggregates_post_insert on post;
drop trigger site_aggregates_community on community; drop trigger site_aggregates_post_delete on post;
drop trigger site_aggregates_comment_insert on comment;
drop trigger site_aggregates_comment_delete on comment;
drop trigger site_aggregates_community_insert on community;
drop trigger site_aggregates_community_delete on community;
drop function drop function
site_aggregates_site, site_aggregates_site,
site_aggregates_user, site_aggregates_user_insert,
site_aggregates_post, site_aggregates_user_delete,
site_aggregates_comment, site_aggregates_post_insert,
site_aggregates_community; site_aggregates_post_delete,
site_aggregates_comment_insert,
site_aggregates_comment_delete,
site_aggregates_community_insert,
site_aggregates_community_delete;

View file

@ -10,10 +10,10 @@ create table site_aggregates (
insert into site_aggregates (site_id, users, posts, comments, communities) insert into site_aggregates (site_id, users, posts, comments, communities)
select id as site_id, select id as site_id,
( select coalesce(count(*), 0) from user_) as users, ( select coalesce(count(*), 0) from user_ where local = true) as users,
( select coalesce(count(*), 0) from post) as posts, ( select coalesce(count(*), 0) from post where local = true) as posts,
( select coalesce(count(*), 0) from comment) as comments, ( select coalesce(count(*), 0) from comment where local = true) as comments,
( select coalesce(count(*), 0) from community) as communities ( select coalesce(count(*), 0) from community where local = true) as communities
from site; from site;
-- initial site add -- initial site add
@ -36,91 +36,134 @@ execute procedure site_aggregates_site();
-- Add site aggregate triggers -- Add site aggregate triggers
-- user -- user
create or replace function site_aggregates_user() create function site_aggregates_user_insert()
returns trigger language plpgsql returns trigger language plpgsql
as $$ as $$
begin begin
IF (TG_OP = 'INSERT') THEN
update site_aggregates update site_aggregates
set users = users + 1; set users = users + 1;
ELSIF (TG_OP = 'DELETE') THEN return null;
end $$;
create function site_aggregates_user_delete()
returns trigger language plpgsql
as $$
begin
-- Join to site since the creator might not be there anymore -- Join to site since the creator might not be there anymore
update site_aggregates sa update site_aggregates sa
set users = users - 1 set users = users - 1
from site s from site s
where sa.site_id = s.id; where sa.site_id = s.id;
END IF;
return null; return null;
end $$; end $$;
create trigger site_aggregates_user create trigger site_aggregates_user_insert
after insert or delete on user_ after insert on user_
for each row for each row
execute procedure site_aggregates_user(); when (NEW.local = true)
execute procedure site_aggregates_user_insert();
create trigger site_aggregates_user_delete
after delete on user_
for each row
when (OLD.local = true)
execute procedure site_aggregates_user_delete();
-- post -- post
create function site_aggregates_post() create function site_aggregates_post_insert()
returns trigger language plpgsql returns trigger language plpgsql
as $$ as $$
begin begin
IF (TG_OP = 'INSERT') THEN
update site_aggregates update site_aggregates
set posts = posts + 1; set posts = posts + 1;
ELSIF (TG_OP = 'DELETE') THEN return null;
end $$;
create function site_aggregates_post_delete()
returns trigger language plpgsql
as $$
begin
update site_aggregates sa update site_aggregates sa
set posts = posts - 1 set posts = posts - 1
from site s from site s
where sa.site_id = s.id; where sa.site_id = s.id;
END IF;
return null; return null;
end $$; end $$;
create trigger site_aggregates_post create trigger site_aggregates_post_insert
after insert or delete on post after insert on post
for each row for each row
execute procedure site_aggregates_post(); when (NEW.local = true)
execute procedure site_aggregates_post_insert();
create trigger site_aggregates_post_delete
after delete on post
for each row
when (OLD.local = true)
execute procedure site_aggregates_post_delete();
-- comment -- comment
create function site_aggregates_comment() create function site_aggregates_comment_insert()
returns trigger language plpgsql returns trigger language plpgsql
as $$ as $$
begin begin
IF (TG_OP = 'INSERT') THEN
update site_aggregates update site_aggregates
set comments = comments + 1; set comments = comments + 1;
ELSIF (TG_OP = 'DELETE') THEN return null;
end $$;
create function site_aggregates_comment_delete()
returns trigger language plpgsql
as $$
begin
update site_aggregates sa update site_aggregates sa
set comments = comments - 1 set comments = comments - 1
from site s from site s
where sa.site_id = s.id; where sa.site_id = s.id;
END IF;
return null; return null;
end $$; end $$;
create trigger site_aggregates_comment create trigger site_aggregates_comment_insert
after insert or delete on comment after insert on comment
for each row for each row
execute procedure site_aggregates_comment(); when (NEW.local = true)
execute procedure site_aggregates_comment_insert();
create trigger site_aggregates_comment_delete
after delete on comment
for each row
when (OLD.local = true)
execute procedure site_aggregates_comment_delete();
-- community -- community
create function site_aggregates_community() create function site_aggregates_community_insert()
returns trigger language plpgsql returns trigger language plpgsql
as $$ as $$
begin begin
IF (TG_OP = 'INSERT') THEN
update site_aggregates update site_aggregates
set communities = communities + 1; set communities = communities + 1;
ELSIF (TG_OP = 'DELETE') THEN return null;
end $$;
create function site_aggregates_community_delete()
returns trigger language plpgsql
as $$
begin
update site_aggregates sa update site_aggregates sa
set communities = communities - 1 set communities = communities - 1
from site s from site s
where sa.site_id = s.id; where sa.site_id = s.id;
END IF;
return null; return null;
end $$; end $$;
create trigger site_aggregates_community create trigger site_aggregates_community_insert
after insert or delete on community after insert on community
for each row for each row
execute procedure site_aggregates_community(); when (NEW.local = true)
execute procedure site_aggregates_community_insert();
create trigger site_aggregates_community_delete
after delete on community
for each row
when (OLD.local = true)
execute procedure site_aggregates_community_delete();

View file

@ -46,12 +46,13 @@ async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Err
}, },
protocols, protocols,
usage: NodeInfoUsage { usage: NodeInfoUsage {
// TODO get these again users: NodeInfoUsers {
users: NodeInfoUsers { total: 0 }, total: site_view.counts.users,
local_posts: 0,
local_comments: 0,
open_registrations: site_view.site.open_registration,
}, },
local_posts: site_view.counts.posts,
local_comments: site_view.counts.comments,
},
open_registrations: site_view.site.open_registration,
}; };
Ok(HttpResponse::Ok().json(json)) Ok(HttpResponse::Ok().json(json))
@ -69,11 +70,13 @@ struct NodeInfoWellKnownLinks {
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct NodeInfo { struct NodeInfo {
pub version: String, pub version: String,
pub software: NodeInfoSoftware, pub software: NodeInfoSoftware,
pub protocols: Vec<String>, pub protocols: Vec<String>,
pub usage: NodeInfoUsage, pub usage: NodeInfoUsage,
pub open_registrations: bool,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -88,7 +91,6 @@ struct NodeInfoUsage {
pub users: NodeInfoUsers, pub users: NodeInfoUsers,
pub local_posts: i64, pub local_posts: i64,
pub local_comments: i64, pub local_comments: i64,
pub open_registrations: bool,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]