Skip to content

Commit

Permalink
feat: add hardware info page (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
pietrocomelli authored Apr 17, 2024
1 parent 3859e87 commit 98875a9
Show file tree
Hide file tree
Showing 12 changed files with 546 additions and 1 deletion.
109 changes: 109 additions & 0 deletions app/Http/Controllers/HardwareController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace App\Http\Controllers;

use App\Models\NethserverHardware;
use App\Models\NethsecurityHardware;
use Illuminate\Http\Request;

class HardwareController extends Controller
{
public function index(Request $request, String $installation)
{
// Retrieve search term from request
$searchTerm = $request->input('search_term');
// Initialize variables for storing matches and count
$matchingHardware = [];
$inputMatch = [];
$count = 0;

// If search term is empty, return an empty view
if($searchTerm === null || $searchTerm === '')
{
return view('hardware', ['matchingHardware' => collect(), 'installation' => $installation]);
}
// Perform a query to find all hardware that contain the search term
if($installation === 'NethServer'){
if(!empty($searchTerm)){
$matchingHardware = NethserverHardware::where('product_name', 'ilike', '%' . $searchTerm . '%')
->orWhere('manufacturer', 'ilike', '%' . $searchTerm . '%')
->orWhere('processor', 'ilike', '%' . $searchTerm . '%')
->orWhere('vga_controller', 'ilike', '%' . $searchTerm . '%')
->orWhere('usb_controller', 'ilike', '%' . $searchTerm . '%')
->orWhere('pci_bridge', 'ilike', '%' . $searchTerm . '%')
->orWhere('sata_controller', 'ilike', '%' . $searchTerm . '%')
->orWhere('communication_controller', 'ilike', '%' . $searchTerm . '%')
->orWhere('scsi_controller', 'ilike', '%' . $searchTerm . '%')
->orWhere('ethernet', 'ilike', '%' . $searchTerm . '%')
->get();
}
}else if($installation === 'NethSecurity'){
if(!empty($searchTerm)){
$matchingHardware = NethsecurityHardware::where('product_name', 'ilike', '%' . $searchTerm . '%')
->orWhere('manufacturer', 'ilike', '%' . $searchTerm . '%')
->orWhere('processor', 'ilike', '%' . $searchTerm . '%')
->orWhere('vga_controller', 'ilike', '%' . $searchTerm . '%')
->orWhere('usb_controller', 'ilike', '%' . $searchTerm . '%')
->orWhere('pci_bridge', 'ilike', '%' . $searchTerm . '%')
->orWhere('sata_controller', 'ilike', '%' . $searchTerm . '%')
->orWhere('communication_controller', 'ilike', '%' . $searchTerm . '%')
->orWhere('scsi_controller', 'ilike', '%' . $searchTerm . '%')
->orWhere('ethernet', 'ilike', '%' . $searchTerm . '%')
->get();
}
}
// Returning an array containing only the specific hardware elements that contain the search term
foreach ($matchingHardware as $hardware) {
if(stripos($hardware->product_name, $searchTerm) !== false){
$inputMatch[] = 'Product Name: ' . $hardware->product_name;
}else if(stripos($hardware->manufacturer, $searchTerm) !== false){
$inputMatch[] = 'Manufacturer: ' . $hardware->manufacturer;
}else if(stripos($hardware->processor, $searchTerm) !== false){
$inputMatch[] = 'Processor: ' . $hardware->processor;
}else if(stripos($hardware->vga_controller, $searchTerm) !== false){
$inputMatch[] = 'Vga Controller: ' . $hardware->vga_controller;
}else if(stripos($hardware->usb_controller, $searchTerm) !== false){
$inputMatch[] = 'Usb Controller: ' . $hardware->usb_controller;
}else if(stripos($hardware->pci_bridge, $searchTerm) !== false){
$inputMatch[] = 'Pci Bridge: ' . $hardware->pci_bridge;
}else if(stripos($hardware->sata_controller, $searchTerm) !== false){
$inputMatch[] = 'Sata Controller: ' . $hardware->sata_controller;
}else if(stripos($hardware->communication_controller, $searchTerm) !== false){
$inputMatch[] = 'Communication Controller: ' . $hardware->communication_controller;
}else if(stripos($hardware->scsi_controller, $searchTerm) !== false){
$inputMatch[] = 'SCSI Controller: ' . $hardware->scsi_controller;
}else if(stripos($hardware->ethernet, $searchTerm) !== false){
$inputMatch[] = 'Ethernet: ' . $hardware->ethernet;
}
$count++;
}

// Initialize array for grouping and count of occurrences
$groupedInputMatch = [];
$rowsCount = 0;

// Loop through inputMatch to group similar rows and count occurrences
foreach($inputMatch as $item){
list($value, $row) = explode(': ', $item, 2);

//Check if the row already exists in the corresponding group
$rowExists = in_array($row, $groupedInputMatch[$value]['rows'] ?? []);

//If the row does not exist yet, add it
if(!$rowExists){
if(isset($groupedInputMatch[$value])){
$groupedInputMatch[$value]['rows'][] = $row;
}else{
$groupedInputMatch[$value] = ['rows' => [$row]];
}
}

// Count occurrences of each row
$rowsCount = ($groupedInputMatch[$value]['occurrences'][$row] ?? 0) + 1;
$groupedInputMatch[$value]['occurrences'][$row] = $rowsCount;
}

// Return view with grouped input matches, count, and rows count
return view('hardware', ['groupedInputMatch' => $groupedInputMatch, 'count' => $count, 'installation' => $installation]);
}
}
10 changes: 10 additions & 0 deletions app/Models/NethsecurityHardware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class NethsecurityHardware extends Model
{
protected $table = 'nethsecurity_view';
}
10 changes: 10 additions & 0 deletions app/Models/NethserverHardware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class NethserverHardware extends Model
{
protected $table = 'nethserver_view';
}
68 changes: 68 additions & 0 deletions database/migrations/2024_04_11_145715_nethserver_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::statement("
CREATE OR REPLACE VIEW nethserver_view AS
SELECT
data->'facts'->'nodes'->'1'->'product'->>'name' AS product_name,
data->'facts'->'nodes'->'1'->'product'->>'manufacturer' AS manufacturer,
data->'facts'->'nodes'->'1'->'processors'->>'model' AS processor,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'nodes'->'1'->'pci') AS elem
WHERE elem->>'class_name' = 'VGA compatible controller'
LIMIT 1) AS vga_controller,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'nodes'->'1'->'pci') AS elem
WHERE elem->>'class_name' = 'USB controller'
LIMIT 1) AS usb_controller,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'nodes'->'1'->'pci') AS elem
WHERE elem->>'class_name' = 'PCI bridge'
LIMIT 1) AS pci_bridge,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'nodes'->'1'->'pci') AS elem
WHERE elem->>'class_name' = 'SATA controller'
LIMIT 1) AS sata_controller,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'nodes'->'1'->'pci') AS elem
WHERE elem->>'class_name' = 'Communication controller'
LIMIT 1) AS communication_controller,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'nodes'->'1'->'pci') AS elem
WHERE elem->>'class_name' = 'SCSI storage controller'
LIMIT 1) AS scsi_controller,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'nodes'->'1'->'pci') AS elem
WHERE elem->>'class_name' = 'Ethernet controller'
LIMIT 1) AS ethernet
FROM installations
WHERE data->>'installation' LIKE 'nethserver'
AND (data->'facts'->'nodes'->'1'->'product'->>'name') IS NOT NULL
AND updated_at >= CURRENT_TIMESTAMP - INTERVAL '72 hours';
");
}

