From a74ae46873467ee163224cc0bdee683f347ae6ef Mon Sep 17 00:00:00 2001 From: Katalina Okano Date: Sat, 26 Nov 2022 14:37:13 -0500 Subject: [PATCH] simplify entire world zcount to one call --- services/api/src/graphql/mod.rs | 24 +++++++++++++----------- services/api/src/graphql/types.rs | 26 ++++++++++++++------------ services/websocket/src/main.rs | 2 ++ 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/services/api/src/graphql/mod.rs b/services/api/src/graphql/mod.rs index dc35abb..e5016fe 100644 --- a/services/api/src/graphql/mod.rs +++ b/services/api/src/graphql/mod.rs @@ -10,35 +10,37 @@ pub struct Query; #[graphql_object(context = Context)] impl Query { - fn world(id: ID) -> FieldResult { - Ok(World { id }) + fn world(id: String) -> FieldResult { + Ok(World { + world_id: id.clone(), + }) } fn allWorlds() -> FieldResult> { Ok(vec![ World { - id: ID::from("1".to_string()), + world_id: "1".to_string(), }, World { - id: ID::from("10".to_string()), + world_id: "10".to_string(), }, World { - id: ID::from("13".to_string()), + world_id: "13".to_string(), }, World { - id: ID::from("17".to_string()), + world_id: "17".to_string(), }, World { - id: ID::from("19".to_string()), + world_id: "19".to_string(), }, World { - id: ID::from("40".to_string()), + world_id: "40".to_string(), }, World { - id: ID::from("1000".to_string()), + world_id: "1000".to_string(), }, World { - id: ID::from("2000".to_string()), + world_id: "2000".to_string(), }, ]) } @@ -57,7 +59,7 @@ impl Query { }; Ok(World { - id: ID::from(id.to_string()), + world_id: id.to_string(), }) } diff --git a/services/api/src/graphql/types.rs b/services/api/src/graphql/types.rs index 4f5b218..7c50b3d 100644 --- a/services/api/src/graphql/types.rs +++ b/services/api/src/graphql/types.rs @@ -22,21 +22,24 @@ static WORLD_ID_TO_NAME: Lazy> = Lazy::new(|| { #[derive(Clone, Debug)] pub struct World { - pub id: juniper::ID, + pub world_id: String, } #[graphql_object(context = super::Context)] impl World { + pub fn id(&self) -> juniper::ID { + juniper::ID::from(self.world_id.clone()) + } pub fn name(&self) -> String { WORLD_ID_TO_NAME - .get(&self.id.to_string().as_str()) + .get(&self.world_id.to_string().as_str()) .unwrap_or(&"Unknown") .to_string() } pub async fn population(&self, context: &mut super::Context) -> i32 { let mut con = (*context).con.get().await.unwrap(); - let id = self.id.to_string(); + let id = self.world_id.to_string(); let filter_timestamp = SystemTime::now() .sub(Duration::from_secs(60 * 15)) @@ -44,33 +47,32 @@ impl World { .unwrap() .as_secs(); - let (vs, nc, tr, ns): (i32, i32, i32, i32) = pipe() - .zcount(format!("wp:{}/{}", id, 1), filter_timestamp, "+inf") - .zcount(format!("wp:{}/{}", id, 2), filter_timestamp, "+inf") - .zcount(format!("wp:{}/{}", id, 3), filter_timestamp, "+inf") - .zcount(format!("wp:{}/{}", id, 4), filter_timestamp, "+inf") + let pop: u32 = cmd("ZCOUNT") + .arg(format!("wp:{}", id)) + .arg(filter_timestamp) + .arg("+inf") .query_async(&mut con) .await .unwrap(); - tr + vs + nc + ns + pop as i32 } pub async fn faction_population(&self) -> FactionPopulation { FactionPopulation { - world_id: self.id.clone(), + world_id: juniper::ID::from(self.world_id.clone()), } } pub async fn vehicles(&self) -> Vehicles { Vehicles { - world_id: self.id.clone(), + world_id: juniper::ID::from(self.world_id.clone()), } } pub async fn classes(&self) -> Classes { Classes { - world_id: self.id.clone(), + world_id: juniper::ID::from(self.world_id.clone()), } } } diff --git a/services/websocket/src/main.rs b/services/websocket/src/main.rs index 92ce41a..1677293 100644 --- a/services/websocket/src/main.rs +++ b/services/websocket/src/main.rs @@ -74,6 +74,8 @@ async fn track_pop(pop_event: PopEvent) { } = pop_event; let key = format!("wp:{}/{}", world_id, team_id); + let _: () = con.zadd(key, character_id.clone(), timestamp).unwrap(); + let key = format!("wp:{}", world_id); let _: () = con.zadd(key, character_id, timestamp).unwrap(); }