From ccdb8c1255bd6cd6f3161c88cf735b07bf132727 Mon Sep 17 00:00:00 2001 From: Matt Grogan Date: Thu, 12 Sep 2024 16:45:24 +0100 Subject: [PATCH] Post release (#6380) --- backend/canisters/user_index/CHANGELOG.md | 2 + backend/canisters/user_index/api/can.did | 1 - .../api/src/queries/chit_leaderboard.rs | 1 - backend/canisters/user_index/impl/src/lib.rs | 6 +- .../impl/src/lifecycle/post_upgrade.rs | 60 +------------------ .../impl/src/model/chit_leaderboard.rs | 20 ------- .../src/services/userIndex/candid/idl.js | 1 - .../src/services/userIndex/candid/types.d.ts | 3 +- .../src/services/userIndex/mappers.ts | 39 ++++++------ 9 files changed, 23 insertions(+), 110 deletions(-) diff --git a/backend/canisters/user_index/CHANGELOG.md b/backend/canisters/user_index/CHANGELOG.md index 9d776befe9..fa189cdbb5 100644 --- a/backend/canisters/user_index/CHANGELOG.md +++ b/backend/canisters/user_index/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [unreleased] +## [[2.0.1346](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1346-user_index)] - 2024-09-12 + ### Changed - Extend `chit_leaderboard` to return all_time|this_month|last_month ([#6364](https://github.com/open-chat-labs/open-chat/pull/6364)) diff --git a/backend/canisters/user_index/api/can.did b/backend/canisters/user_index/api/can.did index 63d77a9886..8a0ffd32c8 100644 --- a/backend/canisters/user_index/api/can.did +++ b/backend/canisters/user_index/api/can.did @@ -357,7 +357,6 @@ type ReferralStats = record { }; type ChitLeaderboardResponse = variant { - Success : vec ChitUserBalance; SuccessV2 : record { all_time : vec ChitUserBalance; this_month : vec ChitUserBalance; diff --git a/backend/canisters/user_index/api/src/queries/chit_leaderboard.rs b/backend/canisters/user_index/api/src/queries/chit_leaderboard.rs index 13000bc4d8..c9743ffbce 100644 --- a/backend/canisters/user_index/api/src/queries/chit_leaderboard.rs +++ b/backend/canisters/user_index/api/src/queries/chit_leaderboard.rs @@ -7,7 +7,6 @@ pub type Args = Empty; #[ts_export(user_index, chit_leaderboard)] #[derive(CandidType, Debug)] pub enum Response { - Success(Vec), SuccessV2(SuccessResult), } diff --git a/backend/canisters/user_index/impl/src/lib.rs b/backend/canisters/user_index/impl/src/lib.rs index cfd954c9a3..d89a9f1cac 100644 --- a/backend/canisters/user_index/impl/src/lib.rs +++ b/backend/canisters/user_index/impl/src/lib.rs @@ -353,8 +353,6 @@ struct Data { pub video_call_operators: Vec, pub oc_key_pair: P256KeyPair, pub empty_users: HashSet, - // TODO: Remove this attribute after release - #[serde(default, skip_deserializing)] pub chit_leaderboard: ChitLeaderboard, pub deleted_users: Vec, #[serde(with = "serde_bytes")] @@ -433,7 +431,7 @@ impl Data { video_call_operators, oc_key_pair: P256KeyPair::default(), empty_users: HashSet::new(), - chit_leaderboard: ChitLeaderboard::default(), + chit_leaderboard: ChitLeaderboard::new(now), deleted_users: Vec::new(), ic_root_key, identity_canister_user_sync_queue: VecDeque::new(), @@ -560,7 +558,7 @@ impl Default for Data { video_call_operators: Vec::default(), oc_key_pair: P256KeyPair::default(), empty_users: HashSet::new(), - chit_leaderboard: ChitLeaderboard::default(), + chit_leaderboard: ChitLeaderboard::new(0), deleted_users: Vec::new(), ic_root_key: Vec::new(), identity_canister_user_sync_queue: VecDeque::new(), diff --git a/backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs b/backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs index f6015cc316..45f200730c 100644 --- a/backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs +++ b/backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs @@ -1,16 +1,13 @@ use crate::lifecycle::{init_env, init_state}; use crate::memory::get_upgrades_memory; -use crate::model::chit_leaderboard::{ChitUserBalance, MAX_LEADERS}; -use crate::{mutate_state, Data, RuntimeState}; +use crate::Data; use canister_logger::LogEntry; use canister_tracing_macros::trace; use ic_cdk::post_upgrade; use stable_memory::get_reader; -use std::collections::BinaryHeap; use tracing::info; use user_index_canister::post_upgrade::Args; use utils::cycles::init_cycles_dispenser_client; -use utils::time::MonthKey; #[post_upgrade] #[trace] @@ -26,60 +23,5 @@ fn post_upgrade(args: Args) { init_cycles_dispenser_client(data.cycles_dispenser_canister_id, data.test_mode); init_state(env, data, args.wasm_version); - // TODO: Remove after release - mutate_state(initialize_leaderboards); - info!(version = %args.wasm_version, "Post-upgrade complete"); } - -fn initialize_leaderboards(state: &mut RuntimeState) { - let now = state.env.now(); - let this_month_key = MonthKey::from_timestamp(now); - let last_month_key = this_month_key.previous(); - - let mut all_time = BinaryHeap::new(); - let mut this_month = BinaryHeap::new(); - let mut last_month = BinaryHeap::new(); - - for user in state.data.users.iter() { - let total = user.total_chit_earned(); - - if total > 20_000 { - all_time.push(ChitUserBalance { - balance: total as u32, - user_id: user.user_id, - }); - - this_month.push(ChitUserBalance { - balance: user.current_chit_balance(now) as u32, - user_id: user.user_id, - }); - - last_month.push(ChitUserBalance { - balance: user.chit_per_month.get(&last_month_key).copied().unwrap_or_default() as u32, - user_id: user.user_id, - }); - } - } - - state.data.chit_leaderboard.initialize( - pop_n(&mut all_time, MAX_LEADERS), - pop_n(&mut this_month, MAX_LEADERS), - pop_n(&mut last_month, MAX_LEADERS), - now, - ); -} - -fn pop_n(heap: &mut BinaryHeap, n: usize) -> Vec { - let mut result = Vec::new(); - - for _i in 0..n { - if let Some(v) = heap.pop() { - result.push(v); - } else { - break; - } - } - - result -} diff --git a/backend/canisters/user_index/impl/src/model/chit_leaderboard.rs b/backend/canisters/user_index/impl/src/model/chit_leaderboard.rs index 6f18f3be1f..ef0651c599 100644 --- a/backend/canisters/user_index/impl/src/model/chit_leaderboard.rs +++ b/backend/canisters/user_index/impl/src/model/chit_leaderboard.rs @@ -11,13 +11,6 @@ pub struct ChitLeaderboard { this_month_key: MonthKey, } -// TODO: Delete this after release -impl Default for ChitLeaderboard { - fn default() -> ChitLeaderboard { - ChitLeaderboard::new(0) - } -} - #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] pub struct ChitUserBalance { pub balance: u32, @@ -45,19 +38,6 @@ impl ChitLeaderboard { } } - pub fn initialize( - &mut self, - all_time: Vec, - this_month: Vec, - last_month: Vec, - now: TimestampMillis, - ) { - self.all_time = all_time; - self.this_month = this_month; - self.last_month = last_month; - self.this_month_key = MonthKey::from_timestamp(now); - } - pub fn update_position( &mut self, user_id: UserId, diff --git a/frontend/openchat-agent/src/services/userIndex/candid/idl.js b/frontend/openchat-agent/src/services/userIndex/candid/idl.js index 45f8507656..7d938cf6ab 100644 --- a/frontend/openchat-agent/src/services/userIndex/candid/idl.js +++ b/frontend/openchat-agent/src/services/userIndex/candid/idl.js @@ -53,7 +53,6 @@ export const idlFactory = ({ IDL }) => { 'user_id' : UserId, }); const ChitLeaderboardResponse = IDL.Variant({ - 'Success' : IDL.Vec(ChitUserBalance), 'SuccessV2' : IDL.Record({ 'all_time' : IDL.Vec(ChitUserBalance), 'last_month' : IDL.Vec(ChitUserBalance), diff --git a/frontend/openchat-agent/src/services/userIndex/candid/types.d.ts b/frontend/openchat-agent/src/services/userIndex/candid/types.d.ts index af6511a1d1..eb15a80661 100644 --- a/frontend/openchat-agent/src/services/userIndex/candid/types.d.ts +++ b/frontend/openchat-agent/src/services/userIndex/candid/types.d.ts @@ -327,8 +327,7 @@ export type ChitEarnedReason = { 'DailyClaim' : null } | { 'ExternalAchievement' : string } | { 'MemeContestWinner' : null } | { 'Referral' : ReferralStatus }; -export type ChitLeaderboardResponse = { 'Success' : Array } | - { +export type ChitLeaderboardResponse = { 'SuccessV2' : { 'all_time' : Array, 'last_month' : Array, diff --git a/frontend/openchat-agent/src/services/userIndex/mappers.ts b/frontend/openchat-agent/src/services/userIndex/mappers.ts index f25169c859..a54ae2c6fb 100644 --- a/frontend/openchat-agent/src/services/userIndex/mappers.ts +++ b/frontend/openchat-agent/src/services/userIndex/mappers.ts @@ -71,7 +71,7 @@ export function usersApiResponse(candid: ApiUsersResponse): UsersApiResponse { users: candid.Success.users.map(userSummaryUpdate), deletedUserIds: new Set(candid.Success.deleted.map((d) => d.toString())), currentUser: optional(candid.Success.current_user, (u) => - currentUserSummary(u, timestamp) + currentUserSummary(u, timestamp), ), }; } @@ -80,7 +80,7 @@ export function usersApiResponse(candid: ApiUsersResponse): UsersApiResponse { export function currentUserSummary( candid: ApiCurrentUserSummary, - timestamp: bigint + timestamp: bigint, ): CurrentUserSummary { return { kind: "current_user_summary", @@ -161,7 +161,7 @@ export function diamondStatus(candid: ApiDiamondMembershipStatus): DiamondMember } export function userRegistrationCanisterResponse( - candid: ApiUserRegistrationCanisterResponse + candid: ApiUserRegistrationCanisterResponse, ): string { if ("Success" in candid) { return candid.Success.toString(); @@ -215,7 +215,7 @@ function diamondMembershipStatus(candid: ApiDiamondMembershipStatusFull): Diamon } throw new UnsupportedValueError( "Unexpected ApiDiamondMembershipStatusFull type received", - candid + candid, ); } @@ -228,7 +228,7 @@ function diamondMembership(candid: ApiDiamondMembershipDetails): DiamondMembersh } function diamondMembershipSubscription( - candid: ApiDiamondMembershipSubscription + candid: ApiDiamondMembershipSubscription, ): DiamondMembershipSubscription { if ("OneMonth" in candid) { return "one_month"; @@ -244,7 +244,7 @@ function diamondMembershipSubscription( } throw new UnsupportedValueError( "Unexpected ApiDiamondMembershipSubscription type received", - candid + candid, ); } @@ -369,7 +369,7 @@ export function unsuspendUserResponse(candid: ApiUnsuspendUserResponse): Unsuspe export function payForDiamondMembershipResponse( duration: DiamondMembershipDuration, - candid: ApiPayForDiamondMembershipResponse + candid: ApiPayForDiamondMembershipResponse, ): PayForDiamondMembershipResponse { if ("PaymentAlreadyInProgress" in candid) { return { kind: "payment_already_in_progress" }; @@ -410,12 +410,12 @@ export function payForDiamondMembershipResponse( } throw new UnsupportedValueError( "Unexpected ApiPayForDiamondMembershipResponse type received", - candid + candid, ); } export function apiDiamondDuration( - domain: DiamondMembershipDuration + domain: DiamondMembershipDuration, ): ApiDiamondMembershipPlanDuration { if (domain === "one_month") { return { OneMonth: null }; @@ -433,7 +433,7 @@ export function apiDiamondDuration( } export function diamondMembershipFeesResponse( - candid: ApiDiamondMembershipFeesResponse + candid: ApiDiamondMembershipFeesResponse, ): DiamondMembershipFees[] { if ("Success" in candid) { return candid.Success.map((f) => ({ @@ -446,12 +446,12 @@ export function diamondMembershipFeesResponse( } throw new UnsupportedValueError( "Unexpected ApiDiamondMembershipFeesResponse type received", - candid + candid, ); } export function chitLeaderboardResponse( - candid: ApiChitLeaderboardResponse + candid: ApiChitLeaderboardResponse, ): ChitLeaderboardResponse { if ("SuccessV2" in candid) { return { @@ -459,13 +459,8 @@ export function chitLeaderboardResponse( lastMonth: candid.SuccessV2.last_month.map(chitUserBalance), thisMonth: candid.SuccessV2.this_month.map(chitUserBalance), }; - } else if ("Success" in candid) { - return { - allTime: candid.Success.map(chitUserBalance), - lastMonth: [], - thisMonth: [], - }; } + throw new UnsupportedValueError("Unexpected ApiChitLeaderboardResponse type received", candid); } @@ -478,7 +473,7 @@ function chitUserBalance(candid: ApiChitUserBalance): ChitUserBalance { } export function submitProofOfUniquePersonhoodResponse( - candid: ApiSubmitProofOfUniquePersonhoodResponse + candid: ApiSubmitProofOfUniquePersonhoodResponse, ): SubmitProofOfUniquePersonhoodResponse { if ("Success" in candid) { return CommonResponses.success(); @@ -491,12 +486,12 @@ export function submitProofOfUniquePersonhoodResponse( } throw new UnsupportedValueError( "Unexpected ApiSubmitProofOfUniquePersonhoodResponse type received", - candid + candid, ); } export function externalAchievementsResponse( - candid: ApiExternalAchievementsResponse + candid: ApiExternalAchievementsResponse, ): ExternalAchievementsResponse { if ("Success" in candid) { return { @@ -511,7 +506,7 @@ export function externalAchievementsResponse( } throw new UnsupportedValueError( "Unexpected ApiExternalAchievementsResponse type received", - candid + candid, ); }