<?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;
use Illuminate\Support\Facades\Schema;
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',
'prediction_threshold' => 0.3,
'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']
];
}
public function last(Request $request, $x)
{
if (!Schema::hasTable('mltd')) {
return [];
}
return DB::table('mltd')->orderBy('created_on', 'desc')->limit($x)->get()->toArray();
}
}