Commit 92d105af138e448d3898a650aa14f3450a6aaca0
1 parent
f3b37895ff
Exists in
master
and in
4 other branches
Added VDM persistency
Showing 2 changed files with 52 additions and 25 deletions
api/app/Http/Controllers/V1/Partners/VdmController.php
View file @
92d105a
... | ... | @@ -7,37 +7,27 @@ |
7 | 7 | use App\Http\Requests\VdmRequest; |
8 | 8 | use Carbon\Carbon; |
9 | 9 | use Illuminate\Support\Facades\DB; |
10 | -use Illuminate\Support\Facades\Log; | |
11 | -use Salman\Mqtt\Facades\Mqtt; | |
12 | 10 | |
13 | 11 | class VdmController extends Controller |
14 | 12 | { |
15 | 13 | public function store(VdmRequest $request) |
16 | 14 | { |
17 | -// $dbEvents = []; | |
18 | -// $mqttEvents = []; | |
19 | -// foreach ($request->get('event_alarm') as $ev) { | |
20 | -// $dbEvents[] = [ | |
21 | -// 'asset_id' => $request->get('asset_id'), | |
22 | -// 'event_alarm_id' => $ev['event_alarm_id'], | |
23 | -// 'event_alarm_char' => $ev['event_alarm_char'], | |
24 | -// 'name' => isset($ev['name']) ? $ev['name'] : null, | |
25 | -// 'time' => Carbon::createFromTimestamp($request->get('timestamp')), | |
26 | -// ]; | |
27 | -// $mqttEvents[] = [ | |
28 | -// 'asset_id' => $request->get('asset_id'), | |
29 | -// 'event_alarm_id' => $ev['event_alarm_id'], | |
30 | -// 'event_alarm_char' => $ev['event_alarm_char'], | |
31 | -// 'name' => isset($ev['name']) ? $ev['name'] : null, | |
32 | -// 'time' => $request->get('timestamp'), | |
33 | -// ]; | |
34 | -// } | |
35 | -// | |
36 | -// // publish to MQTT Broker | |
37 | -// // Mqtt::ConnectAndPublish('adt/event', json_encode($mqttEvents), $request->user()->id); | |
38 | -// //Mqtt::ConnectAndPublish('adt/event', json_encode($mqttEvents), 1); | |
39 | -// //DB::table('vdm')->insert($dbEvents); | |
15 | + $records = []; | |
40 | 16 | |
17 | + // merge two input arrays | |
18 | + $cve = $request->get('cve'); | |
19 | + $cwe = $request->get('cwe'); | |
20 | + | |
21 | + $merged = array_merge($cve, $cwe); | |
22 | + | |
23 | + foreach ($merged as $item) { | |
24 | + $records[] = [ | |
25 | + 'asset_id' => $request->get('asset_id'), | |
26 | + 'time' => Carbon::createFromTimestamp($request->get('timestamp')), | |
27 | + 'value' => $item | |
28 | + ]; | |
29 | + } | |
30 | + DB::table('vdm')->insert($records); | |
41 | 31 | return response()->json(['success' => 'success'], 200); |
42 | 32 | } |
43 | 33 | } |
api/database/migrations/2020_04_23_151253_create_vdm_table.php
View file @
92d105a
1 | +<?php | |
2 | + | |
3 | +use Illuminate\Database\Migrations\Migration; | |
4 | +use Illuminate\Database\Schema\Blueprint; | |
5 | +use Illuminate\Support\Facades\Schema; | |
6 | + | |
7 | +class CreateVdmTable extends Migration | |
8 | +{ | |
9 | + /** | |
10 | + * Run the migrations. | |
11 | + * | |
12 | + * @return void | |
13 | + */ | |
14 | + public function up() | |
15 | + { | |
16 | + Schema::create('vdm', function (Blueprint $table) { | |
17 | + $table->bigIncrements('uuid'); | |
18 | + $table->timestamp('time'); | |
19 | + $table->string('asset_id'); | |
20 | + $table->string('value'); | |
21 | + }); | |
22 | + | |
23 | + DB::raw("SELECT create_hypertable('vdm', 'time', 'asset_id', 2);"); | |
24 | + DB::raw("CREATE INDEX ON vdm (asset_id, time desc);"); | |
25 | + DB::raw("CREATE INDEX ON vdm (time desc, asset_id);"); | |
26 | + } | |
27 | + | |
28 | + /** | |
29 | + * Reverse the migrations. | |
30 | + * | |
31 | + * @return void | |
32 | + */ | |
33 | + public function down() | |
34 | + { | |
35 | + Schema::dropIfExists('vdm'); | |
36 | + } | |
37 | +} |