Skip to content

Commit

Permalink
ML data (#346)
Browse files Browse the repository at this point in the history
* ml data

* ml data changes

* memory id changes
  • Loading branch information
komal-sai-yral authored Jul 29, 2024
1 parent f869ac7 commit 0763b61
Show file tree
Hide file tree
Showing 15 changed files with 423 additions and 34 deletions.
53 changes: 36 additions & 17 deletions src/canister/individual_user_template/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,21 @@ type Result_1 = variant {
Ok : BettingStatus;
Err : BetOnCurrentlyViewingPostError;
};
type Result_10 = variant {
type Result_10 = variant { Ok : vec SuccessHistoryItem; Err : text };
type Result_11 = variant {
Ok : vec record { nat64; TokenEvent };
Err : GetPostsOfUserProfileError;
};
type Result_11 = variant { Ok : vec text; Err : NamespaceErrors };
type Result_12 = variant { Ok; Err : MigrationErrors };
type Result_13 = variant { Ok : text; Err : text };
type Result_14 = variant {
type Result_12 = variant { Ok : vec WatchHistoryItem; Err : text };
type Result_13 = variant { Ok : vec text; Err : NamespaceErrors };
type Result_14 = variant { Ok; Err : MigrationErrors };
type Result_15 = variant { Ok : text; Err : text };
type Result_16 = variant {
Ok : UserProfileDetailsForFrontend;
Err : UpdateProfileDetailsError;
};
type Result_15 = variant { Ok; Err : text };
type Result_16 = variant { Ok; Err : UpdateProfileSetUniqueUsernameError };
type Result_17 = variant { Ok; Err : text };
type Result_18 = variant { Ok; Err : UpdateProfileSetUniqueUsernameError };
type Result_2 = variant { Ok : NamespaceForFrontend; Err : NamespaceErrors };
type Result_3 = variant { Ok : opt text; Err : NamespaceErrors };
type Result_4 = variant { Ok; Err : NamespaceErrors };
Expand All @@ -274,6 +276,12 @@ type RoomDetails = record {
type SessionType = variant { AnonymousSession; RegisteredSession };
type SlotDetails = record { room_details : vec record { nat64; RoomDetails } };
type StakeEvent = variant { BetOnHotOrNotPost : PlaceBetArg };
type SuccessHistoryItem = record {
post_id : nat64;
publisher_canister_id : principal;
cf_video_id : text;
interacted_at : SystemTime;
};
type SystemTime = record {
nanos_since_epoch : nat32;
secs_since_epoch : nat64;
Expand Down Expand Up @@ -353,6 +361,13 @@ type UserProfileUpdateDetailsFromFrontend = record {
profile_picture_url : opt text;
display_name : opt text;
};
type WatchHistoryItem = record {
post_id : nat64;
viewed_at : SystemTime;
percentage_watched : float32;
publisher_canister_id : principal;
cf_video_id : text;
};
service : (IndividualUserTemplateInitArgs) -> {
add_post_v2 : (PostDetailsFromFrontend) -> (Result);
backup_data_to_backup_canister : (principal, principal) -> ();
Expand Down Expand Up @@ -395,26 +410,28 @@ service : (IndividualUserTemplateInitArgs) -> {
get_rewarded_for_signing_up : () -> ();
get_session_type : () -> (Result_9) query;
get_stable_memory_size : () -> (nat32) query;
get_success_history : () -> (Result_10) query;
get_user_caniser_cycle_balance : () -> (nat) query;
get_user_utility_token_transaction_history_with_pagination : (
nat64,
nat64,
) -> (Result_10) query;
) -> (Result_11) query;
get_utility_token_balance : () -> (nat64) query;
get_version : () -> (text) query;
get_version_number : () -> (nat64) query;
get_watch_history : () -> (Result_12) query;
get_well_known_principal_value : (KnownPrincipalType) -> (
opt principal,
) query;
http_request : (HttpRequest) -> (HttpResponse) query;
list_namespace_keys : (nat64) -> (Result_11) query;
list_namespace_keys : (nat64) -> (Result_13) query;
list_namespaces : (nat64, nat64) -> (vec NamespaceForFrontend) query;
load_snapshot : (nat64) -> ();
read_key_value_pair : (nat64, text) -> (Result_3) query;
receive_and_save_snaphot : (nat64, blob) -> ();
receive_bet_from_bet_makers_canister : (PlaceBetArg, principal) -> (Result_1);
receive_bet_winnings_when_distributed : (nat64, BetOutcomeForBetMaker) -> ();
receive_data_from_hotornot : (principal, nat64, vec Post) -> (Result_12);
receive_data_from_hotornot : (principal, nat64, vec Post) -> (Result_14);
receive_my_created_posts_from_data_backup_canister : (vec Post) -> ();
receive_my_profile_from_data_backup_canister : (UserProfile) -> ();
receive_my_utility_token_balance_from_data_backup_canister : (nat64) -> ();
Expand All @@ -427,27 +444,29 @@ service : (IndividualUserTemplateInitArgs) -> {
) -> ();
return_cycles_to_user_index_canister : (opt nat) -> ();
save_snapshot_json : () -> (nat32);
transfer_tokens_and_posts : (principal, principal) -> (Result_12);
update_last_access_time : () -> (Result_13);
transfer_tokens_and_posts : (principal, principal) -> (Result_14);
update_last_access_time : () -> (Result_15);
update_last_canister_functionality_access_time : () -> ();
update_post_add_view_details : (nat64, PostViewDetailsFromFrontend) -> ();
update_post_as_ready_to_view : (nat64) -> ();
update_post_increment_share_count : (nat64) -> (nat64);
update_post_status : (nat64, PostStatus) -> ();
update_post_toggle_like_status_by_caller : (nat64) -> (bool);
update_profile_display_details : (UserProfileUpdateDetailsFromFrontend) -> (
Result_14,
Result_16,
);
update_profile_owner : (opt principal) -> (Result_15);
update_profile_set_unique_username_once : (text) -> (Result_16);
update_profile_owner : (opt principal) -> (Result_17);
update_profile_set_unique_username_once : (text) -> (Result_18);
update_profiles_i_follow_toggle_list_with_specified_profile : (
FolloweeArg,
) -> (Result_5);
update_profiles_that_follow_me_toggle_list_with_specified_profile : (
FollowerArg,
) -> (Result_5);
update_referrer_details : (UserCanisterDetails) -> (Result_13);
update_session_type : (SessionType) -> (Result_13);
update_referrer_details : (UserCanisterDetails) -> (Result_15);
update_session_type : (SessionType) -> (Result_15);
update_success_history : (SuccessHistoryItem) -> (Result_15);
update_watch_history : (WatchHistoryItem) -> (Result_15);
update_well_known_principal : (KnownPrincipalType, principal) -> ();
write_key_value_pair : (nat64, text, text) -> (Result_3);
write_multiple_key_value_pairs : (nat64, vec record { text; text }) -> (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use shared_utils::{
canister_specific::individual_user_template::types::ml_data::{
SuccessHistoryItem, WatchHistoryItem,
},
common::utils::permissions::is_caller_controller_or_global_admin,
};

use ic_cdk_macros::query;

use crate::{
api::canister_management::update_last_access_time::update_last_canister_functionality_access_time,
CANISTER_DATA,
};

#[query(guard = "is_caller_controller_or_global_admin")]
fn get_watch_history() -> Result<Vec<WatchHistoryItem>, String> {
update_last_canister_functionality_access_time();

CANISTER_DATA.with(|canister_data| {
let canister_data = canister_data.borrow();
Ok(canister_data
.watch_history
.iter()
.map(|(k, _)| k.clone())
.collect())
})
}

#[query(guard = "is_caller_controller_or_global_admin")]
fn get_success_history() -> Result<Vec<SuccessHistoryItem>, String> {
update_last_canister_functionality_access_time();

CANISTER_DATA.with(|canister_data| {
let canister_data = canister_data.borrow();
Ok(canister_data
.success_history
.iter()
.map(|(k, _)| k.clone())
.collect())
})
}
3 changes: 3 additions & 0 deletions src/canister/individual_user_template/src/api/ml_ops/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod get_history;
pub mod update_success_history;
pub mod update_watch_history;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use shared_utils::{
canister_specific::individual_user_template::types::ml_data::SuccessHistoryItem,
common::utils::permissions::is_caller_controller_or_global_admin,
};

use ic_cdk_macros::update;

use crate::{
api::canister_management::update_last_access_time::update_last_canister_functionality_access_time,
CANISTER_DATA,
};

#[update(guard = "is_caller_controller_or_global_admin")]
fn update_success_history(success_history_item: SuccessHistoryItem) -> Result<String, String> {
update_last_canister_functionality_access_time();

CANISTER_DATA.with(|canister_data| {
let mut canister_data = canister_data.borrow_mut();
canister_data
.success_history
.insert(success_history_item, ());
});

Ok("Success".into())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use shared_utils::{
canister_specific::individual_user_template::types::ml_data::WatchHistoryItem,
common::utils::permissions::is_caller_controller_or_global_admin,
};

use ic_cdk_macros::update;

use crate::{
api::canister_management::update_last_access_time::update_last_canister_functionality_access_time,
CANISTER_DATA,
};

#[update(guard = "is_caller_controller_or_global_admin")]
fn update_watch_history(watch_history_item: WatchHistoryItem) -> Result<String, String> {
update_last_canister_functionality_access_time();

CANISTER_DATA.with(|canister_data| {
let mut canister_data = canister_data.borrow_mut();
canister_data.watch_history.insert(watch_history_item, ());
});

Ok("Success".into())
}
1 change: 1 addition & 0 deletions src/canister/individual_user_template/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod follow;
pub mod hot_or_not_bet;
pub mod http;
pub mod kv_storage;
pub mod ml_ops;
pub mod monitoring;
pub mod post;
pub mod profile;
Expand Down
10 changes: 10 additions & 0 deletions src/canister/individual_user_template/src/data_model/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const POST_PRINCIPAL_MEMORY: MemoryId = MemoryId::new(3);
const SLOT_DETAILS_MEMORY: MemoryId = MemoryId::new(4);
const KV_STORAGE_NAMESPACE_MEMORY: MemoryId = MemoryId::new(5);
const KV_STORAGE_NAMESPACE_KEY_VALUE_MEMORY: MemoryId = MemoryId::new(6);
const WATCH_HISTORY_MEMORY: MemoryId = MemoryId::new(7);
const SUCCESS_HISTORY_MEMORY: MemoryId = MemoryId::new(8);

pub type Memory = VirtualMemory<DefaultMemoryImpl>;

Expand Down Expand Up @@ -54,6 +56,14 @@ pub fn get_kv_storage_namespace_key_value_memory() -> Memory {
MEMORY_MANAGER.with(|m| m.borrow_mut().get(KV_STORAGE_NAMESPACE_KEY_VALUE_MEMORY))
}

pub fn get_watch_history_memory() -> Memory {
MEMORY_MANAGER.with(|m| m.borrow_mut().get(WATCH_HISTORY_MEMORY))
}

pub fn get_success_history_memory() -> Memory {
MEMORY_MANAGER.with(|m| m.borrow_mut().get(SUCCESS_HISTORY_MEMORY))
}

pub fn init_memory_manager() {
MEMORY_MANAGER.with(|m| {
*m.borrow_mut() = MemoryManager::init_with_bucket_size(DefaultMemoryImpl::default(), 1);
Expand Down
18 changes: 18 additions & 0 deletions src/canister/individual_user_template/src/data_model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use candid::{Deserialize, Principal};
use ic_cdk::api::management_canister::provisional::CanisterId;
use memory::{get_success_history_memory, get_watch_history_memory};
use serde::Serialize;
use shared_utils::{
canister_specific::individual_user_template::types::{
Expand All @@ -15,6 +16,7 @@ use shared_utils::{
SlotDetailsV1, SlotId, StablePrincipal,
},
migration::MigrationInfo,
ml_data::{SuccessHistoryItem, WatchHistoryItem},
post::{FeedScore, Post, PostViewStatistics},
profile::UserProfile,
session::SessionType,
Expand Down Expand Up @@ -74,6 +76,10 @@ pub struct CanisterData {
pub migration_info: MigrationInfo,
#[serde(default)]
pub app_storage: AppStorage,
#[serde(skip, default = "_default_watch_history")]
pub watch_history: ic_stable_structures::btreemap::BTreeMap<WatchHistoryItem, (), Memory>,
#[serde(skip, default = "_default_success_history")]
pub success_history: ic_stable_structures::btreemap::BTreeMap<SuccessHistoryItem, (), Memory>,
}

pub fn _default_room_details(
Expand All @@ -96,6 +102,16 @@ pub fn _default_slot_details_map(
ic_stable_structures::btreemap::BTreeMap::init(get_slot_details_memory())
}

pub fn _default_watch_history(
) -> ic_stable_structures::btreemap::BTreeMap<WatchHistoryItem, (), Memory> {
ic_stable_structures::btreemap::BTreeMap::init(get_watch_history_memory())
}

pub fn _default_success_history(
) -> ic_stable_structures::btreemap::BTreeMap<SuccessHistoryItem, (), Memory> {
ic_stable_structures::btreemap::BTreeMap::init(get_success_history_memory())
}

impl Default for CanisterData {
fn default() -> Self {
Self {
Expand All @@ -120,6 +136,8 @@ impl Default for CanisterData {
last_canister_functionality_access_time: None,
migration_info: MigrationInfo::NotMigrated,
app_storage: AppStorage::default(),
watch_history: _default_watch_history(),
success_history: _default_success_history(),
}
}
}
1 change: 1 addition & 0 deletions src/canister/individual_user_template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use shared_utils::{
hot_or_not::{BetOutcomeForBetMaker, BettingStatus, PlacedBetDetail},
kv_storage::{NamespaceErrors, NamespaceForFrontend},
migration::MigrationErrors,
ml_data::{SuccessHistoryItem, WatchHistoryItem},
post::{
Post, PostDetailsForFrontend, PostDetailsFromFrontend, PostViewDetailsFromFrontend,
},
Expand Down
10 changes: 5 additions & 5 deletions src/canister/platform_orchestrator/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ type CanisterUpgradeStatus = record {
type HttpRequest = record {
url : text;
method : text;
body : vec nat8;
body : blob;
headers : vec record { text; text };
};
type HttpResponse = record {
body : vec nat8;
body : blob;
headers : vec record { text; text };
status_code : nat16;
};
Expand All @@ -32,7 +32,7 @@ type PlatformOrchestratorGenericArgumentType = variant {
UpgradeSubnetCanisters : UpgradeCanisterArg;
RemoveSubnetOrchestratorFromAvailableList : principal;
ProvisionSubnetOrchestrator : principal;
UploadWasm : record { WasmType; vec nat8 };
UploadWasm : record { WasmType; blob };
};
type PlatformOrchestratorGenericResultType = variant {
UpgradeSubnetCanisters : Result;
Expand All @@ -47,7 +47,7 @@ type Result_2 = variant { Ok; Err : text };
type UpgradeCanisterArg = record {
version : text;
canister : WasmType;
wasm_blob : vec nat8;
wasm_blob : blob;
};
type WasmType = variant {
IndividualUserWasm;
Expand Down Expand Up @@ -90,7 +90,7 @@ service : (PlatformOrchestratorInitArgs) -> {
) -> (Result);
upgrade_canisters_in_network : (UpgradeCanisterArg) -> (Result);
upgrade_specific_individual_canister : (principal) -> ();
upload_wasms : (WasmType, vec nat8) -> (Result);
upload_wasms : (WasmType, blob) -> (Result);
validate_platform_orchestrator_generic_function : (
PlatformOrchestratorGenericArgumentType,
) -> (Result);
Expand Down
4 changes: 2 additions & 2 deletions src/canister/post_cache/can.did
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
type HttpRequest = record {
url : text;
method : text;
body : vec nat8;
body : blob;
headers : vec record { text; text };
};
type HttpResponse = record {
body : vec nat8;
body : blob;
headers : vec record { text; text };
status_code : nat16;
};
Expand Down
Loading

0 comments on commit 0763b61

Please sign in to comment.