/**
* Reverse the migrations.
*/
public function down(): void
{
DB::statement('DROP VIEW IF EXISTS nethserver_view');
}
};
67 changes: 67 additions & 0 deletions database/migrations/2024_04_11_154216_nethsecurity_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::statement("
CREATE OR REPLACE VIEW nethsecurity_view AS
SELECT
data->'facts'->'product'->>'name' AS product_name,
data->'facts'->'product'->>'manufacturer' AS manufacturer,
data->'facts'->'processors'->>'model' AS processor,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'pci') AS elem
WHERE elem->>'class_name' = 'VGA compatible controller'
LIMIT 1) AS vga_controller,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'pci') AS elem
WHERE elem->>'device_name' = 'USB controller'
LIMIT 1) AS usb_controller,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'pci') AS elem
WHERE elem->>'class_name' = 'PCI bridge'
LIMIT 1) AS pci_bridge,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'pci') AS elem
WHERE elem->>'class_name' = 'SATA controller'
LIMIT 1) AS sata_controller,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'pci') AS elem
WHERE elem->>'class_name' = 'Communication controller'
LIMIT 1) AS communication_controller,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'pci') AS elem
WHERE elem->>'class_name' = 'SCSI storage controller'
LIMIT 1) AS scsi_controller,
(SELECT DISTINCT ON (elem->>'class_id')
elem->>'device_name'
FROM json_array_elements(data->'facts'->'pci') AS elem
WHERE elem->>'class_name' = 'Ethernet controller'
LIMIT 1) AS ethernet
FROM installations
WHERE data->>'installation' LIKE 'nethsecurity'
AND updated_at >= CURRENT_TIMESTAMP - INTERVAL '72 hours';
");
}

/**
* Reverse the migrations.
*/
public function down(): void
{
DB::statement("DROP VIEW IF EXISTS nethsecurity_view");
}
};
Loading

0 comments on commit 98875a9

Please sign in to comment.