stat
This commit is contained in:
parent
58f5b1d93c
commit
f074727130
6 changed files with 101 additions and 22 deletions
|
@ -1,3 +1,6 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use futures::StreamExt;
|
||||
use mongod::{Model, Sort};
|
||||
use mongodb::bson::doc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -274,15 +277,65 @@ impl Variant {
|
|||
(false, 0)
|
||||
}
|
||||
|
||||
pub async fn stat(&self) -> serde_json::Value {
|
||||
pub async fn stat(&self, full: bool) -> serde_json::Value {
|
||||
let active_transactions = self.inventory().await;
|
||||
|
||||
// fix : ignores currency
|
||||
let total_price: f64 = active_transactions.iter().map(|x| x.price.value).sum();
|
||||
|
||||
if !full {
|
||||
return json!({
|
||||
"amount": active_transactions.len(),
|
||||
"total_price": total_price
|
||||
});
|
||||
}
|
||||
|
||||
let all_transactions = Transaction::find(
|
||||
doc! { "item": &self.item, "variant": &self.variant},
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut expired_count = 0.0;
|
||||
|
||||
for t in &all_transactions {
|
||||
if t.is_expired().await {
|
||||
expired_count += 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
let expiry_rate = expired_count / all_transactions.len() as f64;
|
||||
let mut origin_stat = HashMap::new();
|
||||
|
||||
for origin in self.get_unique_origins().await {
|
||||
let transactions_from_origin = active_transactions
|
||||
.iter()
|
||||
.filter(|x| x.origin.as_ref().map(|x| *x == origin).unwrap_or(false))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let prices = self
|
||||
.price_history_by_origin(&origin, None)
|
||||
.await
|
||||
.into_iter()
|
||||
.map(|x| x.value)
|
||||
.collect::<Vec<_>>();
|
||||
let prices_len = prices.len() as f64;
|
||||
let prices_summed = prices.into_iter().reduce(|acc, e| acc + e).unwrap_or(0.0);
|
||||
|
||||
let stat_json = json!({
|
||||
"average_price": prices_summed / prices_len,
|
||||
"inventory": transactions_from_origin.len()
|
||||
});
|
||||
|
||||
origin_stat.insert(origin, stat_json);
|
||||
}
|
||||
|
||||
json!({
|
||||
"amount": active_transactions.len(),
|
||||
"total_price": total_price
|
||||
"amount": active_transactions.len(),
|
||||
"total_price": total_price,
|
||||
"expiry_rate": expiry_rate,
|
||||
"origins": origin_stat
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue