Skip to content

Commit

Permalink
add departureTime and arrivalTime and name for Geo Data
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinro committed Sep 2, 2020
1 parent 27cff3c commit 1bee2d0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
2 changes: 2 additions & 0 deletions app/GeoLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class GeoLocation extends Model
protected $fillable = [
'latitude',
'longitude',
'name',
];

/**
Expand All @@ -31,5 +32,6 @@ class GeoLocation extends Model
protected $visible = [
'latitude',
'longitude',
'name',
];
}
8 changes: 5 additions & 3 deletions app/Jobs/BessermitfahrenConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand All @@ -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([
Expand Down
42 changes: 25 additions & 17 deletions app/Jobs/MifazConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -117,38 +121,42 @@ 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();

while ($currDay->isBefore($timeRange[1])) {
if (in_array($currDay->weekday(), $weekdays)) {
$dates[] = $currDay->copy();
}

$currDay->add(1, 'day');
}

if($timeRange[0]==$timeRange[1]){
$dates[] = $timeRange[0];
}
return collect($dates);
}
}
11 changes: 0 additions & 11 deletions app/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->)
// }


}
2 changes: 2 additions & 0 deletions app/Trip.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Trip extends Model
'name',
'image',
'description',
'departureTime',
'arrivalTime',
'availableSeats',
'connector',
Expand Down Expand Up @@ -59,6 +60,7 @@ class Trip extends Model
'created',
'modified',
'arrivalTime',
'departureTime',
];

public function offer()
Expand Down
11 changes: 7 additions & 4 deletions app/Wrapper/SearchWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,23 +47,20 @@ public static function find(string $searchId): array

if ($search != null) {


$trip_start = Redis::geoRadius(
'trip_start',
$search->startPoint->location->longitude,
$search->startPoint->location->latitude,
$search->startPoint->radius,
"km"
);

$trip_end = Redis::geoRadius(
'trip_end',
$search->endPoint->location->longitude,
$search->endPoint->location->latitude,
$search->endPoint->radius,
"km"
);

$start_set = new Set($trip_start);
$end_set = new Set($trip_end);

Expand All @@ -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();
Expand Down

0 comments on commit 1bee2d0

Please sign in to comment.