Skip to content

Commit

Permalink
Merge pull request #15 from izzatbey:manual-generate
Browse files Browse the repository at this point in the history
Add Manual Generate Report
  • Loading branch information
izzatbey authored Feb 19, 2025
2 parents efbf3ba + fa9a492 commit 1d4ed09
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 100 deletions.
2 changes: 2 additions & 0 deletions app/Helpers/TemplateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
function prettifyTemplateName($templateName)
{
switch ($templateName) {
case 'today_report':
return 'Today Report';
case 'daily_report':
return 'Daily Report';
case 'monthly_report':
Expand Down
172 changes: 90 additions & 82 deletions app/Http/Controllers/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ public function generateReport()
{
try {
$alertMetrics = AlertMetric::with('alertMessage.classification.priority')
->whereBetween('timestamp', [Carbon::now()->subMonth(), Carbon::now()])
->whereBetween('timestamp', [Carbon::today()->startOfDay(), Carbon::today()->endOfDay()])
->get();
$sensorMetrics = SensorMetric::with('sensor')
->whereBetween('timestamp', [Carbon::now()->subMonth(), Carbon::now()])
->whereBetween('timestamp', [Carbon::today()->startOfDay(), Carbon::today()->endOfDay()])
->get();
$priorities = Priority::all()->pluck('id', 'name')->toArray();
$traffics = Traffic::with('sourceIdentity', 'destinationIdentity')
->whereBetween('timestamp', [Carbon::now()->subMonth(), Carbon::now()])
->whereBetween('timestamp', [Carbon::today()->startOfDay(), Carbon::today()->endOfDay()])
->get();
$sensors = Sensor::all();
$totalEvents = 0;
Expand All @@ -210,11 +210,7 @@ public function generateReport()
}
$priorityCounts[$priorityName] += $alertMetric->count;
}

// Sort priorities by count in descending order and take top 10
$priorityCounts = collect($priorityCounts)->sortByDesc(function ($count, $priority) {
return $count;
})->take(10)->toArray();
$priorityCounts = collect($priorityCounts)->sortByDesc(fn($count) => $count)->take(10)->toArray();

// Top 10 Alert
$groupedAlertMetrics = $alertMetrics->groupBy(function ($alertMetric) {
Expand All @@ -230,8 +226,6 @@ public function generateReport()
'count' => $group->sum('count'),
];
});

// Sort grouped alert metrics by priority and count
$sortedAlertMetrics = $groupedAlertMetrics->sort(function ($a, $b) use ($priorities) {
$priorityA = $priorities[$a['priority']] ?? 999;
$priorityB = $priorities[$b['priority']] ?? 999;
Expand All @@ -240,7 +234,6 @@ public function generateReport()
}
return $priorityA <=> $priorityB;
});

$topAlertData = $sortedAlertMetrics->take(10)->values();

// Top 10 Source IP
Expand All @@ -253,14 +246,13 @@ public function generateReport()
$topSourceIps = $sourceIpCounts->sortByDesc('count')->take(10)->values();

// Top 10 Source Country
$sourceCountryCounts = collect($traffics)->groupBy(function ($item) {
return $item->sourceIdentity->country_name ?? 'Unknown';
})->map(function ($items, $key) {
return [
'country' => $key,
'count' => $items->sum('count')
];
});
$sourceCountryCounts = collect($traffics)->groupBy(fn($item) => $item->sourceIdentity->country_name ?? 'Unknown')
->map(function ($items, $key) {
return [
'country' => $key,
'count' => $items->sum('count')
];
});
$topSourceCountries = $sourceCountryCounts->sortByDesc('count')->take(10)->values();

// Top 10 Destination IP
Expand All @@ -273,14 +265,13 @@ public function generateReport()
$topDestinationIps = $destinationIpCounts->sortByDesc('count')->take(10)->values();

// Top 10 Destination Country
$destinationCountryCounts = collect($traffics)->groupBy(function ($item) {
return $item->destinationIdentity->country_name ?? 'Unknown';
})->map(function ($items, $key) {
return [
'country' => $key,
'count' => $items->sum('count')
];
});
$destinationCountryCounts = collect($traffics)->groupBy(fn($item) => $item->destinationIdentity->country_name ?? 'Unknown')
->map(function ($items, $key) {
return [
'country' => $key,
'count' => $items->sum('count')
];
});
$topDestinationCountries = $destinationCountryCounts->sortByDesc('count')->take(10)->values();

