stat
This commit is contained in:
parent
58f5b1d93c
commit
f074727130
6 changed files with 101 additions and 22 deletions
|
@ -133,11 +133,17 @@ pub async fn unique_field_route(
|
|||
}
|
||||
|
||||
#[get("/items/expired?<days>")]
|
||||
pub async fn expired_items_route(days: Option<&str>, t: Token, c: &State<Config>) -> FallibleApiResponse {
|
||||
pub async fn expired_items_route(
|
||||
days: Option<&str>,
|
||||
t: Token,
|
||||
c: &State<Config>,
|
||||
) -> FallibleApiResponse {
|
||||
check_auth!(t, c);
|
||||
|
||||
let days = if let Some(days) = days {
|
||||
days.parse().map_err(|_| api_error("Invalid days value")).ok()
|
||||
days.parse()
|
||||
.map_err(|_| api_error("Invalid days value"))
|
||||
.ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
|
|
@ -130,10 +130,11 @@ pub async fn inventory_route_variant(
|
|||
}
|
||||
|
||||
/// Returns statistics for the Item Variant
|
||||
#[get("/item/<item_id>/<variant_id>/stat")]
|
||||
#[get("/item/<item_id>/<variant_id>/stat?<full>")]
|
||||
pub async fn variant_stat_route(
|
||||
item_id: &str,
|
||||
variant_id: &str,
|
||||
full: Option<&str>,
|
||||
itemdb: &State<ItemDB>,
|
||||
t: Token,
|
||||
c: &State<Config>,
|
||||
|
@ -146,5 +147,7 @@ pub async fn variant_stat_route(
|
|||
.variant(variant_id)
|
||||
.ok_or_else(variant_does_not_exist_error)?;
|
||||
|
||||
Ok(variant.stat().await)
|
||||
Ok(variant
|
||||
.stat(full.map(|x| x == "1" || x == "true").unwrap_or(false))
|
||||
.await)
|
||||
}
|
||||
|
|
|
@ -115,10 +115,27 @@ impl Transaction {
|
|||
|
||||
pub async fn is_expired_in_days(&self, days: i64) -> bool {
|
||||
let current_time = chrono::Utc::now().timestamp();
|
||||
self.is_expired_at(current_time + (days * 24 * 60 * 60)).await
|
||||
self.is_expired_at(current_time + (days * 24 * 60 * 60))
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn is_expired(&self) -> bool {
|
||||
if self.consumed.is_some() {
|
||||
if let Some(expiry) = Item::get(&self.item)
|
||||
.await
|
||||
.unwrap()
|
||||
.variant(&self.variant)
|
||||
.unwrap()
|
||||
.expiry
|
||||
{
|
||||
let time_around = self.timestamp - self.consumed.as_ref().unwrap().timestamp;
|
||||
let expiration_ts = expiry * 24 * 60 * 60;
|
||||
return time_around > expiration_ts;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
let current_time = chrono::Utc::now().timestamp();
|
||||
self.is_expired_at(current_time).await
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
json!({
|
||||
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,
|
||||
"expiry_rate": expiry_rate,
|
||||
"origins": origin_stat
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue