Some changes to WX template layout and cache the API call out #61

This commit is contained in:
Nabeel Shahzad 2018-04-02 07:06:00 -05:00
parent f84ff8de92
commit c000034cab
3 changed files with 55 additions and 28 deletions

View File

@ -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,6 +24,11 @@ class CheckWx extends Widget
if (!config('checkwx.api_key')) {
$data = null;
} else {
// 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' => [
@ -32,6 +38,9 @@ class CheckWx extends Widget
]);
$data = json_decode($data);
return $data;
});
if($data->results === 1) {
$data = $data->data[0];

View File

@ -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
],
],

View File

@ -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)
<p>METAR/TAF data could not be retrieved</p>
@else
<table class="table">
<tr>
<td colspan="2">
<div style="line-height:1.5em;min-height: 3em;">
{{$data->raw_text}}
</div>
</td>
</tr>
<tr>
<td colspan="2">Updated: {{$data->observed}}</td>
</tr>
<tr>
<td>Conditions</td>
<td>
@ -21,6 +17,8 @@
@else
{{$data->temperature->celsius}}°C
@endif
@if($data->visibility->miles)
, visibility
@if($unit_dist === 'km')
{{intval(str_replace(',', '', $data->visibility->meters)) / 1000}}
@ -28,7 +26,11 @@
{{$data->visibility->miles}}
@endif
&nbsp;
@endif
@if($data->humidity_percent)
{{$data->humidity_percent}}% humidity
@endif
</td>
</tr>
<tr>
@ -60,5 +62,17 @@
@endif
</td>
</tr>
<tr>
<td>Metar</td>
<td>
<div style="line-height:1.5em;min-height: 3em;">
{{$data->raw_text}}
</div>
</td>
</tr>
<tr>
<td>Updated</td>
<td>{{$data->observed}}</td>
</tr>
</table>
@endif