Skip to content

Commit

Permalink
update caching for open meteo
Browse files Browse the repository at this point in the history
  • Loading branch information
skybluesofa committed Feb 4, 2024
1 parent 54fafca commit 57e61e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
78 changes: 41 additions & 37 deletions app/Apis/OpenMeteo/OpenMeteo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Facades\TemperatureBlanketConfig;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

Expand All @@ -18,6 +17,8 @@ class OpenMeteo

protected string $timezone;

protected int $ttl = 60 * 60 * 24;

public function __construct(?Carbon $date)
{
$this->date = $date ?? new Carbon();
Expand All @@ -26,64 +27,67 @@ public function __construct(?Carbon $date)
$this->timezone = TemperatureBlanketConfig::get('timezone');
}

public function get(?Carbon $date = null): Collection
public function get(?Carbon $date = null): ?array
{
$this->date = $date ?? new Carbon();

return collect(Cache::remember($this->getCacheKey($this->date), 60 * 60, function () {
if (! Cache::has($this->getCacheKey($this->date))) {
$now = Carbon::now();
$today = new Carbon($this->date);
$startDate = $today->clone()->subMonth()->format('Y-m-01');
$endDate = $today->clone()->addMonth();
$endDate = ($endDate > $now) ? $now->format('Y-m-d') : $endDate->format('Y-m-01');
$baseUrl = 'https://archive-api.open-meteo.com/v1/archive?latitude='.$this->latitude.'&longitude='.$this->longitude.'&start_date='.$startDate.'&end_date='.$endDate.'&daily=temperature_2m_max,temperature_2m_min,temperature_2m_mean,daylight_duration,rain_sum,snowfall_sum&temperature_unit=fahrenheit&precipitation_unit=inch&timezone='.$this->timezone;
$response = Http::get($baseUrl);

$data = [];
$weatherInfo = json_decode($response, true);

$weatherInfo = json_decode(Http::get($baseUrl), true);
foreach ($weatherInfo['daily']['time'] as $key => $date) {
$data[$date] = [
'date' => $date,
'temp' => [
'high' => null,
'low' => null,
'avg' => null,
],
'daylight' => number_format(($weatherInfo['daily']['daylight_duration'][$key] / 3600), 2),
'precipitation' => [
'rain' => null,
'snow' => null,
],
];

if ((new Carbon($date))->format('Ymd') <= (new Carbon())->format('Ymd')) {
$data[$date]['precipitation']['rain'] = number_format($weatherInfo['daily']['rain_sum'][$key], 2);
$data[$date]['precipitation']['snow'] = number_format($weatherInfo['daily']['snowfall_sum'][$key], 2);
$data[$date]['temp']['high'] = $weatherInfo['daily']['temperature_2m_max'][$key];
$data[$date]['temp']['low'] = $weatherInfo['daily']['temperature_2m_min'][$key];

$average = $weatherInfo['daily']['temperature_2m_mean'][$key];
if (empty($average) && ! is_null($weatherInfo['daily']['temperature_2m_max'][$key]) && ! is_null($weatherInfo['daily']['temperature_2m_min'][$key])) {
$average = ($weatherInfo['daily']['temperature_2m_max'][$key] + $weatherInfo['daily']['temperature_2m_min'][$key]) / 2;
}
$data[$date]['temp']['avg'] = $average;
$weatherDate = new Carbon($date);
if ($weatherDate->format('Ymd') > $now->format('Ymd')) {
continue;
}

$average = $weatherInfo['daily']['temperature_2m_mean'][$key];
if (empty($average) && ! is_null($weatherInfo['daily']['temperature_2m_max'][$key]) && ! is_null($weatherInfo['daily']['temperature_2m_min'][$key])) {
$average = ($weatherInfo['daily']['temperature_2m_max'][$key] + $weatherInfo['daily']['temperature_2m_min'][$key]) / 2;
}

$data = null;
if (! is_null($average)) {
$data = [
'date' => $date,
'temp' => [
'high' => $weatherInfo['daily']['temperature_2m_max'][$key],
'low' => $weatherInfo['daily']['temperature_2m_min'][$key],
'avg' => $average,
],
'daylight' => number_format(($weatherInfo['daily']['daylight_duration'][$key] / 3600), 2),
'precipitation' => [
'rain' => number_format($weatherInfo['daily']['rain_sum'][$key], 2),
'snow' => number_format($weatherInfo['daily']['snowfall_sum'][$key], 2),
],
];
}

Cache::set($this->getCacheKey($weatherDate), $data, $this->ttl);
Cache::set($this->getCacheWrittenKey($weatherDate), $now, $this->ttl);
}
}

Cache::set($this->getCachWritteneKey($today), Carbon::now($this->timezone));
return Cache::get($this->getCacheKey($this->date));
}

return $data;
}));
public function cachedDate(Carbon $date): Carbon
{
return Cache::get($this->getCacheWrittenKey($date));
}

protected function getCacheKey(Carbon $date): string
{
return 'openmeteo.'.$this->latitude.'.'.$this->longitude.'.'.$date->format('Ymd');
}

protected function getCachWritteneKey(Carbon $date): string
protected function getCacheWrittenKey(Carbon $date): string
{
return $this->getCacheKey($date).'written';
return $this->getCacheKey($date).'.written';
}
}
16 changes: 6 additions & 10 deletions app/Http/Controllers/TemperatureBlanketController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;

class TemperatureBlanketController extends Controller
{
Expand All @@ -29,17 +28,14 @@ public function show()

protected function getWeatherData(): Collection
{
$cachedWeatherData = OpenMeteo::get($this->date);

$yesterday = $this->date->clone()->subDay(1);
$tomorrow = $this->date->clone()->addDay(1);
$displayData = [
$yesterday->format('Y-m-d') => $cachedWeatherData[$yesterday->format('Y-m-d')] ?? null,
$this->date->format('Y-m-d') => $cachedWeatherData[$this->date->format('Y-m-d')] ?? null,
$tomorrow->format('Y-m-d') => $cachedWeatherData[$tomorrow->format('Y-m-d')] ?? null,
];

$this->weatherData = collect($displayData);
$this->weatherData = collect([
$yesterday->format('Y-m-d') => OpenMeteo::get($yesterday) ?? null,
$this->date->format('Y-m-d') => OpenMeteo::get($this->date) ?? null,
$tomorrow->format('Y-m-d') => OpenMeteo::get($tomorrow) ?? null,
]);

return $this->weatherData;
}
Expand All @@ -48,7 +44,7 @@ protected function getJsonOutput()
{
return [
'meta' => [
'cachedDate' => Cache::get('openmeteo.'.$this->date->format('Ymd').'.written')?->format('Y-m-d h:i:sa') ?? null,
'cachedDate' => OpenMeteo::cachedDate($this->date)?->format('Y-m-d h:i:sa') ?? null,
'columns' => TemperatureBlanketConfig::get('columns'),
'design' => TemperatureBlanketConfig::design(),
'colors' => TemperatureBlanketConfig::colors(),
Expand Down

0 comments on commit 57e61e5

Please sign in to comment.