// Aggregate counts by sensor
Expand Down Expand Up @@ -324,19 +315,21 @@ public function generateReport()
}
}
$sensor->priorityCounts = $priorityCountsSensor;
$groupedAlertMetrics = $alertMetrics->where('sensor_id', $sensor->id)->groupBy(function ($alertMetric) {
return $alertMetric->alertMessage->classification->priority->name . '|' .
$alertMetric->alertMessage->classification . '|' .
$alertMetric->alertMessage->alert_message;
})->map(function ($group) {
$first = $group->first();
return [
'priority' => $first->alertMessage->classification->priority->name,
'classification' => $first->alertMessage->classification->classification,
'message' => $first->alertMessage->alert_message,
'count' => $group->sum('count'),
];
});

$groupedAlertMetrics = $alertMetrics->where('sensor_id', $sensor->id)
->groupBy(function ($alertMetric) {
return $alertMetric->alertMessage->classification->priority->name . '|' .
$alertMetric->alertMessage->classification . '|' .
$alertMetric->alertMessage->alert_message;
})->map(function ($group) {
$first = $group->first();
return [
'priority' => $first->alertMessage->classification->priority->name,
'classification' => $first->alertMessage->classification->classification,
'message' => $first->alertMessage->alert_message,
'count' => $group->sum('count'),
];
});
$sortedAlertMetrics = $groupedAlertMetrics->sort(function ($a, $b) use ($priorities) {
$priorityA = $priorities[$a['priority']] ?? 999;
$priorityB = $priorities[$b['priority']] ?? 999;
Expand All @@ -346,51 +339,61 @@ public function generateReport()
return $priorityA <=> $priorityB;
});
$sensor->topAlertData = $sortedAlertMetrics->take(10)->values();
$sourceIpCounts = collect($traffics)->where('sensor_id', $sensor->id)->groupBy('source_ip')->map(function ($items, $key) {
return [
'sourceIp' => $key,
'count' => $items->sum('count')
];
});

$sourceIpCounts = collect($traffics)->where('sensor_id', $sensor->id)
->groupBy('source_ip')->map(function ($items, $key) {
return [
'sourceIp' => $key,
'count' => $items->sum('count')
];
});
$sensor->topSourceIps = $sourceIpCounts->sortByDesc('count')->take(10)->values();
$sourceCountryCounts = collect($traffics)->where('sensor_id', $sensor->id)->groupBy(function ($item) {
return $item->sourceIdentity->country_name ?? 'Unknown';
})->map(function ($items, $key) {
return [
'country' => $key,
'count' => $items->sum('count')
];
});

$sourceCountryCounts = collect($traffics)->where('sensor_id', $sensor->id)
->groupBy(fn($item) => $item->sourceIdentity->country_name ?? 'Unknown')
->map(function ($items, $key) {
return [
'country' => $key,
'count' => $items->sum('count')
];
});
$sensor->topSourceCountries = $sourceCountryCounts->sortByDesc('count')->take(10)->values();
$destinationIpCounts = collect($traffics)->where('sensor_id', $sensor->id)->groupBy('destination_ip')->map(function ($items, $key) {
return [
'destinationIp' => $key,
'count' => $items->sum('count')
];
});

$destinationIpCounts = collect($traffics)->where('sensor_id', $sensor->id)
->groupBy('destination_ip')->map(function ($items, $key) {
return [
'destinationIp' => $key,
'count' => $items->sum('count')
];
});
$sensor->topDestinationIps = $destinationIpCounts->sortByDesc('count')->take(10)->values();
$destinationCountryCounts = collect($traffics)->where('sensor_id', $sensor->id)->groupBy(function ($item) {
return $item->destinationIdentity->country_name ?? 'Unknown';
})->map(function ($items, $key) {
return [
'country' => $key,
'count' => $items->sum('count')
];
});

$destinationCountryCounts = collect($traffics)->where('sensor_id', $sensor->id)
->groupBy(fn($item) => $item->destinationIdentity->country_name ?? 'Unknown')
->map(function ($items, $key) {
return [
'country' => $key,
'count' => $items->sum('count')
];
});
$sensor->topDestinationCountries = $destinationCountryCounts->sortByDesc('count')->take(10)->values();
$sourcePortCounts = collect($traffics)->where('sensor_id', $sensor->id)->groupBy('source_port')->map(function ($items, $key) {
return [
'sourcePort' => $key,
'count' => $items->sum('count')
];
});

$sourcePortCounts = collect($traffics)->where('sensor_id', $sensor->id)
->groupBy('source_port')->map(function ($items, $key) {
return [
'sourcePort' => $key,
'count' => $items->sum('count')
];
});
$sensor->topSourcePorts = $sourcePortCounts->sortByDesc('count')->take(10)->values();
$destinationPortCounts = collect($traffics)->where('sensor_id', $sensor->id)->groupBy('destination_port')->map(function ($items, $key) {
return [
'destinationPort' => $key,
'count' => $items->sum('count')
];
});

