From 9f38b837a779d38142d208a38d1686d4e4cd9c00 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sat, 6 Feb 2016 12:14:34 -0800 Subject: [PATCH] add a button to send a trip to the micropub endpoint for the visible time range --- compass/app/Http/Controllers/Api.php | 36 +++++++++++++++++++ compass/app/Http/routes.php | 16 +-------- compass/app/Jobs/TripComplete.php | 5 ++- compass/public/assets/map.js | 13 +++++++ compass/resources/views/layouts/map.blade.php | 2 ++ compass/resources/views/map.blade.php | 30 ++++++++++++---- 6 files changed, 79 insertions(+), 23 deletions(-) diff --git a/compass/app/Http/Controllers/Api.php b/compass/app/Http/Controllers/Api.php index b23e1c6..b763e0b 100644 --- a/compass/app/Http/Controllers/Api.php +++ b/compass/app/Http/Controllers/Api.php @@ -255,4 +255,40 @@ class Api extends BaseController return response(json_encode(['result' => 'ok', 'saved' => $num, 'trips' => $trips]))->header('Content-Type', 'application/json'); } + public function trip_complete(Request $request) { + $token = $request->input('token'); + if(!$token) + return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json'); + + $db = DB::table('databases')->where('write_token','=',$token)->first(); + if(!$db) + return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json'); + + if($request->input('tz')) { + $tz = new DateTimeZone($request->input('tz')); + } else { + $tz = new DateTimeZone('UTC'); + } + $start = new DateTime($request->input('start'), $tz); + $end = new DateTime($request->input('end'), $tz); + + $loc = [ + 'properties' => [ + 'mode' => $request->input('mode'), + 'start' => $start->format('c'), + 'end' => $end->format('c'), + ] + ]; + + try { + $job = (new TripComplete($db->id, $loc))->onQueue('compass'); + $this->dispatch($job); + Log::info('Got a manual trip record: '.$start->format('c').' '.$end->format('c')); + } catch(Exception $e) { + Log::warning('Received invalid trip'); + } + + return response(json_encode(['result' => 'ok']))->header('Content-Type', 'application/json'); + } + } diff --git a/compass/app/Http/routes.php b/compass/app/Http/routes.php index 7ac1e44..0106d07 100644 --- a/compass/app/Http/routes.php +++ b/compass/app/Http/routes.php @@ -1,16 +1,5 @@ get('/', 'Controller@index'); $app->post('/auth/start', 'IndieAuth@start'); @@ -27,7 +16,4 @@ $app->get('/api/query', 'Api@query'); $app->get('/api/last', 'Api@last'); $app->get('/api/input', 'Api@account'); $app->post('/api/input', 'Api@input'); - -// Event::listen('illuminate.query', function($query){ -// Log::debug($query); -// }); +$app->post('/api/trip-complete', 'Api@trip_complete'); diff --git a/compass/app/Jobs/TripComplete.php b/compass/app/Jobs/TripComplete.php index d36dc91..97bbe4e 100644 --- a/compass/app/Jobs/TripComplete.php +++ b/compass/app/Jobs/TripComplete.php @@ -44,9 +44,10 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue $results = $qz->queryRange($start, $end); $features = []; foreach($results as $id=>$record) { + // Don't include app action tracking data if(!property_exists($record->data->properties, 'action')) { $record->data->properties = array_filter((array)$record->data->properties, function($k){ - // Remove some of the app-specific tracking keys + // Remove some of the app-specific tracking keys from each record return !in_array($k, ['locations_in_payload','desired_accuracy','significant_change','pauses','deferred']); }, ARRAY_FILTER_USE_KEY); $features[] = $record->data; @@ -182,6 +183,7 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue $endTime = strtotime($features[count($features)-1]->properties['timestamp']); $duration = $endTime - $startTime; $params['trip']['properties']['duration']['properties']['num'] = $duration; + $params['trip']['properties']['duration']['properties']['unit'] = 'second'; Log::debug("Overriding duration to $duration"); $points = array_map(function($f){ @@ -198,6 +200,7 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue } if($distance) { $params['trip']['properties']['distance']['properties']['num'] = $distance; + $params['trip']['properties']['distance']['properties']['unit'] = 'meter'; Log::debug("Overriding distance to $distance"); } } diff --git a/compass/public/assets/map.js b/compass/public/assets/map.js index e956997..f7a9ec4 100644 --- a/compass/public/assets/map.js +++ b/compass/public/assets/map.js @@ -164,6 +164,19 @@ jQuery(function($){ } }); + $("#trip-create").click(function(){ + $("#trip-create").addClass("loading"); + $.post('/api/trip-complete', { + start: $("#range-from").val(), + end: $("#range-to").val(), + tz: $("#timezone").val(), + mode: $("#trip-mode").val(), + token: $("#database").data("write-token") + }, function(response) { + $("#trip-create").removeClass("loading"); + }); + }); + if($("#range-from").val() == "") { console.log("Autoselecting calendar day"); $(".calendar a[data-date="+((new Date()).toISOString().slice(0,10))+"]").focus().click(); diff --git a/compass/resources/views/layouts/map.blade.php b/compass/resources/views/layouts/map.blade.php index f419f66..8394682 100644 --- a/compass/resources/views/layouts/map.blade.php +++ b/compass/resources/views/layouts/map.blade.php @@ -18,6 +18,8 @@ + + diff --git a/compass/resources/views/map.blade.php b/compass/resources/views/map.blade.php index fcdd7cd..6fc0d5b 100644 --- a/compass/resources/views/map.blade.php +++ b/compass/resources/views/map.blade.php @@ -5,12 +5,28 @@ @include('partials/logged-in')
-
-
-
-
- -
+
+
+
+
+
+ +
+
+ @if($database->micropub_endpoint) +
+
+
+ +
+ +
+
+ @endif
@@ -44,6 +60,6 @@
-
+
@endsection