fix: get aliases separately
This commit is contained in:
parent
fd7ab070f1
commit
79f966723e
|
@ -11,10 +11,16 @@ pub struct CachedStatistics {
|
||||||
pub seven_days: Statistics,
|
pub seven_days: Statistics,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
pub struct Aliases {
|
||||||
|
#[serde(deserialize_with = "alias_de")]
|
||||||
|
pub aliases: HashMap<u32, Vec<Alias>>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
pub struct Statistics {
|
pub struct Statistics {
|
||||||
pub count: Vec<Count>,
|
pub count: Vec<Count>,
|
||||||
#[serde(deserialize_with = "alias_de")]
|
#[serde(default)]
|
||||||
pub aliases: HashMap<u32, Vec<Alias>>,
|
pub aliases: HashMap<u32, Vec<Alias>>,
|
||||||
pub duties: Vec<DutyInfo>,
|
pub duties: Vec<DutyInfo>,
|
||||||
pub hosts: Vec<HostInfo>,
|
pub hosts: Vec<HostInfo>,
|
||||||
|
|
|
@ -3,7 +3,7 @@ use chrono::{Duration, Utc};
|
||||||
use mongodb::bson::{Document, doc};
|
use mongodb::bson::{Document, doc};
|
||||||
use mongodb::options::AggregateOptions;
|
use mongodb::options::AggregateOptions;
|
||||||
use tokio_stream::StreamExt;
|
use tokio_stream::StreamExt;
|
||||||
use crate::stats::Statistics;
|
use crate::stats::{Aliases, Statistics};
|
||||||
use crate::web::State;
|
use crate::web::State;
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
|
@ -15,19 +15,6 @@ lazy_static::lazy_static! {
|
||||||
"$count": "count",
|
"$count": "count",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
"aliases": [
|
|
||||||
{
|
|
||||||
"$group": {
|
|
||||||
"_id": "$listing.content_id_lower",
|
|
||||||
"aliases": {
|
|
||||||
"$addToSet": {
|
|
||||||
"name": "$listing.name",
|
|
||||||
"home_world": "$listing.home_world",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"duties": [
|
"duties": [
|
||||||
{
|
{
|
||||||
"$group": {
|
"$group": {
|
||||||
|
@ -102,6 +89,26 @@ lazy_static::lazy_static! {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
static ref ALIASES_QUERY: [Document; 1] = [
|
||||||
|
doc! {
|
||||||
|
"$facet": {
|
||||||
|
"aliases": [
|
||||||
|
{
|
||||||
|
"$group": {
|
||||||
|
"_id": "$listing.content_id_lower",
|
||||||
|
"aliases": {
|
||||||
|
"$addToSet": {
|
||||||
|
"name": "$listing.name",
|
||||||
|
"home_world": "$listing.home_world",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_stats(state: &State) -> Result<Statistics> {
|
pub async fn get_stats(state: &State) -> Result<Statistics> {
|
||||||
|
@ -132,6 +139,28 @@ async fn get_stats_internal(state: &State, docs: impl IntoIterator<Item = Docume
|
||||||
.await?;
|
.await?;
|
||||||
let doc = cursor.try_next().await?;
|
let doc = cursor.try_next().await?;
|
||||||
let doc = doc.ok_or_else(|| anyhow::anyhow!("missing document"))?;
|
let doc = doc.ok_or_else(|| anyhow::anyhow!("missing document"))?;
|
||||||
let stats = mongodb::bson::from_document(doc)?;
|
let mut stats: Statistics = mongodb::bson::from_document(doc)?;
|
||||||
|
|
||||||
|
let ids: Vec<u32> = stats.hosts.iter().map(|host| host.content_id_lower).collect();
|
||||||
|
let mut aliases_query: Vec<Document> = ALIASES_QUERY.iter().cloned().collect();
|
||||||
|
aliases_query.insert(0, doc! {
|
||||||
|
"$match": {
|
||||||
|
"listing.content_id_lower": {
|
||||||
|
"$in": ids,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let mut cursor = state
|
||||||
|
.collection()
|
||||||
|
.aggregate(aliases_query, AggregateOptions::builder()
|
||||||
|
.allow_disk_use(true)
|
||||||
|
.build())
|
||||||
|
.await?;
|
||||||
|
let doc = cursor.try_next().await?;
|
||||||
|
let doc = doc.ok_or_else(|| anyhow::anyhow!("missing document"))?;
|
||||||
|
let aliases: Aliases = mongodb::bson::from_document(doc)?;
|
||||||
|
|
||||||
|
stats.aliases = aliases.aliases;
|
||||||
|
|
||||||
Ok(stats)
|
Ok(stats)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user