diff --git a/backend/canisters/local_group_index/CHANGELOG.md b/backend/canisters/local_group_index/CHANGELOG.md index cac3555ae9..9bfd8811fa 100644 --- a/backend/canisters/local_group_index/CHANGELOG.md +++ b/backend/canisters/local_group_index/CHANGELOG.md @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [unreleased] +### Added + +- Expose the cycles top-ups of Group/Community canisters ([#7053](https://github.com/open-chat-labs/open-chat/pull/7053)) + +## [[2.0.1505](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1505-local_group_index)] - 2024-12-09 + ### Changed - Increase Windoge community canister's reserved cycles limit ([#7022](https://github.com/open-chat-labs/open-chat/pull/7022)) diff --git a/backend/canisters/local_group_index/impl/src/lifecycle/post_upgrade.rs b/backend/canisters/local_group_index/impl/src/lifecycle/post_upgrade.rs index 838fe8762e..45e0149e2c 100644 --- a/backend/canisters/local_group_index/impl/src/lifecycle/post_upgrade.rs +++ b/backend/canisters/local_group_index/impl/src/lifecycle/post_upgrade.rs @@ -1,16 +1,12 @@ use crate::lifecycle::{init_env, init_state}; use crate::memory::get_upgrades_memory; -use crate::{mutate_state, read_state, Data}; +use crate::Data; use canister_logger::LogEntry; use canister_tracing_macros::trace; -use ic_cdk::api::management_canister::main::{CanisterSettings, UpdateSettingsArgument}; use ic_cdk::post_upgrade; use local_group_index_canister::post_upgrade::Args; use stable_memory::get_reader; -use std::time::Duration; use tracing::info; -use types::{CanisterId, CyclesTopUp}; -use utils::canister::deposit_cycles; use utils::cycles::init_cycles_dispenser_client; #[post_upgrade] @@ -29,39 +25,4 @@ fn post_upgrade(args: Args) { init_state(env, data, args.wasm_version); info!(version = %args.wasm_version, "Post-upgrade complete"); - - ic_cdk_timers::set_timer(Duration::ZERO, || { - ic_cdk::spawn(increase_windoge_reserved_cycles_limit_public()) - }); -} - -async fn increase_windoge_reserved_cycles_limit_public() { - let windoge_canister_id = CanisterId::from_text("ow6el-gyaaa-aaaar-av5na-cai").unwrap(); - - if read_state(|state| state.data.local_communities.get(&windoge_canister_id.into()).is_none()) { - return; - } - - ic_cdk::api::management_canister::main::update_settings(UpdateSettingsArgument { - canister_id: windoge_canister_id, - settings: CanisterSettings { - reserved_cycles_limit: Some(20_000_000_000_000u128.into()), - ..Default::default() - }, - }) - .await - .unwrap(); - - let amount = 20_000_000_000_000u128; - deposit_cycles(windoge_canister_id, amount).await.unwrap(); - - mutate_state(|state| { - state.data.local_communities.mark_cycles_top_up( - &windoge_canister_id.into(), - CyclesTopUp { - amount, - date: state.env.now(), - }, - ); - }) } diff --git a/backend/canisters/local_group_index/impl/src/queries/http_request.rs b/backend/canisters/local_group_index/impl/src/queries/http_request.rs index 83ea8cdf64..2e2d864c94 100644 --- a/backend/canisters/local_group_index/impl/src/queries/http_request.rs +++ b/backend/canisters/local_group_index/impl/src/queries/http_request.rs @@ -1,7 +1,8 @@ use crate::{read_state, RuntimeState}; use http_request::{build_json_response, encode_logs, extract_route, Route}; use ic_cdk::query; -use types::{HttpRequest, HttpResponse, TimestampMillis}; +use std::collections::HashMap; +use types::{CanisterId, HttpRequest, HttpResponse, TimestampMillis}; #[query] fn http_request(request: HttpRequest) -> HttpResponse { @@ -21,11 +22,24 @@ fn http_request(request: HttpRequest) -> HttpResponse { build_json_response(&state.metrics()) } + fn get_top_ups(qs: HashMap, state: &RuntimeState) -> HttpResponse { + let canister_id = CanisterId::from_text(qs.get("canister_id").unwrap()).unwrap(); + + if let Some(group) = state.data.local_groups.get(&canister_id.into()) { + build_json_response(&group.cycle_top_ups) + } else if let Some(community) = state.data.local_communities.get(&canister_id.into()) { + build_json_response(&community.cycle_top_ups) + } else { + HttpResponse::not_found() + } + } + match extract_route(&request.url) { Route::Errors(since) => get_errors_impl(since), Route::Logs(since) => get_logs_impl(since), Route::Traces(since) => get_traces_impl(since), Route::Metrics => read_state(get_metrics_impl), + Route::Other(p, qs) if p == "top_ups" => read_state(|state| get_top_ups(qs, state)), _ => HttpResponse::not_found(), } } diff --git a/backend/canisters/local_user_index/CHANGELOG.md b/backend/canisters/local_user_index/CHANGELOG.md index 2ed6a3694c..0ee881feb0 100644 --- a/backend/canisters/local_user_index/CHANGELOG.md +++ b/backend/canisters/local_user_index/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [unreleased] +### Added + +- Expose the cycles top-ups of User canisters ([#7053](https://github.com/open-chat-labs/open-chat/pull/7053)) + ### Changed - Expose size of each virtual stable memory in metrics ([#6981](https://github.com/open-chat-labs/open-chat/pull/6981)) diff --git a/backend/canisters/local_user_index/impl/src/queries/http_request.rs b/backend/canisters/local_user_index/impl/src/queries/http_request.rs index 870f748f09..500b1b38f7 100644 --- a/backend/canisters/local_user_index/impl/src/queries/http_request.rs +++ b/backend/canisters/local_user_index/impl/src/queries/http_request.rs @@ -4,7 +4,7 @@ use ic_cdk::query; use serde::Serialize; use std::collections::{BTreeMap, HashMap}; use std::str::FromStr; -use types::{BuildVersion, HttpRequest, HttpResponse, TimestampMillis, UserId}; +use types::{BuildVersion, CanisterId, HttpRequest, HttpResponse, TimestampMillis, UserId}; #[query] fn http_request(request: HttpRequest) -> HttpResponse { @@ -24,6 +24,16 @@ fn http_request(request: HttpRequest) -> HttpResponse { build_json_response(&state.metrics()) } + fn get_top_ups(qs: HashMap, state: &RuntimeState) -> HttpResponse { + let user_id: UserId = CanisterId::from_text(qs.get("canister_id").unwrap()).unwrap().into(); + + let Some(user) = state.data.local_users.get(&user_id) else { + return HttpResponse::not_found(); + }; + + build_json_response(&user.cycle_top_ups) + } + fn get_user_canister_versions(state: &RuntimeState) -> HttpResponse { let mut map = BTreeMap::new(); for (user_id, user) in state.data.local_users.iter() { @@ -60,6 +70,7 @@ fn http_request(request: HttpRequest) -> HttpResponse { Route::Logs(since) => get_logs_impl(since), Route::Traces(since) => get_traces_impl(since), Route::Metrics => read_state(get_metrics_impl), + Route::Other(p, qs) if p == "top_ups" => read_state(|state| get_top_ups(qs, state)), Route::Other(p, _) if p == "user_canister_versions" => read_state(get_user_canister_versions), Route::Other(p, qs) if p == "remote_user_events" => read_state(|state| get_remote_user_events(qs, state)), _ => HttpResponse::not_found(),