OdController.php 2.59 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 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
 82
 83
 84
 85
 86
 87
 88
 89
 90
<?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;

class OdController extends Controller
{
public function start(Request $request)
{
$data = [
'timeDb_database'=> env('DB_DATABASE'),
'timeDb_host'=> env('DB_HOST'),
'timeDb_password'=> env('DB_PASSWORD'),
'timeDb_port'=> env('DB_PORT'),
'timeDb_ssl'=> 'true',
'timeDb_table'=> 'od',
'timeDb_username'=> env('DB_USERNAME'),
'k'=> '20',
'measurement'=> 'packets-loss',
"mqtt_host" => config('mqtt.host'),
"mqtt_password"=> "",
"mqtt_port"=> config('mqtt.port'),
"mqtt_topic"=> "auth/od",
"mqtt_usermane"=> "",
"outlier_life"=> "0",
"r"=> "0.1",
"slide"=> "10",
"w"=> "60"
];

$url = 'http://' . env('OD_HOST') . ':' . env('OD_PORT') . '/api/v1/od';

$response = Http::withHeaders([
'Application' => 'application/json'
])->post($url, $data);

if (!isset($response->json()['pid'])) {
return response()->json(['error' => 'error'], 504);
}

$ret = [
'component' => 'od',
'process_id' => $response->json()['pid'],
'created_at' => Carbon::now()
];

DB::table('running_processes')->insert($ret);

return $ret;
}

public function status(Request $request)
{
return DB::table('running_processes')
->where('component', '=', 'od')
->get()->toArray();
}

public function stop(Request $request, $pid)
{
$url = 'http://' . env('OD_HOST') . ':' . env('OD_PORT') . '/api/v1/od/stop/' . $pid;
$response = Http::get($url);

DB::table('running_processes')
->where('component', '=', 'od')
->where('process_id', '=', $pid)->delete();

return $response;
}

public function analyze(Request $request, $pid)
{
$url = 'http://' . env('OD_HOST') . ':' . env('OD_PORT') . '/api/v1/od/analyse/' . $pid;
$response = Http::withHeaders([
'Application' => 'application/json'
])->attach('file', $request->file('file')->get(), 'file')
->post($url);
return $response;
}

public function last(Request $request, $x)
{
return DB::table('od')->orderBy('created_on', 'desc')->limit($x)->get()->toArray();
}
}