diff --git a/api/app/Http/Controllers/V1/Partners/VdmController.php b/api/app/Http/Controllers/V1/Partners/VdmController.php index 57697ac..6a56f47 100644 --- a/api/app/Http/Controllers/V1/Partners/VdmController.php +++ b/api/app/Http/Controllers/V1/Partners/VdmController.php @@ -7,37 +7,27 @@ use App\Http\Controllers\Controller; use App\Http\Requests\VdmRequest; use Carbon\Carbon; use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Log; -use Salman\Mqtt\Facades\Mqtt; class VdmController extends Controller { public function store(VdmRequest $request) { -// $dbEvents = []; -// $mqttEvents = []; -// foreach ($request->get('event_alarm') as $ev) { -// $dbEvents[] = [ -// 'asset_id' => $request->get('asset_id'), -// 'event_alarm_id' => $ev['event_alarm_id'], -// 'event_alarm_char' => $ev['event_alarm_char'], -// 'name' => isset($ev['name']) ? $ev['name'] : null, -// 'time' => Carbon::createFromTimestamp($request->get('timestamp')), -// ]; -// $mqttEvents[] = [ -// 'asset_id' => $request->get('asset_id'), -// 'event_alarm_id' => $ev['event_alarm_id'], -// 'event_alarm_char' => $ev['event_alarm_char'], -// 'name' => isset($ev['name']) ? $ev['name'] : null, -// 'time' => $request->get('timestamp'), -// ]; -// } -// -// // publish to MQTT Broker -// // Mqtt::ConnectAndPublish('adt/event', json_encode($mqttEvents), $request->user()->id); -// //Mqtt::ConnectAndPublish('adt/event', json_encode($mqttEvents), 1); -// //DB::table('vdm')->insert($dbEvents); + $records = []; + // merge two input arrays + $cve = $request->get('cve'); + $cwe = $request->get('cwe'); + + $merged = array_merge($cve, $cwe); + + foreach ($merged as $item) { + $records[] = [ + 'asset_id' => $request->get('asset_id'), + 'time' => Carbon::createFromTimestamp($request->get('timestamp')), + 'value' => $item + ]; + } + DB::table('vdm')->insert($records); return response()->json(['success' => 'success'], 200); } } diff --git a/api/database/migrations/2020_04_23_151253_create_vdm_table.php b/api/database/migrations/2020_04_23_151253_create_vdm_table.php new file mode 100644 index 0000000..742ea37 --- /dev/null +++ b/api/database/migrations/2020_04_23_151253_create_vdm_table.php @@ -0,0 +1,37 @@ +bigIncrements('uuid'); + $table->timestamp('time'); + $table->string('asset_id'); + $table->string('value'); + }); + + DB::raw("SELECT create_hypertable('vdm', 'time', 'asset_id', 2);"); + DB::raw("CREATE INDEX ON vdm (asset_id, time desc);"); + DB::raw("CREATE INDEX ON vdm (time desc, asset_id);"); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('vdm'); + } +}