Split trip data retrieval into own controller method

This commit is contained in:
Ben Goldsworthy 2022-11-01 02:29:33 +00:00
parent 733094b435
commit 9956dbb5d3

View file

@ -12,19 +12,64 @@ use Illuminate\Support\Facades\Storage;
class TrackerController extends Controller
{
/**
* Show the list of past trips.
* TODO: Get these from the Wayward API rather than hard-coding them.
*
* @return \Illuminate\View\View
*/
public function show_past_trips_list() {
return view('past-trips');
}
/**
* Show a trip in tracker app.
*
* @param Request $request
* @param string? $tripId
* @return \Illuminate\View\View
*/
public function show_trip(Request $request, string $tripId = null) {
if (!$tripId && !config('app.current_trip_id')) return view('no-trip');
$tripId = $tripId ?? config('app.current_trip_id');
if (!$tripId) return view('no-trip');
$viewMode = $request->input('show', null);
$tripData = $this->get_trip_data($tripId);
// If there is a file in the local cache that is less than 13 hours old
// (depending on whether it was actively tracking at last check), use that.
return view(
'tracker',
[
'trip' => $tripData,
'showAllCheckins' => ($viewMode === 'all')
]
);
}
/**
* Retrieves a trip from the local cache, or from the Wayward API.
*
* The retrieval algorithm is as follows:
* 1. If no local cache exists, download the trip data
* 2. If a local cache exists:
* 1. If the trip is inactive (i.e., finished), use the cache
* 2. If the trip is active:
* 1. If the trip was actively tracking at last check, use the
* cache if it's less than an hour old. Otherwise, download
* the new trip data.
* 2. If the trip was not actively tracking at lest check, use
* the cache if it's less than three hours old. Otherwise,
* download the new trip data.
* 3. If new trip data is downloaded, overwrite any cached data.
*
* @param string $tripId
* @return string
*/
public function get_trip_data(string $tripId) {
$tripFileName = ( $tripId ?? config('app.current_trip_id') ) . '.json';
// Returns the cached trip data if the trip is inactive (i.e., finished)
// or less than 13 hours old, depending on whether it was tracking
// at last check or not.
if (Storage::disk('local')->exists($tripFileName)) {
Log::debug("Cached trip file '{$tripFileName}' found...");
$cachedData = json_decode(Storage::disk('local')->get($tripFileName))->trip;
@ -39,17 +84,15 @@ class TrackerController extends Controller
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')]);
return $cachedData;
}
} else {
Log::debug("Cached trip file '{$tripFileName}' is for an old trip, showing from cache...");
return view('tracker', ['trip' => $cachedData, 'showAllCheckins' => ($viewMode === 'all')]);
return $cachedData;
}
}
/*
* Otherwise, download the latest tracking data.
*/
// Otherwise, download the trip data from the Wayward API.
Log::debug("No cached trip file found for '{$tripFileName}'.");
$client = new Client([
'base_uri' => 'https://app.wayward.travel/',
@ -57,11 +100,12 @@ class TrackerController extends Controller
]);
$response = $client->get('trip/'.($tripId ?? config('app.current_trip_id')).'/user/zmld8ko6qy7d9j3xvq10/json');
if ($response->getStatusCode() == 200) {
switch ($response->getStatusCode()) {
case 200:
$data = json_decode($response->getBody());
// Cache the downloaded file if it does not exist locally.
if (Storage::disk('local')->missing($tripFileName)) {
// Cache the downloaded file if it does not exist locally.
if (Storage::disk('local')->missing($tripFileName)) {
Log::debug("Caching new trip file '{$tripFileName}'.");
Storage::disk('local')->put($tripFileName, json_encode($data));
} else {
@ -76,10 +120,10 @@ class TrackerController extends Controller
}
}
return view('tracker', ['trip' => $data->trip, 'showAllCheckins' => ($viewMode === 'all')]);
} else {
// TODO: Return proper error
return 'Something went wrong';
return $data->trip;
default:
// TODO: Add proper error handling.
return "Something went wrong";
}
}
}