This commit is contained in:
JMARyA 2024-09-27 09:19:32 +02:00
parent 58f5b1d93c
commit f074727130
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 101 additions and 22 deletions

View file

@ -20,4 +20,4 @@ uuid = { version = "1.8.0", features = ["v4"] }
mongod = { git = "https://git.hydrar.de/jmarya/mongod" }
env_logger = "0.11.5"
walkdir = "2.5.0"
reqwest = { version = "0.11", features = ["json"] }
reqwest = { version = "0.11", features = ["json"] }

View file

@ -133,15 +133,21 @@ 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
};
let t = Transaction::active_expired(days).await;
Ok(json!(t))

View file

@ -83,4 +83,4 @@ pub async fn item_stat_route(
"total_transactions": transaction_count,
"total_price": total_price
}))
}
}

View file

@ -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)
}

View file

@ -97,28 +97,45 @@ impl Transaction {
pub async fn is_expired_at(&self, time: i64) -> bool {
if let Some(expiry) = Item::get(&self.item)
.await
.unwrap()
.variant(&self.variant)
.unwrap()
.expiry
{
let date_added = self.timestamp;
.await
.unwrap()
.variant(&self.variant)
.unwrap()
.expiry
{
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 {
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
}

View file

@ -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
})
}