Add logging
This commit is contained in:
parent
40134331b5
commit
3c6bf47402
1 changed files with 24 additions and 10 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
|
@ -14,27 +15,36 @@ Route::get('/{tripId?}', function (Request $request, $tripId = null) {
|
||||||
|
|
||||||
$viewMode = $request->input('show', null);
|
$viewMode = $request->input('show', null);
|
||||||
|
|
||||||
/*
|
// If there is a file in the local cache that is less than 1–3 hours old
|
||||||
* If there is a file in the local cache that is less than 1–3 hours old
|
// (depending on whether it was actively tracking at last check), use that.
|
||||||
* (depending on whether it was actively tracking at last check), use that.
|
|
||||||
*/
|
|
||||||
$tripFileName = ( $tripId ?? config('app.current_trip_id') ) . '.json';
|
$tripFileName = ( $tripId ?? config('app.current_trip_id') ) . '.json';
|
||||||
|
|
||||||
if (Storage::disk('local')->exists($tripFileName)) {
|
if (Storage::disk('local')->exists($tripFileName)) {
|
||||||
$cachedData = json_decode(Storage::disk('local')->get($tripFileName));
|
Log::debug("Cached trip file '{$tripFileName}' found...");
|
||||||
if ($cachedData->trip->is_active) {
|
$cachedData = json_decode(Storage::disk('local')->get($tripFileName))->trip;
|
||||||
$cachedDataUpdatedAt = new DateTime($cachedData->trip->updated_at);
|
|
||||||
|
if ($cachedData->is_active) {
|
||||||
|
Log::debug("Cached trip file '{$tripFileName}' is for an active trip.");
|
||||||
|
|
||||||
|
$cachedDataUpdatedAt = new DateTime($cachedData->updated_at);
|
||||||
$now = new DateTime();
|
$now = new DateTime();
|
||||||
if (intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600) <= (($cachedData->trip->is_tracking) ? 1 : 3)) {
|
$cachingTimeout = ($cachedData->is_tracking) ? 1 : 3;
|
||||||
return view('tracker', ['trip' => $cachedData->trip, 'showAllCheckins' => ($viewMode === 'all')]);
|
$cachedDataAge = intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600);
|
||||||
|
|
||||||
|
if ($cachedDataAge <= $cachingTimeout) {
|
||||||
|
Log::debug("Cached trip file '{$tripFileName}' is younger than {$cachingTimeout} hours, showing from cache...");
|
||||||
|
return view('tracker', ['trip' => $cachedData, 'showAllCheckins' => ($viewMode === 'all')]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return view('tracker', ['trip' => $cachedData->trip, 'showAllCheckins' => ($viewMode === 'all')]);
|
Log::debug("Cached trip file '{$tripFileName}' is for an old trip, showing from cache...");
|
||||||
|
return view('tracker', ['trip' => $cachedData, 'showAllCheckins' => ($viewMode === 'all')]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Otherwise, download the latest tracking data.
|
* Otherwise, download the latest tracking data.
|
||||||
*/
|
*/
|
||||||
|
Log::debug("No cached trip file found for '{$tripFileName}'.");
|
||||||
$client = new Client([
|
$client = new Client([
|
||||||
'base_uri' => 'https://app.wayward.travel/',
|
'base_uri' => 'https://app.wayward.travel/',
|
||||||
'timeout' => 3.0
|
'timeout' => 3.0
|
||||||
|
@ -46,19 +56,23 @@ Route::get('/{tripId?}', function (Request $request, $tripId = null) {
|
||||||
|
|
||||||
// Cache the downloaded file if it does not exist locally.
|
// Cache the downloaded file if it does not exist locally.
|
||||||
if (Storage::disk('local')->missing($tripFileName)) {
|
if (Storage::disk('local')->missing($tripFileName)) {
|
||||||
|
Log::debug("Caching new trip file '{$tripFileName}'.");
|
||||||
Storage::disk('local')->put($tripFileName, json_encode($data));
|
Storage::disk('local')->put($tripFileName, json_encode($data));
|
||||||
} else {
|
} else {
|
||||||
$cachedData = json_decode(Storage::disk('local')->get($tripFileName));
|
$cachedData = json_decode(Storage::disk('local')->get($tripFileName));
|
||||||
if ($data->trip->updated_at !== $cachedData->trip->updated_at) {
|
if ($data->trip->updated_at !== $cachedData->trip->updated_at) {
|
||||||
|
Log::debug("Cached trip file '{$tripFileName}' has different 'updated_at' time, updating cache...");
|
||||||
Storage::disk('local')->put($tripFileName, json_encode($data));
|
Storage::disk('local')->put($tripFileName, json_encode($data));
|
||||||
// TODO: Cache photos locally
|
// TODO: Cache photos locally
|
||||||
} else {
|
} else {
|
||||||
|
Log::debug("Cached trip file '{$tripFileName}' has same 'updated_at' time, showing from cache...");
|
||||||
$data = $cachedData;
|
$data = $cachedData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return view('tracker', ['trip' => $data->trip, 'showAllCheckins' => ($viewMode === 'all')]);
|
return view('tracker', ['trip' => $data->trip, 'showAllCheckins' => ($viewMode === 'all')]);
|
||||||
} else {
|
} else {
|
||||||
|
// TODO: Return proper error
|
||||||
return 'Something went wrong';
|
return 'Something went wrong';
|
||||||
}
|
}
|
||||||
})->where('tripId', '[0-9a-z]{20}');
|
})->where('tripId', '[0-9a-z]{20}');
|
||||||
|
|
Loading…
Reference in a new issue