Compare commits

..

4 Commits

Author SHA1 Message Date
hackerESQ 689aa4d50b fix: multi currency seeders 2025-05-15 20:05:14 -05:00
hackerESQ 26370c03c4 fix: optimize migration to multi-currency 2025-05-03 13:22:45 -05:00
hackerESQ 80b043219a prevent pre-releases from triggering image build 2025-05-02 20:07:38 -05:00
hackerESQ de54b6843d Fix multi-currency imports (#94) 2025-05-02 18:14:06 -05:00
15 changed files with 172 additions and 343 deletions
+12 -6
View File
@@ -43,7 +43,16 @@ jobs:
- name: Extract version from tag
id: extract-version
run: |
echo "version=${GITHUB_REF_NAME#v}" >> $GITHUB_ENV
VERSION="${GITHUB_REF_NAME#v}"
TAGS="investbrainapp/investbrain:${VERSION},ghcr.io/investbrainapp/investbrain:${VERSION}"
# Conditionally add 'latest' tags unless 'pre-release' is in the version
if [[ "${GITHUB_REF_NAME}" != *alpha* && "${GITHUB_REF_NAME}" != *beta* && "${GITHUB_REF_NAME}" != *rc* ]]; then
TAGS="$TAGS,investbrainapp/investbrain:latest,ghcr.io/investbrainapp/investbrain:latest"
fi
echo "tags=$TAGS" >> $GITHUB_OUTPUT
- name: Build and push
uses: docker/build-push-action@v6
@@ -51,8 +60,5 @@ jobs:
platforms: linux/amd64,linux/arm64
file: ./docker/Dockerfile
push: true
tags: |
investbrainapp/investbrain:latest
investbrainapp/investbrain:${{ env.version }}
ghcr.io/investbrainapp/investbrain:latest
ghcr.io/investbrainapp/investbrain:${{ env.version }}
tags: ${{ steps.extract-version.outputs.tags }}
+1 -2
View File
@@ -7,7 +7,6 @@ namespace App\Console\Commands;
use App\Models\Holding;
use App\Models\MarketData;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class RefreshMarketData extends Command
{
@@ -61,7 +60,7 @@ class RefreshMarketData extends Command
try {
MarketData::getMarketData($holding->symbol, $force);
} catch (\Throwable $e) {
Log::error('Could not refresh '.$holding->symbol.' ('.$e->getMessage().')');
$this->line('Could not refresh '.$holding->symbol.' ('.$e->getMessage().')');
}
}
}
+4 -3
View File
@@ -47,12 +47,13 @@ class ConfigSheet implements FromCollection, WithHeadings, WithTitle
]);
// reinvested holdings
Holding::myHoldings()->where('reinvest_dividends', true)->get()->each(function ($holding) use (&$configs) {
$reinvested_holdings = Holding::myHoldings()->where('reinvest_dividends', true)->get(['portfolio_id', 'symbol']);
if ($reinvested_holdings->isNotEmpty()) {
$configs->push([
'key' => 'reinvested_dividends',
'value' => $holding->id,
'value' => $reinvested_holdings->toJson(),
]);
});
}
return $configs;
}
+11 -5
View File
@@ -54,11 +54,17 @@ class ConfigSheet implements SkipsEmptyRows, ToCollection, WithEvents, WithHeadi
$this->backupImport->user->save();
break;
case 'reinvest_dividends':
Holding::myHoldings()->where('id', $config['value'])->update([
'reinvest_dividends' => true,
]);
case 'reinvested_dividends':
if (json_validate($config['value'])) {
foreach (json_decode($config['value'], true) as $reinvest) {
Holding::myHoldings($this->backupImport->user->id)
->where('portfolio_id', $reinvest['portfolio_id'])
->where('symbol', $reinvest['symbol'])
->update([
'reinvest_dividends' => true,
]);
}
}
break;
default:
+1 -1
View File
@@ -48,7 +48,7 @@ class TransactionsSheet implements SkipsEmptyRows, ToCollection, WithEvents, Wit
// if has any transactions not in base currency, need to sync timeseries conversion rates
if ($transactions->where('currency', '!=', config('investbrain.base_currency'))->isNotEmpty()) {
CurrencyRate::timeSeriesRates('', $transactions->min('date'));
CurrencyRate::timeSeriesRates('', $transactions->min('date'), $transactions->max('date'));
}
$totalBatches = count($transactions) / $this->batchSize();
+65 -69
View File
@@ -111,7 +111,7 @@ class CurrencyRate extends Model
*
* @return array<string, float>
*/
public static function timeSeriesRates(string $currency, mixed $start = null, mixed $end = null): array
public static function timeSeriesRates(?string $currency = null, mixed $start = null, mixed $end = null): array
{
if (empty($start)) {
return [];
@@ -119,112 +119,110 @@ class CurrencyRate extends Model
$end = $end ?? now();
dump('Creating period');
$period = CarbonPeriod::create($start, $end);
// No need to send network request - just generate 1s
if ($currency === config('investbrain.base_currency')) {
dump('same curr');
$dateRange = [];
foreach ($period as $date) {
$dateRange[$date->toDateString()] = 1;
}
return $dateRange;
}
dump('diff curr');
// handle currency alias
if (! empty($currency)) {
[$currency, $adjustment] = self::getCurrencyAliasAdjustments($currency);
[$currency, $adjustment] = self::getCurrencyAliasAdjustments($currency);
$currencies = Currency::all()->pluck('currency')->toArray();
} else {
dump('got currencies');
$currency = Currency::all()->pluck('currency')->toArray();
}
// call api in chunks
foreach (collect($period)->chunk(500) as $chunk) {
// get rates
$rates = Frankfurter::setSymbols($currency)->timeSeries($period->first(), $period->last());
dump('calling frankf time series');
$rates = collect(Arr::get($rates, 'rates', []))->sortKeys()->toArray();
$chunkRates = Frankfurter::setSymbols($currencies)->timeSeries($chunk->min(), $chunk->max());
$datesOnly = array_keys($rates);
$rates = Arr::get($chunkRates, 'rates', []);
// loop through each date
$updates = [];
foreach ($period as $date) {
// loop through each date
$updates = [];
$lookupDate = self::getNearestPastDate($date, $datesOnly, $rates);
foreach ($chunk as $date) {
if (is_null($lookupDate)) {
continue;
}
$lookupDate = self::getNearestPastDate($date, $rates);
if (is_null($lookupDate)) {
continue;
}
// loop through each rate
foreach ($rates[$lookupDate->toDateString()] as $curr => $rate) {
// add to updates
$updates[] = [
'currency' => $curr,
'date' => $date->toDateString(),
'rate' => $rate,
'updated_at' => now()->toDateTimeString(),
'created_at' => now()->toDateTimeString(),
];
}
dump('inserting');
// persist
self::chunkInsert($updates);
// loop through each rate
foreach ($rates[$lookupDate->toDateString()] as $curr => $rate) {
// add to updates
$updates[] = [
'currency' => $curr,
'date' => $date->toDateString(),
'rate' => $rate,
'updated_at' => now()->toDateTimeString(),
'created_at' => now()->toDateTimeString(),
];
}
}
dump('done');
// persist
self::chunkInsert($updates);
return collect($updates)
->whereBetween('date', [$start, $end ?? now()])
->where('currency', $currency)
->mapWithKeys(fn ($rate) => [
$rate['date'] => $rate['rate'] * $adjustment,
])
->toArray();
if (is_string($currency)) {
return collect($updates)
->whereBetween('date', [$start, $end ?? now()])
->where('currency', $currency)
->mapWithKeys(fn ($rate) => [
$rate['date'] => $rate['rate'] * ($adjustment ?? 1),
])
->toArray();
}
return [];
}
private static function getNearestPastDate(CarbonInterface $date, array $rates): ?CarbonInterface
private static function getNearestPastDate(CarbonInterface $date, array $datesOnly, array $rates): ?CarbonInterface
{
$datesWithRates = array_keys($rates);
sort($datesWithRates);
// if no dates, nothing to do...
if (empty($datesOnly)) {
return null;
}
$mutableDate = $date->copy();
$weekAgo = $date->copy()->subWeek();
$firstDate = Carbon::parse($datesOnly[0]);
// get rates or find closest valid rate (handles missing weekend rates)
while (! isset($rates[$date->toDateString()])) {
while (! isset($rates[$mutableDate->toDateString()])) {
// prevent runaway infinite loops
if ($mutableDate->lessThan($weekAgo)) {
return null;
}
// is this the start of a range that falls on a weekend?
if ($date->lessThan($first_date = Carbon::parse($datesWithRates[0]))) {
if ($mutableDate->lessThan($firstDate)) {
$date = $first_date;
break;
return $firstDate;
}
// try the day before then
$date = Carbon::parse($date)->subDay();
// prevent runaway infinite loops
if ($date->lessThan($date->copy()->subWeek())) {
$date = null;
break;
}
$mutableDate = $mutableDate->subDay();
}
return $date;
return $mutableDate;
}
public static function refreshCurrencyData($force = false): void
@@ -265,15 +263,13 @@ class CurrencyRate extends Model
public static function chunkInsert(array $updates): void
{
$chunks = array_chunk($updates, 500);
foreach ($chunks as $chunk) {
foreach (array_chunk($updates, 500) as $chunk) {
QueuedCurrencyRateInsertJob::dispatch($chunk);
}
}
protected static function getCurrencyAliasAdjustments($currency)
protected static function getCurrencyAliasAdjustments(string $currency)
{
$adjustment = 1;
+18 -32
View File
@@ -95,49 +95,36 @@ class Dividend extends Model
return;
}
dump('1. getting div data for '.$symbol);
try {
// get some data
if ($dividend_data = collect() && $start_date && $end_date) {
$dividend_data = app(MarketDataInterface::class)->dividends($symbol, $start_date, $end_date);
}
} catch (\Throwable $e) {
dump('exception: '.$e->getMessage());
// get some data
if ($dividend_data = collect() && $start_date && $end_date) {
$dividend_data = app(MarketDataInterface::class)->dividends($symbol, $start_date, $end_date);
}
dump('2. got div data for '.$symbol);
// ah, we found some dividends...
if ($dividend_data->isNotEmpty()) {
$dividend_data = $dividend_data->sortBy('date');
dump('3. getting mkt data for '.$symbol);
$market_data = MarketData::getMarketData($symbol);
dump('4. got market data for '.$symbol);
$dividend_data
->chunk(10)
->each(function ($chunk) use ($market_data) {
// todo: use this for start_date - $dividend_data->first()->get('date')
// get historic conversion rates
$rate_to_base = CurrencyRate::timeSeriesRates($market_data->currency, $chunk->min('date'), $chunk->max('date'));
// get historic conversion rates
$rate_to_base = CurrencyRate::timeSeriesRates($market_data->currency, $dividend_data->first()->get('date'), $end_date);
// create mass insert
foreach ($chunk as $index => $dividend) {
$rate_to_base_date = 1 / Arr::get($rate_to_base, Carbon::parse(Arr::get($dividend, 'date'))->toDateString(), 1);
dump('5. got time series for '.$symbol);
// create mass insert
foreach ($dividend_data as $index => $dividend) {
$rate_to_base_date = 1 / Arr::get($rate_to_base, Carbon::parse(Arr::get($dividend, 'date'))->toDateString(), 1);
$dividend['dividend_amount_base'] = $dividend['dividend_amount'] * $rate_to_base_date;
$dividend['dividend_amount_base'] = $dividend['dividend_amount'] * $rate_to_base_date;
$chunk[$index] = [...$dividend, ...['id' => Str::uuid()->toString(), 'updated_at' => now(), 'created_at' => now()]];
}
$dividend_data[$index] = [...$dividend, ...['id' => Str::uuid()->toString(), 'updated_at' => now(), 'created_at' => now()]];
}
// insert records
(new self)->insertOrIgnore($chunk->toArray());
});
// insert records
(new self)->insertOrIgnore($dividend_data->toArray());
dump('6. inserted for '.$symbol);
// sync to holdings
self::syncHoldings($symbol);
@@ -147,9 +134,7 @@ class Dividend extends Model
// sync last dividend amount to market data table
$market_data->last_dividend_amount = $dividend_data->sortByDesc('date')->first()['dividend_amount'];
$market_data->save();
}
}
public static function syncHoldings(string $symbol): void
@@ -206,6 +191,7 @@ class Dividend extends Model
'date' => $dividend['date'],
'portfolio_id' => $holding->portfolio_id,
'symbol' => $holding->symbol,
'currency' => $holding->market_data->currency,
'transaction_type' => 'BUY',
'reinvested_dividend' => true,
'cost_basis' => 0,
+7 -3
View File
@@ -152,7 +152,12 @@ class Portfolio extends Model
$total_performance = [];
$holdings->each(function ($holding) use (&$total_performance) {
// get unique currencies for holdings
foreach ($holdings->groupBy('market_data.currency')->keys() as $currency) {
$currency_rates[$currency] = CurrencyRate::timeSeriesRates($currency, $holdings->min('first_transaction_date'), now());
}
$holdings->each(function ($holding) use (&$total_performance, $currency_rates) {
$period = CarbonPeriod::create(
$holding->first_transaction_date,
@@ -163,7 +168,6 @@ class Portfolio extends Model
$daily_performance = $holding->dailyPerformance($holding->first_transaction_date, now());
$all_history = app(MarketDataInterface::class)->history($holding->symbol, $holding->first_transaction_date, now());
$currency_rates = CurrencyRate::timeSeriesRates($holding->market_data->currency, $holding->first_transaction_date, now());
$holding_performance = [];
@@ -179,7 +183,7 @@ class Portfolio extends Model
$holding_performance[$date] = [
'date' => $date,
'portfolio_id' => $this->id,
'total_market_value' => $total_market_value * (1 / Arr::get($currency_rates, $date, 1)),
'total_market_value' => $total_market_value * (1 / Arr::get($currency_rates[$holding->market_data->currency], $date, 1)),
];
}
}
@@ -3,6 +3,7 @@
declare(strict_types=1);
use App\Models\CurrencyRate;
use App\Models\Holding;
use App\Models\Transaction;
use Database\Seeders\CurrencySeeder;
use Database\Seeders\MarketDataSeeder;
@@ -96,9 +97,14 @@ return new class extends Migration
'--force' => true,
]);
CurrencyRate::timeSeriesRates(
'', // use fake currency to force
Transaction::min('date')
Holding::all()->groupBy('market_data.currency')->keys()->each(
fn ($currency) => dispatch(
function () use ($currency) {
CurrencyRate::timeSeriesRates(
$currency,
Transaction::min('date')
);
})
);
CurrencyRate::refreshCurrencyData();
+6 -4
View File
@@ -54,8 +54,7 @@ class MarketDataSeeder extends Seeder
$rowCount++;
if ($rowCount % $chunkSize == 0) {
DB::table('market_data')->upsert($this->rows, ['symbol'], ['name', 'currency', 'meta_data']);
$this->rows = [];
$this->bulkInsert($this->rows);
}
}
}
@@ -77,11 +76,14 @@ class MarketDataSeeder extends Seeder
}
}
public function bulkInsert(array $rows)
public function bulkInsert($rows)
{
try {
DB::table('market_data')->insertOrIgnore($rows);
dispatch(
fn () => DB::table('market_data')->upsert($rows, ['symbol'], ['name', 'currency', 'meta_data'])
);
$this->rows = [];
} catch (\Throwable $e) {
File diff suppressed because it is too large Load Diff
Binary file not shown.
-1
View File
@@ -84,7 +84,6 @@ class ImportExportTest extends TestCase
]);
$holding = Holding::create([
'id' => '9cf8a662-7347-49fb-b9de-0cc1430a8d1f',
'portfolio_id' => '9e792bb8-94e7-4ed3-b8cc-43b50d34c337',
'symbol' => 'ACME',
'quantity' => 0,
+1 -1
View File
@@ -21,7 +21,7 @@ class MarketDataTest extends TestCase
'--force' => true,
]);
$this->assertEquals(14464, MarketData::count('symbol'));
$this->assertEquals(14262, MarketData::count('symbol'));
}
public function test_can_get_quote_from_provider()
+37 -11
View File
@@ -133,11 +133,8 @@ class MultiCurrencyTest extends TestCase
$portfolio = Portfolio::factory()->create();
$transaction = Transaction::factory()->buy()->lastYear()->portfolio($portfolio->id)->symbol('ACME')->create();
$expected_num_calls = count(collect(CarbonPeriod::create($transaction->date, now()))->chunk(500));
Frankfurter::expects('setSymbols')
->andReturnSelf()
->times($expected_num_calls);
->andReturnSelf();
Frankfurter::expects('timeSeries')
->andReturn(['rates' => [
now()->subDays(3)->toDateString() => [
@@ -152,8 +149,7 @@ class MultiCurrencyTest extends TestCase
now()->toDateString() => [
'ZZZ' => .01,
],
]])
->times($expected_num_calls);
]]);
CurrencyRate::timeSeriesRates(
'', // use fake currency to force
@@ -229,8 +225,40 @@ class MultiCurrencyTest extends TestCase
->andReturn(['rates' => $results]);
$result = CurrencyRate::timeSeriesRates('ZZZ', $start, $end);
$this->assertEquals(count($period) - 1, count($result));
$result = CurrencyRate::all();
$this->assertEquals(count($period), count($result));
}
public function test_can_get_time_series_rates_with_null_currency()
{
$start = now()->subWeeks(2);
$end = now();
$period = CarbonPeriod::create($start, $end);
// mock response from Frankfurter
$results = [];
collect($period->copy()->filter('isWeekday'))->each(function ($date) use (&$results) {
$date = $date->toDateString();
$results[$date] = [
'FOO' => random_int(10, 150) / 1000,
];
});
Frankfurter::expects('setSymbols')
->andReturnSelf();
Frankfurter::expects('timeSeries')
->andReturn(['rates' => $results]);
$result = CurrencyRate::timeSeriesRates(null, $start, $end);
$this->assertEquals(0, count($result));
$result = CurrencyRate::all();
$this->assertEquals(count($period), count($result));
}
public function test_time_series_rate_calls_are_chunked()
@@ -252,11 +280,9 @@ class MultiCurrencyTest extends TestCase
});
Frankfurter::expects('setSymbols')
->andReturnSelf()
->times(4);
->andReturnSelf();
Frankfurter::expects('timeSeries')
->andReturn(['rates' => $results])
->times(4);
->andReturn(['rates' => $results]);
CurrencyRate::timeSeriesRates('ZZZ', $start, $end);
}