Commit 7d8c6fcc3b83639d0be55fbd23a6594b32664127

Authored by Chris Bellas
1 parent e90472e8b3

Added new endpoints for OD, MLTD

Showing 3 changed files with 68 additions and 0 deletions

api/app/Http/Controllers/V1/Components/MltdController.php View file @ 7d8c6fc
... ... @@ -133,5 +133,38 @@
133 133 }
134 134 return $query->get();
135 135 }
  136 +
  137 + public function last(Request $request)
  138 + {
  139 + $request->validate([
  140 + 'k' => 'required|integer',
  141 + ]);
  142 +
  143 + if (!Schema::hasTable('mltd')) {
  144 + return response()->json([
  145 + 'error' => 'Relation for MLTD component does not exist'
  146 + ], 503);
  147 + }
  148 +
  149 + Carbon::now()->format('Y-m-d');
  150 +
  151 + $start = Carbon::now()->subDays($request->get('k'))->format('Y-m-d');
  152 + $end = Carbon::now()->format('Y-m-d');
  153 +
  154 + return DB::select("
  155 + select series.day, coalesce(count, 0) as count from
  156 + (
  157 + SELECT created_on::date as day, count(*)
  158 + FROM mltd
  159 + WHERE created_on::date >= '" . $start . "'::date
  160 + GROUP BY day
  161 + ) AS cnt
  162 + right outer join
  163 + ( SELECT ts::date as day FROM generate_series ( '" . $start . "'::date, '" . $end . "'::date, '1 day') AS ts )
  164 + as series
  165 + on series.day = cnt.day
  166 + order by series.day;
  167 + ");
  168 + }
136 169 }
api/app/Http/Controllers/V1/Components/OdController.php View file @ 7d8c6fc
... ... @@ -137,5 +137,38 @@
137 137 }
138 138 return $query->get();
139 139 }
  140 +
  141 + public function last(Request $request)
  142 + {
  143 + $request->validate([
  144 + 'k' => 'required|integer',
  145 + ]);
  146 +
  147 + if (!Schema::hasTable('od')) {
  148 + return response()->json([
  149 + 'error' => 'Relation for OD component does not exist'
  150 + ], 503);
  151 + }
  152 +
  153 + Carbon::now()->format('Y-m-d');
  154 +
  155 + $start = Carbon::now()->subDays($request->get('k'))->format('Y-m-d');
  156 + $end = Carbon::now()->format('Y-m-d');
  157 +
  158 + return DB::select("
  159 + select series.day, coalesce(count, 0) as count from
  160 + (
  161 + SELECT incident_date::date as day, count(*)
  162 + FROM od
  163 + WHERE incident_date::date >= '" . $start . "'::date
  164 + GROUP BY day
  165 + ) AS cnt
  166 + right outer join
  167 + ( SELECT ts::date as day FROM generate_series ( '" . $start . "'::date, '" . $end . "'::date, '1 day') AS ts )
  168 + as series
  169 + on series.day = cnt.day
  170 + order by series.day;
  171 + ");
  172 + }
140 173 }
api/routes/api.php View file @ 7d8c6fc
... ... @@ -33,6 +33,7 @@
33 33 Route::get('{train_id}/{top}', 'V1\Components\MltdController@train');
34 34 Route::get('events/count','V1\Components\MltdController@count');
35 35 Route::get('events/group','V1\Components\MltdController@group');
  36 + Route::get('events/last','V1\Components\MltdController@last');
36 37 });
37 38 Route::group(['prefix' => 'od'], function () {
38 39 Route::get('start', 'V1\Components\OdController@start');
... ... @@ -41,6 +42,7 @@
41 42 Route::post('analyze/{pid}', 'V1\Components\OdController@analyze');
42 43 Route::get('events/count','V1\Components\OdController@count');
43 44 Route::get('events/group','V1\Components\OdController@group');
  45 + Route::get('events/last','V1\Components\OdController@last');
44 46 });
45 47 });
46 48