$destinationPortCounts = collect($traffics)->where('sensor_id', $sensor->id)
->groupBy('destination_port')->map(function ($items, $key) {
return [
'destinationPort' => $key,
'count' => $items->sum('count')
];
});
$sensor->topDestinationPorts = $destinationPortCounts->sortByDesc('count')->take(10)->values();
};

Expand All @@ -411,13 +414,18 @@ public function generateReport()
];

Report::query()->create([
'template_id' => 'generate_report',
'template_id' => 'today_report',
'data' => $data,
]);

Log::info('Report generated');
session()->flash('message', 'Report generated successfully');

return redirect()->route('dashboard');
} catch (\Exception $e) {
Log::error('Error generating report: ' . $e);
Log::error('Error generating report: ' . $e->getMessage());
session()->flash('error', 'An error occurred while generating the report.');
return redirect()->route('dashboard');
}
}

Expand Down
11 changes: 7 additions & 4 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
<div class="md:col-span-1">
<div class="bg-white dark:bg-gray-800 shadow-xl rounded-lg p-6">
<h3 class="text-lg font-bold mb-4 text-gray-800 dark:text-gray-100">
Generate Manual Report
Generate Today Report
</h3>
<button class="w-full inline-flex items-center justify-center px-4 py-2 bg-indigo-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-indigo-500 active:bg-indigo-700 focus:outline-none focus:ring focus:ring-indigo-200 disabled:opacity-25 transition">
Generate Report
</button>
<form action="{{ route('generate.report') }}" method="GET">
@csrf
<button type="submit" class="w-full inline-flex items-center justify-center px-4 py-2 bg-indigo-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-indigo-500 active:bg-indigo-700 focus:outline-none focus:ring focus:ring-indigo-200 disabled:opacity-25 transition">
Generate Report
</button>
</form>
</div>
</div>
<div class="md:col-span-2">
Expand Down
8 changes: 2 additions & 6 deletions resources/views/reports/report-style.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
margin: 20px;
background-color: white;
}
hr {
display: block;
height: 1px;
Expand Down Expand Up @@ -47,15 +48,13 @@
gap: 15px;
}
/* Box Styles */
.box, .middle-row {
border: 1px solid #ccc;
border-radius: 2px;
padding: 15px;
background-color: white;
}
/* Total Events Box */
.box h3 {
margin-top: 0;
margin-bottom: 15px;
Expand Down Expand Up @@ -247,15 +246,13 @@
}
.ip-table .ip-column {
font-family: monospace; /* For better IP address alignment */
font-family: monospace;
}
/* Hover effect */
.ip-table tbody tr:hover {
background-color: #f8f9fa;
}
/* Print styles */
@media print {
.source-ip-box {
break-inside: avoid;
Expand All @@ -270,7 +267,6 @@
}
}
/* Bottom grid layout */
.bottom-grid {
display: grid;
grid-template-columns: 2fr 1fr; /* 2/3 for IP, 1/3 for Country */
Expand Down
4 changes: 0 additions & 4 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
use Illuminate\Support\Facades\Schedule;
use Illuminate\Support\Facades\Log;

Schedule::job(new GenerateDailyReport)->everyMinute()->onSuccess(function () {
Log::info('GenerateDailyReport job dispatched successfully at ' . now());
});

Schedule::job(new GenerateDailyReport)->dailyAt('00:00')->onSuccess(function () {
Log::info('GenerateDailyReport job dispatched successfully at ' . now());
});
Expand Down
6 changes: 2 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ReportController;
use App\Models\Report;
use Illuminate\Support\Facades\Route;

Route::group(['middleware' => ['auth:sanctum']], function () {
Expand All @@ -13,7 +11,7 @@
Route::get('/reports/download/{id}', [ReportController::class, 'downloadReport'])->name('download.report');
Route::get('/reports/view/{id}', [ReportController::class, 'viewReport'])->name('view.report');
Route::delete('/reports/{id}', [ReportController::class, 'destroy'])->name('delete.report');
Route::get('/report-generate', [ReportController::class, 'generateReport'])->name('generate.report');
});

Route::get('/reports/{id}/view', [ReportController::class, 'viewReport'])
->name('reports.view');
Route::get('/reports/{id}/view', [ReportController::class, 'viewReport'])->name('reports.view');

0 comments on commit 1d4ed09

Please sign in to comment.