Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions .github/workflows/query_stats.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: query_stats

on:
push:
branches: [master]
pull_request:
paths:
- motoko/query_stats/**
- .github/workflows/query_stats.yml

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
motoko-query_stats:
runs-on: ubuntu-24.04
container: ghcr.io/dfinity/icp-dev-env-motoko:0.3.2
env:
ICP_CLI_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
- name: Deploy and test
working-directory: motoko/query_stats
run: |
icp network start -d
icp deploy
make test
20 changes: 0 additions & 20 deletions motoko/query_stats/.devcontainer/devcontainer.json

This file was deleted.

113 changes: 0 additions & 113 deletions motoko/query_stats/BUILD.md

This file was deleted.

17 changes: 17 additions & 0 deletions motoko/query_stats/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.PHONY: test

test:
@echo "=== Test 1/2: load() returns a non-zero timestamp ==="
@result=$$(icp canister call backend load '()') && \
echo "$$result" && \
echo "$$result" | grep -qE '\([0-9][0-9_]* : int\)' && \
echo "PASS" || (echo "FAIL" && exit 1)

@echo "=== Test 2/2: get_current_query_stats_as_string() reports all four stat fields ==="
@result=$$(icp canister call backend get_current_query_stats_as_string '()') && \
echo "$$result" && \
echo "$$result" | grep -q 'Number of calls' && \
echo "$$result" | grep -q 'Number of instructions' && \
echo "$$result" | grep -q 'Request payload bytes' && \
echo "$$result" | grep -q 'Response payload bytes' && \
echo "PASS" || (echo "FAIL" && exit 1)
50 changes: 27 additions & 23 deletions motoko/query_stats/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
# Query stats

## Deploying from ICP Ninja

[![](https://icp.ninja/assets/open.svg)](https://icp.ninja/editor?g=https://github.com/dfinity/examples/tree/master/motoko/query_stats)

## Build and deploy from the command-line

### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install)

### 2. Download your project from ICP Ninja using the 'Download files' button on the upper left corner, or [clone the GitHub examples repository.](https://github.com/dfinity/examples/)

### 3. Navigate into the project's directory.

### 4. Deploy the project to your local environment:

```
dfx start --background --clean && dfx deploy
```

## Security considerations and best practices

If you base your application on this example, it is recommended that you familiarize yourself with and adhere to the [security best practices](https://internetcomputer.org/docs/building-apps/security/overview) for developing on ICP. This example may not implement all the best practices.
# Query Stats

This example demonstrates how a canister can read its own query statistics from the management canister. It uses `ic.canister_status` to retrieve metrics such as the total number of calls, instructions executed, and payload bytes for the canister's query methods.

## Build and deploy from the command line

### Prerequisites
- Node.js
- icp-cli: `npm install -g @icp-sdk/icp-cli @icp-sdk/ic-wasm`

### Install
```bash
git clone https://github.com/dfinity/examples
cd examples/motoko/query_stats
```

### Deploy and test
```bash
icp network start -d
icp deploy
make test
icp network stop
```

## Security considerations and best practices

Refer to the [security best practices](https://docs.internetcomputer.org/guides/security/overview) for information on security and best practices for your ICP app.
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import Nat "mo:core/Nat";
import Time "mo:core/Time";
import Principal "mo:core/Principal";
import Time "mo:core/Time";
import { ic } "mo:ic";

persistent actor QueryStats {

transient let IC = actor "aaaaa-aa" : actor {
canister_status : { canister_id : Principal } -> async {
query_stats : {
num_calls_total : Nat;
num_instructions_total : Nat;
request_payload_bytes_total : Nat;
response_payload_bytes_total : Nat;
};
};
};

public query func load() : async Int {
Time.now();
};

public func get_current_query_stats_as_string() : async Text {
let stats = await IC.canister_status({
let stats = await ic.canister_status({
canister_id = Principal.fromActor(QueryStats);
});
"Number of calls: " # stats.query_stats.num_calls_total.toText() # " - Number of instructions: " # stats.query_stats.num_instructions_total.toText() # " - Request payload bytes: " # stats.query_stats.request_payload_bytes_total.toText() # " - Response payload bytes: " # stats.query_stats.response_payload_bytes_total.toText();
Expand Down
16 changes: 0 additions & 16 deletions motoko/query_stats/dfx.json

This file was deleted.

4 changes: 4 additions & 0 deletions motoko/query_stats/icp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
canisters:
- name: backend
recipe:
type: "@dfinity/motoko@v5.0.0"
19 changes: 10 additions & 9 deletions motoko/query_stats/mops.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Motoko dependencies (https://mops.one/)

[toolchain]
moc = "1.5.1"
moc = "1.9.0"

[dependencies]
core = "2.4.0"

core = "2.5.0"
ic = "4.0.0"

[moc]
# M0236: use context dot notation (e.g. x.toText() instead of Nat.toText(x))
# M0237: redundant explicit implicit arguments (e.g. Nat.compare is inferred automatically)
# M0223: redundant type instantiation (e.g. Array.tabulate instead of Array.tabulate<T>)
args = ["-W=M0236,M0237,M0223"]
# M0236: use context dot notation
# M0237: redundant explicit implicit arguments
# M0223: redundant type instantiation
args = ["--default-persistent-actors", "-W=M0236,M0237,M0223"]

[canisters.backend]
main = "backend/app.mo"
Loading