use heromodels::db::{Collection, Db}; use heromodels::models::grid4::{Bid, BidStatus, BillingPeriod}; use heromodels::models::grid4::bid::bid_index::customer_id; use heromodels_core::Model; // Helper function to print bid details fn print_bid_details(bid: &Bid) { println!("\n--- Bid Details ---"); println!("ID: {}", bid.get_id()); println!("Customer ID: {}", bid.customer_id); println!("Compute Slices: {}", bid.compute_slices_nr); println!("Compute Slice Price: ${:.2}", bid.compute_slice_price); println!("Storage Slices: {}", bid.storage_slices_nr); println!("Storage Slice Price: ${:.2}", bid.storage_slice_price); println!("Status: {:?}", bid.status); println!("Obligation: {}", bid.obligation); println!("Start Date: {}", bid.start_date); println!("End Date: {}", bid.end_date); println!("Billing Period: {:?}", bid.billing_period); println!("Signature User: {}", bid.signature_user); println!("Created At: {}", bid.base_data.created_at); println!("Modified At: {}", bid.base_data.modified_at); } fn main() { // Create a new DB instance in /tmp/grid4_db, and reset before every run let db = heromodels::db::hero::OurDB::new("/tmp/grid4_db", true).expect("Can create DB"); println!("Grid4 Bid Models - Basic Usage Example"); println!("====================================="); // Create bids with different configurations // Bid 1 - Small compute request let bid1 = Bid::new() .customer_id(101) .compute_slices_nr(4) .compute_slice_price(0.05) .storage_slices_nr(10) .storage_slice_price(0.02) .status(BidStatus::Pending) .obligation(false) .start_date(1640995200) // 2022-01-01 .end_date(1672531200) // 2023-01-01 .billing_period(BillingPeriod::Monthly) .signature_user("sig_user_101_abc123".to_string()); // Bid 2 - Large compute request with obligation let bid2 = Bid::new() .customer_id(102) .compute_slices_nr(16) .compute_slice_price(0.04) .storage_slices_nr(50) .storage_slice_price(0.015) .status(BidStatus::Confirmed) .obligation(true) .start_date(1640995200) .end_date(1704067200) // 2024-01-01 .billing_period(BillingPeriod::Yearly) .signature_user("sig_user_102_def456".to_string()); // Bid 3 - Storage-heavy request let bid3 = Bid::new() .customer_id(103) .compute_slices_nr(2) .compute_slice_price(0.06) .storage_slices_nr(100) .storage_slice_price(0.01) .status(BidStatus::Assigned) .obligation(true) .start_date(1640995200) .end_date(1672531200) .billing_period(BillingPeriod::Hourly) .signature_user("sig_user_103_ghi789".to_string()); // Bid 4 - Cancelled bid let bid4 = Bid::new() .customer_id(104) .compute_slices_nr(8) .compute_slice_price(0.055) .storage_slices_nr(25) .storage_slice_price(0.018) .status(BidStatus::Cancelled) .obligation(false) .start_date(1640995200) .end_date(1672531200) .billing_period(BillingPeriod::Monthly) .signature_user("sig_user_104_jkl012".to_string()); // Save all bids to database and get their assigned IDs and updated models let (bid1_id, db_bid1) = db .collection() .expect("can open bid collection") .set(&bid1) .expect("can set bid"); let (bid2_id, db_bid2) = db .collection() .expect("can open bid collection") .set(&bid2) .expect("can set bid"); let (bid3_id, db_bid3) = db .collection() .expect("can open bid collection") .set(&bid3) .expect("can set bid"); let (bid4_id, db_bid4) = db .collection() .expect("can open bid collection") .set(&bid4) .expect("can set bid"); println!("Bid 1 assigned ID: {}", bid1_id); println!("Bid 2 assigned ID: {}", bid2_id); println!("Bid 3 assigned ID: {}", bid3_id); println!("Bid 4 assigned ID: {}", bid4_id); // Print all bids retrieved from database println!("\n--- Bids Retrieved from Database ---"); println!("\n1. Small compute bid:"); print_bid_details(&db_bid1); println!("\n2. Large compute bid with obligation:"); print_bid_details(&db_bid2); println!("\n3. Storage-heavy bid:"); print_bid_details(&db_bid3); println!("\n4. Cancelled bid:"); print_bid_details(&db_bid4); // Demonstrate different ways to retrieve bids from the database println!("\n--- Retrieving Bids by Different Methods ---"); println!("\n1. By Customer ID Index (Customer 102):"); let customer_bids = db .collection::() .expect("can open bid collection") .get::(&102u32) .expect("can load bids by customer"); assert_eq!(customer_bids.len(), 1); print_bid_details(&customer_bids[0]); println!("\n2. Updating Bid Status:"); let mut updated_bid = db_bid1.clone(); updated_bid.status = BidStatus::Confirmed; let (_, confirmed_bid) = db .collection::() .expect("can open bid collection") .set(&updated_bid) .expect("can update bid"); println!("Updated bid status to Confirmed:"); print_bid_details(&confirmed_bid); // 3. Delete a bid and show the updated results println!("\n3. After Deleting a Bid:"); println!("Deleting bid with ID: {}", bid4_id); db.collection::() .expect("can open bid collection") .delete_by_id(bid4_id) .expect("can delete existing bid"); // Show remaining bids let all_bids = db .collection::() .expect("can open bid collection") .get_all() .expect("can load all bids"); println!("Remaining bids count: {}", all_bids.len()); assert_eq!(all_bids.len(), 3); // Calculate total compute and storage requested println!("\n--- Bid Analytics ---"); let total_compute_slices: i32 = all_bids.iter().map(|b| b.compute_slices_nr).sum(); let total_storage_slices: i32 = all_bids.iter().map(|b| b.storage_slices_nr).sum(); let avg_compute_price: f64 = all_bids.iter().map(|b| b.compute_slice_price).sum::() / all_bids.len() as f64; let avg_storage_price: f64 = all_bids.iter().map(|b| b.storage_slice_price).sum::() / all_bids.len() as f64; println!("Total Compute Slices Requested: {}", total_compute_slices); println!("Total Storage Slices Requested: {}", total_storage_slices); println!("Average Compute Price: ${:.3}", avg_compute_price); println!("Average Storage Price: ${:.3}", avg_storage_price); // Count bids by status let confirmed_count = all_bids.iter().filter(|b| matches!(b.status, BidStatus::Confirmed)).count(); let assigned_count = all_bids.iter().filter(|b| matches!(b.status, BidStatus::Assigned)).count(); let pending_count = all_bids.iter().filter(|b| matches!(b.status, BidStatus::Pending)).count(); println!("\nBids by Status:"); println!(" Confirmed: {}", confirmed_count); println!(" Assigned: {}", assigned_count); println!(" Pending: {}", pending_count); println!("\n--- Model Information ---"); println!("Bid DB Prefix: {}", Bid::db_prefix()); }