Skip to content

Commit

Permalink
feat: add search by PCI id (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
pietrocomelli authored Apr 24, 2024
1 parent c433099 commit eb338d3
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 4 deletions.
9 changes: 7 additions & 2 deletions app/Http/Controllers/HardwareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function index(Request $request, string $installation)

// If search term is empty, return an empty view
if ($searchTerm === null || $searchTerm === '') {
return view('hardware', ['matchingHardware' => collect(), 'installation' => $installation]);
return view('hardware', ['matchingHardware' => collect(),
'installation' => $installation,
'searchTerm' => $searchTerm]);
}
// Perform a query to find all hardware that contain the search term
if ($installation === 'NethServer') {
Expand Down Expand Up @@ -103,6 +105,9 @@ public function index(Request $request, string $installation)
}

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

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\NethsecurityPCI;
use App\Models\NethserverPCI;

class PciController extends Controller
{
public function index(Request $request, string $installation){
$pciSearch = $request->input('pci_search');
$pciHardwareMatch = [];
$count = 0;

if($pciSearch === null || $pciSearch === ''){
return view('hardwarePci', ['pciHardwareMatch' => collect(),
'installation' => $installation,
'pciSearch' => $pciSearch]);
}

if($installation === 'NethSecurity'){
if(! empty($pciSearch)){
if (preg_match('/^[0-9a-fA-F]{4}:[0-9a-fA-F]{4}$/', $pciSearch)){
$ids = explode(':', $pciSearch);
$vendorId = $ids[0];
$deviceId = $ids[1];

$pciHardwareMatch = NethsecurityPCI::where('vendor_id', $vendorId)
->where('device_id', $deviceId)
->get();
}else{
return view('hardwarePci', ['installation' => $installation, 'pciSearch' => $pciSearch])
-> with('error','The format of the PCI ID is incorrect. Make sure to enter a value in the correct
format (xxxx:xxxx), where xxxx represents a sequence of 4 hexadecimal characters.');
}
}
}else if($installation === 'NethServer'){
if(! empty($pciSearch)){
if (preg_match('/^[0-9a-fA-F]{4}:[0-9a-fA-F]{4}$/', $pciSearch)) {
$ids = explode(':', $pciSearch);
$vendorId = $ids[0];
$deviceId = $ids[1];

$pciHardwareMatch = NethserverPCI::where('vendor_id', $vendorId)
->where('device_id', $deviceId)
->get();
}else {
return view('hardwarePci', ['installation' => $installation, 'pciSearch' => $pciSearch])
-> with('error','The format of the PCI ID is incorrect. Make sure to enter a value in the correct
format (xxxx:xxxx), where xxxx represents a sequence of 4 hexadecimal characters.');
}
}
}

$count = $pciHardwareMatch->count();

$pciHardware = [];
$hardwareCounts = [];

foreach ($pciHardwareMatch as $hardware){
$key = $hardware->class_id . '_'
. $hardware->vendor_id . '_'
. $hardware->device_id . '_'
. $hardware->class_name . '_'
. $hardware->vendor_name . '_'
. $hardware->device_name . '_'
. $hardware->driver;

if(array_key_exists($key, $pciHardware)){
$hardwareCounts[$key]++;
}else{
$pciHardware[$key] = $hardware;
$hardwareCounts[$key] = 1;
}
}

return view('hardwarePci', ['pciHardware' => $pciHardware,
'count' => $count,
'installation' => $installation,
'hardwareCounts' => $hardwareCounts,
'pciSearch' => $pciSearch]);
}
}
10 changes: 10 additions & 0 deletions app/Models/NethsecurityPCI.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 NethsecurityPCI extends Model
{
protected $table= 'nethsecurity_pci';
}
10 changes: 10 additions & 0 deletions app/Models/NethserverPCI.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 NethserverPCI extends Model
{
protected $table= 'nethserver_pci';
}
38 changes: 38 additions & 0 deletions database/migrations/2024_04_23_102211_nethserver_pci_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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_pci AS
SELECT
pci_obj->>'class_id' as class_id,
pci_obj->>'vendor_id' as vendor_id,
pci_obj->>'device_id' as device_id,
pci_obj->>'class_name' as class_name,
pci_obj->>'vendor_name' as vendor_name,
pci_obj->>'device_name' as device_name,
pci_obj->>'driver' as driver
FROM installations,
json_array_elements(data->'facts'->'nodes'->'1'->'pci') as pci_obj
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_pci");
}
};
37 changes: 37 additions & 0 deletions database/migrations/2024_04_23_102939_nethsecurity_pci_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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_pci AS
SELECT
pci_obj->>'class_id' as class_id,
pci_obj->>'vendor_id' as vendor_id,
pci_obj->>'device_id' as device_id,
pci_obj->>'class_name' as class_name,
pci_obj->>'vendor_name' as vendor_name,
pci_obj->>'device_name' as device_name,
pci_obj->>'driver' as driver
FROM installations,
json_array_elements(data->'facts'->'pci') as pci_obj
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_pci");
}
};
31 changes: 31 additions & 0 deletions public/css/hardware.css
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,35 @@ body {

.nav-item{
padding: 0px;
}

.btn.btn-secondary{
height: 43.5px;
line-height: center;
padding: 9px 20px;
margin-bottom: 3px;
font-size: 15px;
}

.card {
text-align: left;
width: auto;
margin: 0 auto;
}

.dropdown-item {
margin-left: 0px;
}

.dropdown-item:hover {
background-color: lightslategrey;
margin-left: 0px;
}

.dropdown-item.search-by-name {
color: blue;
}

.dropdown-item.search-by-id {
color: blue;
}
14 changes: 12 additions & 2 deletions resources/views/hardware.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Hardware</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="{{ asset('css/hardware.css') }}">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
Expand All @@ -28,14 +29,23 @@
<div class="container">
<h1>Find {{$installation}} Hardware</h1>
<p>Enter a search term in the input box below to find {{$installation}} hardware matching your requirements.</p>

<form action="{{ route('hardware', ['installation' => $installation]) }}" method="GET">
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Search by name</button>
<div class="dropdown-menu" style="width: 10px;" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item search-by-name" href="{{ route('hardware', ['installation' => $installation]) }}">Search by name </a>
<a class="dropdown-item" href="{{ route('hardware-pci', ['installation' => $installation]) }}">Search by PCI id</a>
</div>
</div>
@csrf
<input type="text" name="search_term" id="search_term" placeholder="Search hardware...">
<button type="submit">Search</button>

</form>

@if (empty($groupedInputMatch))
@if ($searchTerm === null || $searchTerm = '')
<p></p>
@elseif (empty($groupedInputMatch))
<p>No hardware found</p>
@else
<ul>
Expand Down
82 changes: 82 additions & 0 deletions resources/views/hardwarePci.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hardware PCI</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="{{ asset('css/hardware.css') }}">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<link rel="icon" href="{{ asset('images/logoNethesis.png')}}">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand"><img src="{{ asset('images/logoNethesis.png') }}" alt="Logo Nethesis"></a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" href="{{route('map')}}">Map</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="{{route('select-hardware')}}">Select Hardware</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="scrollable-content">
<div class="container">
<h1>Find {{$installation}} Hardware PCI</h1>
<p>Enter the Vendor ID and Device ID in the input fields below to find {{$installation}} PCI hardware matching your requirements.</p>

<form action="{{ route('hardware-pci', ['installation' => $installation]) }}" method="GET">
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Search by PCI id
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="{{ route('hardware', ['installation' => $installation]) }}">Search by name</a>
<a class="dropdown-item search-by-id" href="{{ route('hardware-pci', ['installation' => $installation]) }}">Search by PCI id</a>
</div>
</div>
@csrf
<input type="text" name="pci_search" id="pci_search" placeholder="Search hardware PCI (xxxx:xxxx) ...">
<button type="submit">Search</button>
</form>

@if($pciSearch === null || $pciSearch = '')
<p></p>
@elseif (isset($error))
<div class="alert alert-danger" role="alert">
{{ $error }}
</div>
@elseif (empty($pciHardware))
<p>No hardware found</p>
@else
<p>{{$count}} hardware found </p>
@foreach ($pciHardware as $key => $hardware)
<div class="card">
<ul class="list-group list-group-flush">
<li calss="list-group-item">
<div class="card-header">
<strong> Hardware occurences ({{ $hardwareCounts[$key] }})</strong><br>
</div>
<br>
<li class="list-group-item"><strong><label>Class ID:</label></strong> {{ $hardware->class_id }}</li><br>
<li class="list-group-item"><strong>Vendor ID:</strong> {{ $hardware->vendor_id }}</li><br>
<li class="list-group-item"><strong>Device ID:</strong> {{ $hardware->device_id }}</li><br>
<li class="list-group-item"><strong>Class Name:</strong> {{ $hardware->class_name }}</li><br>
<li class="list-group-item"><strong>Vendor Name:</strong> {{ $hardware->vendor_name }}</li><br>
<li class="list-group-item"><strong>Device Name:</strong> {{ $hardware->device_name }}</li><br>
<li class="list-group-item"><strong>Driver:</strong> {{ $hardware->driver ?? 'N/A' }}</li><br>
<br>
</li>
</ul>
</div>
<br>
@endforeach
@endif
</div>
</body>
</html>
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Http\Controllers\CompatibilityController;
use App\Http\Controllers\HardwareController;
use App\Http\Controllers\PciController;
use Illuminate\Support\Facades\Route;

/*
Expand All @@ -28,3 +29,5 @@
})->name('select-hardware');

Route::get('/hardware/{installation}', [HardwareController::class, 'index'])->name('hardware');

Route::get('/hardware-pci/{installation}', [PciController::class, 'index'])->name('hardware-pci');

0 comments on commit eb338d3

Please sign in to comment.