Skip to content

Commit

Permalink
Post release (#6380)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Sep 12, 2024
1 parent dcec074 commit ccdb8c1
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 110 deletions.
2 changes: 2 additions & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 0 additions & 1 deletion backend/canisters/user_index/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ type ReferralStats = record {
};

type ChitLeaderboardResponse = variant {
Success : vec ChitUserBalance;
SuccessV2 : record {
all_time : vec ChitUserBalance;
this_month : vec ChitUserBalance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub type Args = Empty;
#[ts_export(user_index, chit_leaderboard)]
#[derive(CandidType, Debug)]
pub enum Response {
Success(Vec<ChitUserBalance>),
SuccessV2(SuccessResult),
}

Expand Down
6 changes: 2 additions & 4 deletions backend/canisters/user_index/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,6 @@ struct Data {
pub video_call_operators: Vec<Principal>,
pub oc_key_pair: P256KeyPair,
pub empty_users: HashSet<UserId>,
// TODO: Remove this attribute after release
#[serde(default, skip_deserializing)]
pub chit_leaderboard: ChitLeaderboard,
pub deleted_users: Vec<DeletedUser>,
#[serde(with = "serde_bytes")]
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
60 changes: 1 addition & 59 deletions backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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<T: Ord>(heap: &mut BinaryHeap<T>, n: usize) -> Vec<T> {
let mut result = Vec::new();

for _i in 0..n {
if let Some(v) = heap.pop() {
result.push(v);
} else {
break;
}
}

result
}
20 changes: 0 additions & 20 deletions backend/canisters/user_index/impl/src/model/chit_leaderboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -45,19 +38,6 @@ impl ChitLeaderboard {
}
}

pub fn initialize(
&mut self,
all_time: Vec<ChitUserBalance>,
this_month: Vec<ChitUserBalance>,
last_month: Vec<ChitUserBalance>,
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,7 @@ export type ChitEarnedReason = { 'DailyClaim' : null } |
{ 'ExternalAchievement' : string } |
{ 'MemeContestWinner' : null } |
{ 'Referral' : ReferralStatus };
export type ChitLeaderboardResponse = { 'Success' : Array<ChitUserBalance> } |
{
export type ChitLeaderboardResponse = {
'SuccessV2' : {
'all_time' : Array<ChitUserBalance>,
'last_month' : Array<ChitUserBalance>,
Expand Down
39 changes: 17 additions & 22 deletions frontend/openchat-agent/src/services/userIndex/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
};
}
Expand All @@ -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",
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -215,7 +215,7 @@ function diamondMembershipStatus(candid: ApiDiamondMembershipStatusFull): Diamon
}
throw new UnsupportedValueError(
"Unexpected ApiDiamondMembershipStatusFull type received",
candid
candid,
);
}

Expand All @@ -228,7 +228,7 @@ function diamondMembership(candid: ApiDiamondMembershipDetails): DiamondMembersh
}

function diamondMembershipSubscription(
candid: ApiDiamondMembershipSubscription
candid: ApiDiamondMembershipSubscription,
): DiamondMembershipSubscription {
if ("OneMonth" in candid) {
return "one_month";
Expand All @@ -244,7 +244,7 @@ function diamondMembershipSubscription(
}
throw new UnsupportedValueError(
"Unexpected ApiDiamondMembershipSubscription type received",
candid
candid,
);
}

Expand Down Expand Up @@ -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" };
Expand Down Expand Up @@ -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 };
Expand All @@ -433,7 +433,7 @@ export function apiDiamondDuration(
}

export function diamondMembershipFeesResponse(
candid: ApiDiamondMembershipFeesResponse
candid: ApiDiamondMembershipFeesResponse,
): DiamondMembershipFees[] {
if ("Success" in candid) {
return candid.Success.map((f) => ({
Expand All @@ -446,26 +446,21 @@ 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 {
allTime: candid.SuccessV2.all_time.map(chitUserBalance),
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);
}

Expand All @@ -478,7 +473,7 @@ function chitUserBalance(candid: ApiChitUserBalance): ChitUserBalance {
}

export function submitProofOfUniquePersonhoodResponse(
candid: ApiSubmitProofOfUniquePersonhoodResponse
candid: ApiSubmitProofOfUniquePersonhoodResponse,
): SubmitProofOfUniquePersonhoodResponse {
if ("Success" in candid) {
return CommonResponses.success();
Expand All @@ -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 {
Expand All @@ -511,7 +506,7 @@ export function externalAchievementsResponse(
}
throw new UnsupportedValueError(
"Unexpected ApiExternalAchievementsResponse type received",
candid
candid,
);
}

Expand Down

0 comments on commit ccdb8c1

Please sign in to comment.