Skip to content

Commit

Permalink
Do hot rank updates in batches
Browse files Browse the repository at this point in the history
  • Loading branch information
sunaurus committed Jun 18, 2023
1 parent d97ff65 commit 68eab28
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 117 deletions.
3 changes: 3 additions & 0 deletions crates/db_schema/src/aggregates/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct CommentAggregates {
/// The total number of children in this comment branch.
pub child_count: i32,
pub hot_rank: i32,
pub hot_rank_updated: chrono::NaiveDateTime,
}

#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -55,6 +56,7 @@ pub struct CommunityAggregates {
/// The number of users with any activity in the last year.
pub users_active_half_year: i64,
pub hot_rank: i32,
pub hot_rank_updated: chrono::NaiveDateTime,
}

#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -96,6 +98,7 @@ pub struct PostAggregates {
pub featured_local: bool,
pub hot_rank: i32,
pub hot_rank_active: i32,
pub hot_rank_updated: chrono::NaiveDateTime,
}

#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
Expand Down
1 change: 1 addition & 0 deletions crates/db_schema/src/impls/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ from (
join comment c2 on c2.path <@ c.path and c2.path != c.path
and c.path <@ '{top_parent}'
group by c.id
order by c.id
) as c
where ca.comment_id = c.id"
);
Expand Down
145 changes: 74 additions & 71 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// @generated automatically by Diesel CLI.

pub mod sql_types {
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "listing_type_enum"))]
pub struct ListingTypeEnum;
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "listing_type_enum"))]
pub struct ListingTypeEnum;

#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "registration_mode_enum"))]
pub struct RegistrationModeEnum;
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "registration_mode_enum"))]
pub struct RegistrationModeEnum;

#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "sort_type_enum"))]
pub struct SortTypeEnum;
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "sort_type_enum"))]
pub struct SortTypeEnum;
}

diesel::table! {
Expand Down Expand Up @@ -96,6 +96,7 @@ diesel::table! {
published -> Timestamp,
child_count -> Int4,
hot_rank -> Int4,
hot_rank_updated -> Timestamp
}
}

Expand Down Expand Up @@ -193,6 +194,7 @@ diesel::table! {
users_active_month -> Int8,
users_active_half_year -> Int8,
hot_rank -> Int4,
hot_rank_updated -> Timestamp
}
}

Expand Down Expand Up @@ -675,6 +677,7 @@ diesel::table! {
featured_local -> Bool,
hot_rank -> Int4,
hot_rank_active -> Int4,
hot_rank_updated -> Timestamp
}
}

Expand Down Expand Up @@ -911,66 +914,66 @@ diesel::joinable!(site_language -> site (site_id));
diesel::joinable!(tagline -> local_site (local_site_id));

diesel::allow_tables_to_appear_in_same_query!(
activity,
admin_purge_comment,
admin_purge_community,
admin_purge_person,
admin_purge_post,
comment,
comment_aggregates,
comment_like,
comment_reply,
comment_report,
comment_saved,
community,
community_aggregates,
community_block,
community_follower,
community_language,
community_moderator,
community_person_ban,
custom_emoji,
custom_emoji_keyword,
email_verification,
federation_allowlist,
federation_blocklist,
instance,
language,
local_site,
local_site_rate_limit,
local_user,
local_user_language,
mod_add,
mod_add_community,
mod_ban,
mod_ban_from_community,
mod_feature_post,
mod_hide_community,
mod_lock_post,
mod_remove_comment,
mod_remove_community,
mod_remove_post,
mod_transfer_community,
password_reset_request,
person,
person_aggregates,
person_ban,
person_block,
person_follower,
person_mention,
person_post_aggregates,
post,
post_aggregates,
post_like,
post_read,
post_report,
post_saved,
private_message,
private_message_report,
registration_application,
secret,
site,
site_aggregates,
site_language,
tagline,
activity,
admin_purge_comment,
admin_purge_community,
admin_purge_person,
admin_purge_post,
comment,
comment_aggregates,
comment_like,
comment_reply,
comment_report,
comment_saved,
community,
community_aggregates,
community_block,
community_follower,
community_language,
community_moderator,
community_person_ban,
custom_emoji,
custom_emoji_keyword,
email_verification,
federation_allowlist,
federation_blocklist,
instance,
language,
local_site,
local_site_rate_limit,
local_user,
local_user_language,
mod_add,
mod_add_community,
mod_ban,
mod_ban_from_community,
mod_feature_post,
mod_hide_community,
mod_lock_post,
mod_remove_comment,
mod_remove_community,
mod_remove_post,
mod_transfer_community,
password_reset_request,
person,
person_aggregates,
person_ban,
person_block,
person_follower,
person_mention,
person_post_aggregates,
post,
post_aggregates,
post_like,
post_read,
post_report,
post_saved,
private_message,
private_message_report,
registration_application,
secret,
site,
site_aggregates,
site_language,
tagline,
);
1 change: 1 addition & 0 deletions crates/db_views/src/comment_report_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ mod tests {
published: agg.published,
child_count: 0,
hot_rank: 1728,
hot_rank_updated: agg.hot_rank_updated,
},
my_vote: None,
resolver: None,
Expand Down
1 change: 1 addition & 0 deletions crates/db_views/src/comment_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ mod tests {
published: agg.published,
child_count: 5,
hot_rank: 1728,
hot_rank_updated: agg.hot_rank_updated,
},
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/db_views/src/post_report_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ mod tests {
featured_local: false,
hot_rank: 1728,
hot_rank_active: 1728,
hot_rank_updated: agg.hot_rank_updated,
},
resolver: None,
};
Expand Down
1 change: 1 addition & 0 deletions crates/db_views/src/post_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ mod tests {
featured_local: false,
hot_rank: 1728,
hot_rank_active: 1728,
hot_rank_updated: agg.hot_rank_updated,
},
subscribed: SubscribedType::NotSubscribed,
read: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- This file should undo anything in `up.sql`
alter table comment_aggregates
drop column hot_rank_updated;
alter table post_aggregates
drop column hot_rank_updated;
alter table community_aggregates
drop column hot_rank_updated;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Your SQL goes here

alter table comment_aggregates
add column hot_rank_updated timestamp default now();
alter table post_aggregates
add column hot_rank_updated timestamp default now();
alter table community_aggregates
add column hot_rank_updated timestamp default now();

0 comments on commit 68eab28

Please sign in to comment.