-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #372 from HendrikPrinsZA/challenge/fx-conversion
Fixed issue with benchmark + added FxConversion challenge + Laravel v11
- Loading branch information
Showing
40 changed files
with
1,601 additions
and
2,028 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace App\Challenges\A; | ||
|
||
use App\Challenges\Modules\FxConversionModule; | ||
use App\Enums\CurrencyCode; | ||
use App\KataChallenge; | ||
use Carbon\CarbonPeriod; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Facades\Config; | ||
|
||
/** | ||
* Test some approaches for fx conversions | ||
* | ||
* A. Fetch each rate from db | ||
* B. Fetch each rate from db and cache | ||
* C. Fetch each rate from db and cache in chunks of (month/year) | ||
* D. Cache all rates (extreme?) | ||
* | ||
* General notes | ||
* - New rates should be retained in cache for max 24hrs | ||
*/ | ||
class FxConversion extends KataChallenge | ||
{ | ||
protected const DATE_FROM = '2023-01-01'; | ||
|
||
protected const DATE_TO = '2024-01-01'; | ||
|
||
protected const BASE_CURRENCY_CODE = CurrencyCode::EUR; | ||
|
||
protected const TARGET_CURRENCY_CODE = CurrencyCode::USD; | ||
|
||
public function useScriptCache(int $iteration): float | ||
{ | ||
Config::set('modules.fx-conversion.options.script-caching.enabled', false); | ||
Config::set('modules.fx-conversion.options.script-caching.strategy', 'monthly'); | ||
Config::set('modules.fx-conversion.options.global-caching.enabled', false); | ||
|
||
return $this->calculateTotalExchangeRate($iteration); | ||
} | ||
|
||
protected function calculateTotalExchangeRate(int $iteration, bool $useSingleton = false): float | ||
{ | ||
$amount = 420.69; | ||
$total = 0; | ||
|
||
$dateFrom = Carbon::createFromFormat('Y-m-d', self::DATE_FROM); | ||
$dateTo = $dateFrom->copy()->addDays($iteration); | ||
|
||
$dateToMax = Carbon::createFromFormat('Y-m-d', self::DATE_TO); | ||
if ($dateTo > $dateToMax) { | ||
$dateTo = $dateToMax; | ||
} | ||
|
||
$fxConversionModule = $useSingleton ? FxConversionModule::make() : null; | ||
|
||
$dates = []; | ||
$carbonPeriod = CarbonPeriod::create($dateFrom, $dateTo); | ||
foreach ($carbonPeriod as $date) { | ||
$dates[] = $date; | ||
$total += $useSingleton | ||
? $fxConversionModule->convert(self::BASE_CURRENCY_CODE, self::TARGET_CURRENCY_CODE, $date, $amount) | ||
: FxConversionModule::convert(self::BASE_CURRENCY_CODE, self::TARGET_CURRENCY_CODE, $date, $amount); | ||
} | ||
|
||
// No do it in reverse | ||
while (! empty($dates)) { | ||
$date = array_pop($dates); | ||
$total += $useSingleton | ||
? $fxConversionModule->convert(self::BASE_CURRENCY_CODE, self::TARGET_CURRENCY_CODE, $date, $amount) | ||
: FxConversionModule::convert(self::BASE_CURRENCY_CODE, self::TARGET_CURRENCY_CODE, $date, $amount); | ||
} | ||
|
||
return $total; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Challenges\B; | ||
|
||
use App\Challenges\A\FxConversion as AFxConversion; | ||
use Illuminate\Support\Facades\Config; | ||
|
||
class FxConversion extends AFxConversion | ||
{ | ||
public function useScriptCache(int $iteration): float | ||
{ | ||
Config::set('modules.fx-conversion.options.script-caching.enabled', true); | ||
Config::set('modules.fx-conversion.options.script-caching.strategy', 'monthly'); | ||
Config::set('modules.fx-conversion.options.global-caching.enabled', false); | ||
|
||
return $this->calculateTotalExchangeRate($iteration, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace App\Challenges\Modules; | ||
|
||
use App\Enums\CurrencyCode; | ||
use App\Models\Currency; | ||
use App\Models\ExchangeRate; | ||
use Clockwork\Support\Laravel\Facade; | ||
use Exception; | ||
use Illuminate\Support\Carbon; | ||
|
||
class FxConversionModule extends Facade | ||
{ | ||
protected static ?FxConversionModule $instance = null; | ||
|
||
protected static array $cachedRates = []; | ||
|
||
public static function make(): self | ||
{ | ||
self::$instance ??= new self(); | ||
|
||
return self::$instance; | ||
} | ||
|
||
public static function convert( | ||
CurrencyCode $baseCurrencyCode, | ||
CurrencyCode $targetCurrencyCode, | ||
Carbon $date, | ||
float $amount | ||
): float { | ||
return $amount * self::getRate($baseCurrencyCode, $targetCurrencyCode, $date); | ||
} | ||
|
||
public static function getRate( | ||
CurrencyCode $baseCurrencyCode, | ||
CurrencyCode $targetCurrencyCode, | ||
Carbon $date | ||
): float { | ||
if ($baseCurrencyCode === $targetCurrencyCode) { | ||
return 1; | ||
} | ||
|
||
$baseCurrency = $baseCurrencyCode->getModel(); | ||
$targetCurrency = $targetCurrencyCode->getModel(); | ||
|
||
if (config('modules.fx-conversion.options.script-caching.enabled')) { | ||
$cacheKey = sprintf('%d:%d:%s', $baseCurrency->id, $targetCurrency->id, $date->toDateString()); | ||
|
||
if (isset(self::$cachedRates[$cacheKey]['rate'])) { | ||
return self::$cachedRates[$cacheKey]['rate']; | ||
} | ||
} | ||
|
||
return self::getRateFromDatabase($baseCurrency, $targetCurrency, $date); | ||
} | ||
|
||
protected static function getRateFromDatabase( | ||
Currency $baseCurrency, | ||
Currency $targetCurrency, | ||
Carbon $date | ||
): float { | ||
$dateString = $date->toDateString(); | ||
|
||
$exchangeRates = ExchangeRate::query() | ||
->select('rate', 'date') | ||
->where('base_currency_id', $baseCurrency->id) | ||
->where('target_currency_id', $targetCurrency->id); | ||
|
||
$exchangeRates = match (config('modules.fx-conversion.options.script-caching.strategy')) { | ||
'daily' => $exchangeRates->where('date', $dateString), | ||
'monthly' => $exchangeRates->whereBetween('date', [ | ||
$date->copy()->startOfMonth()->subMonth(), | ||
$date->copy()->addMonth()->endOfMonth(), | ||
]), | ||
'yearly' => $exchangeRates->whereBetween('date', [ | ||
$date->copy()->startOfMonth()->subMonth(), | ||
$date->copy()->addMonth()->endOfMonth(), | ||
]), | ||
'all' => $exchangeRates, | ||
}; | ||
|
||
$exchangeRates = $exchangeRates->get()->mapWithKeys(fn (ExchangeRate $exchangeRate) => [ | ||
sprintf('%d:%d:%s', $baseCurrency->id, $targetCurrency->id, $exchangeRate->date->format('Y-m-d')) => [ | ||
'rate' => $exchangeRate->rate, | ||
'monthly_rate_open' => $exchangeRate->monthly_rate_open, | ||
'monthly_rate_average' => $exchangeRate->monthly_rate_average, | ||
'monthly_rate_close' => $exchangeRate->monthly_rate_close, | ||
], | ||
])->toArray(); | ||
|
||
$actualExchangeRateKey = sprintf('%d:%d:%s', $baseCurrency->id, $targetCurrency->id, $date->toDateString()); | ||
$actualExchangeRate = $exchangeRates[$actualExchangeRateKey] ?? null; | ||
if (is_null($actualExchangeRate)) { | ||
throw new Exception(sprintf( | ||
'No exchange rate found for %s to %s on %s', | ||
$baseCurrency->code->value, | ||
$targetCurrency->code->value, | ||
$dateString | ||
)); | ||
} | ||
|
||
if (config('modules.fx-conversion.options.script-caching.enabled')) { | ||
self::$cachedRates = [ | ||
...self::$cachedRates, | ||
...$exchangeRates, | ||
]; | ||
} | ||
|
||
return $actualExchangeRate['rate']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace App\Challenges\Modules; | ||
|
||
use App\Enums\CurrencyCode; | ||
use App\Models\Currency; | ||
use App\Models\ExchangeRate; | ||
use Exception; | ||
use Illuminate\Support\Carbon; | ||
|
||
class FxConversionModule | ||
{ | ||
protected static array $cachedRates = []; | ||
|
||
public static function convert( | ||
CurrencyCode $baseCurrencyCode, | ||
CurrencyCode $targetCurrencyCode, | ||
Carbon $date, | ||
float $amount | ||
): float { | ||
return $amount * self::getRate($baseCurrencyCode, $targetCurrencyCode, $date); | ||
} | ||
|
||
public static function getRate( | ||
CurrencyCode $baseCurrencyCode, | ||
CurrencyCode $targetCurrencyCode, | ||
Carbon $date | ||
): float { | ||
if ($baseCurrencyCode === $targetCurrencyCode) { | ||
return 1; | ||
} | ||
|
||
$baseCurrency = $baseCurrencyCode->getModel(); | ||
$targetCurrency = $targetCurrencyCode->getModel(); | ||
|
||
if (config('modules.fx-conversion.options.script-caching.enabled')) { | ||
$cacheKey = sprintf('%d:%d:%s', $baseCurrency->id, $targetCurrency->id, $date->toDateString()); | ||
|
||
if (isset(self::$cachedRates[$cacheKey]['rate'])) { | ||
return self::$cachedRates[$cacheKey]['rate']; | ||
} | ||
} | ||
|
||
return self::getRateFromDatabase($baseCurrency, $targetCurrency, $date); | ||
} | ||
|
||
protected static function getRateFromDatabase( | ||
Currency $baseCurrency, | ||
Currency $targetCurrency, | ||
Carbon $date | ||
): float { | ||
$dateString = $date->toDateString(); | ||
|
||
$exchangeRates = ExchangeRate::query() | ||
->select('rate', 'date') | ||
->where('base_currency_id', $baseCurrency->id) | ||
->where('target_currency_id', $targetCurrency->id); | ||
|
||
$exchangeRates = match (config('modules.fx-conversion.options.script-caching.strategy')) { | ||
'daily' => $exchangeRates->where('date', $dateString), | ||
'monthly' => $exchangeRates->whereBetween('date', [ | ||
$date->copy()->startOfMonth()->subMonth(), | ||
$date->copy()->addMonth()->endOfMonth(), | ||
]), | ||
'yearly' => $exchangeRates->whereBetween('date', [ | ||
$date->copy()->startOfMonth()->subMonth(), | ||
$date->copy()->addMonth()->endOfMonth(), | ||
]), | ||
'all' => $exchangeRates, | ||
}; | ||
|
||
$exchangeRates = $exchangeRates->get()->mapWithKeys(fn (ExchangeRate $exchangeRate) => [ | ||
sprintf('%d:%d:%s', $baseCurrency->id, $targetCurrency->id, $exchangeRate->date->format('Y-m-d')) => [ | ||
'rate' => $exchangeRate->rate, | ||
'monthly_rate_open' => $exchangeRate->monthly_rate_open, | ||
'monthly_rate_average' => $exchangeRate->monthly_rate_average, | ||
'monthly_rate_close' => $exchangeRate->monthly_rate_close, | ||
], | ||
])->toArray(); | ||
|
||
$actualExchangeRateKey = sprintf('%d:%d:%s', $baseCurrency->id, $targetCurrency->id, $date->toDateString()); | ||
$actualExchangeRate = $exchangeRates[$actualExchangeRateKey] ?? null; | ||
if (is_null($actualExchangeRate)) { | ||
throw new Exception(sprintf( | ||
'No exchange rate found for %s to %s on %s', | ||
$baseCurrency->code->value, | ||
$targetCurrency->code->value, | ||
$dateString | ||
)); | ||
} | ||
|
||
if (config('modules.fx-conversion.options.script-caching.enabled')) { | ||
self::$cachedRates = [ | ||
...self::$cachedRates, | ||
...$exchangeRates, | ||
]; | ||
} | ||
|
||
return $actualExchangeRate['rate']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
class KataChallengeScoreOutputsMd5Exception extends KataChallengeScoreException | ||
{ | ||
} |
Oops, something went wrong.