fix(perf): resolve F-11 — cap PDF exports to 90-day date range to prevent memory exhaustion
All three PDF exports (rainfall, water level, siren) now default to last 90 days. Accepts optional ?from=&to= query params, capped at 90-day max range. Shared applyDateRange helper in NotificationController.
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Carbon\Carbon;
|
||||
|
||||
// Add This For Import DOMPDF Library
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
@@ -88,34 +89,54 @@ class NotificationController extends Controller
|
||||
}
|
||||
|
||||
//Function Export PDF for Rainfall History
|
||||
public function exportHistoryRfPDF()
|
||||
public function exportHistoryRfPDF(Request $request)
|
||||
{
|
||||
$rfHistory = DB::table('station')
|
||||
->join('notification','station.stationid','notification.stationid')
|
||||
->select('station.*','notification.*')
|
||||
->where('notification.stationtype',1)
|
||||
->orderByDesc('notification.timestamp')->get();
|
||||
$query = DB::table('station')
|
||||
->join('notification', 'station.stationid', 'notification.stationid')
|
||||
->select('station.*', 'notification.*')
|
||||
->where('notification.stationtype', 1);
|
||||
|
||||
$pdf = Pdf::loadView('pdf.rfhistory',compact('rfHistory'))
|
||||
->setPaper('a4','portrait');
|
||||
$this->applyDateRange($query, $request, 'notification.timestamp');
|
||||
|
||||
$rfHistory = $query->orderByDesc('notification.timestamp')->get();
|
||||
|
||||
$pdf = Pdf::loadView('pdf.rfhistory', compact('rfHistory'))
|
||||
->setPaper('a4', 'portrait');
|
||||
|
||||
return $pdf->download('Rainfall Alarm History.pdf');
|
||||
|
||||
}
|
||||
|
||||
// Function Export PDF for Water Level History
|
||||
public function exportHistoryWlPDF()
|
||||
public function exportHistoryWlPDF(Request $request)
|
||||
{
|
||||
$wlHistory = DB::table('station')
|
||||
->join('notification','station.stationid','notification.stationid')
|
||||
->select('station.*','notification.*')
|
||||
->where('notification.stationtype',2)
|
||||
->orderByDesc('notification.timestamp')->get();
|
||||
$query = DB::table('station')
|
||||
->join('notification', 'station.stationid', 'notification.stationid')
|
||||
->select('station.*', 'notification.*')
|
||||
->where('notification.stationtype', 2);
|
||||
|
||||
$pdf = Pdf::loadView('pdf.wlhistory',compact('wlHistory'))
|
||||
->setPaper('a4','portrait');
|
||||
$this->applyDateRange($query, $request, 'notification.timestamp');
|
||||
|
||||
$wlHistory = $query->orderByDesc('notification.timestamp')->get();
|
||||
|
||||
$pdf = Pdf::loadView('pdf.wlhistory', compact('wlHistory'))
|
||||
->setPaper('a4', 'portrait');
|
||||
|
||||
return $pdf->download('Water Level Alarm History.pdf');
|
||||
}
|
||||
|
||||
private function applyDateRange($query, Request $request, string $column): void
|
||||
{
|
||||
$maxDays = 90;
|
||||
|
||||
if ($request->filled('from') && $request->filled('to')) {
|
||||
$from = Carbon::parse($request->input('from'))->startOfDay();
|
||||
$to = Carbon::parse($request->input('to'))->endOfDay();
|
||||
$diff = $from->diffInDays($to);
|
||||
if ($diff > $maxDays) {
|
||||
$from = $to->copy()->subDays($maxDays);
|
||||
}
|
||||
$query->whereBetween($column, [$from, $to]);
|
||||
} else {
|
||||
$query->where($column, '>=', now()->subDays($maxDays));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
use Carbon\Carbon;
|
||||
|
||||
|
||||
class SirenController extends Controller
|
||||
@@ -54,26 +55,36 @@ class SirenController extends Controller
|
||||
}
|
||||
|
||||
// Function for download Siren History to PDF
|
||||
public function exportHistorySirenPDF()
|
||||
public function exportHistorySirenPDF(Request $request)
|
||||
{
|
||||
$sirenHistory = DB::table('station')
|
||||
->join('siren','station.stationid','siren.stationid')
|
||||
->select('station.*','siren.*')
|
||||
$maxDays = 90;
|
||||
$query = DB::table('station')
|
||||
->join('siren', 'station.stationid', 'siren.stationid')
|
||||
->select('station.*', 'siren.*')
|
||||
->where('siren.level', '!=', 'N')
|
||||
->where('siren.level', '!=', '')
|
||||
->where('station.siren',1)
|
||||
->orderByDesc('siren.active_time')
|
||||
->get();
|
||||
->where('station.siren', 1);
|
||||
|
||||
$pdf = Pdf::loadView('pdf.sirenhistory',compact('sirenHistory'))
|
||||
->setPaper('a4','portrait');
|
||||
if ($request->filled('from') && $request->filled('to')) {
|
||||
$from = Carbon::parse($request->input('from'))->startOfDay();
|
||||
$to = Carbon::parse($request->input('to'))->endOfDay();
|
||||
if ($from->diffInDays($to) > $maxDays) {
|
||||
$from = $to->copy()->subDays($maxDays);
|
||||
}
|
||||
$query->whereBetween('siren.active_time', [$from, $to]);
|
||||
} else {
|
||||
$query->where('siren.active_time', '>=', now()->subDays($maxDays));
|
||||
}
|
||||
|
||||
$sirenHistory = $query->orderByDesc('siren.active_time')->get();
|
||||
|
||||
$pdf = Pdf::loadView('pdf.sirenhistory', compact('sirenHistory'))
|
||||
->setPaper('a4', 'portrait');
|
||||
|
||||
return $pdf->download(
|
||||
'Station_Siren_History.pdf',
|
||||
[],
|
||||
['Content-Type' => 'application/pdf']
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user