-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9cba736
commit 181981a
Showing
8 changed files
with
357 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
namespace Laraflow\Sms\Controllers; | ||
|
||
|
||
use Carbon\CarbonImmutable; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Routing\Controller; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Storage; | ||
use Illuminate\Support\Str; | ||
|
||
class SmsLogController extends Controller | ||
{ | ||
public Collection $entries; | ||
|
||
public function __construct() | ||
{ | ||
$this->entries = collect(); | ||
|
||
$this->loadEntries(); | ||
} | ||
|
||
private function loadEntries(): void | ||
{ | ||
foreach (Storage::disk('log')->files() as $file) { | ||
if (!str_contains($file, 'sms-')) { | ||
continue; | ||
} | ||
|
||
$date = CarbonImmutable::createFromFormat('Y-m-d', preg_replace('/sms-(\d{4}-\d{2}-\d{2})\.log/i', '$1', $file)); | ||
|
||
$this->entries->put($date->format('Y-m-d'), [ | ||
'position' => now()->diffInDays($date), | ||
'file' => $file, | ||
'date' => $date, | ||
'size' => Storage::disk('log')->size($file), | ||
'modified_at' => CarbonImmutable::parse(Storage::disk('log')->lastModified($file)), | ||
]); | ||
} | ||
|
||
$this->entries = $this->entries->sortBy('position'); | ||
} | ||
|
||
/** | ||
* Display a listing of the resource. | ||
*/ | ||
public function index(Request $request) | ||
{ | ||
$current = $request->has('date') ? $this->entries->get($request->input('date')) : $this->entries->first(); | ||
|
||
if (!$current) { | ||
throw (new ModelNotFoundException)->setModel('SmsLog', $request->input('date', now()->format('Y-m-d'))); | ||
} | ||
|
||
$current['logs'] = $this->parseFileContent($current['file']); | ||
|
||
return view('sms::index', [ | ||
'entries' => $this->entries, | ||
'current' => $current | ||
]); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
*/ | ||
public function download(string $date) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
*/ | ||
public function destroy(string $id) | ||
{ | ||
// | ||
} | ||
|
||
private function parseFileContent(string $file): Collection | ||
{ | ||
$file = Storage::disk('log')->readStream($file); | ||
|
||
$entries = collect(); | ||
|
||
while (!feof($file)) { | ||
$item = $this->parseLine(fgets($file)); | ||
|
||
if (!empty($item)) { | ||
$entries->push($item); | ||
} | ||
} | ||
|
||
return $entries->sortBy('position'); | ||
} | ||
|
||
private function parseLine(string $line): array | ||
{ | ||
$pattern = '/\[(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\] .*\.INFO:\sResponse:\s+(.*)/'; | ||
|
||
$matches = []; | ||
|
||
preg_match($pattern, $line, $matches); | ||
|
||
$data = []; | ||
|
||
if (!empty($matches)) { | ||
|
||
$timestamp = CarbonImmutable::parse($matches[1] ?? null, 'Asia/Dhaka'); | ||
|
||
$data['position'] = now()->diffInSeconds($timestamp); | ||
$data['timestamp'] = $timestamp; | ||
|
||
$response = json_decode($matches[2] ?? '{}', true); | ||
$data['vendor'] = Str::studly($response['vendor'] ?? 'N/A'); | ||
$data['mode'] = Str::studly($response['mode'] ?? 'N/A'); | ||
$data['code'] = Str::studly($response['status_code'] ?? 'N/A'); | ||
$data['response'] = json_encode($response['response'] ?? [], JSON_PRETTY_PRINT); | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route; | ||
|
||
if (config('sms.log_viewer.enabled')) { | ||
Route::prefix(config('sms.log_viewer.uri'))->group(function () { | ||
Route::get('/', [\Laraflow\Sms\Controllers\SmsLogController::class, 'index'])->name('sms-logs.index'); | ||
Route::get('/{date}', [\Laraflow\Sms\Controllers\SmsLogController::class, 'show'])->name('sms-logs.show'); | ||
Route::delete('/{date}', [\Laraflow\Sms\Controllers\SmsLogController::class, 'destroy'])->name('sms-logs.destroy'); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
@extends('sms::layout') | ||
|
||
@section('content') | ||
<div | ||
class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"> | ||
<h1 class="h2">{{ $current['date']->format('D, d M Y') }}</h1> | ||
<div class="btn-toolbar mb-2 mb-md-0 gap-3"> | ||
<button type="button" class="btn btn-sm btn-outline-primary"> | ||
<span data-feather="file"></span> | ||
Download | ||
</button> | ||
<button type="button" class="btn btn-sm btn-outline-danger"> | ||
<span data-feather="file"></span> | ||
Delete | ||
</button> | ||
</div> | ||
</div> | ||
<div class="table-responsive"> | ||
<table class="table table-bordered table-hover" id="logs"> | ||
<thead> | ||
<tr class="text-center"> | ||
<th scope="col">Timestamp</th> | ||
<th scope="col">Vendor</th> | ||
<th scope="col">Mode</th> | ||
<th scope="col">Code</th> | ||
<th scope="col">Response</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
@foreach($current['logs'] as $response) | ||
<tr> | ||
<td>{{ $response['timestamp']->format('D, d M Y h:m:s A') }}</td> | ||
<td>{{ $response['vendor'] ?? 'N/A' }}</td> | ||
<td>{{ $response['mode'] ?? 'N/A' }}</td> | ||
<td>{{ $response['code'] ?? 'N/A' }}</td> | ||
<td><code><pre>{{ $response['response'] ?? '{}' }}</pre></code></td> | ||
</tr> | ||
@endforeach | ||
</tbody> | ||
</table> | ||
</div> | ||
@endsection |
Oops, something went wrong.