Skip to content

Commit

Permalink
Merge pull request #35 from Overmuse/SR/refactor
Browse files Browse the repository at this point in the history
SR/refactor
  • Loading branch information
SebRollen authored Jul 8, 2021
2 parents 13ac0d4 + 9f34777 commit a724b0f
Show file tree
Hide file tree
Showing 25 changed files with 345 additions and 330 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "order-manager"
version = "6.1.0"
version = "6.1.1"
authors = ["Sebastian Rollen <seb@overmu.se>"]
edition = "2018"

Expand Down
91 changes: 9 additions & 82 deletions src/db/allocations.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,27 @@
use crate::manager::{Allocation, Owner, Position};
use anyhow::Result;
use crate::types::{Allocation, Owner};
use std::convert::TryInto;
use std::sync::Arc;
use tokio_postgres::Client;
use tokio_postgres::{Client, Error};
use tracing::trace;
use uuid::Uuid;

#[tracing::instrument(skip(client))]
pub(crate) async fn get_allocations(client: Arc<Client>) -> Result<Vec<Allocation>> {
pub async fn get_allocations(client: Arc<Client>) -> Result<Vec<Allocation>, Error> {
trace!("Getting allocations");
client
.query("SELECT * FROM allocations", &[])
.await?
.into_iter()
.map(|row| -> Result<Allocation> {
let owner = if row.try_get::<usize, &str>(0)? == "House" {
Owner::House
} else {
Owner::Strategy(row.try_get(0)?, row.try_get(1)?)
};
Ok(Allocation {
id: row.try_get(7)?,
owner,
claim_id: row.try_get(2)?,
lot_id: row.try_get(3)?,
ticker: row.try_get(4)?,
shares: row.try_get(5)?,
basis: row.try_get(6)?,
})
})
.map(TryInto::try_into)
.collect()
}

#[tracing::instrument(skip(client, owner))]
pub(crate) async fn get_positions_by_owner(
client: Arc<Client>,
owner: Owner,
) -> Result<Vec<Position>> {
let (owner, sub_owner) = match owner {
Owner::House => ("House".to_string(), None),
Owner::Strategy(owner, sub_owner) => (owner, sub_owner),
};
let res = match sub_owner {
Some(sub_owner) => {
client.query("SELECT owner, sub_owner, ticker, sum(shares), sum(basis) FROM allocations WHERE owner = $1 AND sub_owner = $2 GROUP BY owner, sub_owner, ticker", &[&owner, &sub_owner]).await?
}
None => {
client.query("SELECT owner, null, ticker, sum(shares), sum(basis) FROM allocations WHERE owner = $1 GROUP BY owner, ticker", &[&owner]).await?
}
};

res.into_iter()
.map(|row| -> Result<Position> {
let owner = if row.try_get::<usize, &str>(0)? == "House" {
Owner::House
} else {
Owner::Strategy(row.try_get(0)?, row.try_get(1)?)
};
Ok(Position::new(
owner,
row.try_get(2)?,
row.try_get(3)?,
row.try_get(4)?,
))
})
.collect()
}

pub(crate) async fn set_allocation_owner(
pub async fn set_allocation_owner(
client: Arc<Client>,
id: Uuid,
owner: Owner,
) -> Result<()> {
) -> Result<(), Error> {
trace!("Updating allocation owner");
let (owner, sub_owner) = match owner {
Owner::House => ("House".to_string(), None),
Owner::Strategy(owner, sub_owner) => (owner, sub_owner),
Expand All @@ -96,32 +47,8 @@ pub(crate) async fn set_allocation_owner(
Ok(())
}

#[tracing::instrument(skip(client, ticker))]
pub(crate) async fn get_positions_by_ticker(
client: Arc<Client>,
ticker: &str,
) -> Result<Vec<Position>> {
client.query("SELECT owner, sub_owner, ticker, sum(shares), sum(basis) FROM allocations WHERE ticker = $1 GROUP BY owner, sub_owner, ticker", &[&ticker])
.await?
.into_iter()
.map(|row| -> Result<Position> {
let owner = if row.try_get::<usize, &str>(0)? == "House" {
Owner::House
} else {
Owner::Strategy(row.try_get(0)?, row.try_get(1)?)
};
Ok(Position::new(
owner,
row.try_get(2)?,
row.try_get(3)?,
row.try_get(4)?,
))
})
.collect()
}

#[tracing::instrument(skip(client, allocation))]
pub(crate) async fn save_allocation(client: Arc<Client>, allocation: Allocation) -> Result<()> {
pub async fn save_allocation(client: Arc<Client>, allocation: Allocation) -> Result<(), Error> {
trace!("Saving allocation");
let (owner, sub_owner) = match allocation.owner {
Owner::House => ("House".to_string(), None),
Expand Down
62 changes: 15 additions & 47 deletions src/db/claims.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,49 @@
use crate::manager::Claim;
use anyhow::Result;
use crate::types::Claim;
use position_intents::AmountSpec;
use rust_decimal::prelude::*;
use std::convert::TryInto;
use std::sync::Arc;
use tokio_postgres::Client;
use tokio_postgres::{Client, Error};
use tracing::trace;
use uuid::Uuid;

#[tracing::instrument(skip(client))]
pub(crate) async fn get_claims(client: Arc<Client>) -> Result<Vec<Claim>> {
pub async fn get_claims(client: Arc<Client>) -> Result<Vec<Claim>, Error> {
trace!("Getting claims");
client
.query("SELECT * FROM claims", &[])
.await?
.into_iter()
.map(|row| -> Result<Claim> {
Ok(Claim {
id: row.try_get(0)?,
strategy: row.try_get(1)?,
sub_strategy: row.try_get(2)?,
ticker: row.try_get(3)?,
amount: unite_amount_spec(row.try_get(4)?, row.try_get(5)?),
})
})
.map(TryInto::try_into)
.collect()
}

#[tracing::instrument(skip(client, ticker))]
pub(crate) async fn get_claims_by_ticker(client: Arc<Client>, ticker: &str) -> Result<Vec<Claim>> {
pub async fn get_claims_by_ticker(client: Arc<Client>, ticker: &str) -> Result<Vec<Claim>, Error> {
trace!(ticker, "Getting claims");
client
.query("SELECT * FROM claims WHERE ticker = $1", &[&ticker])
.await?
.into_iter()
.map(|row| -> Result<Claim> {
Ok(Claim {
id: row.try_get(0)?,
strategy: row.try_get(1)?,
sub_strategy: row.try_get(2)?,
ticker: row.try_get(3)?,
amount: unite_amount_spec(row.try_get(4)?, row.try_get(5)?),
})
})
.map(TryInto::try_into)
.collect()
}

#[tracing::instrument(skip(client, id))]
pub(crate) async fn get_claim_by_id(client: Arc<Client>, id: Uuid) -> Result<Claim> {
pub async fn get_claim_by_id(client: Arc<Client>, id: Uuid) -> Result<Claim, Error> {
trace!(%id, "Getting claim");
let row = client
client
.query_one("SELECT * FROM claims WHERE id = $1", &[&id])
.await?;
Ok(Claim {
id: row.try_get(0)?,
strategy: row.try_get(1)?,
sub_strategy: row.try_get(2)?,
ticker: row.try_get(3)?,
amount: unite_amount_spec(row.try_get(4)?, row.try_get(5)?),
})
.await?
.try_into()
}

#[tracing::instrument(skip(client, id, amount))]
pub(crate) async fn update_claim_amount(
pub async fn update_claim_amount(
client: Arc<Client>,
id: Uuid,
amount: AmountSpec,
) -> Result<()> {
) -> Result<(), Error> {
trace!(%id, ?amount, "Updating claim amount");
let (amount, unit) = split_amount_spec(amount);
client
Expand All @@ -78,7 +56,7 @@ pub(crate) async fn update_claim_amount(
}

#[tracing::instrument(skip(client, id))]
pub(crate) async fn delete_claim_by_id(client: Arc<Client>, id: Uuid) -> Result<()> {
pub async fn delete_claim_by_id(client: Arc<Client>, id: Uuid) -> Result<(), Error> {
trace!(%id, "Deleting claim");
client
.execute("DELETE FROM claims WHERE id = $1;", &[&id])
Expand All @@ -87,7 +65,7 @@ pub(crate) async fn delete_claim_by_id(client: Arc<Client>, id: Uuid) -> Result<
}

#[tracing::instrument(skip(client, claim))]
pub(crate) async fn save_claim(client: Arc<Client>, claim: Claim) -> Result<()> {
pub async fn save_claim(client: Arc<Client>, claim: Claim) -> Result<(), Error> {
trace!(id = %claim.id, "Saving claim");
let (amount, unit) = split_amount_spec(claim.amount);
client.execute("INSERT INTO claims (id, strategy, sub_strategy, ticker, amount, unit) VALUES ($1, $2, $3, $4, $5, $6);", &[
Expand All @@ -109,13 +87,3 @@ fn split_amount_spec(amount_spec: AmountSpec) -> (Decimal, &'static str) {
AmountSpec::Zero => (Decimal::ZERO, "zero"),
}
}

fn unite_amount_spec(amount: Decimal, unit: &str) -> AmountSpec {
match unit {
"dollars" => AmountSpec::Dollars(amount),
"shares" => AmountSpec::Shares(amount),
"percent" => AmountSpec::Percent(amount),
"zero" => AmountSpec::Zero,
_ => unreachable!(),
}
}
7 changes: 2 additions & 5 deletions src/db/dependent_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio_postgres::Client;
use tracing::trace;

#[tracing::instrument(skip(client, id, dependent_order))]
pub(crate) async fn save_dependent_order(
pub async fn save_dependent_order(
client: Arc<Client>,
id: &str,
dependent_order: OrderIntent,
Expand Down Expand Up @@ -43,10 +43,7 @@ pub(crate) async fn save_dependent_order(
}

#[tracing::instrument(skip(client, id))]
pub(crate) async fn take_dependent_orders(
client: Arc<Client>,
id: &str,
) -> Result<Vec<OrderIntent>> {
pub async fn take_dependent_orders(client: Arc<Client>, id: &str) -> Result<Vec<OrderIntent>> {
trace!(id, "Saving dependent order");
client
.query(
Expand Down
25 changes: 8 additions & 17 deletions src/db/lots.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
use crate::manager::Lot;
use anyhow::Result;
use crate::types::Lot;
use std::convert::TryInto;
use std::sync::Arc;
use tokio_postgres::Client;
use tokio_postgres::{Client, Error};
use tracing::trace;

#[tracing::instrument(skip(client))]
pub(crate) async fn get_lots(client: Arc<Client>) -> Result<Vec<Lot>> {
pub async fn get_lots(client: Arc<Client>) -> Result<Vec<Lot>, Error> {
trace!("Getting lots");
client
.query("SELECT * FROM lots", &[])
.await?
.into_iter()
.map(|row| -> Result<Lot> {
Ok(Lot {
id: row.try_get(0)?,
order_id: row.try_get(1)?,
ticker: row.try_get(2)?,
fill_time: row.try_get(3)?,
price: row.try_get(4)?,
shares: row.try_get(5)?,
})
})
.map(TryInto::try_into)
.collect()
}

#[tracing::instrument(skip(client, order_id))]
pub(crate) async fn get_lots_by_order_id(client: Arc<Client>, order_id: &str) -> Result<Vec<Lot>> {
pub async fn get_lots_by_order_id(client: Arc<Client>, order_id: &str) -> Result<Vec<Lot>, Error> {
trace!(order_id, "Getting lots");
client
.query("SELECT * FROM lots WHERE order_id = $1", &[&order_id])
.await?
.into_iter()
.map(|row| -> Result<Lot> {
.map(|row| -> Result<Lot, Error> {
Ok(Lot {
id: row.try_get(0)?,
order_id: row.try_get(1)?,
Expand All @@ -45,7 +36,7 @@ pub(crate) async fn get_lots_by_order_id(client: Arc<Client>, order_id: &str) ->
}

#[tracing::instrument(skip(client, lot))]
pub(crate) async fn save_lot(client: Arc<Client>, lot: Lot) -> Result<()> {
pub async fn save_lot(client: Arc<Client>, lot: Lot) -> Result<(), Error> {
trace!(id = %lot.id, "Saving lot");
client.execute("INSERT INTO lots (id, order_id, ticker, fill_time, price, shares) VALUES ($1, $2, $3, $4, $5, $6);", &[
&lot.id,
Expand Down
12 changes: 7 additions & 5 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ mod claims;
mod dependent_orders;
mod lots;
mod pending_orders;
pub(crate) use allocations::*;
pub(crate) use claims::*;
pub(crate) use dependent_orders::*;
pub(crate) use lots::*;
pub(crate) use pending_orders::*;
mod positions;
pub use allocations::*;
pub use claims::*;
pub use dependent_orders::*;
pub use lots::*;
pub use pending_orders::*;
pub use positions::*;
Loading

0 comments on commit a724b0f

Please sign in to comment.