cdb/src/variant.rs

378 lines
11 KiB
Rust
Raw Normal View History

2024-09-27 09:19:32 +02:00
use std::collections::HashMap;
use futures::StreamExt;
2024-09-12 13:11:44 +02:00
use mongod::{Model, Sort};
2024-05-03 18:22:59 +02:00
use mongodb::bson::doc;
2024-07-24 16:38:56 +02:00
use serde::{Deserialize, Serialize};
2024-06-22 02:05:22 +02:00
use serde_json::json;
2024-05-03 18:22:59 +02:00
2024-08-28 09:38:10 +02:00
use crate::transaction::{Price, Transaction};
2024-05-03 18:22:59 +02:00
2024-08-28 09:38:10 +02:00
pub fn sort_by_timestamp() -> mongodb::bson::Document {
doc! { "timestamp": mongod::Sort::Descending }
2024-08-28 09:14:33 +02:00
}
pub fn timestamp_range(year: i32, month: u32) -> (i64, i64) {
let d = chrono::NaiveDate::from_ymd_opt(year, month, 0).unwrap();
let t = chrono::NaiveTime::from_hms_milli_opt(0, 0, 0, 0).unwrap();
let start = chrono::NaiveDateTime::new(d, t).and_utc().timestamp();
assert!(month <= 12);
let end = if month == 12 {
let d = chrono::NaiveDate::from_ymd_opt(year + 1, month, 0).unwrap();
let t = chrono::NaiveTime::from_hms_milli_opt(0, 0, 0, 0).unwrap();
chrono::NaiveDateTime::new(d, t).and_utc().timestamp()
} else {
let d = chrono::NaiveDate::from_ymd_opt(year, month + 1, 0).unwrap();
let t = chrono::NaiveTime::from_hms_milli_opt(0, 0, 0, 0).unwrap();
chrono::NaiveDateTime::new(d, t).and_utc().timestamp()
};
(start, end)
}
2024-05-03 18:22:59 +02:00
/// Represents a specific instance of an item with potential variations.
///
/// This struct is used to describe a particular variation or instance of an item
/// in the real world. It may include attributes or properties that deviate from
/// the standard definition of the item. For example, different colors, sizes, or
/// configurations.
2024-07-24 16:38:56 +02:00
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
2024-05-03 18:22:59 +02:00
pub struct Variant {
2024-05-10 11:59:05 +02:00
/// Associated Item
2024-05-03 18:22:59 +02:00
pub item: String,
2024-08-28 09:38:10 +02:00
/// Variant ID
2024-05-03 18:22:59 +02:00
pub variant: String,
2024-08-28 09:38:10 +02:00
/// Variant Name
pub name: String,
2024-08-30 14:13:56 +02:00
/// Minimum amount
pub min: Option<i64>,
/// Days until expiry
pub expiry: Option<i64>,
2024-05-03 18:22:59 +02:00
}
impl Variant {
2024-05-10 11:59:05 +02:00
/// Create variant from itemdb yaml
2024-05-06 08:24:23 +02:00
pub fn from_yml(json: &serde_yaml::Value, variant: &str, item: &str) -> Self {
Self {
item: item.to_string(),
variant: variant.to_string(),
2024-08-28 09:38:10 +02:00
name: json
2024-05-06 08:24:23 +02:00
.as_mapping()
.unwrap()
2024-08-28 09:38:10 +02:00
.get("name")
.unwrap()
.as_str()
2024-05-10 10:56:07 +02:00
.unwrap()
2024-08-28 09:38:10 +02:00
.to_string(),
2024-08-30 14:13:56 +02:00
min: json
.as_mapping()
.unwrap()
.get("min")
.map(|x| x.as_i64().unwrap()),
expiry: json
.as_mapping()
.unwrap()
.get("expiry")
.map(|x| x.as_i64().unwrap()),
2024-05-06 08:24:23 +02:00
}
}
2024-09-13 14:17:55 +02:00
pub fn item_variant_id(&self) -> String {
format!("{}::{}", self.item, self.variant)
}
2024-09-12 13:11:44 +02:00
/// Returns the IDs of Transactions from this Item Variant.
2024-05-10 11:59:05 +02:00
pub async fn supply_log(&self) -> Vec<String> {
let filter = doc! {
"item": &self.item,
"variant": &self.variant
};
2024-08-28 09:14:33 +02:00
let result = Transaction::find_partial(filter, json!({}), None, None)
2024-08-13 04:13:38 +02:00
.await
.unwrap();
2024-05-10 11:59:05 +02:00
let mut ret = Vec::new();
for doc in result {
2024-07-24 16:38:56 +02:00
ret.push(doc._id);
2024-05-10 11:59:05 +02:00
}
ret
}
2024-09-12 13:11:44 +02:00
/// Returns the active Transaction of this Item Variant which are not yet consumed.
2024-08-13 04:13:38 +02:00
pub async fn inventory(&self) -> Vec<Transaction> {
let filter = doc! {
"item": &self.item,
"variant": &self.variant,
2024-08-30 14:13:56 +02:00
"consumed": { "$not": { "$type": "object" } }
2024-08-13 04:13:38 +02:00
};
2024-08-28 09:14:33 +02:00
Transaction::find(filter, None, None).await.unwrap()
2024-08-13 04:13:38 +02:00
}
2024-09-12 13:11:44 +02:00
/// Returns the IDs of the Transactions from this Item Variant which are consumed.
pub async fn demand_log(&self, destination: Option<&str>) -> Vec<String> {
let filter = if let Some(dest) = destination {
doc! {
"item": &self.item,
"variant": &self.variant,
"consumed": { "destination": dest }
}
} else {
doc! {
"item": &self.item,
"variant": &self.variant,
"consumed": { "$type": "object" }
}
2024-05-10 11:59:05 +02:00
};
2024-08-28 09:14:33 +02:00
let result = Transaction::find_partial(filter, json!({}), None, None)
2024-08-13 04:13:38 +02:00
.await
.unwrap();
2024-05-10 11:59:05 +02:00
let mut ret = Vec::new();
for doc in result {
2024-07-24 16:38:56 +02:00
ret.push(doc._id);
2024-05-10 11:59:05 +02:00
}
ret
}
2024-09-12 11:48:09 +02:00
pub async fn demand(uuid: &str, price: Price, destination: &str) -> Option<Transaction> {
2024-07-24 16:38:56 +02:00
// check if transaction exists
let mut t = Transaction::get(uuid).await?;
2024-09-12 11:48:09 +02:00
t = t.consume(price, destination).await;
Some(t)
2024-05-03 18:22:59 +02:00
}
/// Records a supply transaction in the database.
///
/// # Arguments
///
/// * `price` - The price of the supplied items.
/// * `origin` - The origin or source of the supplied items.
///
/// # Returns
///
/// Returns a UUID string representing the transaction.
2024-09-02 18:40:02 +02:00
pub async fn supply(
&self,
price: Price,
origin: Option<&str>,
location: Option<&str>,
2024-09-21 01:38:22 +02:00
note: Option<&str>,
2024-09-12 11:48:09 +02:00
) -> Transaction {
2024-09-21 01:38:22 +02:00
let t = Transaction::new(&self.item, &self.variant, price, origin, location, note).await;
2024-05-03 18:22:59 +02:00
2024-07-24 16:38:56 +02:00
t.insert().await.unwrap();
2024-05-10 10:56:07 +02:00
2024-09-12 11:48:09 +02:00
t
2024-05-03 18:22:59 +02:00
}
2024-05-10 10:56:07 +02:00
2024-09-12 13:11:44 +02:00
/// Returns all Transactions of this Item Variant
2024-08-28 09:14:33 +02:00
pub async fn get_all_transactions(&self) -> Vec<Transaction> {
let filter = doc! {
"item": &self.item,
"variant": &self.variant
};
2024-09-12 13:11:44 +02:00
Transaction::find(filter, None, Some(doc! { "timestamp": Sort::Descending }))
.await
.unwrap()
2024-08-28 09:14:33 +02:00
}
pub async fn get_transaction_timeslice(&self, year: i32, month: u32) -> Vec<Transaction> {
let (start, end) = timestamp_range(year, month);
Transaction::find(
doc! {
"timestamp": {
"$gte": start,
"$lte": end
}
},
None,
2024-08-28 09:38:10 +02:00
Some(sort_by_timestamp()),
2024-08-28 09:14:33 +02:00
)
.await
.unwrap()
}
pub async fn get_unique_origins(&self) -> Vec<String> {
2024-09-12 16:55:36 +02:00
unique_flows(
&Transaction::unique(
doc! {
"item": &self.item,
"variant": &self.variant
},
"origin",
)
.await,
2024-08-28 09:14:33 +02:00
)
}
pub async fn get_unique_destinations(&self) -> Vec<String> {
2024-09-12 16:55:36 +02:00
unique_flows(
&Transaction::unique(
doc! {
"item": &self.item,
"variant": &self.variant
},
"consumed.destination",
)
.await,
2024-08-28 09:14:33 +02:00
)
}
2024-09-16 08:10:26 +02:00
pub async fn price_history_by_origin(&self, origin: &str, limit: Option<i64>) -> Vec<Price> {
2024-08-30 14:13:56 +02:00
Transaction::find(
doc! {
"item": &self.item,
"variant": &self.variant,
"origin": origin
},
2024-09-16 08:10:26 +02:00
limit,
2024-08-30 14:13:56 +02:00
Some(sort_by_timestamp()),
)
.await
.unwrap()
.into_iter()
.map(|x| x.price)
.collect()
}
2024-08-28 09:14:33 +02:00
pub async fn get_latest_price(&self, origin: Option<String>) -> Price {
let mut filter = doc! {
"item": &self.item,
"variant": &self.variant
};
if let Some(origin) = origin {
filter.insert("origin", origin);
}
2024-08-28 09:38:10 +02:00
Transaction::find(filter, Some(1), Some(sort_by_timestamp()))
2024-08-28 09:14:33 +02:00
.await
.unwrap()
.first()
.unwrap()
.price
.clone()
}
2024-09-12 10:58:49 +02:00
/// Check if item variant is below minimum. Returns if this is the case and the number needed to fulfill minimum
pub async fn is_below_min(&self) -> (bool, i64) {
if let Some(min) = self.min {
let amount = self.inventory().await.len() as i64;
if amount < min {
return (true, min - amount);
}
}
(false, 0)
}
2024-09-27 09:19:32 +02:00
pub async fn stat(&self, full: bool) -> serde_json::Value {
2024-08-27 23:33:09 +02:00
let active_transactions = self.inventory().await;
// fix : ignores currency
let total_price: f64 = active_transactions.iter().map(|x| x.price.value).sum();
2024-09-27 09:19:32 +02:00
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);
}
2024-08-27 23:33:09 +02:00
json!({
2024-09-27 09:19:32 +02:00
"amount": active_transactions.len(),
"total_price": total_price,
"expiry_rate": expiry_rate,
"origins": origin_stat
2024-08-27 23:33:09 +02:00
})
}
2024-06-22 02:05:22 +02:00
pub fn api_json(&self) -> serde_json::Value {
json!({
"item": self.item,
"variant": self.variant,
2024-08-30 14:13:56 +02:00
"name": self.name,
"min": self.min,
"expiry": self.expiry
2024-06-22 02:05:22 +02:00
})
}
2024-05-03 18:22:59 +02:00
}
2024-09-12 16:55:36 +02:00
pub fn unique_flows(i: &[String]) -> Vec<String> {
let mut unique_vec: Vec<String> = Vec::new();
for s in i {
// Check if the string starts with "flow::"
if let Some(suffix) = s.strip_prefix("flow::") {
// Extract the part after "flow::" and split on "::" to get the kind (ignoring id)
let parts: Vec<&str> = suffix.split("::").collect();
2024-09-23 09:26:11 +02:00
if let Some(kind) = parts.first() {
2024-09-12 16:55:36 +02:00
// Build the common prefix "flow::kind"
let common_prefix = format!("flow::{}", kind);
if !unique_vec.contains(&common_prefix) {
unique_vec.push(common_prefix);
}
}
} else {
// If the string doesn't start with "flow::", retain it
// Assumption: Except "flow::" values, everything should be already unique
unique_vec.push(s.to_string());
}
}
unique_vec
}