Skip to content

Commit

Permalink
✨ Preparations for multiple data sources (#3034)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrLevin authored Dec 13, 2024
1 parent 4a80d07 commit 6ee2bcd
Show file tree
Hide file tree
Showing 24 changed files with 546 additions and 488 deletions.
43 changes: 25 additions & 18 deletions app/Console/Commands/RefreshCurrentTrips.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace App\Console\Commands;

use App\DataProviders\DataProviderBuilder;
use App\DataProviders\DataProviderInterface;
use App\DataProviders\HafasStopoverService;
use App\Enum\TripSource;
use App\Exceptions\HafasException;
use App\Http\Controllers\HafasController;
use App\Models\Trip;
use App\Models\Checkin;
use App\Models\Trip;
use Illuminate\Console\Command;
use PDOException;

Expand All @@ -15,6 +17,11 @@ class RefreshCurrentTrips extends Command
protected $signature = 'trwl:refreshTrips';
protected $description = 'Refresh delay data from current active trips';

private function getDataProvider(): DataProviderInterface {
// Probably only HafasController is needed here, because this Command is very Hafas specific
return (new DataProviderBuilder)->build();
}

public function handle(): int {
$this->info('Getting trips to be refreshed...');

Expand All @@ -23,22 +30,22 @@ public function handle(): int {
->join('train_stopovers as origin_stopovers', 'origin_stopovers.id', '=', 'train_checkins.origin_stopover_id')
->join('train_stopovers as destination_stopovers', 'destination_stopovers.id', '=', 'train_checkins.destination_stopover_id')
->where(function($query) {
$query->where('destination_stopovers.arrival_planned', '>=', now()->subMinutes(20))
->orWhere('destination_stopovers.arrival_real', '>=', now()->subMinutes(20));
})
$query->where('destination_stopovers.arrival_planned', '>=', now()->subMinutes(20))
->orWhere('destination_stopovers.arrival_real', '>=', now()->subMinutes(20));
})
->where(function($query) {
$query->where('origin_stopovers.departure_planned', '<=', now()->addMinutes(20))
->orWhere('origin_stopovers.departure_real', '<=', now()->addMinutes(20));
})
$query->where('origin_stopovers.departure_planned', '<=', now()->addMinutes(20))
->orWhere('origin_stopovers.departure_real', '<=', now()->addMinutes(20));
})
->where(function($query) {
$query->where('hafas_trips.last_refreshed', '<', now()->subMinutes(5))
->orWhereNull('hafas_trips.last_refreshed');
})
->where('hafas_trips.source', TripSource::HAFAS->value)
->select('hafas_trips.*')
->distinct()
->orderBy('hafas_trips.last_refreshed')
->get();
$query->where('hafas_trips.last_refreshed', '<', now()->subMinutes(5))
->orWhereNull('hafas_trips.last_refreshed');
})
->where('hafas_trips.source', TripSource::HAFAS->value)
->select('hafas_trips.*')
->distinct()
->orderBy('hafas_trips.last_refreshed')
->get();

if ($trips->isEmpty()) {
$this->warn('No trips to be refreshed');
Expand All @@ -53,8 +60,8 @@ public function handle(): int {
$this->info('Refreshing trip ' . $trip->trip_id . ' (' . $trip->linename . ')...');
$trip->update(['last_refreshed' => now()]);

$rawHafas = HafasController::fetchRawHafasTrip($trip->trip_id, $trip->linename);
$updatedCounts = HafasController::refreshStopovers($rawHafas);
$rawHafas = $this->getDataProvider()->fetchRawHafasTrip($trip->trip_id, $trip->linename);
$updatedCounts = HafasStopoverService::refreshStopovers($rawHafas);
$this->info('Updated ' . $updatedCounts->stopovers . ' stopovers.');

//set duration for refreshed trips to null, so it will be recalculated
Expand Down
10 changes: 10 additions & 0 deletions app/DataProviders/DataProviderBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\DataProviders;

class DataProviderBuilder
{
public function build(): DataProviderInterface {
return new Hafas();
}
}
24 changes: 24 additions & 0 deletions app/DataProviders/DataProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\DataProviders;

use App\Enum\TravelType;
use App\Models\Station;
use Carbon\Carbon;

interface DataProviderInterface
{
public function fetchHafasTrip(string $tripID, string $lineName);

public function fetchRawHafasTrip(string $tripId, string $lineName);

public function getStations(string $query, int $results);

public function getDepartures(Station $station, Carbon $when, int $duration = 15, TravelType $type = null, bool $localtime = false);

public function getNearbyStations(float $latitude, float $longitude, int $results);

public function getStationByRilIdentifier(string $rilIdentifier);

public function getStationsByFuzzyRilIdentifier(string $rilIdentifier);
}
12 changes: 12 additions & 0 deletions app/DataProviders/FptfHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\DataProviders;

use App\Enum\TravelType;

class FptfHelper
{
public static function checkTravelType(?TravelType $type, TravelType $travelType): string {
return (is_null($type) || $type === $travelType) ? 'true' : 'false';
}
}
Loading

0 comments on commit 6ee2bcd

Please sign in to comment.