Skip to content

Commit

Permalink
feat: rename count_docs|assets to count_collection_docs|assets (#700)
Browse files Browse the repository at this point in the history
* feat: rename count_docs|assets to count_collection_docs|assets

Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>

* feat: query if necessary

Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>

* test: adapt

Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>

---------

Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>
  • Loading branch information
peterpeterparker committed Aug 24, 2024
1 parent 8c556b8 commit b1ad2b9
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/declarations/satellite/satellite.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ export interface UploadChunkResult {
export interface _SERVICE {
build_version: ActorMethod<[], string>;
commit_asset_upload: ActorMethod<[CommitBatch], undefined>;
count_assets: ActorMethod<[string], bigint>;
count_docs: ActorMethod<[string], bigint>;
count_collection_assets: ActorMethod<[string], bigint>;
count_collection_docs: ActorMethod<[string], bigint>;
del_asset: ActorMethod<[string, string], undefined>;
del_assets: ActorMethod<[string], undefined>;
del_controllers: ActorMethod<[DeleteControllersArgs], Array<[Principal, Controller]>>;
Expand Down
4 changes: 2 additions & 2 deletions src/declarations/satellite/satellite.factory.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ export const idlFactory = ({ IDL }) => {
return IDL.Service({
build_version: IDL.Func([], [IDL.Text], ['query']),
commit_asset_upload: IDL.Func([CommitBatch], [], []),
count_assets: IDL.Func([IDL.Text], [IDL.Nat64], ['query']),
count_docs: IDL.Func([IDL.Text], [IDL.Nat64], ['query']),
count_collection_assets: IDL.Func([IDL.Text], [IDL.Nat64], ['query']),
count_collection_docs: IDL.Func([IDL.Text], [IDL.Nat64], ['query']),
del_asset: IDL.Func([IDL.Text, IDL.Text], [], []),
del_assets: IDL.Func([IDL.Text], [], []),
del_controllers: IDL.Func(
Expand Down
4 changes: 2 additions & 2 deletions src/declarations/satellite/satellite.factory.did.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ export const idlFactory = ({ IDL }) => {
return IDL.Service({
build_version: IDL.Func([], [IDL.Text], ['query']),
commit_asset_upload: IDL.Func([CommitBatch], [], []),
count_assets: IDL.Func([IDL.Text], [IDL.Nat64], ['query']),
count_docs: IDL.Func([IDL.Text], [IDL.Nat64], ['query']),
count_collection_assets: IDL.Func([IDL.Text], [IDL.Nat64], ['query']),
count_collection_docs: IDL.Func([IDL.Text], [IDL.Nat64], ['query']),
del_asset: IDL.Func([IDL.Text, IDL.Text], [], []),
del_assets: IDL.Func([IDL.Text], [], []),
del_controllers: IDL.Func(
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/src/lib/api/satellites.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export const depositCycles = async ({
});
};

export const countAssets = async ({
export const countCollectionAssets = async ({
satelliteId,
collection,
identity
Expand All @@ -298,6 +298,6 @@ export const countAssets = async ({
collection: string;
identity: OptionIdentity;
}): Promise<bigint> => {
const { count_assets } = await getSatelliteActor({ satelliteId, identity });
return count_assets(collection);
const { count_collection_assets } = await getSatelliteActor({ satelliteId, identity });
return count_collection_assets(collection);
};
22 changes: 19 additions & 3 deletions src/frontend/src/lib/components/hosting/HostingCount.svelte
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
<script lang="ts">
import { fade } from 'svelte/transition';
import { onMount } from 'svelte';
import { countAssets } from '$lib/api/satellites.api';
import { countCollectionAssets, satelliteVersion } from '$lib/api/satellites.api';
import type { Satellite } from '$declarations/mission_control/mission_control.did';
import { authStore } from '$lib/stores/auth.store';
import { toasts } from '$lib/stores/toasts.store';
import { i18n } from '$lib/stores/i18n.store';
import { toasts } from '$lib/stores/toasts.store';
import { compare } from 'semver';
import { versionStore } from '$lib/stores/version.store';
export let satellite: Satellite;
let assets = 0n;
onMount(async () => {
try {
assets = await countAssets({
// TODO: load versions globally and use store value instead of fetching version again
const version =
$versionStore.satellites[satellite.satellite_id.toText()]?.current ??
(await satelliteVersion({
satelliteId: satellite.satellite_id,
identity: $authStore.identity
}));
if (compare(version, '0.0.20') < 0) {
// For simplicity reasons we do not display the information for not up-to-date Satellite.
// In Satellite v0.0.20, the endpoint to list the number of assets in a collection was renamed from `count_assets` to `count_collection_assets`.
return;
}
assets = await countCollectionAssets({
satelliteId: satellite.satellite_id,
collection: '#dapp',
identity: $authStore.identity
Expand Down
4 changes: 2 additions & 2 deletions src/libs/satellite/satellite.did
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ type UploadChunk = record {
type UploadChunkResult = record { chunk_id : nat };
service : () -> {
commit_asset_upload : (CommitBatch) -> ();
count_assets : (text) -> (nat64) query;
count_docs : (text) -> (nat64) query;
count_collection_assets : (text) -> (nat64) query;
count_collection_docs : (text) -> (nat64) query;
del_asset : (text, text) -> ();
del_assets : (text) -> ();
del_controllers : (DeleteControllersArgs) -> (
Expand Down
2 changes: 1 addition & 1 deletion src/libs/satellite/src/db/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ fn delete_docs_impl(
/// - `Err(String)`: An error message if counting fails.
///
/// This function provides a convenient way to determine the number of documents in a Juno collection's store.
pub fn count_docs_store(collection: &CollectionKey) -> Result<usize, String> {
pub fn count_collection_docs_store(collection: &CollectionKey) -> Result<usize, String> {
let rule = get_state_rule(collection)?;

match rule.mem() {
Expand Down
14 changes: 7 additions & 7 deletions src/libs/satellite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use junobuild_storage::types::interface::{
// Re-export types

pub use crate::db::store::{
count_docs_store, delete_doc_store, delete_docs_store, get_doc_store, list_docs_store,
count_collection_docs_store, delete_doc_store, delete_docs_store, get_doc_store, list_docs_store,
set_doc_store,
};
use crate::db::types::config::DbConfig;
Expand All @@ -50,7 +50,7 @@ pub use crate::logs::loggers::{
pub use crate::logs::types::logs::{Log, LogLevel};
pub use crate::storage::handlers::set_asset_handler;
pub use crate::storage::store::{
count_assets_store, delete_asset_store, delete_assets_store, get_asset_store,
count_collection_assets_store, delete_asset_store, delete_assets_store, get_asset_store,
get_content_chunks_store, list_assets_store,
};
pub use crate::types::hooks::{
Expand Down Expand Up @@ -144,8 +144,8 @@ pub fn del_docs(collection: CollectionKey) {

#[doc(hidden)]
#[query(guard = "caller_is_controller")]
pub fn count_docs(collection: CollectionKey) -> usize {
satellite::count_docs(collection)
pub fn count_collection_docs(collection: CollectionKey) -> usize {
satellite::count_collection_docs(collection)
}

/// Rules
Expand Down Expand Up @@ -336,8 +336,8 @@ pub fn del_assets(collection: CollectionKey) {

#[doc(hidden)]
#[query(guard = "caller_is_controller")]
pub fn count_assets(collection: CollectionKey) -> usize {
satellite::count_assets(collection)
pub fn count_collection_assets(collection: CollectionKey) -> usize {
satellite::count_collection_assets(collection)
}

#[doc(hidden)]
Expand Down Expand Up @@ -395,7 +395,7 @@ pub fn memory_size() -> MemorySize {
macro_rules! include_satellite {
() => {
use junobuild_satellite::{
commit_asset_upload, count_assets, count_docs, del_asset, del_assets, del_controllers,
commit_asset_upload, count_collection_assets, count_collection_docs, del_asset, del_assets, del_controllers,
del_custom_domain, del_doc, del_docs, del_many_assets, del_many_docs, del_rule,
deposit_cycles, get_asset, get_auth_config, get_config, get_db_config, get_doc,
get_many_assets, get_many_docs, get_storage_config, http_request,
Expand Down
12 changes: 6 additions & 6 deletions src/libs/satellite/src/satellite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::controllers::store::{
set_controllers as set_controllers_store,
};
use crate::db::store::{
count_docs_store, delete_doc_store, delete_docs_store, get_config_store as get_db_config_store,
count_collection_docs_store, delete_doc_store, delete_docs_store, get_config_store as get_db_config_store,
get_doc_store, list_docs_store, set_config_store as set_db_config_store, set_doc_store,
};
use crate::db::types::config::DbConfig;
Expand All @@ -25,7 +25,7 @@ use crate::rules::store::{
};
use crate::storage::certified_assets::upgrade::defer_init_certified_assets;
use crate::storage::store::{
commit_batch_store, count_assets_store, create_batch_store, create_chunk_store,
commit_batch_store, count_collection_assets_store, create_batch_store, create_chunk_store,
delete_asset_store, delete_assets_store, delete_domain_store, get_asset_store,
get_config_store as get_storage_config_store, get_custom_domains_store, list_assets_store,
set_config_store as set_storage_config_store, set_domain_store,
Expand Down Expand Up @@ -205,8 +205,8 @@ pub fn del_docs(collection: CollectionKey) {
}
}

pub fn count_docs(collection: CollectionKey) -> usize {
let result = count_docs_store(&collection);
pub fn count_collection_docs(collection: CollectionKey) -> usize {
let result = count_collection_docs_store(&collection);

match result {
Ok(value) => value,
Expand Down Expand Up @@ -439,8 +439,8 @@ pub fn del_assets(collection: CollectionKey) {
}
}

pub fn count_assets(collection: CollectionKey) -> usize {
let result = count_assets_store(&collection);
pub fn count_collection_assets(collection: CollectionKey) -> usize {
let result = count_collection_assets_store(&collection);

match result {
Ok(value) => value,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/satellite/src/storage/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ fn delete_assets_impl(
/// - `Err(String)`: An error message if counting fails.
///
/// This function provides a convenient way to determine the number of assets in a Juno collection's store.
pub fn count_assets_store(collection: &CollectionKey) -> Result<usize, String> {
pub fn count_collection_assets_store(collection: &CollectionKey) -> Result<usize, String> {
let rule = get_state_rule(collection)?;

match rule.mem() {
Expand Down
4 changes: 2 additions & 2 deletions src/satellite/satellite.did
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ type UploadChunk = record {
type UploadChunkResult = record { chunk_id : nat };
service : () -> {
commit_asset_upload : (CommitBatch) -> ();
count_assets : (text) -> (nat64) query;
count_docs : (text) -> (nat64) query;
count_collection_assets : (text) -> (nat64) query;
count_collection_docs : (text) -> (nat64) query;
del_asset : (text, text) -> ();
del_assets : (text) -> ();
del_controllers : (DeleteControllersArgs) -> (
Expand Down
4 changes: 2 additions & 2 deletions src/tests/satellite.datastore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ describe.each([{ memory: { Heap: null } }, { memory: { Stable: null } }])(
});

it('should delete all documents', async () => {
const { del_docs, count_docs } = actor;
const { del_docs, count_collection_docs } = actor;

await del_docs(TEST_COLLECTION);

const count = await count_docs(TEST_COLLECTION);
const count = await count_collection_docs(TEST_COLLECTION);

expect(count).toBe(0n);
});
Expand Down
8 changes: 4 additions & 4 deletions src/tests/satellite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ describe('Satellite', () => {
});

it('should throw errors on counting docs', async () => {
const { count_docs } = actor;
const { count_collection_docs } = actor;

await expect(count_docs('test')).rejects.toThrow(CONTROLLER_ERROR_MSG);
await expect(count_collection_docs('test')).rejects.toThrow(CONTROLLER_ERROR_MSG);
});

it('should throw errors on deleting assets', async () => {
Expand All @@ -264,9 +264,9 @@ describe('Satellite', () => {
});

it('should throw errors on counting assets', async () => {
const { count_assets } = actor;
const { count_collection_assets } = actor;

await expect(count_assets('test')).rejects.toThrow(CONTROLLER_ERROR_MSG);
await expect(count_collection_assets('test')).rejects.toThrow(CONTROLLER_ERROR_MSG);
});

it('should throw errors on deposit cycles', async () => {
Expand Down

0 comments on commit b1ad2b9

Please sign in to comment.