From 00a58e012c6e69c6c18e840bf16349c490457ea8 Mon Sep 17 00:00:00 2001 From: akarras Date: Tue, 30 Jan 2024 19:06:52 -0700 Subject: [PATCH] cleanup db a tiny bit --- Cargo.toml | 4 +- ultros-db/src/lib.rs | 1 - ultros-db/src/price_optimizer.rs | 143 ------------------------------- 3 files changed, 2 insertions(+), 146 deletions(-) delete mode 100644 ultros-db/src/price_optimizer.rs diff --git a/Cargo.toml b/Cargo.toml index 3bbeabf1..69f15523 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,8 +30,8 @@ chrono = "0.4.23" itertools = "0.12.0" image = "0.24.6" plotters = "0.3" -sea-orm = {version = "0.12.2", features = ["sqlx-postgres", "runtime-tokio-rustls"]} -sea-orm-migration = {version = "0.12.2", features = ["sqlx-postgres", "runtime-tokio-rustls"]} +sea-orm = {version = "0.12", features = ["sqlx-postgres", "runtime-tokio-rustls"]} +sea-orm-migration = {version = "0.12", features = ["sqlx-postgres", "runtime-tokio-rustls"]} sea-query = "0.30.0" yoke = "0.7.1" xiv-gen = {path = "./xiv-gen", features = ["item", "item_ui_category", "item_search_category", "class_job", "recipe", diff --git a/ultros-db/src/lib.rs b/ultros-db/src/lib.rs index 961ed421..dcd4d376 100644 --- a/ultros-db/src/lib.rs +++ b/ultros-db/src/lib.rs @@ -6,7 +6,6 @@ mod ffxiv_character; pub mod listings; pub mod lists; pub mod partial_diff_iterator; -pub mod price_optimizer; pub mod recently_updated; mod regions_and_datacenters; pub mod retainers; diff --git a/ultros-db/src/price_optimizer.rs b/ultros-db/src/price_optimizer.rs deleted file mode 100644 index 05039824..00000000 --- a/ultros-db/src/price_optimizer.rs +++ /dev/null @@ -1,143 +0,0 @@ -use std::fmt::Display; - -use chrono::Duration; -use chrono::Utc; -use migration::Alias; -use migration::BinOper; -use migration::ColumnRef; -use migration::Expr; -use migration::Query; -use migration::SeaRc; -use migration::SimpleExpr; -use tracing::instrument; - -use crate::entity::*; -use crate::UltrosDb; -use anyhow::Result; -use sea_orm::*; - -#[derive(FromQueryResult)] -pub struct BestResellResults { - pub item_id: i32, - pub profit: i32, - pub margin: f32, -} - -pub struct Margin(f32); - -impl Display for Margin { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:.2}%", self.0 * 100.0) - } -} - -impl UltrosDb { - #[instrument] - /// Tries to calculate what the best item to resell for the given world is - /// Assumes that the user is willing to travel to all worlds in the region - /// Parameters: - /// * world_id - World you want to sale items on - /// * sale_amount_threshold - How many recent sales within the window should have occured (see next argument) - /// * sale_window - How long ago should sales be considered for this query - #[instrument] - pub async fn get_best_item_to_resell_on_world( - &self, - world_id: i32, - sale_amount_threshold: i32, - sale_window: Duration, - ) -> Result> { - let min_sale_price_alias: DynIden = SeaRc::new(Alias::new("min_sale_price")); - let world_sale_history_query = Query::select() - .from(sale_history::Entity) - .column(sale_history::Column::SoldItemId) - .expr_as( - sale_history::Column::PricePerItem.min(), - min_sale_price_alias.clone(), - ) - .and_where(sale_history::Column::WorldId.eq(world_id)) - .and_where(sale_history::Column::SoldDate.gt(Utc::now() - sale_window)) - .and_having( - Expr::val(sale_amount_threshold) - .lt(Expr::col(sale_history::Column::Quantity).sum()), - ) - .group_by_col(sale_history::Column::SoldItemId) - .to_owned(); - let all_worlds_in_region_query = Query::select() - .from(world::Entity) - .column(world::Column::Id) - .and_where( - world::Column::DatacenterId.in_subquery( - Query::select() - .from(datacenter::Entity) - .column(datacenter::Column::Id) - .and_where( - datacenter::Column::Id.in_subquery( - Query::select() - .column(world::Column::DatacenterId) - .and_where(world::Column::Id.eq(world_id)) - .from(world::Entity) - .to_owned(), - ), - ) - .to_owned(), - ), - ) - .to_owned(); - let query_iden: DynIden = SeaRc::new(Alias::new("sale_hist")); - let profit: DynIden = SeaRc::new(Alias::new("profit")); - let margin: DynIden = SeaRc::new(Alias::new("margin")); - let all_query = Query::select() - .from(active_listing::Entity) - .column(active_listing::Column::ItemId) - .expr_as( - SimpleExpr::Column(ColumnRef::TableColumn( - query_iden.clone(), - min_sale_price_alias.clone(), - )) - .sub(active_listing::Column::PricePerUnit.min()), - profit.clone(), - ) - .expr_as( - SimpleExpr::Binary( - Box::new( - SimpleExpr::Column(ColumnRef::TableColumn( - query_iden.clone(), - min_sale_price_alias.clone(), - )) - .cast_as(Alias::new("float4")), - ), - BinOper::Div, - Box::new( - active_listing::Column::PricePerUnit - .min() - .cast_as(Alias::new("float4")), - ), - ), - margin.clone(), - ) - .join_subquery( - JoinType::InnerJoin, - world_sale_history_query, - query_iden.clone(), - Expr::col((active_listing::Entity, active_listing::Column::ItemId)) - .equals((query_iden.clone(), sale_history::Column::SoldItemId)), - ) - .and_where(active_listing::Column::WorldId.in_subquery(all_worlds_in_region_query)) - .group_by_col(active_listing::Column::ItemId) - .group_by_col((query_iden.clone(), min_sale_price_alias.clone())) - .order_by(profit, Order::Desc) - .limit(25) - .to_owned(); - let query = self.db.get_database_backend().build(&all_query); - let mut results = BestResellResults::find_by_statement(query) - .all(&self.db) - .await?; - // multiply margin by 100, subtract by 100 - results.iter_mut().for_each(|e| { - e.margin *= 100.0; - e.margin -= 100.0; - }); - // now find sale history for *all* items in our server - Ok(results) - } -}