Skip to content

Commit

Permalink
Expose count of new users per day (#4873)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Nov 28, 2023
1 parent 07e5a71 commit 170eea2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Added

- Expose count of new users per day ([#4873](https://github.com/open-chat-labs/open-chat/pull/4873))

## [[2.0.952](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.952-user_index)] - 2023-11-28

### Changed
Expand Down
2 changes: 1 addition & 1 deletion backend/canisters/user_index/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ canister_tracing_macros = { path = "../../../libraries/canister_tracing_macros"
chat_events = { path = "../../../libraries/chat_events" }
community_canister = { path = "../../community/api" }
community_canister_c2c_client = { path = "../../community/c2c_client" }
time = { workspace = true }
futures = { workspace = true }
group_canister = { path = "../../group/api" }
group_canister_c2c_client = { path = "../../group/c2c_client" }
Expand Down Expand Up @@ -53,6 +52,7 @@ sha2 = { workspace = true }
stable_memory = { path = "../../../libraries/stable_memory" }
storage_index_canister = { path = "../../storage_index/api" }
storage_index_canister_c2c_client = { path = "../../storage_index/c2c_client" }
time = { workspace = true }
tracing = { workspace = true }
types = { path = "../../../libraries/types" }
user_canister = { path = "../../user/api" }
Expand Down
12 changes: 12 additions & 0 deletions backend/canisters/user_index/impl/src/queries/http_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{read_state, RuntimeState};
use http_request::{build_json_response, encode_logs, extract_route, Route};
use ic_cdk_macros::query;
use std::collections::BTreeMap;
use types::{HttpRequest, HttpResponse, TimestampMillis};

#[query]
Expand All @@ -17,10 +18,21 @@ fn http_request(request: HttpRequest) -> HttpResponse {
build_json_response(&state.metrics())
}

fn get_new_users_per_day(state: &RuntimeState) -> HttpResponse {
let mut grouped: BTreeMap<String, u32> = BTreeMap::new();
for user in state.data.users.iter().filter(|u| u.date_created > 0) {
let date = time::OffsetDateTime::from_unix_timestamp((user.date_created / 1000) as i64).unwrap();
let date_string = format!("{}-{}-{}", date.year(), u8::from(date.month()), date.day());
*grouped.entry(date_string).or_default() += 1;
}
build_json_response(&grouped)
}

match extract_route(&request.url) {
Route::Logs(since) => get_logs_impl(since),
Route::Traces(since) => get_traces_impl(since),
Route::Metrics => read_state(get_metrics_impl),
Route::Other(path, _) if path == "new_users_per_day" => read_state(get_new_users_per_day),
_ => HttpResponse::not_found(),
}
}

0 comments on commit 170eea2

Please sign in to comment.