From 7d8c6fcc3b83639d0be55fbd23a6594b32664127 Mon Sep 17 00:00:00 2001 From: Chris Bellas Date: Wed, 10 Mar 2021 00:21:57 +0200 Subject: [PATCH] Added new endpoints for OD, MLTD --- .../Controllers/V1/Components/MltdController.php | 33 ++++++++++++++++++++++ .../Controllers/V1/Components/OdController.php | 33 ++++++++++++++++++++++ api/routes/api.php | 2 ++ 3 files changed, 68 insertions(+) diff --git a/api/app/Http/Controllers/V1/Components/MltdController.php b/api/app/Http/Controllers/V1/Components/MltdController.php index 5f746a2..7ba8a84 100644 --- a/api/app/Http/Controllers/V1/Components/MltdController.php +++ b/api/app/Http/Controllers/V1/Components/MltdController.php @@ -133,4 +133,37 @@ class MltdController extends Controller } return $query->get(); } + + public function last(Request $request) + { + $request->validate([ + 'k' => 'required|integer', + ]); + + if (!Schema::hasTable('mltd')) { + return response()->json([ + 'error' => 'Relation for MLTD component does not exist' + ], 503); + } + + Carbon::now()->format('Y-m-d'); + + $start = Carbon::now()->subDays($request->get('k'))->format('Y-m-d'); + $end = Carbon::now()->format('Y-m-d'); + + return DB::select(" + select series.day, coalesce(count, 0) as count from + ( + SELECT created_on::date as day, count(*) + FROM mltd + WHERE created_on::date >= '" . $start . "'::date + GROUP BY day + ) AS cnt + right outer join + ( SELECT ts::date as day FROM generate_series ( '" . $start . "'::date, '" . $end . "'::date, '1 day') AS ts ) + as series + on series.day = cnt.day + order by series.day; + "); + } } diff --git a/api/app/Http/Controllers/V1/Components/OdController.php b/api/app/Http/Controllers/V1/Components/OdController.php index 3610279..331809f 100644 --- a/api/app/Http/Controllers/V1/Components/OdController.php +++ b/api/app/Http/Controllers/V1/Components/OdController.php @@ -137,4 +137,37 @@ class OdController extends Controller } return $query->get(); } + + public function last(Request $request) + { + $request->validate([ + 'k' => 'required|integer', + ]); + + if (!Schema::hasTable('od')) { + return response()->json([ + 'error' => 'Relation for OD component does not exist' + ], 503); + } + + Carbon::now()->format('Y-m-d'); + + $start = Carbon::now()->subDays($request->get('k'))->format('Y-m-d'); + $end = Carbon::now()->format('Y-m-d'); + + return DB::select(" + select series.day, coalesce(count, 0) as count from + ( + SELECT incident_date::date as day, count(*) + FROM od + WHERE incident_date::date >= '" . $start . "'::date + GROUP BY day + ) AS cnt + right outer join + ( SELECT ts::date as day FROM generate_series ( '" . $start . "'::date, '" . $end . "'::date, '1 day') AS ts ) + as series + on series.day = cnt.day + order by series.day; + "); + } } diff --git a/api/routes/api.php b/api/routes/api.php index e867e87..e4ba419 100644 --- a/api/routes/api.php +++ b/api/routes/api.php @@ -33,6 +33,7 @@ Route::group(['middleware' => 'auth:api'], function () { Route::get('{train_id}/{top}', 'V1\Components\MltdController@train'); Route::get('events/count','V1\Components\MltdController@count'); Route::get('events/group','V1\Components\MltdController@group'); + Route::get('events/last','V1\Components\MltdController@last'); }); Route::group(['prefix' => 'od'], function () { Route::get('start', 'V1\Components\OdController@start'); @@ -41,6 +42,7 @@ Route::group(['middleware' => 'auth:api'], function () { Route::post('analyze/{pid}', 'V1\Components\OdController@analyze'); Route::get('events/count','V1\Components\OdController@count'); Route::get('events/group','V1\Components\OdController@group'); + Route::get('events/last','V1\Components\OdController@last'); }); }); -- 2.2.2