Skip to content

Commit

Permalink
Add write_query scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
ThetaSinner committed May 29, 2024
1 parent c682964 commit 7059745
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 7 deletions.
71 changes: 70 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [
"scenarios/zome_call_single_value",
"scenarios/first_call",
"scenarios/write_read",
"scenarios/write_query",

"zomes/return_single_value/coordinator",
"zomes/crud/coordinator",
Expand Down Expand Up @@ -61,8 +62,7 @@ url = "2.5.0"
tabled = "0.15.0"
indicatif = "0.17.8"
# TODO waiting for 0.7.3+ relase to use the new reqwest-client-native-tls-vendored feature
# TODO waiting for Holochain 0.3 to add the feature `serde` back here, conflicts at 0.2
influxdb = { version = "0.7.3-beta.1", package = "ts_influxdb", default-features = false, features = ["reqwest-client-native-tls-vendored"] }
influxdb = { version = "0.7.3-beta.1", package = "ts_influxdb", features = ["reqwest-client-native-tls-vendored"] }
influxive-core = "0.0.2-alpha.1"
nanoid = "0.4.0"
which = "6.0.1"
Expand Down
2 changes: 1 addition & 1 deletion framework/instruments/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ anyhow = { workspace = true }
opentelemetry_api = { workspace = true }
parking_lot = { workspace = true }
tabled = { workspace = true }
influxdb = { workspace = true, default-features = false }
influxdb = { workspace = true }
tokio = { workspace = true }
log = { workspace = true }
influxive-core = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion scenarios/first_call/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## app_install
## first_call

### Description

Expand Down
24 changes: 24 additions & 0 deletions scenarios/write_query/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "write_query"
version = "0.1.0"
edition = "2021"
build = "../scenario_build.rs"

[dependencies]
anyhow = { workspace = true }
holochain_types = { workspace = true }
holochain_wind_tunnel_runner = { workspace = true }

[build-dependencies]
happ_builder = { workspace = true }

[lints]
workspace = true

[package.metadata.required-dna]
name = "crud"
zomes = ["crud"]

[package.metadata.required-happ]
name = "crud"
dnas = ["crud"]
11 changes: 11 additions & 0 deletions scenarios/write_query/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## write_query

### Description

Creates an entry, then queries the source chain and performs a simple operation on the entries, then repeat.

### Suggested command

```bash
RUST_LOG=info cargo run --package write_query -- --connection-string ws://localhost:8888 --duration 300
```
66 changes: 66 additions & 0 deletions scenarios/write_query/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use holochain_types::prelude::ActionHash;
use holochain_wind_tunnel_runner::prelude::*;
use holochain_wind_tunnel_runner::scenario_happ_path;
use std::path::Path;

#[derive(Debug, Default)]
struct ScenarioValues {
call_count: u32,
}

impl UserValuesConstraint for ScenarioValues {}


fn setup(ctx: &mut RunnerContext<HolochainRunnerContext>) -> HookResult {
configure_app_ws_url(ctx)?;
Ok(())
}

fn agent_setup(
ctx: &mut AgentContext<HolochainRunnerContext, HolochainAgentContext<ScenarioValues>>,
) -> HookResult {
install_app(ctx, scenario_happ_path!("crud"), &"crud".to_string())?;

Ok(())
}

fn agent_behaviour(
ctx: &mut AgentContext<HolochainRunnerContext, HolochainAgentContext<ScenarioValues>>,
) -> HookResult {
let _: ActionHash = call_zome(
ctx,
"crud",
"create_sample_entry",
"this is a test entry value",
)?;

let response: u32 = call_zome(ctx, "crud", "chain_query_count_len", ())?;

let values = &mut ctx.get_mut().scenario_values;
values.call_count += 1;

// Minimal check that we're querying the right content and getting the expected result from the
// calculation in this zome function.
assert_eq!(values.call_count * 26, response, "Expected call count to match response");

Ok(())
}

fn main() -> WindTunnelResult<()> {
let builder =
ScenarioDefinitionBuilder::<HolochainRunnerContext, HolochainAgentContext<ScenarioValues>>::new_with_init(
env!("CARGO_PKG_NAME"),
)
.with_default_duration_s(60)
.use_setup(setup)
.use_agent_setup(agent_setup)
.use_agent_behaviour(agent_behaviour)
.use_agent_teardown(|ctx| {
uninstall_app(ctx, None).ok();
Ok(())
});

run(builder)?;

Ok(())
}
2 changes: 1 addition & 1 deletion scenarios/write_read/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## single_write_many_read
## write_read

### Description

Expand Down
17 changes: 16 additions & 1 deletion zomes/crud/coordinator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crud_integrity::{EntryTypes, SampleEntry};
use crud_integrity::{EntryTypes, UnitEntryTypes, SampleEntry};
use hdk::prelude::*;

#[hdk_extern]
Expand All @@ -24,3 +24,18 @@ fn create_sample_entry(value: String) -> ExternResult<ActionHash> {
fn get_sample_entry(hash: ActionHash) -> ExternResult<Option<Record>> {
get(hash, GetOptions::local())
}

#[hdk_extern]
fn chain_query_count_len() -> ExternResult<u32> {
let q = ChainQueryFilter::new()
.include_entries(true)
.entry_type(UnitEntryTypes::SampleEntry.try_into()?);
let results = query(q)?;

let sum = results.into_iter().filter_map(|r| {
let se: SampleEntry = r.entry.into_option().unwrap().try_into().ok()?;
Some(se)
}).map(|se| se.value.len()).fold(0, |acc, len| acc + len);

Ok(sum as u32)
}

0 comments on commit 7059745

Please sign in to comment.