Skip to content

Commit

Permalink
chore: merge staging into master (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsANameToo authored Oct 9, 2023
2 parents 5e70933 + 5e5415e commit 1c5ac65
Show file tree
Hide file tree
Showing 134 changed files with 2,373 additions and 1,551 deletions.
15 changes: 15 additions & 0 deletions app/Console/Commands/CachePrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function handle(CryptoDataCache $crypto, PriceChartCache $cache, MarketDa
if (! $prices->isEmpty()) {
$crypto->setPrices($currency.'.'.$period, $prices);
$cache->setHistorical($currency, $period, $this->statsByPeriod($period, $prices));
$cache->setHistoricalRaw($currency, $period, $this->statsByPeriodRaw($period, $prices));
}
});
});
Expand All @@ -75,6 +76,20 @@ private function statsByPeriod(string $period, Collection $datasets): Collection
};
}

private function statsByPeriodRaw(string $period, Collection $datasets): Collection
{
$data = match ($period) {
'day' => $datasets->take(-24),
'week' => $datasets->take(-7),
'month' => $datasets->take(-30),
'quarter' => $datasets->take(-120),
'year' => $datasets->take(-365),
default => $datasets,
};

return $this->groupByDate($data, 'U');
}

private function groupByDate(Collection $datasets, string $format): Collection
{
return $datasets
Expand Down
43 changes: 2 additions & 41 deletions app/Http/Controllers/TransactionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@
namespace App\Http\Controllers;

use App\Services\BigNumber;
use App\Services\Timestamp;
use Carbon\Carbon;
use App\Services\Cache\Statistics;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

final class TransactionsController
{
public const STATS_TTL = 300;

public function __invoke(): View
{
$data = $this->transactionData();
$data = Statistics::transactionData();

return view('app.transactions', [
'transactionCount' => $data['transaction_count'],
Expand All @@ -26,38 +21,4 @@ public function __invoke(): View
'averageFee' => BigNumber::new($data['average_fee'])->toFloat(),
]);
}

private function transactionData(): array
{
return Cache::remember('transactions:stats', self::STATS_TTL, function () {
$timestamp = Timestamp::fromUnix(Carbon::now()->subDays(1)->unix())->unix();
$data = (array) DB::connection('explorer')
->table('transactions')
->select([
'multipayment_volume' => function ($query) use ($timestamp) {
$query->selectRaw('SUM(MP_AMOUNT)')
->from(function ($query) use ($timestamp) {
$query->selectRaw('(jsonb_array_elements(t.asset->\'payments\')->>\'amount\')::numeric as MP_AMOUNT')
->from('transactions', 't')
->where('type', 6)
->where('timestamp', '>', $timestamp);
}, 'b');
},
])
->selectRaw('COUNT(*) as transaction_count')
->selectRaw('SUM(amount) as volume')
->selectRaw('SUM(fee) as total_fees')
->selectRaw('AVG(fee) as average_fee')
->from('transactions')
->where('timestamp', '>', $timestamp)
->first();

return [
'transaction_count' => $data['transaction_count'],
'volume' => ($data['volume'] ?? 0) + ($data['multipayment_volume'] ?? 0),
'total_fees' => $data['total_fees'] ?? 0,
'average_fee' => $data['average_fee'] ?? 0,
];
});
}
}
2 changes: 1 addition & 1 deletion app/Http/Livewire/BlockTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function render(): View
public function getNoResultsMessageProperty(): null|string
{
if ($this->blocks->total() === 0) {
return trans('tables.blocks.no_results.no_results');
return trans('tables.blocks.no_results');
}

return null;
Expand Down
14 changes: 7 additions & 7 deletions app/Http/Livewire/Concerns/AvailablePeriods.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ trait AvailablePeriods
{
private function defaultPeriod(): string
{
return StatsPeriods::WEEK;
return StatsPeriods::DAY;
}

private function availablePeriods(): array
{
return [
StatsPeriods::DAY => trans('forms.statistics.periods.day'),
StatsPeriods::WEEK => trans('forms.statistics.periods.week'),
StatsPeriods::MONTH => trans('forms.statistics.periods.month'),
StatsPeriods::QUARTER => trans('forms.statistics.periods.quarter'),
StatsPeriods::YEAR => trans('forms.statistics.periods.year'),
StatsPeriods::ALL => trans('forms.statistics.periods.all'),
StatsPeriods::DAY => trans('forms.statistics.periods.day'),
StatsPeriods::WEEK => trans('forms.statistics.periods.week'),
StatsPeriods::MONTH => trans('forms.statistics.periods.month'),
StatsPeriods::QUARTER => trans('forms.statistics.periods.quarter'),
StatsPeriods::YEAR => trans('forms.statistics.periods.year'),
StatsPeriods::ALL => trans('forms.statistics.periods.all'),
];
}
}
17 changes: 0 additions & 17 deletions app/Http/Livewire/Concerns/ManagesLatestBlocks.php

This file was deleted.

50 changes: 0 additions & 50 deletions app/Http/Livewire/Concerns/ManagesLatestTransactions.php

This file was deleted.

6 changes: 5 additions & 1 deletion app/Http/Livewire/Concerns/StatisticsChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ private function chartTheme(string $color): Collection
return collect(['name' => $color, 'mode' => Settings::theme()]);
}

private function chartHistoricalPrice(string $period): Collection
private function chartHistoricalPrice(string $period, bool $rawDates = false): Collection
{
if ($rawDates) {
return collect((new PriceChartCache())->getHistoricalRaw(Settings::currency(), $period));
}

return collect((new PriceChartCache())->getHistorical(Settings::currency(), $period));
}

Expand Down
55 changes: 55 additions & 0 deletions app/Http/Livewire/Home/Blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace App\Http\Livewire\Home;

use App\Http\Livewire\Concerns\DeferLoading;
use App\Http\Livewire\Concerns\HasTablePagination;
use App\Models\Block;
use App\Models\Scopes\OrderByHeightScope;
use App\ViewModels\ViewModelFactory;
use Illuminate\Contracts\View\View;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Component;

/**
* @property LengthAwarePaginator $blocks
* */
final class Blocks extends Component
{
use DeferLoading;
use HasTablePagination;

/** @var mixed */
protected $listeners = [
'setBlocksReady' => 'setIsReady',
'currencyChanged' => '$refresh',
];

public function render(): View
{
return view('livewire.home.blocks', [
'blocks' => ViewModelFactory::paginate($this->blocks),
]);
}

public function getNoResultsMessageProperty(): ?string
{
if ($this->blocks->total() === 0) {
return trans('tables.blocks.no_results');
}

return null;
}

public function getBlocksProperty(): LengthAwarePaginator
{
if (! $this->isReady) {
return new LengthAwarePaginator([], 0, $this->perPage);
}

return Block::withScope(OrderByHeightScope::class)
->paginate($this->perPage);
}
}
103 changes: 103 additions & 0 deletions app/Http/Livewire/Home/Chart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace App\Http\Livewire\Home;

use App\Enums\StatsPeriods;
use App\Facades\Network;
use App\Facades\Settings;
use App\Http\Livewire\Concerns\AvailablePeriods;
use App\Http\Livewire\Concerns\StatisticsChart;
use App\Services\Cache\NetworkStatusBlockCache;
use App\Services\NumberFormatter as ServiceNumberFormatter;
use Illuminate\View\View;
use Livewire\Component;

final class Chart extends Component
{
use AvailablePeriods;
use StatisticsChart;

public bool $show = true;

public string $period = '';

public string $refreshInterval = '';

/** @var mixed */
protected $listeners = [
'currencyChanged' => '$refresh',
'themeChanged' => '$refresh',
'updateChart' => '$refresh',
];

public function mount(): void
{
$this->refreshInterval = (string) config('arkscan.statistics.refreshInterval', '60');
$this->period = $this->defaultPeriod();
$this->show = Network::canBeExchanged();
}

public function render(): View
{
$chartData = $this->chartHistoricalPrice($this->period, true);

/** @var array<float> $datasets */
$datasets = $chartData->get('datasets');

/** @var array<int> $labels */
$labels = $chartData->get('labels');

return view('livewire.home.chart', [
'mainValueFiat' => $this->mainValueFiat(),
'datasets' => collect($datasets),
'labels' => collect($labels),
'chartTheme' => $this->chartTheme($this->mainValueVariation($chartData->get('datasets', [])) === 'up' ? 'green' : 'red'),
'options' => $this->availablePeriods(),
'refreshInterval' => $this->refreshInterval,
]);
}

public function setPeriod(string $period): void
{
if (! in_array($period, array_keys($this->availablePeriods()), true)) {
return;
}

$this->period = $period;
}

private function mainValueFiat(): string
{
$currency = Settings::currency();
$price = $this->getPrice($currency);

return ServiceNumberFormatter::currency($price, $currency);
}

private function getPrice(string $currency): float
{
return (new NetworkStatusBlockCache())->getPrice(Network::currency(), $currency) ?? 0.0;
}

private function mainValueVariation(array $dataset): string
{
// Determine difference based on first datapoint
$initialValue = collect($dataset)->first();
$currentValue = $this->getPrice(Settings::currency());

return $initialValue > $currentValue ? 'down' : 'up';
}

private function availablePeriods(): array
{
return [
StatsPeriods::ALL => trans('pages.home.charts.periods.all'),
StatsPeriods::DAY => trans('pages.home.charts.periods.day'),
StatsPeriods::WEEK => trans('pages.home.charts.periods.week'),
StatsPeriods::MONTH => trans('pages.home.charts.periods.month'),
StatsPeriods::YEAR => trans('pages.home.charts.periods.year'),
];
}
}
Loading

0 comments on commit 1c5ac65

Please sign in to comment.