simplify entire world zcount to one call
This commit is contained in:
parent
318ffde050
commit
a74ae46873
3 changed files with 29 additions and 23 deletions
|
@ -10,35 +10,37 @@ pub struct Query;
|
||||||
|
|
||||||
#[graphql_object(context = Context)]
|
#[graphql_object(context = Context)]
|
||||||
impl Query {
|
impl Query {
|
||||||
fn world(id: ID) -> FieldResult<World> {
|
fn world(id: String) -> FieldResult<World> {
|
||||||
Ok(World { id })
|
Ok(World {
|
||||||
|
world_id: id.clone(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn allWorlds() -> FieldResult<Vec<World>> {
|
fn allWorlds() -> FieldResult<Vec<World>> {
|
||||||
Ok(vec![
|
Ok(vec![
|
||||||
World {
|
World {
|
||||||
id: ID::from("1".to_string()),
|
world_id: "1".to_string(),
|
||||||
},
|
},
|
||||||
World {
|
World {
|
||||||
id: ID::from("10".to_string()),
|
world_id: "10".to_string(),
|
||||||
},
|
},
|
||||||
World {
|
World {
|
||||||
id: ID::from("13".to_string()),
|
world_id: "13".to_string(),
|
||||||
},
|
},
|
||||||
World {
|
World {
|
||||||
id: ID::from("17".to_string()),
|
world_id: "17".to_string(),
|
||||||
},
|
},
|
||||||
World {
|
World {
|
||||||
id: ID::from("19".to_string()),
|
world_id: "19".to_string(),
|
||||||
},
|
},
|
||||||
World {
|
World {
|
||||||
id: ID::from("40".to_string()),
|
world_id: "40".to_string(),
|
||||||
},
|
},
|
||||||
World {
|
World {
|
||||||
id: ID::from("1000".to_string()),
|
world_id: "1000".to_string(),
|
||||||
},
|
},
|
||||||
World {
|
World {
|
||||||
id: ID::from("2000".to_string()),
|
world_id: "2000".to_string(),
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
@ -57,7 +59,7 @@ impl Query {
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(World {
|
Ok(World {
|
||||||
id: ID::from(id.to_string()),
|
world_id: id.to_string(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,21 +22,24 @@ static WORLD_ID_TO_NAME: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct World {
|
pub struct World {
|
||||||
pub id: juniper::ID,
|
pub world_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[graphql_object(context = super::Context)]
|
#[graphql_object(context = super::Context)]
|
||||||
impl World {
|
impl World {
|
||||||
|
pub fn id(&self) -> juniper::ID {
|
||||||
|
juniper::ID::from(self.world_id.clone())
|
||||||
|
}
|
||||||
pub fn name(&self) -> String {
|
pub fn name(&self) -> String {
|
||||||
WORLD_ID_TO_NAME
|
WORLD_ID_TO_NAME
|
||||||
.get(&self.id.to_string().as_str())
|
.get(&self.world_id.to_string().as_str())
|
||||||
.unwrap_or(&"Unknown")
|
.unwrap_or(&"Unknown")
|
||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn population(&self, context: &mut super::Context) -> i32 {
|
pub async fn population(&self, context: &mut super::Context) -> i32 {
|
||||||
let mut con = (*context).con.get().await.unwrap();
|
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()
|
let filter_timestamp = SystemTime::now()
|
||||||
.sub(Duration::from_secs(60 * 15))
|
.sub(Duration::from_secs(60 * 15))
|
||||||
|
@ -44,33 +47,32 @@ impl World {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_secs();
|
.as_secs();
|
||||||
|
|
||||||
let (vs, nc, tr, ns): (i32, i32, i32, i32) = pipe()
|
let pop: u32 = cmd("ZCOUNT")
|
||||||
.zcount(format!("wp:{}/{}", id, 1), filter_timestamp, "+inf")
|
.arg(format!("wp:{}", id))
|
||||||
.zcount(format!("wp:{}/{}", id, 2), filter_timestamp, "+inf")
|
.arg(filter_timestamp)
|
||||||
.zcount(format!("wp:{}/{}", id, 3), filter_timestamp, "+inf")
|
.arg("+inf")
|
||||||
.zcount(format!("wp:{}/{}", id, 4), filter_timestamp, "+inf")
|
|
||||||
.query_async(&mut con)
|
.query_async(&mut con)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
tr + vs + nc + ns
|
pop as i32
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn faction_population(&self) -> FactionPopulation {
|
pub async fn faction_population(&self) -> FactionPopulation {
|
||||||
FactionPopulation {
|
FactionPopulation {
|
||||||
world_id: self.id.clone(),
|
world_id: juniper::ID::from(self.world_id.clone()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn vehicles(&self) -> Vehicles {
|
pub async fn vehicles(&self) -> Vehicles {
|
||||||
Vehicles {
|
Vehicles {
|
||||||
world_id: self.id.clone(),
|
world_id: juniper::ID::from(self.world_id.clone()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn classes(&self) -> Classes {
|
pub async fn classes(&self) -> Classes {
|
||||||
Classes {
|
Classes {
|
||||||
world_id: self.id.clone(),
|
world_id: juniper::ID::from(self.world_id.clone()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,6 +74,8 @@ async fn track_pop(pop_event: PopEvent) {
|
||||||
} = pop_event;
|
} = pop_event;
|
||||||
|
|
||||||
let key = format!("wp:{}/{}", world_id, team_id);
|
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();
|
let _: () = con.zadd(key, character_id, timestamp).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue