Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bbf7c7c
feat: use Datastore for user usage
peterpeterparker Feb 9, 2025
76fbe85
feat: remove getter
peterpeterparker Feb 9, 2025
70926e0
feat: update did
peterpeterparker Feb 9, 2025
7174aac
feat: update datastore
peterpeterparker Feb 9, 2025
91f7962
feat: in progress
peterpeterparker Feb 9, 2025
7804f24
feat: use a single usage collection
peterpeterparker Feb 9, 2025
7c58ac6
Merge branch 'main' into feat/user-db-for-user-usage
peterpeterparker Feb 9, 2025
f3a76ab
feat: timestamps are in doc
peterpeterparker Feb 9, 2025
66a8600
Merge branch 'main' into feat/user-db-for-user-usage
peterpeterparker Feb 9, 2025
fadea8d
chore: fmt and clippy
peterpeterparker Feb 9, 2025
7ab5ffb
feat: in progress
peterpeterparker Feb 9, 2025
2cfe5ff
Merge branch 'main' into feat/user-db-for-user-usage
peterpeterparker Feb 9, 2025
187f318
feat: parse key
peterpeterparker Feb 9, 2025
20427da
test: adapt
peterpeterparker Feb 9, 2025
eeb44de
feat: only controllers can read usage
peterpeterparker Feb 9, 2025
572ba6e
test: adapt for controllers
peterpeterparker Feb 9, 2025
a874c2e
chore: fmt
peterpeterparker Feb 9, 2025
b94f70a
refactor: object param
peterpeterparker Feb 9, 2025
2606226
test: revert to previous stable expected memory
peterpeterparker Feb 9, 2025
82f8a0f
chore: singular
peterpeterparker Feb 9, 2025
216facc
fix: create #user-usage rule on migration
peterpeterparker Feb 9, 2025
2f69c57
test: migrate collection
peterpeterparker Feb 9, 2025
5cea2a7
chore: merge main
peterpeterparker Feb 10, 2025
08ad6ea
chore: fmt
peterpeterparker Feb 10, 2025
85b2625
Merge branch 'main' into feat/user-db-for-user-usage
peterpeterparker Feb 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/declarations/satellite/satellite.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,6 @@ export interface SetRule {
write: Permission;
max_changes_per_user: [] | [number];
}
export interface SetUserUsage {
changes_count: number;
}
export interface StorageConfig {
iframe: [] | [StorageConfigIFrame];
rewrites: Array<[string, string]>;
Expand Down Expand Up @@ -243,12 +240,6 @@ export interface UploadChunk {
export interface UploadChunkResult {
chunk_id: bigint;
}
export interface UserUsage {
updated_at: bigint;
created_at: bigint;
version: [] | [bigint];
changes_count: number;
}
export interface _SERVICE {
build_version: ActorMethod<[], string>;
commit_asset_upload: ActorMethod<[CommitBatch], undefined>;
Expand Down Expand Up @@ -277,7 +268,6 @@ export interface _SERVICE {
get_many_docs: ActorMethod<[Array<[string, string]>], Array<[string, [] | [Doc]]>>;
get_rule: ActorMethod<[CollectionType, string], [] | [Rule]>;
get_storage_config: ActorMethod<[], StorageConfig>;
get_user_usage: ActorMethod<[string, CollectionType, [] | [Principal]], [] | [UserUsage]>;
http_request: ActorMethod<[HttpRequest], HttpResponse>;
http_request_streaming_callback: ActorMethod<
[StreamingCallbackToken],
Expand All @@ -298,7 +288,6 @@ export interface _SERVICE {
set_many_docs: ActorMethod<[Array<[string, string, SetDoc]>], Array<[string, Doc]>>;
set_rule: ActorMethod<[CollectionType, string, SetRule], Rule>;
set_storage_config: ActorMethod<[StorageConfig], undefined>;
set_user_usage: ActorMethod<[string, CollectionType, Principal, SetUserUsage], UserUsage>;
upload_asset_chunk: ActorMethod<[UploadChunk], UploadChunkResult>;
version: ActorMethod<[], string>;
}
Expand Down
17 changes: 0 additions & 17 deletions src/declarations/satellite/satellite.factory.certified.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,6 @@ export const idlFactory = ({ IDL }) => {
write: Permission,
max_changes_per_user: IDL.Opt(IDL.Nat32)
});
const UserUsage = IDL.Record({
updated_at: IDL.Nat64,
created_at: IDL.Nat64,
version: IDL.Opt(IDL.Nat64),
changes_count: IDL.Nat32
});
const HttpRequest = IDL.Record({
url: IDL.Text,
method: IDL.Text,
Expand Down Expand Up @@ -240,7 +234,6 @@ export const idlFactory = ({ IDL }) => {
write: Permission,
max_changes_per_user: IDL.Opt(IDL.Nat32)
});
const SetUserUsage = IDL.Record({ changes_count: IDL.Nat32 });
const UploadChunk = IDL.Record({
content: IDL.Vec(IDL.Nat8),
batch_id: IDL.Nat,
Expand Down Expand Up @@ -287,11 +280,6 @@ export const idlFactory = ({ IDL }) => {
),
get_rule: IDL.Func([CollectionType, IDL.Text], [IDL.Opt(Rule)], []),
get_storage_config: IDL.Func([], [StorageConfig], []),
get_user_usage: IDL.Func(
[IDL.Text, CollectionType, IDL.Opt(IDL.Principal)],
[IDL.Opt(UserUsage)],
[]
),
http_request: IDL.Func([HttpRequest], [HttpResponse], []),
http_request_streaming_callback: IDL.Func(
[StreamingCallbackToken],
Expand Down Expand Up @@ -321,11 +309,6 @@ export const idlFactory = ({ IDL }) => {
),
set_rule: IDL.Func([CollectionType, IDL.Text, SetRule], [Rule], []),
set_storage_config: IDL.Func([StorageConfig], [], []),
set_user_usage: IDL.Func(
[IDL.Text, CollectionType, IDL.Principal, SetUserUsage],
[UserUsage],
[]
),
upload_asset_chunk: IDL.Func([UploadChunk], [UploadChunkResult], []),
version: IDL.Func([], [IDL.Text], [])
});
Expand Down
17 changes: 0 additions & 17 deletions src/declarations/satellite/satellite.factory.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,6 @@ export const idlFactory = ({ IDL }) => {
write: Permission,
max_changes_per_user: IDL.Opt(IDL.Nat32)
});
const UserUsage = IDL.Record({
updated_at: IDL.Nat64,
created_at: IDL.Nat64,
version: IDL.Opt(IDL.Nat64),
changes_count: IDL.Nat32
});
const HttpRequest = IDL.Record({
url: IDL.Text,
method: IDL.Text,
Expand Down Expand Up @@ -240,7 +234,6 @@ export const idlFactory = ({ IDL }) => {
write: Permission,
max_changes_per_user: IDL.Opt(IDL.Nat32)
});
const SetUserUsage = IDL.Record({ changes_count: IDL.Nat32 });
const UploadChunk = IDL.Record({
content: IDL.Vec(IDL.Nat8),
batch_id: IDL.Nat,
Expand Down Expand Up @@ -287,11 +280,6 @@ export const idlFactory = ({ IDL }) => {
),
get_rule: IDL.Func([CollectionType, IDL.Text], [IDL.Opt(Rule)], ['query']),
get_storage_config: IDL.Func([], [StorageConfig], ['query']),
get_user_usage: IDL.Func(
[IDL.Text, CollectionType, IDL.Opt(IDL.Principal)],
[IDL.Opt(UserUsage)],
['query']
),
http_request: IDL.Func([HttpRequest], [HttpResponse], ['query']),
http_request_streaming_callback: IDL.Func(
[StreamingCallbackToken],
Expand Down Expand Up @@ -321,11 +309,6 @@ export const idlFactory = ({ IDL }) => {
),
set_rule: IDL.Func([CollectionType, IDL.Text, SetRule], [Rule], []),
set_storage_config: IDL.Func([StorageConfig], [], []),
set_user_usage: IDL.Func(
[IDL.Text, CollectionType, IDL.Principal, SetUserUsage],
[UserUsage],
[]
),
upload_asset_chunk: IDL.Func([UploadChunk], [UploadChunkResult], []),
version: IDL.Func([], [IDL.Text], ['query'])
});
Expand Down
17 changes: 0 additions & 17 deletions src/declarations/satellite/satellite.factory.did.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,6 @@ export const idlFactory = ({ IDL }) => {
write: Permission,
max_changes_per_user: IDL.Opt(IDL.Nat32)
});
const UserUsage = IDL.Record({
updated_at: IDL.Nat64,
created_at: IDL.Nat64,
version: IDL.Opt(IDL.Nat64),
changes_count: IDL.Nat32
});
const HttpRequest = IDL.Record({
url: IDL.Text,
method: IDL.Text,
Expand Down Expand Up @@ -240,7 +234,6 @@ export const idlFactory = ({ IDL }) => {
write: Permission,
max_changes_per_user: IDL.Opt(IDL.Nat32)
});
const SetUserUsage = IDL.Record({ changes_count: IDL.Nat32 });
const UploadChunk = IDL.Record({
content: IDL.Vec(IDL.Nat8),
batch_id: IDL.Nat,
Expand Down Expand Up @@ -287,11 +280,6 @@ export const idlFactory = ({ IDL }) => {
),
get_rule: IDL.Func([CollectionType, IDL.Text], [IDL.Opt(Rule)], ['query']),
get_storage_config: IDL.Func([], [StorageConfig], ['query']),
get_user_usage: IDL.Func(
[IDL.Text, CollectionType, IDL.Opt(IDL.Principal)],
[IDL.Opt(UserUsage)],
['query']
),
http_request: IDL.Func([HttpRequest], [HttpResponse], ['query']),
http_request_streaming_callback: IDL.Func(
[StreamingCallbackToken],
Expand Down Expand Up @@ -321,11 +309,6 @@ export const idlFactory = ({ IDL }) => {
),
set_rule: IDL.Func([CollectionType, IDL.Text, SetRule], [Rule], []),
set_storage_config: IDL.Func([StorageConfig], [], []),
set_user_usage: IDL.Func(
[IDL.Text, CollectionType, IDL.Principal, SetUserUsage],
[UserUsage],
[]
),
upload_asset_chunk: IDL.Func([UploadChunk], [UploadChunkResult], []),
version: IDL.Func([], [IDL.Text], ['query'])
});
Expand Down
35 changes: 16 additions & 19 deletions src/frontend/src/lib/api/satellites.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import type {
ListResults as ListAssets,
ListResults_1 as ListDocs,
Rule,
SetRule,
UserUsage
SetRule
} from '$declarations/satellite/satellite.did';
import { getSatelliteActor } from '$lib/api/actors/actor.juno.api';
import type { CustomDomains } from '$lib/types/custom-domain';
Expand All @@ -33,6 +32,21 @@ export const listDocs = async ({
return list_docs(collection, toListParams(params));
};

export const getDoc = async ({
satelliteId,
collection,
key,
identity
}: {
satelliteId: Principal;
collection: string;
key: string;
identity: OptionIdentity;
}): Promise<[] | [Doc]> => {
const { get_doc } = await getSatelliteActor({ satelliteId, identity });
return get_doc(collection, key);
};

export const listAssets = async ({
satelliteId,
collection,
Expand Down Expand Up @@ -291,20 +305,3 @@ export const countCollectionAssets = async ({
const { count_collection_assets } = await getSatelliteActor({ satelliteId, identity });
return count_collection_assets(collection);
};

export const getUsageUsage = async ({
satelliteId,
collection,
collectionType,
userId,
identity
}: {
satelliteId: Principal;
collection: string;
collectionType: CollectionType;
userId: Principal;
identity: OptionIdentity;
}): Promise<[] | [UserUsage]> => {
const { get_user_usage } = await getSatelliteActor({ satelliteId, identity });
return get_user_usage(collection, collectionType, toNullable(userId));
};
22 changes: 14 additions & 8 deletions src/frontend/src/lib/services/user.services.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type { CollectionType } from '$declarations/satellite/satellite.did';
import { getUsageUsage, listRules } from '$lib/api/satellites.api';
import { getDoc, listRules } from '$lib/api/satellites.api';
import { DbCollectionType, StorageCollectionType } from '$lib/constants/rules.constants';
import { busy } from '$lib/stores/busy.store';
import { i18n } from '$lib/stores/i18n.store';
import { toasts } from '$lib/stores/toasts.store';
import type { OptionIdentity } from '$lib/types/itentity';
import type { User } from '$lib/types/user';
import type { UserUsageCollection } from '$lib/types/user-usage';
import type { UserUsage, UserUsageCollection } from '$lib/types/user-usage';
import { emit } from '$lib/utils/events.utils';
import { waitReady } from '$lib/utils/timeout.utils';
import type { Identity } from '@dfinity/agent';
import type { Principal } from '@dfinity/principal';
import { assertNonNullish, fromNullable, isNullish } from '@dfinity/utils';
import { fromArray } from '@junobuild/utils';
import { get } from 'svelte/store';

interface OpenUserDetailParams {
Expand Down Expand Up @@ -88,19 +89,24 @@ const loadUserUsages = async ({
collectionType: CollectionType;
maxChangesPerUser: number | undefined;
}): Promise<UserUsageCollection> => {
const usage = await getUsageUsage({
const key = `${user.owner.toText()}#${'Storage' in collectionType ? 'storage' : 'db'}#${collection}`;

const result = await getDoc({
satelliteId,
collection,
collectionType,
identity,
userId: user.owner
collection: '#user-usage',
key,
identity
});

const doc = fromNullable(result);

const usage = isNullish(doc) ? undefined : await fromArray<UserUsage>(doc.data);

return {
collection,
collectionType,
maxChangesPerUser,
usage: fromNullable(usage)
usage
};
};

Expand Down
6 changes: 5 additions & 1 deletion src/frontend/src/lib/types/user-usage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { CollectionType, UserUsage } from '$declarations/satellite/satellite.did';
import type { CollectionType } from '$declarations/satellite/satellite.did';

export interface UserUsage {
changes_count: number;
}

export interface UserUsageCollection {
collection: string;
Expand Down
17 changes: 16 additions & 1 deletion src/libs/collections/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,24 @@ pub const COLLECTION_LOG_DEFAULT_RULE: SetRule = SetRule {
rate_config: None,
};

pub const DEFAULT_DB_COLLECTIONS: [(&str, SetRule); 2] = [
pub const USER_USAGE_COLLECTION_KEY: &str = "#user-usage";

pub const DEFAULT_USER_USAGE_RULE: SetRule = SetRule {
read: Controllers,
write: Controllers,
memory: Some(Memory::Stable),
mutable_permissions: Some(false),
max_size: None,
max_capacity: None,
max_changes_per_user: None,
version: None,
rate_config: None,
};

pub const DEFAULT_DB_COLLECTIONS: [(&str, SetRule); 3] = [
(COLLECTION_USER_KEY, COLLECTION_USER_DEFAULT_RULE),
(COLLECTION_LOG_KEY, COLLECTION_LOG_DEFAULT_RULE),
(USER_USAGE_COLLECTION_KEY, DEFAULT_USER_USAGE_RULE),
];

pub const DB_COLLECTIONS_WITHOUT_USER_USAGE: [&str; 1] = [COLLECTION_LOG_KEY];
Expand Down
13 changes: 0 additions & 13 deletions src/libs/satellite/satellite.did
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ type SetRule = record {
write : Permission;
max_changes_per_user : opt nat32;
};
type SetUserUsage = record { changes_count : nat32 };
type StorageConfig = record {
iframe : opt StorageConfigIFrame;
rewrites : vec record { text; text };
Expand Down Expand Up @@ -201,12 +200,6 @@ type UploadChunk = record {
order_id : opt nat;
};
type UploadChunkResult = record { chunk_id : nat };
type UserUsage = record {
updated_at : nat64;
created_at : nat64;
version : opt nat64;
changes_count : nat32;
};
service : () -> {
commit_asset_upload : (CommitBatch) -> ();
count_assets : (text, ListParams) -> (nat64) query;
Expand Down Expand Up @@ -240,9 +233,6 @@ service : () -> {
) query;
get_rule : (CollectionType, text) -> (opt Rule) query;
get_storage_config : () -> (StorageConfig) query;
get_user_usage : (text, CollectionType, opt principal) -> (
opt UserUsage,
) query;
http_request : (HttpRequest) -> (HttpResponse) query;
http_request_streaming_callback : (StreamingCallbackToken) -> (
StreamingCallbackHttpResponse,
Expand All @@ -266,9 +256,6 @@ service : () -> {
);
set_rule : (CollectionType, text, SetRule) -> (Rule);
set_storage_config : (StorageConfig) -> ();
set_user_usage : (text, CollectionType, principal, SetUserUsage) -> (
UserUsage,
);
upload_asset_chunk : (UploadChunk) -> (UploadChunkResult);
version : () -> (text) query;
}
16 changes: 15 additions & 1 deletion src/libs/satellite/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::memory::init_stable_state;
use crate::types::state::{HeapState, RuntimeState, State};
use crate::types::state::{CollectionType, HeapState, RuntimeState, State};
use std::fmt::{Display, Formatter, Result as FmtResult};

impl Default for State {
fn default() -> Self {
Expand All @@ -10,3 +11,16 @@ impl Default for State {
}
}
}

impl Display for CollectionType {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(
f,
"{}",
match self {
CollectionType::Db => "db",
CollectionType::Storage => "storage",
}
)
}
}
Loading