refactor(dashboard,history): replace ViewModels with Pinia stores and improve backend
This commit is contained in:
@@ -9,12 +9,10 @@ use Spatie\LaravelData\Data;
|
||||
class DashboardData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public ElectricityData $electricity,
|
||||
public ?ElectricityData $electricity,
|
||||
public ?ElectricityData $electricityCheapest,
|
||||
public ?ElectricityData $electricityDurable,
|
||||
public ?ElectricityData $electricityPriciest,
|
||||
public GasData $gas,
|
||||
public ?GasData $gasCheapest,
|
||||
public ?GasData $gasPriciest,
|
||||
public ?GasData $gas,
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -14,19 +14,25 @@ class DashboardService
|
||||
|
||||
public function getDashboardData(): DashboardData
|
||||
{
|
||||
$electricity = $this->repository->retrieveElectricity();
|
||||
$gas = $this->repository->retrieveGas();
|
||||
|
||||
$now = Carbon::now();
|
||||
|
||||
$electricity = $this->repository->retrieveElectricity();
|
||||
|
||||
$eNow = $electricity->whereBetween('startDate', [$now->copy()->subHour(), $now])->first();
|
||||
$eNow = $electricity->first(
|
||||
fn ($item) => $item->startDate instanceof Carbon
|
||||
&& $item->startDate->gte($now->copy()->subHour())
|
||||
&& $item->startDate->lte($now)
|
||||
);
|
||||
$eCheapest = $electricity->where('totalPriceTaxIncluded', $electricity->min('totalPriceTaxIncluded'))->first();
|
||||
$eDurable = $electricity->where('sustainabilityScore', $electricity->max('sustainabilityScore'))->first();
|
||||
$ePriciest = $electricity->where('totalPriceTaxIncluded', $electricity->max('totalPriceTaxIncluded'))->first();
|
||||
|
||||
$gas = $this->repository->retrieveGas();
|
||||
$gNow = $gas->whereBetween('startDate', [$now->copy()->setHour(0), $now])->first();
|
||||
$gCheapest = $gas->where('totalPriceTaxIncluded', $gas->min('totalPriceTaxIncluded'))->first();
|
||||
$gPriciest = $gas->where('totalPriceTaxIncluded', $gas->max('totalPriceTaxIncluded'))->first();
|
||||
$gNow = $gas->first(
|
||||
fn ($item) => $item->startDate instanceof Carbon
|
||||
&& $item->startDate->gte($now->copy()->startOfDay())
|
||||
&& $item->startDate->lte($now)
|
||||
);
|
||||
|
||||
return new DashboardData(
|
||||
$eNow,
|
||||
@@ -34,8 +40,6 @@ class DashboardService
|
||||
$eDurable,
|
||||
$ePriciest,
|
||||
$gNow,
|
||||
$gCheapest,
|
||||
$gPriciest
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
namespace App\Features\History\Services;
|
||||
|
||||
use App\Contracts\PriceRepository;
|
||||
use App\Data\ElectricityData;
|
||||
use App\Data\GasData;
|
||||
use App\Features\History\Data\HistoryData;
|
||||
use Carbon\Carbon;
|
||||
|
||||
@@ -16,9 +14,12 @@ class HistoryService
|
||||
|
||||
public function getHistoryData(Carbon $date): HistoryData
|
||||
{
|
||||
$electricity = ElectricityData::collect($this->repository->retrieveElectricityForDate($date));
|
||||
$gas = GasData::collect($this->repository->retrieveGasForDate($date));
|
||||
$electricity = $this->repository->retrieveElectricityForDate($date);
|
||||
$gas = $this->repository->retrieveGasForDate($date);
|
||||
|
||||
return new HistoryData($electricity, $gas);
|
||||
return new HistoryData(
|
||||
electricity: $electricity,
|
||||
gas: $gas
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,39 +13,74 @@ use Override;
|
||||
|
||||
class ZonneplanPriceRepository implements ContractsPriceRepository
|
||||
{
|
||||
private int $cacheTtl;
|
||||
private string $cachePrefix;
|
||||
|
||||
public function __construct(
|
||||
public PriceClient $client
|
||||
) {}
|
||||
) {
|
||||
$this->cacheTtl = config('cache_prices.ttl', 3600);
|
||||
$this->cachePrefix = config('cache_prices.prefix', 'zonneplan:');
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function retrieveElectricity(): Collection
|
||||
{
|
||||
$data = Cache::remember('electricity:upcomming', 3600, fn () => $this->client->getElectricityPrices()->toArray());
|
||||
$raw = $this->remember(
|
||||
'electricity:upcoming',
|
||||
fn () => $this->client->getElectricityPrices()->toArray()
|
||||
);
|
||||
|
||||
return ElectricityData::collect($data, Collection::class);
|
||||
return ElectricityData::collect($raw, Collection::class);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function retrieveGas(): Collection
|
||||
{
|
||||
$data = Cache::remember('gas:upcomming', 3600, fn () => $this->client->getGasPrices()->toArray());
|
||||
$raw = $this->remember(
|
||||
'gas:upcoming',
|
||||
fn () => $this->client->getGasPrices()->toArray()
|
||||
);
|
||||
|
||||
return GasData::collect($data, Collection::class);
|
||||
return GasData::collect($raw, Collection::class);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function retrieveElectricityForDate(Carbon $date): Collection
|
||||
{
|
||||
$data = Cache::remember('electricity:'.$date->format('Y-m-d'), 3600, fn () => $this->client->getElectricityPricesForDate($date)->toArray());
|
||||
$raw = $this->remember(
|
||||
'electricity:' . $date->format('Y-m-d'),
|
||||
fn () => $this->client->getElectricityPricesForDate($date)->toArray()
|
||||
);
|
||||
|
||||
return GasData::collect($data, Collection::class);
|
||||
return ElectricityData::collect($raw, Collection::class);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function retrieveGasForDate(Carbon $date): Collection
|
||||
{
|
||||
$data = Cache::remember('gas:'.$date->format('Y-m-d'), 3600, fn () => $this->client->getGasPricesForDate($date)->toArray());
|
||||
$raw = $this->remember(
|
||||
'gas:' . $date->format('Y-m-d'),
|
||||
fn () => $this->client->getGasPricesForDate($date)->toArray()
|
||||
);
|
||||
|
||||
return GasData::collect($data, Collection::class);
|
||||
return GasData::collect($raw, Collection::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache via JSON encoding to ensure clean array structure on retrieval.
|
||||
*
|
||||
* @param callable(): array $callback
|
||||
* @return array
|
||||
*/
|
||||
private function remember(string $key, callable $callback): array
|
||||
{
|
||||
$cached = Cache::remember(
|
||||
$this->cachePrefix . $key,
|
||||
$this->cacheTtl,
|
||||
fn () => json_encode($callback())
|
||||
);
|
||||
|
||||
return json_decode($cached, true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user