Blame view

api/app/Http/Controllers/V1/Components/MltdController.php 5.15 KB
0d8c0f816   Thanasis Naskos   initial commit
1
2
3
4
5
6
7
8
9
10
  <?php
  
  namespace App\Http\Controllers\V1\Components;
  
  use App\Http\Controllers\Controller;
  use Carbon\Carbon;
  use Illuminate\Http\Request;
  use Illuminate\Support\Facades\DB;
  use Illuminate\Support\Facades\Http;
  use Illuminate\Support\Facades\Log;
a6a20feca   Chris Bellas   Check if relation...
11
  use Illuminate\Support\Facades\Schema;
0d8c0f816   Thanasis Naskos   initial commit
12
13
14
15
16
17
18
19
20
21
  
  class MltdController extends Controller
  {
      public function start(Request $request)
      {
          $data = [
              'model_id'  => 1,
              'mqtt_host' => config('mqtt.host'),
              'mqtt_port' => config('mqtt.port'),
              'mqtt_topic'    => 'xlsiem/event',
315e4f2aa   Thanasis Naskos   Lowering MLTD pre...
22
              'prediction_threshold'  => 0.003,
0d8c0f816   Thanasis Naskos   initial commit
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
              'report_timedb_host' => env('DB_HOST'),
              'report_timedb_port' => env('DB_PORT'),
              'report_timedb_username' => env('DB_USERNAME'),
              'report_timedb_password' => env('DB_PASSWORD'),
              'report_timedb_database' => env('DB_DATABASE'),
              'report_timedb_table' => 'mltd',
              'report_timedb_ssl' => 'False',
              'report_asset_id' => 'server',
          ];
  
          $url = 'http://' . env('MLTD_HOST') . ':' . env('MLTD_PORT') . '/api/v1.0/mltd/prediction';
  
          $response = Http::withHeaders([
              'Application' => 'application/json'
          ])->post($url, $data);
  
          if (!isset($response->json()['process_id'])) {
              return response()->json(['error' => 'error'], 504);
          }
  
          $ret = [
              'component'  => 'mltd',
              'process_id' => $response->json()['process_id'],
              'created_at' => Carbon::now()
          ];
  
          DB::table('running_processes')->insert($ret);
  
          return $ret;
      }
  
      public function status(Request $request)
      {
          return DB::table('running_processes')
              ->where('component', '=', 'mltd')
              ->get()->toArray();
      }
  
      public function stop(Request $request, $pid)
      {
          $url = 'http://' . env('MLTD_HOST') . ':' . env('MLTD_PORT') . '/api/v1.0/mltd/prediction/stop/' . $pid;
          $response = Http::get($url);
  
          DB::table('running_processes')
              ->where('component', '=', 'mltd')
              ->where('process_id', '=', $pid)->delete();
  
          return $response;
      }
  
      public function train(Request $request, $train_id, $top)
      {
          $url = 'http://' . env('MLTD_HOST') . ':' . env('MLTD_PORT') . '/api/v1.0/mltd/threat-identification/' . $train_id . '/' . $top;
          $response = Http::get($url);
          return [
              'important_events' => explode(",", substr($response->json()['important_events'], 1, -1)),
              'timeframe' => $response->json()['timeframe']
          ];
      }
25df08e6c   Chris Bellas   Merged changes fr...
82

7dff14cf4   Chris Bellas   Change OD, MLTD e...
83
      public function count(Request $request)
25df08e6c   Chris Bellas   Merged changes fr...
84
      {
7adbee538   Chris Bellas   Check if OD, MLTD...
85
86
87
88
89
          if (!Schema::hasTable('mltd')) {
              return response()->json([
                  'error' => 'Relation for MLTD component does not exist'
              ], 503);
          }
7dff14cf4   Chris Bellas   Change OD, MLTD e...
90
91
92
93
          if ($request->has('start')) {
              $start = Carbon::parse($request->get('start'));
          } else {
              $start = Carbon::now()->subHour();
a6a20feca   Chris Bellas   Check if relation...
94
          }
7dff14cf4   Chris Bellas   Change OD, MLTD e...
95
96
97
98
99
100
101
102
103
104
105
106
107
  
          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()
          ];
25df08e6c   Chris Bellas   Merged changes fr...
108
      }
e90472e8b   Chris Bellas   Added events/grou...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  
      public function group(Request $request)
      {
          $request->validate([
              'unit' => 'required|string|in:second,minute,hour,day',
              'k' => 'required|integer',
              'limit' => 'nullable|integer'
          ]);
  
          if (!Schema::hasTable('mltd')) {
              return response()->json([
                  'error' => 'Relation for MLTD component does not exist'
              ], 503);
          }
  
          $q = $request->get('k') . ' ' . $request->get('unit');
          $query = DB::table('mltd')
              ->select(DB::raw('count(*)'), DB::raw("time_bucket('" . $q . "', created_on) AS interval"))
              ->groupby('interval')
              ->orderBy('interval', 'desc');
  
          if ($request->has('limit')) {
              $query = $query->limit($request->get('limit'));
          }
          return $query->get();
      }
7d8c6fcc3   Chris Bellas   Added new endpoin...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  
      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;
          ");
      }
0d8c0f816   Thanasis Naskos   initial commit
168
  }