From 1bee2d0828d42b854a80cad92cb9cc4f41a3806b Mon Sep 17 00:00:00 2001 From: Merlin Roth Date: Wed, 2 Sep 2020 14:53:44 +0200 Subject: [PATCH] add departureTime and arrivalTime and name for Geo Data --- app/GeoLocation.php | 2 ++ app/Jobs/BessermitfahrenConnector.php | 8 +++-- app/Jobs/MifazConnector.php | 42 ++++++++++++++++----------- app/Search.php | 11 ------- app/Trip.php | 2 ++ app/Wrapper/SearchWrapper.php | 11 ++++--- 6 files changed, 41 insertions(+), 35 deletions(-) diff --git a/app/GeoLocation.php b/app/GeoLocation.php index 72f5cdd..01926f6 100644 --- a/app/GeoLocation.php +++ b/app/GeoLocation.php @@ -16,6 +16,7 @@ class GeoLocation extends Model protected $fillable = [ 'latitude', 'longitude', + 'name', ]; /** @@ -31,5 +32,6 @@ class GeoLocation extends Model protected $visible = [ 'latitude', 'longitude', + 'name', ]; } diff --git a/app/Jobs/BessermitfahrenConnector.php b/app/Jobs/BessermitfahrenConnector.php index 852d6a5..5fa9236 100644 --- a/app/Jobs/BessermitfahrenConnector.php +++ b/app/Jobs/BessermitfahrenConnector.php @@ -84,7 +84,9 @@ public function convertEntryToTrips($entry, $search): Collection 'startPoint' => new GeoLocation(['latitude' => $tripStart['latitude'], 'longitude' => $tripStart['longitude']]), 'endPoint' => new GeoLocation(['latitude' => $tripEnd['latitude'], 'longitude' => $tripEnd['longitude']]), 'connector' => "Bessermitfahren", - 'timestamp' => Carbon::now() + 'timestamp' => Carbon::now(), + 'departureTime' => Carbon::parse($search->departure['time'])->setTime(...explode(':',$entry[0][1][0])), + 'arrivalTime' => Carbon::parse($search->departure['time'])->setTime(...explode(':',$entry[0][2][0])), ]); $trip->setAttribute('id', 'bessermitfahren-' . md5($entry[0][0])); @@ -93,8 +95,8 @@ public function convertEntryToTrips($entry, $search): Collection 'url' => $entry[0][0], 'name' => '', 'image' => '', - 'availabilityStarts' => $search->departure['time'], - 'availabilityEnds' => $search->departure['time'] + 'availabilityStarts' => Carbon::parse($search->departure['time'])->setTime(...explode(':',$entry[0][1][0])), + 'availabilityEnds' => Carbon::parse($search->departure['time'])->setTime(...explode(':',$entry[0][2][0])), ]); $transport = new Transport([ diff --git a/app/Jobs/MifazConnector.php b/app/Jobs/MifazConnector.php index 83ab78e..b4f4682 100644 --- a/app/Jobs/MifazConnector.php +++ b/app/Jobs/MifazConnector.php @@ -72,27 +72,31 @@ public function handle() $entries = $client->getEntries($start['latitude'], $start['longitude'], $end['latitude'], $end['longitude'], $options); - $entries->each(function($entry) use ($search) - { + $entries->each(function ($entry) use ($search) { + $trips = $this->convertEntryToTrips($entry, $search); - $trips->each(function($trip) { SearchWrapper::insert($trip); }); + + $trips->each(function ($trip) { + SearchWrapper::insert($trip); + }); }); } public function convertEntryToTrips($entry, $search): Collection { - return $this->findCorrespondingDates($entry, $search)->map(function($date) use (&$entry, &$search) - { + return $this->findCorrespondingDates($entry, $search)->map(function ($date) use (&$entry, &$search) { $tripStart = explode(' ', $entry['startcoord']); $tripEnd = explode(' ', $entry['goalcoord']); $trip = new Trip([ 'created' => Carbon::create($entry['creationdate']), 'modified' => Carbon::create($entry['lastupdate']), - 'startPoint' => new GeoLocation(['latitude' => $tripStart[0], 'longitude' => $tripStart[1]]), - 'endPoint' => new GeoLocation(['latitude' => $tripEnd[0], 'longitude' => $tripEnd[1]]), + 'startPoint' => new GeoLocation(['latitude' => $tripStart[0], 'longitude' => $tripStart[1], 'name' => $entry['startloc']]), + 'endPoint' => new GeoLocation(['latitude' => $tripEnd[0], 'longitude' => $tripEnd[1], 'name' => $entry['goalloc']]), 'connector' => "Mifaz", - 'timestamp' => Carbon::now() + 'timestamp' => Carbon::now(), + 'departureTime' => $date->copy()->setTime(...explode(':', $entry['starttimebegin'])), + 'arrivalTime' => $date->copy()->setTime(...explode(':', $entry['starttimeend'])) ]); $trip->setAttribute('id', 'mifaz-' . $entry['id'] . '-' . $date->format('Ymd')); @@ -117,27 +121,30 @@ public function convertEntryToTrips($entry, $search): Collection }); } - public function findCorrespondingDates($entry, $search): Collection { + public function findCorrespondingDates($entry, $search): Collection + { // single trip - if (isset($entry['regulary']) && $entry['regulary'] == '0') - { + if (isset($entry['regulary']) && $entry['regulary'] == '0') { $date = isset($entry['journeydate']) ? Carbon::create($entry['journeydate']) : Carbon::now(); return collect([$date]); } // no offer, only search - if (isset($entry['type']) && $entry['type'] == '0') - { + if (isset($entry['type']) && $entry['type'] == '0') { return collect(); } $dates = []; // just assume empty times mean every weekday?! - if ($entry['times'] == '') { $entry['times'] = 'Mo,Di,Mi,Do,Fr,Sa,So'; } + if ($entry['times'] == '') { + $entry['times'] = 'Mo,Di,Mi,Do,Fr,Sa,So'; + } $exploded = explode(',', $entry['times']); - $weekdays = array_map(function ($w) { return self::WEEKDAYS[$w]; }, $exploded); + $weekdays = array_map(function ($w) { + return self::WEEKDAYS[$w]; + }, $exploded); $timeRange = $search->getDepartureRange(); $currDay = $timeRange[0]->copy(); @@ -145,10 +152,11 @@ public function findCorrespondingDates($entry, $search): Collection { if (in_array($currDay->weekday(), $weekdays)) { $dates[] = $currDay->copy(); } - $currDay->add(1, 'day'); } - + if($timeRange[0]==$timeRange[1]){ + $dates[] = $timeRange[0]; + } return collect($dates); } } diff --git a/app/Search.php b/app/Search.php index 991cc07..08093f3 100644 --- a/app/Search.php +++ b/app/Search.php @@ -63,20 +63,9 @@ public function trips() public function getDepartureRange(): array { $tolerance = $this->departure['toleranceInDays']; - $rangeStart = Carbon::createFromTimeString($this->departure['time'])->addDays($tolerance * -1)->startOfDay(); $rangeEnd = Carbon::createFromTimeString($this->departure['time'])->addDays($tolerance)->startOfDay(); return [$rangeStart, $rangeEnd]; } -// -// public function toJson($options = 0) -// { -// /** @var ParameterBag $json */ -// $json = parent::toJson($options); -// -// $json->set('startPoint', $this->startPoint->) -// } - - } diff --git a/app/Trip.php b/app/Trip.php index 38383a8..377173c 100644 --- a/app/Trip.php +++ b/app/Trip.php @@ -27,6 +27,7 @@ class Trip extends Model 'name', 'image', 'description', + 'departureTime', 'arrivalTime', 'availableSeats', 'connector', @@ -59,6 +60,7 @@ class Trip extends Model 'created', 'modified', 'arrivalTime', + 'departureTime', ]; public function offer() diff --git a/app/Wrapper/SearchWrapper.php b/app/Wrapper/SearchWrapper.php index c4e357e..187a6f9 100644 --- a/app/Wrapper/SearchWrapper.php +++ b/app/Wrapper/SearchWrapper.php @@ -5,6 +5,8 @@ use App\Search; use App\Trip; +use Carbon\Carbon; +use Carbon\CarbonPeriod; use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Cache; use Ds\Set; @@ -45,7 +47,6 @@ public static function find(string $searchId): array if ($search != null) { - $trip_start = Redis::geoRadius( 'trip_start', $search->startPoint->location->longitude, @@ -53,7 +54,6 @@ public static function find(string $searchId): array $search->startPoint->radius, "km" ); - $trip_end = Redis::geoRadius( 'trip_end', $search->endPoint->location->longitude, @@ -61,7 +61,6 @@ public static function find(string $searchId): array $search->endPoint->radius, "km" ); - $start_set = new Set($trip_start); $end_set = new Set($trip_end); @@ -75,7 +74,11 @@ public static function find(string $searchId): array if (count($keys) != 0) { $trips = collect(); foreach (array_filter(array_values(Cache::many($keys))) as $trip) { - $trips->push($trip); + $search_start = Carbon::parse($search->departure['time'])->addDays(-$search->departure['toleranceInDays']); + $search_end = Carbon::parse($search->departure['time'])->addDays($search->departure['toleranceInDays']); + + if ($trip->offer->availabilityStarts >= $search_start && $trip->offer->availabilityStarts <= $search_end) + $trips->push($trip); } $trips->sortByDesc('timestamp'); return $trips->toArray();