From 7dff14cf4c994598ad1bb0f6a12c87d19cf1cd97 Mon Sep 17 00:00:00 2001 From: Chris Bellas Date: Mon, 8 Mar 2021 15:54:29 +0200 Subject: [PATCH] Change OD, MLTD event api endpoints to plain counts within timeframe --- .../Controllers/V1/Components/MltdController.php | 22 ++++++++++++++++++---- .../Controllers/V1/Components/OdController.php | 21 +++++++++++++++++++-- api/routes/api.php | 4 ++-- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/api/app/Http/Controllers/V1/Components/MltdController.php b/api/app/Http/Controllers/V1/Components/MltdController.php index 08fe7fb..faf78c4 100644 --- a/api/app/Http/Controllers/V1/Components/MltdController.php +++ b/api/app/Http/Controllers/V1/Components/MltdController.php @@ -80,11 +80,25 @@ class MltdController extends Controller ]; } - public function last(Request $request, $x) + public function count(Request $request) { - if (!Schema::hasTable('mltd')) { - return []; + if ($request->has('start')) { + $start = Carbon::parse($request->get('start')); + } else { + $start = Carbon::now()->subHour(); } - return DB::table('mltd')->orderBy('created_on', 'desc')->limit($x)->get()->toArray(); + + if ($request->has('end')) + $end = Carbon::parse($request->get('end')); + else { + $end = Carbon::now(); + } + + return [ + 'count' => + DB::table('mltd') + ->whereBetween('created_on', [$start, $end]) + ->count() + ]; } } diff --git a/api/app/Http/Controllers/V1/Components/OdController.php b/api/app/Http/Controllers/V1/Components/OdController.php index b8f3d40..aaa0584 100644 --- a/api/app/Http/Controllers/V1/Components/OdController.php +++ b/api/app/Http/Controllers/V1/Components/OdController.php @@ -83,8 +83,25 @@ class OdController extends Controller return $response; } - public function last(Request $request, $x) + public function count(Request $request) { - return DB::table('od')->orderBy('created_on', 'desc')->limit($x)->get()->toArray(); + if ($request->has('start')) { + $start = Carbon::parse($request->get('start')); + } else { + $start = Carbon::now()->subHour(); + } + + if ($request->has('end')) + $end = Carbon::parse($request->get('end')); + else { + $end = Carbon::now(); + } + + return [ + 'count' => + DB::table('od') + ->whereBetween('incident_date', [$start, $end]) + ->count() + ]; } } diff --git a/api/routes/api.php b/api/routes/api.php index 9d67069..297c13c 100644 --- a/api/routes/api.php +++ b/api/routes/api.php @@ -32,14 +32,14 @@ Route::group(['middleware' => 'auth:api'], function () { Route::get('status', 'V1\Components\MltdController@status'); Route::get('stop/{pid}', 'V1\Components\MltdController@stop'); Route::get('{train_id}/{top}', 'V1\Components\MltdController@train'); - Route::get('events/last/{x}','V1\Components\MltdController@last'); + Route::get('events/count','V1\Components\MltdController@count'); }); Route::group(['prefix' => 'od'], function () { Route::get('start', 'V1\Components\OdController@start'); Route::get('status', 'V1\Components\OdController@status'); Route::get('stop/{pid}', 'V1\Components\OdController@stop'); Route::post('analyze/{pid}', 'V1\Components\OdController@analyze'); - Route::get('events/last/{x}','V1\Components\OdController@last'); + Route::get('events/count','V1\Components\OdController@count'); }); }); -- 2.2.2