stat
This commit is contained in:
parent
58f5b1d93c
commit
f074727130
6 changed files with 101 additions and 22 deletions
|
@ -20,4 +20,4 @@ uuid = { version = "1.8.0", features = ["v4"] }
|
||||||
mongod = { git = "https://git.hydrar.de/jmarya/mongod" }
|
mongod = { git = "https://git.hydrar.de/jmarya/mongod" }
|
||||||
env_logger = "0.11.5"
|
env_logger = "0.11.5"
|
||||||
walkdir = "2.5.0"
|
walkdir = "2.5.0"
|
||||||
reqwest = { version = "0.11", features = ["json"] }
|
reqwest = { version = "0.11", features = ["json"] }
|
||||||
|
|
|
@ -133,15 +133,21 @@ pub async fn unique_field_route(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/items/expired?<days>")]
|
#[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);
|
check_auth!(t, c);
|
||||||
|
|
||||||
let days = if let Some(days) = days {
|
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 {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let t = Transaction::active_expired(days).await;
|
let t = Transaction::active_expired(days).await;
|
||||||
|
|
||||||
Ok(json!(t))
|
Ok(json!(t))
|
||||||
|
|
|
@ -83,4 +83,4 @@ pub async fn item_stat_route(
|
||||||
"total_transactions": transaction_count,
|
"total_transactions": transaction_count,
|
||||||
"total_price": total_price
|
"total_price": total_price
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,10 +130,11 @@ pub async fn inventory_route_variant(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns statistics for the Item 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(
|
pub async fn variant_stat_route(
|
||||||
item_id: &str,
|
item_id: &str,
|
||||||
variant_id: &str,
|
variant_id: &str,
|
||||||
|
full: Option<&str>,
|
||||||
itemdb: &State<ItemDB>,
|
itemdb: &State<ItemDB>,
|
||||||
t: Token,
|
t: Token,
|
||||||
c: &State<Config>,
|
c: &State<Config>,
|
||||||
|
@ -146,5 +147,7 @@ pub async fn variant_stat_route(
|
||||||
.variant(variant_id)
|
.variant(variant_id)
|
||||||
.ok_or_else(variant_does_not_exist_error)?;
|
.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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,28 +97,45 @@ impl Transaction {
|
||||||
|
|
||||||
pub async fn is_expired_at(&self, time: i64) -> bool {
|
pub async fn is_expired_at(&self, time: i64) -> bool {
|
||||||
if let Some(expiry) = Item::get(&self.item)
|
if let Some(expiry) = Item::get(&self.item)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.variant(&self.variant)
|
.variant(&self.variant)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.expiry
|
.expiry
|
||||||
{
|
{
|
||||||
let date_added = self.timestamp;
|
let date_added = self.timestamp;
|
||||||
|
|
||||||
let expiration_ts = expiry * 24 * 60 * 60;
|
let expiration_ts = expiry * 24 * 60 * 60;
|
||||||
|
|
||||||
return (date_added + expiration_ts) < time;
|
return (date_added + expiration_ts) < time;
|
||||||
}
|
}
|
||||||
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn is_expired_in_days(&self, days: i64) -> bool {
|
pub async fn is_expired_in_days(&self, days: i64) -> bool {
|
||||||
let current_time = chrono::Utc::now().timestamp();
|
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 {
|
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();
|
let current_time = chrono::Utc::now().timestamp();
|
||||||
self.is_expired_at(current_time).await
|
self.is_expired_at(current_time).await
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use futures::StreamExt;
|
||||||
use mongod::{Model, Sort};
|
use mongod::{Model, Sort};
|
||||||
use mongodb::bson::doc;
|
use mongodb::bson::doc;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -274,15 +277,65 @@ impl Variant {
|
||||||
(false, 0)
|
(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;
|
let active_transactions = self.inventory().await;
|
||||||
|
|
||||||
// fix : ignores currency
|
// fix : ignores currency
|
||||||
let total_price: f64 = active_transactions.iter().map(|x| x.price.value).sum();
|
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!({
|
json!({
|
||||||
"amount": active_transactions.len(),
|
"amount": active_transactions.len(),
|
||||||
"total_price": total_price
|
"total_price": total_price,
|
||||||
|
"expiry_rate": expiry_rate,
|
||||||
|
"origins": origin_stat
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue