Skip to content

Commit

Permalink
refactor: remove one more use of MarketPrices
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Feb 25, 2024
1 parent 33626d6 commit 323009a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
28 changes: 13 additions & 15 deletions packages/cli/bin/contracts.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import 'package:cli/behavior/trader.dart';
import 'package:cli/cache/contract_snapshot.dart';
import 'package:cli/cache/market_prices.dart';
import 'package:cli/cli.dart';
import 'package:cli/printing.dart';

void printContracts(
Future<void> printContracts(
Database db,
String label,
List<Contract> contracts,
MarketPriceSnapshot marketPrices, {
List<Contract> contracts, {
required bool describeContracts,
}) {
}) async {
if (contracts.isEmpty) {
return;
}
Expand All @@ -21,36 +20,35 @@ void printContracts(
for (final contract in contracts) {
logger
..info(contractDescription(contract))
..info(describeExpectedContractProfit(marketPrices, contract));
..info(await describeExpectedContractProfit(db, contract));
}
}

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
final printAll = argResults['all'] as bool;
final contractSnapshot = await ContractSnapshot.load(db);
final marketPrices = await MarketPriceSnapshot.load(db);
printContracts(
await printContracts(
db,
'completed',
contractSnapshot.completedContracts,
marketPrices,
describeContracts: printAll,
);
printContracts(
await printContracts(
db,
'expired',
contractSnapshot.expiredContracts,
marketPrices,
describeContracts: printAll,
);
printContracts(
await printContracts(
db,
'active',
contractSnapshot.activeContracts,
marketPrices,
describeContracts: true,
);
printContracts(
await printContracts(
db,
'unaccepted',
contractSnapshot.unacceptedContracts,
marketPrices,
describeContracts: true,
);
}
Expand Down
18 changes: 9 additions & 9 deletions packages/cli/lib/behavior/trader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -513,16 +513,16 @@ Future<SupplyConstruction201ResponseData?>
return response;
}

int? _expectedContractProfit(
Future<int?> _expectedContractProfit(
Database db,
Contract contract,
MarketPriceSnapshot marketPrices,
) {
) async {
// Add up the total expected outlay.
final terms = contract.terms;
final tradeSymbols = terms.deliver.map((d) => d.tradeSymbolObject).toSet();
final medianPricesBySymbol = <TradeSymbol, int>{};
for (final tradeSymbol in tradeSymbols) {
final medianPrice = marketPrices.medianPurchasePrice(tradeSymbol);
final medianPrice = await db.medianPurchasePrice(tradeSymbol);
if (medianPrice == null) {
return null;
}
Expand All @@ -540,11 +540,11 @@ int? _expectedContractProfit(
}

/// Returns a string describing the expected profit of a contract.
String describeExpectedContractProfit(
MarketPriceSnapshot marketPrices,
Future<String> describeExpectedContractProfit(
Database db,
Contract contract,
) {
final profit = _expectedContractProfit(contract, marketPrices);
) async {
final profit = await _expectedContractProfit(db, contract);
final profitString = profit == null ? 'unknown' : creditsString(profit);
return 'Expected profit: $profitString';
}
Expand All @@ -567,7 +567,7 @@ Future<DateTime?> acceptContractsIfNeeded(
ship,
shipCache,
);
shipInfo(ship, describeExpectedContractProfit(marketPrices, contract));
shipInfo(ship, await describeExpectedContractProfit(db, contract));
return null;
}
final unacceptedContracts = await db.unacceptedContracts();
Expand Down
9 changes: 9 additions & 0 deletions packages/db/lib/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,15 @@ class Database {
return queryOne(query, marketPriceFromColumnMap);
}

Future<int?> medianPurchasePrice(TradeSymbol tradeSymbol) async {
final query = medianPurchasePriceQuery(tradeSymbol);
final result = await connection.execute(
pg.Sql.named(query.fmtString),
parameters: query.parameters,
);
return result[0][0] as int?;
}

/// Get all shipyard prices from the database.
Future<Iterable<ShipyardPrice>> allShipyardPrices() async {
return queryMany(allShipyardPricesQuery(), shipyardPriceFromColumnMap);
Expand Down
11 changes: 11 additions & 0 deletions packages/db/lib/src/market_price.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ Query marketPriceQuery(WaypointSymbol waypoint, TradeSymbol trade) => Query(
},
);

/// Query to get the median purchase price for a trade symbol.
Query medianPurchasePriceQuery(TradeSymbol trade) => Query(
'''
SELECT percentile_disc(0.5)
WITHIN GROUP (ORDER BY purchase_price)
FROM market_price_
WHERE trade_symbol = @trade;
''',
parameters: {'trade': trade.toJson()},
);

/// Build a market price from a column map.
MarketPrice marketPriceFromColumnMap(Map<String, dynamic> values) {
return MarketPrice(
Expand Down

0 comments on commit 323009a

Please sign in to comment.