Browse Source

better error checking for missing trip properties, add cost

* add cost property
* support finding start/end location from the trip route instead of as input
pull/5/head
Aaron Parecki 9 years ago
parent
commit
4c27d75ecc
3 changed files with 431 additions and 319 deletions
  1. +2
    -1
      compass/app/Http/Controllers/Api.php
  2. +91
    -46
      compass/app/Jobs/TripComplete.php
  3. +338
    -272
      compass/composer.lock

+ 2
- 1
compass/app/Http/Controllers/Api.php View File

@ -124,7 +124,7 @@ class Api extends BaseController
} }
if($input=$request->input('before')) { if($input=$request->input('before')) {
if(preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $input)) {
if(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/', $input)) {
// If the input date is given in YYYY-mm-dd HH:mm:ss format, interpret it in the timezone given // If the input date is given in YYYY-mm-dd HH:mm:ss format, interpret it in the timezone given
$date = DateTime::createFromFormat('Y-m-d H:i:s', $input, new DateTimeZone($tz)); $date = DateTime::createFromFormat('Y-m-d H:i:s', $input, new DateTimeZone($tz));
} else { } else {
@ -233,6 +233,7 @@ class Api extends BaseController
$job = (new TripComplete($db->id, $loc))->onQueue('compass'); $job = (new TripComplete($db->id, $loc))->onQueue('compass');
$this->dispatch($job); $this->dispatch($job);
$trips++; $trips++;
Log::info('Got a trip record');
} catch(Exception $e) { } catch(Exception $e) {
Log::warning('Received invalid trip'); Log::warning('Received invalid trip');
} }

+ 91
- 46
compass/app/Jobs/TripComplete.php View File

@ -29,6 +29,8 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue
Log::info("Starting job for ".$db->name); Log::info("Starting job for ".$db->name);
Log::debug(json_encode($this->_data));
if(!$db->micropub_endpoint) { if(!$db->micropub_endpoint) {
Log::info('No micropub endpoint configured for database ' . $db->name); Log::info('No micropub endpoint configured for database ' . $db->name);
return; return;
@ -59,35 +61,57 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue
$file_path = tempnam(sys_get_temp_dir(), 'compass'); $file_path = tempnam(sys_get_temp_dir(), 'compass');
file_put_contents($file_path, json_encode($geojson)); file_put_contents($file_path, json_encode($geojson));
// Reverse geocode the start and end location to get an h-adr
$startAdr = [
'type' => 'h-adr',
'properties' => [
'latitude' => $this->_data['properties']['start-coordinates'][1],
'longitude' => $this->_data['properties']['start-coordinates'][0],
]
];
$endAdr = [
'type' => 'h-adr',
'properties' => [
'latitude' => $this->_data['properties']['end-coordinates'][1],
'longitude' => $this->_data['properties']['end-coordinates'][0],
]
];
Log::info('Looking up start and end locations');
$start = self::geocode($this->_data['properties']['start-coordinates'][1], $this->_data['properties']['start-coordinates'][0]);
if($start) {
$startAdr['properties']['locality'] = $start->locality;
$startAdr['properties']['region'] = $start->region;
$startAdr['properties']['country'] = $start->country;
Log::info('Found start: '.$start->full_name.' '.$start->timezone);
// If there are no start/end coordinates in the request, use the first and last coordinates
if(count($features)) {
if(!array_key_exists('start-coordinates', $this->_data['properties'])) {
$this->_data['properties']['start-coordinates'] = $features[0]->geometry->coordinates;
}
if(!array_key_exists('end-coordinates', $this->_data['properties'])) {
$this->_data['properties']['end-coordinates'] = $features[count($features)-1]->geometry->coordinates;
}
} }
$end = self::geocode($this->_data['properties']['end-coordinates'][1], $this->_data['properties']['end-coordinates'][0]);
if($end) {
$endAdr['properties']['locality'] = $end->locality;
$endAdr['properties']['region'] = $end->region;
$endAdr['properties']['country'] = $end->country;
Log::info('Found end: '.$end->full_name.' '.$end->timezone);
$startAdr = false;
if(array_key_exists('start-coordinates', $this->_data['properties'])) {
// Reverse geocode the start and end location to get an h-adr
$startAdr = [
'type' => 'h-adr',
'properties' => [
'latitude' => $this->_data['properties']['start-coordinates'][1],
'longitude' => $this->_data['properties']['start-coordinates'][0],
]
];
Log::info('Looking up start location');
$start = self::geocode($this->_data['properties']['start-coordinates'][1], $this->_data['properties']['start-coordinates'][0]);
if($start) {
$startAdr['properties']['locality'] = $start->locality;
$startAdr['properties']['region'] = $start->region;
$startAdr['properties']['country'] = $start->country;
Log::info('Found start: '.$start->full_name.' '.$start->timezone);
}
} else {
$start = false;
}
$endAdr = false;
if(array_key_exists('end-coordinates', $this->_data['properties'])) {
$endAdr = [
'type' => 'h-adr',
'properties' => [
'latitude' => $this->_data['properties']['end-coordinates'][1],
'longitude' => $this->_data['properties']['end-coordinates'][0],
]
];
Log::info('Looking up end location');
$end = self::geocode($this->_data['properties']['end-coordinates'][1], $this->_data['properties']['end-coordinates'][0]);
if($end) {
$endAdr['properties']['locality'] = $end->locality;
$endAdr['properties']['region'] = $end->region;
$endAdr['properties']['country'] = $end->country;
Log::info('Found end: '.$end->full_name.' '.$end->timezone);
}
} else {
$end = false;
} }
// Set the timezone of the dates based on the location // Set the timezone of the dates based on the location
@ -95,6 +119,7 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue
if($start && $start->timezone) { if($start && $start->timezone) {
$startDate->setTimeZone(new DateTimeZone($start->timezone)); $startDate->setTimeZone(new DateTimeZone($start->timezone));
} }
$endDate = new DateTime($this->_data['properties']['end']); $endDate = new DateTime($this->_data['properties']['end']);
if($end && $end->timezone) { if($end && $end->timezone) {
$endDate->setTimeZone(new DateTimeZone($end->timezone)); $endDate->setTimeZone(new DateTimeZone($end->timezone));
@ -109,29 +134,49 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue
'mode-of-transport' => $this->_data['properties']['mode'], 'mode-of-transport' => $this->_data['properties']['mode'],
'start' => $startDate->format('c'), 'start' => $startDate->format('c'),
'end' => $endDate->format('c'), 'end' => $endDate->format('c'),
'start-location' => $startAdr,
'end-location' => $endAdr,
'distance' => [
'type' => 'h-measure',
'properties' => [
'num' => round($this->_data['properties']['distance']),
'unit' => 'meter'
]
],
'duration' => [
'type' => 'h-measure',
'properties' => [
'num' => round($this->_data['properties']['duration']),
'unit' => 'second'
]
],
'route' => 'route.json' 'route' => 'route.json'
// TODO: avgpace
// TODO: avgspeed
// TODO: avgpace for runs
// TODO: avgspeed for bike rides
// TODO: avg heart rate if available
] ]
] ]
]; ];
if($startAdr) {
$params['trip']['properties']['start-location'] = $startAdr;
}
if($endAdr) {
$params['trip']['properties']['end-location'] = $endAdr;
}
if(array_key_exists('distance', $this->_data['properties'])) {
$params['trip']['properties']['distance'] = [
'type' => 'h-measure',
'properties' => [
'num' => round($this->_data['properties']['distance']),
'unit' => 'meter'
]
];
}
if(array_key_exists('duration', $this->_data['properties'])) {
$params['trip']['properties']['duration'] = [
'type' => 'h-measure',
'properties' => [
'num' => round($this->_data['properties']['duration']),
'unit' => 'second'
]
];
}
if(array_key_exists('cost', $this->_data['properties'])) {
$params['trip']['properties']['cost'] = [
'type' => 'h-measure',
'properties' => [
'num' => round($this->_data['properties']['cost'], 2),
'unit' => 'USD'
]
];
}
// echo "Micropub Params\n"; // echo "Micropub Params\n";
// print_r($params); // print_r($params);

+ 338
- 272
compass/composer.lock
File diff suppressed because it is too large
View File


Loading…
Cancel
Save