fix: get aliases separately

This commit is contained in:
Anna 2021-12-27 19:59:13 -05:00
parent fd7ab070f1
commit 79f966723e
2 changed files with 51 additions and 16 deletions

View File

@ -11,10 +11,16 @@ pub struct CachedStatistics {
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)]
pub struct Statistics {
pub count: Vec<Count>,
#[serde(deserialize_with = "alias_de")]
#[serde(default)]
pub aliases: HashMap<u32, Vec<Alias>>,
pub duties: Vec<DutyInfo>,
pub hosts: Vec<HostInfo>,

View File

@ -3,7 +3,7 @@ use chrono::{Duration, Utc};
use mongodb::bson::{Document, doc};
use mongodb::options::AggregateOptions;
use tokio_stream::StreamExt;
use crate::stats::Statistics;
use crate::stats::{Aliases, Statistics};
use crate::web::State;
lazy_static::lazy_static! {
@ -15,19 +15,6 @@ lazy_static::lazy_static! {
"$count": "count",
},
],
"aliases": [
{
"$group": {
"_id": "$listing.content_id_lower",
"aliases": {
"$addToSet": {
"name": "$listing.name",
"home_world": "$listing.home_world",
},
},
}
}
],
"duties": [
{
"$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> {
@ -132,6 +139,28 @@ async fn get_stats_internal(state: &State, docs: impl IntoIterator<Item = Docume
.await?;
let doc = cursor.try_next().await?;
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)
}