49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?php
|
|
|
|
// app/Services/Base/BaseMetricService.php
|
|
|
|
namespace App\Services\Base;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
abstract class BaseMetricService
|
|
{
|
|
protected array $cache = [];
|
|
|
|
protected array $config = [];
|
|
|
|
abstract protected function getMetricId(): int;
|
|
|
|
abstract protected function calculate(array $departmentIds, string $startDate, string $endDate): array;
|
|
|
|
public function getCached(array $departmentIds, string $startDate, string $endDate): array
|
|
{
|
|
$cacheKey = $this->getCacheKey($departmentIds, $startDate, $endDate);
|
|
|
|
if (isset($this->cache[$cacheKey])) {
|
|
return $this->cache[$cacheKey];
|
|
}
|
|
|
|
try {
|
|
$result = $this->calculate($departmentIds, $startDate, $endDate);
|
|
$this->cache[$cacheKey] = $result;
|
|
|
|
return $result;
|
|
} catch (\Exception $e) {
|
|
Log::error('Error in '.static::class.': '.$e->getMessage());
|
|
|
|
return array_fill_keys($departmentIds, 0);
|
|
}
|
|
}
|
|
|
|
protected function getCacheKey(array $departmentIds, string $startDate, string $endDate): string
|
|
{
|
|
return static::class.'_'.md5(implode(',', $departmentIds).$startDate.$endDate);
|
|
}
|
|
|
|
public function clearCache(): void
|
|
{
|
|
$this->cache = [];
|
|
}
|
|
}
|