Skip to content

Commit e4ab128

Browse files
SawchordDfinity-skaestlesesi200JasonJason I
authored
Add simple Rust and Motoko examples for the Query Statistics feature (#857)
* Query stats sample program with timer-based rate support * Fixes * Various minor fixes * Added instructions on how to use dfx to query the canister * Minor cleanup * Minor * Official support from ic-cdk 0.12.1 now sufficient * Update rust/query-stats/dfx.json Co-authored-by: Severin Siffert <[email protected]> * Update rust/query-stats/README.md Co-authored-by: Severin Siffert <[email protected]> * Minor README changes * Added first bit of Motoko example * chore: add CI jobs * Make rust example smaller * Update readme * Rename example folder * Update rust/query_stats/README.md Co-authored-by: Jessie Mongeon <[email protected]> --------- Co-authored-by: Stefan Kaestle <[email protected]> Co-authored-by: Severin Siffert <[email protected]> Co-authored-by: Jason <[email protected]> Co-authored-by: Jason I <[email protected]> Co-authored-by: Jessie Mongeon <[email protected]>
1 parent b7e47bf commit e4ab128

File tree

11 files changed

+757
-0
lines changed

11 files changed

+757
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: motoko-query-stats
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
paths:
8+
- motoko/query_stats/**
9+
- .github/workflows/provision-darwin.sh
10+
- .github/workflows/provision-linux.sh
11+
- .github/workflows/motoko-query-stats.yaml
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: true
15+
jobs:
16+
rust-basic-bitcoin-darwin:
17+
runs-on: macos-12
18+
steps:
19+
- uses: actions/checkout@v2
20+
with:
21+
submodules: recursive
22+
- name: Provision Darwin
23+
run: bash .github/workflows/provision-darwin.sh
24+
- name: Motoko Query Stats Darwin
25+
run: |
26+
dfx start --background
27+
pushd motoko/query_stats
28+
dfx deploy query_stats
29+
popd
30+
rust-basic-bitcoin-linux:
31+
runs-on: ubuntu-20.04
32+
steps:
33+
- uses: actions/checkout@v2
34+
with:
35+
submodules: recursive
36+
- name: Provision Linux
37+
run: bash .github/workflows/provision-linux.sh
38+
- name: Motoko Query Stats Linux
39+
run: |
40+
dfx start --background
41+
pushd motoko/query_stats
42+
dfx deploy query_stats
43+
popd
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: rust-query-stats
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
paths:
8+
- rust/query_stats/**
9+
- .github/workflows/provision-darwin.sh
10+
- .github/workflows/provision-linux.sh
11+
- .github/workflows/rust-query-stats.yaml
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: true
15+
jobs:
16+
rust-basic-bitcoin-darwin:
17+
runs-on: macos-12
18+
steps:
19+
- uses: actions/checkout@v2
20+
with:
21+
submodules: recursive
22+
- name: Provision Darwin
23+
run: bash .github/workflows/provision-darwin.sh
24+
- name: Rust Query Stats Darwin
25+
run: |
26+
dfx start --background
27+
pushd rust/query_stats
28+
dfx deploy query_stats
29+
popd
30+
rust-basic-bitcoin-linux:
31+
runs-on: ubuntu-20.04
32+
steps:
33+
- uses: actions/checkout@v2
34+
with:
35+
submodules: recursive
36+
- name: Provision Linux
37+
run: bash .github/workflows/provision-linux.sh
38+
- name: Rust Query Stats Linux
39+
run: |
40+
dfx start --background
41+
pushd rust/query_stats
42+
dfx deploy query_stats
43+
popd

motoko/query_stats/README.md

Whitespace-only changes.

motoko/query_stats/dfx.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"canisters": {
3+
"query_stats": {
4+
"main": "src/query_stats_backend/main.mo",
5+
"type": "motoko"
6+
}
7+
},
8+
"defaults": {
9+
"build": {
10+
"args": "",
11+
"packtool": ""
12+
}
13+
},
14+
"output_env_file": ".env",
15+
"version": 1
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Nat "mo:base/Nat";
2+
import Time "mo:base/Time";
3+
import Principal "mo:base/Principal";
4+
5+
actor QueryStats {
6+
7+
let IC = actor "aaaaa-aa" : actor {
8+
canister_status : { canister_id : Principal } -> async {
9+
query_stats : {
10+
num_calls_total : Nat;
11+
num_instructions_total : Nat;
12+
request_payload_bytes_total : Nat;
13+
response_payload_bytes_total : Nat;
14+
};
15+
};
16+
};
17+
18+
public query func load() : async Int {
19+
return Time.now();
20+
};
21+
22+
public func get_current_query_stats_as_string() : async Text {
23+
let stats = await IC.canister_status({
24+
canister_id = Principal.fromActor(QueryStats);
25+
});
26+
return "Number of calls: " # Nat.toText(stats.query_stats.num_calls_total) # " - Number of instructions: " # Nat.toText(stats.query_stats.num_instructions_total) # " - Request payload bytes: " # Nat.toText(stats.query_stats.request_payload_bytes_total) # " - Response payload bytes: " # Nat.toText(stats.query_stats.response_payload_bytes_total);
27+
};
28+
};

0 commit comments

Comments
 (0)