From c000034cabfc74768c9c094af758381115c70fd5 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 2 Apr 2018 07:06:00 -0500 Subject: [PATCH] Some changes to WX template layout and cache the API call out #61 --- app/Widgets/CheckWx.php | 25 ++++++---- config/cache.php | 10 ++-- .../default/widgets/check_wx.blade.php | 48 ++++++++++++------- 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/app/Widgets/CheckWx.php b/app/Widgets/CheckWx.php index c98a160b..3743190c 100644 --- a/app/Widgets/CheckWx.php +++ b/app/Widgets/CheckWx.php @@ -4,6 +4,7 @@ namespace App\Widgets; use App\Interfaces\Widget; use App\Support\Http; +use Illuminate\Support\Facades\Cache; /** * This is a widget for the 3rd party CheckWX service @@ -23,15 +24,23 @@ class CheckWx extends Widget if (!config('checkwx.api_key')) { $data = null; } else { - $url = config('checkwx.url').'/metar/'.$this->config['icao'].'/decoded'; - $data = Http::get($url, [ - 'headers' => [ - 'X-API-Key' => config('checkwx.api_key'), - 'content-type' => 'application/json', - ] - ]); + // Cache the request so we don't need to repeatedly call out + $cache = config('cache.keys.WEATHER_LOOKUP'); + $key = $cache['key'].$this->config['icao']; + + $data = Cache::remember($key, $cache['time'], function () { + $url = config('checkwx.url').'/metar/'.$this->config['icao'].'/decoded'; + $data = Http::get($url, [ + 'headers' => [ + 'X-API-Key' => config('checkwx.api_key'), + 'content-type' => 'application/json', + ] + ]); + + $data = json_decode($data); + return $data; + }); - $data = json_decode($data); if($data->results === 1) { $data = $data->data[0]; diff --git a/config/cache.php b/config/cache.php index 63f6f1a4..6a7e234e 100755 --- a/config/cache.php +++ b/config/cache.php @@ -7,15 +7,19 @@ return [ 'keys' => [ 'AIRPORT_VACENTRAL_LOOKUP' => [ - 'key' => 'airports:lookup:', + 'key' => 'airports.lookup:', 'time' => 60 * 30, ], + 'WEATHER_LOOKUP' => [ + 'key' => 'airports.weather.', // append icao + 'time' => 60 * 30, // Cache for 30 minutes + ], 'RANKS_PILOT_LIST' => [ - 'key' => 'ranks:pilot_list', + 'key' => 'ranks.pilot_list', 'time' => 60 * 10, ], 'USER_API_KEY' => [ - 'key' => 'user:apikey', + 'key' => 'user.apikey', 'time' => 60 * 5, // 5 min ], ], diff --git a/resources/views/layouts/default/widgets/check_wx.blade.php b/resources/views/layouts/default/widgets/check_wx.blade.php index 794e5bec..ee070a02 100644 --- a/resources/views/layouts/default/widgets/check_wx.blade.php +++ b/resources/views/layouts/default/widgets/check_wx.blade.php @@ -1,17 +1,13 @@ +{{-- + +If you want to edit this, you can reference the CheckWX API docs: +https://api.checkwx.com/#metar-decoded + +--}} @if(!$data)

METAR/TAF data could not be retrieved

@else - - - - - - @@ -60,5 +62,17 @@ @endif + + + + + + + +
-
- {{$data->raw_text}} -
-
Updated: {{$data->observed}}
Conditions @@ -21,14 +17,20 @@ @else {{$data->temperature->celsius}}°C @endif - , visibility - @if($unit_dist === 'km') - {{intval(str_replace(',', '', $data->visibility->meters)) / 1000}} - @else - {{$data->visibility->miles}} - @endif + + @if($data->visibility->miles) + , visibility + @if($unit_dist === 'km') + {{intval(str_replace(',', '', $data->visibility->meters)) / 1000}} + @else + {{$data->visibility->miles}} + @endif   - {{$data->humidity_percent}}% humidity + @endif + + @if($data->humidity_percent) + {{$data->humidity_percent}}% humidity + @endif
Metar +
+ {{$data->raw_text}} +
+
Updated{{$data->observed}}
@endif