diff --git a/README.md b/README.md index 375ca19..d2c3df4 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,14 @@ # CDB -cdb is a powerful software tool designed to automate economic calculations by managing a database of items, their relations, quantities, and tracking demands. This tool facilitates the estimation of demand and supply, aiding in making informed decisions on production, imports, and purchases to meet market needs efficiently. +CDB is an inventory database for economic planning. It can manage your inventory and log supply and demand. + +What does the `C` in `CDB` stand for? You can choose: +- eConomicDB +- CommunismDB +- CraftDB ## Features -- Data Input: Start with a root dataset of items and their relations, including details such as item names, categories, and dependencies. -- Inventory Tracking: Record and update the quantities of items within the database based on real-world transactions, production, and consumption. -- Demand Tracking: Monitor the demand for each item, keeping track of how many units are required over time. -- Supply Calculation: Automatically calculate and update the estimated supply needed to meet the current demand. -- Automated Economics: Utilize the collected data to automate economic decisions, determining optimal production levels, imports, and purchases to fulfill market needs. -- Flexible Reporting: Generate reports and visualizations to analyze the economic trends \ No newline at end of file +- ItemDB: Root Dataset, Items, Variants, Categories +- Inventory: Transactions, Supply, Demand +- Planned Supply: Calculate Supply Needs based on Demand +- Automation +- Stats: Statistics, Graphs, Reports diff --git a/docs/Transaction.md b/docs/Transaction.md index 28a247b..f6ce96e 100644 --- a/docs/Transaction.md +++ b/docs/Transaction.md @@ -1,3 +1,3 @@ # Transaction -A transaction can be either a supply, demand or batch transaction. - +A Transaction represents an actual instance of an Item. This is a one to one mapping to a physical Variant of an Item. It can be consumed either manually or by a Process. +This makes individual physical goods traceable. diff --git a/schema/item.json b/schema/item.json new file mode 100644 index 0000000..18df9cc --- /dev/null +++ b/schema/item.json @@ -0,0 +1,23 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "title": "Item", + "description": "Item Metadata", + "properties": { + "name": { + "type": "string", + "title": "Item Name", + "description": "The name of the Item" + }, + "variants": { + "type": "array", + "minItems": 1, + "items": { + "type": "object", + "title": "Item Variant", + "description": "A Variant of an Item", + "properties": {} + } + } + } +} \ No newline at end of file diff --git a/schema/process.json b/schema/process.json new file mode 100644 index 0000000..86b6087 --- /dev/null +++ b/schema/process.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Process", + "description": "Process Description", + "properties": { + "name": { + "type": "string", + "title": "Process Name", + "description": "The name of the Process" + }, + "depends": { + "type": "array", + "title": "Process Dependencies", + "description": "List of Items a Process depends on", + "items": { + "type": "string" + } + }, + "next": { + "type": "string", + "title": "Next Process", + "description": "The Process that succeeds this one." + }, + "produces": { + "type": "array", + "title": "Produced Items", + "description": "Items this Process will produce", + "items": { + "type": "string" + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 5d2b746..92eba48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use rocket::{http::Method, launch}; mod db; mod item; +mod process; mod routes; mod transaction; mod variant; @@ -51,7 +52,8 @@ async fn rocket() -> _ { routes::item::supply_route, routes::item::demand_route, routes::item::transaction_route, - routes::item::inventory_route + routes::item::inventory_route, + routes::item::variant_stat_route ], ) .manage(itemdb) diff --git a/src/process.rs b/src/process.rs new file mode 100644 index 0000000..704389a --- /dev/null +++ b/src/process.rs @@ -0,0 +1,8 @@ +use std::collections::HashMap; + +pub struct ProcessInfo { + pub name: String, + pub depends: HashMap, + pub next: Option, + pub produces: Option>, +} diff --git a/src/routes/item/supply.rs b/src/routes/item/supply.rs index 08c3d51..bdfb212 100644 --- a/src/routes/item/supply.rs +++ b/src/routes/item/supply.rs @@ -76,3 +76,18 @@ pub async fn inventory_route( .map(|x| x.api_json()) .collect::>())) } + +#[get("/item///stat")] +pub async fn variant_stat_route( + item_id: &str, + variant_id: &str, + itemdb: &State, +) -> FallibleApiResponse { + let variant = itemdb + .get_item(item_id) + .ok_or_else(item_does_not_exist_error)? + .variant(variant_id) + .ok_or_else(variant_does_not_exist_error)?; + + Ok(variant.stat().await) +} diff --git a/src/variant.rs b/src/variant.rs index 2f3ba6e..2a6d499 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -128,6 +128,18 @@ impl Variant { t._id } + pub async fn stat(&self) -> 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!({ + "amount": active_transactions.len(), + "total_price": total_price + }) + } + pub fn api_json(&self) -> serde_json::Value { json!({ "item": self.item,