From a352b704be027a6d69ef709165a2532a4e3f1b28 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Thu, 1 Oct 2015 18:23:51 +0000 Subject: [PATCH] adds route to return the current database name. handles more date formats and better error checking --- compass/app/Http/Controllers/Api.php | 34 +++++++++++++++++++++++++--- compass/app/Http/routes.php | 1 + 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/compass/app/Http/Controllers/Api.php b/compass/app/Http/Controllers/Api.php index 72e2aa8..d25fa19 100644 --- a/compass/app/Http/Controllers/Api.php +++ b/compass/app/Http/Controllers/Api.php @@ -12,6 +12,18 @@ use DateTime, DateTimeZone; class Api extends BaseController { + public function account(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'); + + return response(json_encode(['name' => $db->name]))->header('Content-Type', 'application/json'); + } + public function query(Request $request) { $token = $request->input('token'); if(!$token) @@ -77,14 +89,30 @@ class Api extends BaseController $qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'w'); + $num = 0; foreach($request->input('locations') as $loc) { if(array_key_exists('properties', $loc) && array_key_exists('timestamp', $loc['properties'])) { - $date = DateTime::createFromFormat('U', $loc['properties']['timestamp']); - $line = $qz->add($date, $loc); + try { + if(preg_match('/^\d+\.\d+$/', $loc['properties']['timestamp'])) + $date = DateTime::createFromFormat('U.u', $loc['properties']['timestamp']); + elseif(preg_match('/^\d+$/', $loc['properties']['timestamp'])) + $date = DateTime::createFromFormat('U', $loc['properties']['timestamp']); + else + $date = new DateTime($loc['properties']['timestamp']); + + if($date) { + $num++; + $qz->add($date, $loc); + } else { + Log::warning('Received invalid date: ' . $loc['properties']['timestamp']); + } + } catch(Exception $e) { + Log::warning('Received invalid date: ' . $loc['properties']['timestamp']); + } } } - return response(json_encode(['result' => 'ok']))->header('Content-Type', 'application/json'); + return response(json_encode(['result' => 'ok', 'saved' => $num]))->header('Content-Type', 'application/json'); } } diff --git a/compass/app/Http/routes.php b/compass/app/Http/routes.php index 2a5ba3c..5394aba 100644 --- a/compass/app/Http/routes.php +++ b/compass/app/Http/routes.php @@ -24,6 +24,7 @@ $app->post('/settings/{name:[A-Za-z0-9]+}', 'Controller@updateSettings'); $app->post('/database/create', 'Controller@createDatabase'); $app->get('/api/query', 'Api@query'); +$app->get('/api/input', 'Api@account'); $app->post('/api/input', 'Api@input'); // Event::listen('illuminate.query', function($query){