From 1d80e682dc1eda9e5132384e642cbc51fca70bfe Mon Sep 17 00:00:00 2001 From: Jan_Hill Date: Mon, 23 Dec 2024 07:02:54 +0100 Subject: [PATCH] Add Logs for Caller and modify resources --- app/Http/Controllers/ApiController.php | 61 ++++ app/Http/Controllers/SmartController.php | 16 +- app/Models/CallerLog_DB.php | 12 + .../2024_12_23_044220_caller_log.php | 34 ++ resources/views/smart/caller.blade.php | 54 +-- resources/views/smart/caller_log.blade.php | 327 ++++++++++++++++++ resources/views/smart/dashboard.blade.php | 2 +- routes/api.php | 8 +- routes/web.php | 2 + 9 files changed, 483 insertions(+), 33 deletions(-) create mode 100644 app/Http/Controllers/ApiController.php create mode 100644 app/Models/CallerLog_DB.php create mode 100644 database/migrations/2024_12_23_044220_caller_log.php create mode 100644 resources/views/smart/caller_log.blade.php diff --git a/app/Http/Controllers/ApiController.php b/app/Http/Controllers/ApiController.php new file mode 100644 index 0000000..43fe035 --- /dev/null +++ b/app/Http/Controllers/ApiController.php @@ -0,0 +1,61 @@ +validate([ + "token" => "required", + ]); + + $caller_by = new Caller_DB(); + $caller_by = $caller_by->where('token', $data['token'])->first(); + + if (!$caller_by) { + $this->logRequest($request, null, 'failed', '401 Unauthorized', null); + return response()->json(['error' => 'Unauthorized'], 401); + } + + $this->logRequest($request, $caller_by, 'success', 'accepted command'); + return response()->json(['success' => true], 200); + + } catch (\Illuminate\Validation\ValidationException $exception) { + $this->logRequest($request, null, 'failed', '401 Unauthorized'); + return response()->json(['error' => 'Unauthorized'], 401); + + } catch (\Exception $exception) { + $this->logRequest($request, null, 'failed', $exception->getMessage()); + return response()->json(['error' => 'An unexpected error occurred.' . $exception->getMessage()], 500); + } + } + + private function logRequest($request, $caller_by = null, $status, $error = null) + { + $logger = new CallerLog_DB(); + if ($caller_by) { + $logger->caller_id = $caller_by->id; + $logger->caller_name = $caller_by->node_name; + $logger->token_by = $caller_by->token; + $manager = $caller_by->manager; + } else { + $logger->caller_id = "not responsible"; + $logger->caller_name = "not responsible"; + $logger->token_by = "not responsible"; + } + + $logger->req_ip = $request->ip(); + $logger->req_command = $request->path(); + $logger->status = $status; + $logger->error_exception = $error; + $logger->save(); + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/SmartController.php b/app/Http/Controllers/SmartController.php index f33b45b..ed2ae86 100644 --- a/app/Http/Controllers/SmartController.php +++ b/app/Http/Controllers/SmartController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Caller_DB; +use App\Models\CallerLog_DB; class SmartController extends Controller { @@ -21,8 +22,19 @@ class SmartController extends Controller { $callers = Caller_DB::all(); - $callers_count = [$callers->count()]; - return view('smart.caller', compact('callers', 'callers_count')); + return view('smart.caller', compact('callers')); + } + + public function caller_log() + { + $caller_logs = CallerLog_DB::paginate(15); + $caller_logs->getCollection()->transform(function ($log) { + $caller = Caller_DB::where('created_by', $log->created_by)->first(); + $log->token_by = $caller ? $caller->created_by : null; + return $log; + }); + + return view('smart.caller_log', compact('caller_logs')); } } diff --git a/app/Models/CallerLog_DB.php b/app/Models/CallerLog_DB.php new file mode 100644 index 0000000..a488692 --- /dev/null +++ b/app/Models/CallerLog_DB.php @@ -0,0 +1,12 @@ +id()->autoIncrement(); + $table->string('caller_id'); + $table->string('caller_name'); + $table->string('req_ip'); + $table->string('req_command'); + $table->string('token_by'); + $table->enum('status', ['success', 'failed']); + $table->longtext('error_exception')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // + } +}; diff --git a/resources/views/smart/caller.blade.php b/resources/views/smart/caller.blade.php index a705495..76dca10 100644 --- a/resources/views/smart/caller.blade.php +++ b/resources/views/smart/caller.blade.php @@ -160,7 +160,7 @@
  • - + Logs @@ -270,33 +270,33 @@ - - @php - $counter = 1; - foreach($callers_count as $caller_id) { - echo $counter++; - } - @endphp - - @foreach($callers as $caller) - -
    -
    - {{ $caller->node_name }} -
    + + @php + $counter = 1; + foreach($callers as $caller_id) { + echo $counter++; + } + @endphp + + @foreach($callers as $caller) + +
    +
    + {{ $caller->node_name }}
    - - - - {{ $caller->status }} - - - @endforeach - - - - - +
    + + + + {{ $caller->status }} + + + + + + @endforeach + +
    diff --git a/resources/views/smart/caller_log.blade.php b/resources/views/smart/caller_log.blade.php new file mode 100644 index 0000000..82424d6 --- /dev/null +++ b/resources/views/smart/caller_log.blade.php @@ -0,0 +1,327 @@ + + + + @vite(['resources/css/app.css','resources/js/app.js']) + + NCORE - Smart Caller Logging Request + + + + + + + +
    +
    +
    +
    +
    Smart Call API Logs
    +
    +
    +
    + + + + + + + + + + + + + + + @php + $counter = 1; + @endphp + @foreach($caller_logs as $caller) + + + + + + + + + + + + + + + + @endforeach + +
    IDNodeStatusRequested IPv4Requested UrlToken Managed ByExceptionDate
    {{ $counter++ }}{{ $caller->caller_name }} + @if ( $caller->status === "success" ) + + {{ $caller->status }} + + @else + + {{ $caller->status }} + + @endif + + {{ $caller->req_ip }} + + {{ $caller->req_command }} + + {{ $caller->token_by }} + + {{ $caller->error_exception }} + + {{ $caller->created_at }} +
    +
    +
    +
    +
    + + + + + + + + \ No newline at end of file diff --git a/resources/views/smart/dashboard.blade.php b/resources/views/smart/dashboard.blade.php index d6d04ea..ea54eea 100644 --- a/resources/views/smart/dashboard.blade.php +++ b/resources/views/smart/dashboard.blade.php @@ -160,7 +160,7 @@
  • - + Logs diff --git a/routes/api.php b/routes/api.php index ccc387f..b9ea42f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -3,6 +3,8 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; -Route::get('/user', function (Request $request) { - return $request->user(); -})->middleware('auth:sanctum'); +// The Smart API Caller + +Route::group(['prefix' => 'caller'], function () { + Route::post('register', [\App\Http\Controllers\ApiController::class, 'checker'])->name('checker'); +}); diff --git a/routes/web.php b/routes/web.php index cc518df..878477c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -31,6 +31,8 @@ Route::Group(['prefix' => 'call', 'middleware' => 'web'], function () { Route::Group(['prefix' => 'internal', 'middleware' => 'auth'], function () { Route::get('dashboard/', [\App\Http\Controllers\SmartController::class, 'dashboard'])->name('dashboard'); Route::get('caller/', [\App\Http\Controllers\SmartController::class, 'caller'])->name('caller'); + Route::get('caller/logs', [\App\Http\Controllers\SmartController::class, 'caller_log'])->name('caller_log'); + // Post Request for Internal Handling