update
This commit is contained in:
parent
74b07a0ea3
commit
92e2da9cae
5 changed files with 300 additions and 143 deletions
104
src/variant.rs
104
src/variant.rs
|
@ -5,6 +5,30 @@ use serde_json::json;
|
|||
|
||||
use crate::transaction::{Consumed, Price, Transaction};
|
||||
|
||||
pub fn sort_by_timestamp() -> Option<mongodb::bson::Document> {
|
||||
Some(doc! { "timestamp": mongod::Sort::Descending })
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
/// Represents a specific instance of an item with potential variations.
|
||||
///
|
||||
/// This struct is used to describe a particular variation or instance of an item
|
||||
|
@ -55,7 +79,7 @@ impl Variant {
|
|||
"variant": &self.variant
|
||||
};
|
||||
|
||||
let result = Transaction::find_partial(filter, json!({}), None)
|
||||
let result = Transaction::find_partial(filter, json!({}), None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
@ -75,7 +99,7 @@ impl Variant {
|
|||
"consumed": { "$exists": false }
|
||||
};
|
||||
|
||||
Transaction::find(filter, None).await.unwrap()
|
||||
Transaction::find(filter, None, None).await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn demand_log(&self) -> Vec<String> {
|
||||
|
@ -85,7 +109,7 @@ impl Variant {
|
|||
"consumed": { "$exists": true }
|
||||
};
|
||||
|
||||
let result = Transaction::find_partial(filter, json!({}), None)
|
||||
let result = Transaction::find_partial(filter, json!({}), None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
@ -101,12 +125,7 @@ impl Variant {
|
|||
pub async fn demand(uuid: &str, price: Price, destination: &str) -> Option<()> {
|
||||
// check if transaction exists
|
||||
let mut t = Transaction::get(uuid).await?;
|
||||
t.update(&json!({
|
||||
"consumed": Consumed{ destination: destination.to_string(), price }
|
||||
}))
|
||||
.await
|
||||
.ok()?;
|
||||
|
||||
t.consume(price, destination).await;
|
||||
Some(())
|
||||
}
|
||||
|
||||
|
@ -128,6 +147,73 @@ impl Variant {
|
|||
t._id
|
||||
}
|
||||
|
||||
pub async fn get_all_transactions(&self) -> Vec<Transaction> {
|
||||
let filter = doc! {
|
||||
"item": &self.item,
|
||||
"variant": &self.variant
|
||||
};
|
||||
|
||||
Transaction::find(filter, None, None).await.unwrap()
|
||||
}
|
||||
|
||||
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,
|
||||
sort_by_timestamp(),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn get_unique_origins(&self) -> Vec<String> {
|
||||
Transaction::unique(
|
||||
doc! {
|
||||
"item": &self.item,
|
||||
"variant": &self.variant
|
||||
},
|
||||
"origin",
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_unique_destinations(&self) -> Vec<String> {
|
||||
Transaction::unique(
|
||||
doc! {
|
||||
"item": &self.item,
|
||||
"variant": &self.variant
|
||||
},
|
||||
"consumed.destination",
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Transaction::find(filter, Some(1), sort_by_timestamp())
|
||||
.await
|
||||
.unwrap()
|
||||
.first()
|
||||
.unwrap()
|
||||
.price
|
||||
.clone()
|
||||
}
|
||||
|
||||
pub async fn stat(&self) -> serde_json::Value {
|
||||
let active_transactions = self.inventory().await;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue