Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: populate volume from historic data #1134

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions app/Console/Commands/CacheMarketDataStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,35 +190,27 @@ private function cacheVolumeStats(string $currency, array $data, StatisticsCache

$volumeSorted = $volumes->sortBy('value');

/** @var array{timestamp: int, value: float|null} $volumeAtl */
/** @var array{timestamp: int, value: int|float|null} $volumeAtl */
$volumeAtl = $volumeSorted->first();
/** @var array{timestamp: int, value: float|null} $volumeAth */
/** @var array{timestamp: int, value: int|float|null} $volumeAth */
$volumeAth = $volumeSorted->last();

if ($volumeAtl['value'] !== null) {
if (! $this->hasChanges) {
$existingValue = $cache->getVolumeAtl($currency) ?? [];
if (Arr::get($existingValue, 'timestamp') !== $volumeAtl['timestamp'] / 1000) {
$this->hasChanges = true;
} elseif (Arr::get($existingValue, 'value') !== $volumeAtl['value']) {
$this->hasChanges = true;
}
}
$existingValue = Arr::get($cache->getVolumeAtl($currency) ?? [], 'value');
if ($existingValue === null || (float) $volumeAtl['value'] < (float) $existingValue) {
$this->hasChanges = true;

$cache->setVolumeAtl($currency, $volumeAtl['timestamp'] / 1000, $volumeAtl['value']);
$cache->setVolumeAtl($currency, $volumeAtl['timestamp'] / 1000, $volumeAtl['value']);
}
}

if ($volumeAth['value'] !== null) {
if (! $this->hasChanges) {
$existingValue = $cache->getVolumeAth($currency) ?? [];
if (Arr::get($existingValue, 'timestamp') !== $volumeAth['timestamp'] / 1000) {
$this->hasChanges = true;
} elseif (Arr::get($existingValue, 'value') !== $volumeAth['value']) {
$this->hasChanges = true;
}
}
$existingValue = Arr::get($cache->getVolumeAth($currency) ?? [], 'value');
if ($existingValue === null || (float) $volumeAth['value'] > (float) $existingValue) {
$this->hasChanges = true;

$cache->setVolumeAth($currency, $volumeAth['timestamp'] / 1000, $volumeAth['value']);
$cache->setVolumeAth($currency, $volumeAth['timestamp'] / 1000, $volumeAth['value']);
}
}
}

Expand Down
58 changes: 58 additions & 0 deletions app/Console/Commands/PopulateHistoricVolume.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Services\Cache\StatisticsCache;
use Illuminate\Console\Command;

final class PopulateHistoricVolume extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'explorer:populate-historic-volume';

/**
* The console command description.
*
* @var string|null
*/
protected $description = 'Populate historic volume values for currencies.';

public function handle(StatisticsCache $cache): void
{
/** @var string[] $currencies */
$currencies = array_keys(config('currencies'));

foreach ($currencies as $currency) {
$currencyPath = database_path('/seeders/pricing/marketcap/'.$currency.'.json');
if (! file_exists($currencyPath)) {
$this->output->writeln('Currency file not found for '.$currency);

continue;
}

/** @var string $fileContent */
$fileContent = file_get_contents($currencyPath);

/** @var array{total_volumes:array{0:int, 1:float}[]} $jsonData */
$jsonData = json_decode($fileContent, true);

$data = collect($jsonData['total_volumes'])
->map(fn ($item) => ['timestamp' => $item[0] / 1000, 'value' => floatval($item[1])])
->sortBy('value');

/** @var array{timestamp: int, value: float} $atlValue */
$atlValue = $data->first();
/** @var array{timestamp: int, value: float} $athValue */
$athValue = $data->last();

$cache->setVolumeAtl(strtoupper($currency), $atlValue['timestamp'], $atlValue['value']);
$cache->setVolumeAth(strtoupper($currency), $athValue['timestamp'], $athValue['value']);
}
}
}
20 changes: 4 additions & 16 deletions tests/Unit/Console/Commands/CacheMarketDataStatisticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,9 @@

Event::fake();

$cache->setVolumeAtl($currency, 1688774400, 50548.95038391039);
$existingAtl = $cache->getVolumeAtl($currency);

$this->artisan('explorer:cache-market-data-statistics');

Event::assertDispatchedTimes(MarketData::class, 1);

Event::fake();

$cache->setVolumeAtl($currency, 1688774500, 40548.95038391039);
$cache->setVolumeAtl($currency, 1688774400, $existingAtl['value'] + 1);

$this->artisan('explorer:cache-market-data-statistics');

Expand All @@ -310,15 +304,9 @@

Event::fake();

$cache->setVolumeAth($currency, 1698710400, 543956833.91000223);

$this->artisan('explorer:cache-market-data-statistics');

Event::assertDispatchedTimes(MarketData::class, 1);

Event::fake();
$existingAth = $cache->getVolumeAth($currency);

$cache->setVolumeAth($currency, 1698710500, 443956833.91000223);
$cache->setVolumeAth($currency, 1698710400, $existingAth['value'] - 1);

$this->artisan('explorer:cache-market-data-statistics');

Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/Console/Commands/PopulateHistoricVolumeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

use App\Services\Cache\StatisticsCache;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;

it('should cache data', function () {
$cache = new StatisticsCache();

Artisan::call('explorer:populate-historic-volume');

expect($cache->getVolumeAtl('BTC'))->toEqual([
'timestamp' => 1688774400,
'value' => 1.3374225031894302,
]);

expect($cache->getVolumeAth('USD'))->toEqual([
'timestamp' => 1726963200,
'value' => 478176573.80723304,
]);
});

it('should error for unknown currency', function () {
Config::set('currencies', [
'twd' => [
'currency' => 'TWD',
'symbol' => 'NT$',
'locale' => 'zh_TW',
],
]);

$cache = new StatisticsCache();

$outputBuffer = new Symfony\Component\Console\Output\BufferedOutput();
Artisan::call('explorer:populate-historic-volume', [], $outputBuffer);

expect($cache->getVolumeAtl('TWD'))->toBeNull();

expect($outputBuffer->fetch())->toEqual("Currency file not found for twd\n");
});
Loading