Skip to content

Commit 787962b

Browse files
committed
Tidy up
1 parent 584d875 commit 787962b

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

example_apps/aggregations/src/aggregate.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::collections::HashMap;
1516
use swimos::{
1617
agent::agent_lifecycle::utility::HandlerContext,
1718
agent::event_handler::EventHandler,
19+
agent::event_handler::HandlerActionExt,
20+
agent::lanes::ValueLane,
1821
agent::lanes::{CommandLane, JoinValueLane},
1922
agent::AgentLaneModel,
2023
agent::{lifecycle, projections},
@@ -24,6 +27,7 @@ use swimos::{
2427
#[projections]
2528
pub struct AggregateAgent {
2629
aggregated: JoinValueLane<String, f64>,
30+
average_speed: ValueLane<f64>,
2731
register: CommandLane<String>,
2832
}
2933

@@ -32,6 +36,21 @@ pub struct AggregateLifecycle;
3236

3337
#[lifecycle(AggregateAgent)]
3438
impl AggregateLifecycle {
39+
#[on_update(aggregated)]
40+
fn aggregated(
41+
&self,
42+
context: HandlerContext<AggregateAgent>,
43+
averages: &HashMap<String, f64>,
44+
_key: String,
45+
_prev: Option<f64>,
46+
_new_value: &f64,
47+
) -> impl EventHandler<AggregateAgent> {
48+
let averages = averages.clone();
49+
context
50+
.effect(move || averages.values().sum::<f64>() / averages.len() as f64)
51+
.and_then(move |average: f64| context.set_value(AggregateAgent::AVERAGE_SPEED, average))
52+
}
53+
3554
#[on_command(register)]
3655
pub fn register(
3756
&self,

example_apps/aggregations/src/area.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ impl Area {
6464
pub struct AreaAgent {
6565
register: CommandLane<u64>,
6666
deregister: CommandLane<u64>,
67-
// car_id -> speed
6867
cars: JoinValueLane<u64, u64>,
6968
average_speed: ValueLane<f64>,
7069
}

example_apps/aggregations/src/car.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,12 @@ impl CarLifecycle {
5454
let area = self.area.clone();
5555

5656
let speed_handler = context.schedule_repeatedly(Duration::from_secs(5), move || {
57-
let mut rng = rand::rngs::OsRng::default();
57+
let mut rng = rand::rngs::OsRng;
5858
Some(context.set_value(CarAgent::SPEED, rng.gen_range(10..=70)))
5959
});
60-
let car_handler = move |car_id: u64| {
61-
context.schedule_repeatedly(Duration::from_secs(1), move || {
60+
let area_handler = move |car_id: u64| {
61+
context.schedule_repeatedly(Duration::from_secs(5), move || {
6262
let area = area.clone();
63-
let car_id = car_id.clone();
64-
6563
let assigned_area = &mut *area.lock().expect("Mutex poisoned");
6664
let old_area = replace(assigned_area, Area::random());
6765

@@ -71,7 +69,7 @@ impl CarLifecycle {
7169
None,
7270
format!("/area/{old_area:?}"),
7371
"deregister".to_string(),
74-
car_id.clone(),
72+
car_id,
7573
);
7674
// register this car with its new assigned area
7775
let register_handler = context.send_command(
@@ -97,7 +95,7 @@ impl CarLifecycle {
9795
let car_id = param.expect("Missing car_id URI parameter");
9896
u64::from_str(car_id.as_str()).expect("Failed to parse car ID into u64")
9997
})
100-
.and_then(car_handler)
98+
.and_then(area_handler)
10199
.followed_by(speed_handler)
102100
}
103101
}

example_apps/aggregations/src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
4343
AreaAgent::default,
4444
AreaLifecycle::default().into_lifecycle(),
4545
);
46-
let aggregate_agent = AgentModel::new(
47-
AggregateAgent::default,
48-
AggregateLifecycle::default().into_lifecycle(),
49-
);
46+
let aggregate_agent =
47+
AgentModel::new(AggregateAgent::default, AggregateLifecycle.into_lifecycle());
5048

5149
let server = ServerBuilder::with_plane_name("Example Plane")
5250
.add_route(RoutePattern::parse_str("/cars/:car_id")?, car_agent)

0 commit comments

Comments
 (0)