withCount('healthChecks') ->with(['healthChecks' => function ($query) { $query->where('is_active', true)->limit(5); }]) ->get(); $stats = [ 'total_sites' => $sites->count(), 'total_health_checks' => $sites->sum('health_checks_count'), 'sites_with_checks' => $sites->where('health_checks_count', '>', 0)->count(), ]; return view('health-checks.overview', compact('sites', 'stats')); } /** * Display a listing of the health checks for a site. */ public function index(Site $site): View { // Ensure the user owns the site if ($site->user_id !== Auth::id()) { abort(403, 'Unauthorized access to this site.'); } $healthChecks = $site->healthChecks() ->orderBy('created_at', 'desc') ->get(); return view('health-checks.index', compact('site', 'healthChecks')); } /** * Show the form for creating a new health check. */ public function create(Site $site): View { // Ensure the user owns the site if ($site->user_id !== Auth::id()) { abort(403, 'Unauthorized access to this site.'); } return view('health-checks.create', compact('site')); } /** * Store a newly created health check in storage. */ public function store(Request $request, Site $site): RedirectResponse { // Ensure the user owns the site if ($site->user_id !== Auth::id()) { abort(403, 'Unauthorized access to this site.'); } $validated = $request->validate([ 'endpoint' => ['required', 'string', 'max:255'], 'expected_json_keys' => ['nullable', 'string'], 'performance_threshold' => ['nullable', 'integer', 'min:1', 'max:60000'], 'is_active' => ['boolean'], ]); // Convert comma-separated keys to array $expectedKeys = null; if (!empty($validated['expected_json_keys'])) { $expectedKeys = array_map( 'trim', explode(',', $validated['expected_json_keys']) ); } // Check for duplicate endpoint $exists = $site->healthChecks() ->where('endpoint', $validated['endpoint']) ->exists(); if ($exists) { return back() ->withInput() ->withErrors(['endpoint' => 'This endpoint is already being monitored for this site.']); } $healthCheck = $site->healthChecks()->create([ 'endpoint' => $validated['endpoint'], 'expected_json_keys' => $expectedKeys, 'performance_threshold' => $validated['performance_threshold'] ?? null, 'is_active' => $validated['is_active'] ?? true, ]); return redirect() ->route('health-checks.show', [$site, $healthCheck]) ->with('success', 'Health check created successfully.'); } /** * Display the specified health check. */ public function show(Site $site, HealthCheck $healthCheck): View { // Ensure the user owns the site and the health check belongs to the site if ($site->user_id !== Auth::id() || $healthCheck->site_id !== $site->id) { abort(403, 'Unauthorized access to this health check.'); } return view('health-checks.show', compact('site', 'healthCheck')); } /** * Show the form for editing the specified health check. */ public function edit(Site $site, HealthCheck $healthCheck): View { // Ensure the user owns the site and the health check belongs to the site if ($site->user_id !== Auth::id() || $healthCheck->site_id !== $site->id) { abort(403, 'Unauthorized access to this health check.'); } return view('health-checks.edit', compact('site', 'healthCheck')); } /** * Update the specified health check in storage. */ public function update(Request $request, Site $site, HealthCheck $healthCheck): RedirectResponse { // Ensure the user owns the site and the health check belongs to the site if ($site->user_id !== Auth::id() || $healthCheck->site_id !== $site->id) { abort(403, 'Unauthorized access to this health check.'); } $validated = $request->validate([ 'endpoint' => ['required', 'string', 'max:255'], 'expected_json_keys' => ['nullable', 'string'], 'performance_threshold' => ['nullable', 'integer', 'min:1', 'max:60000'], 'is_active' => ['boolean'], ]); // Convert comma-separated keys to array $expectedKeys = null; if (!empty($validated['expected_json_keys'])) { $expectedKeys = array_map( 'trim', explode(',', $validated['expected_json_keys']) ); } // Check for duplicate endpoint (excluding current health check) $exists = $site->healthChecks() ->where('endpoint', $validated['endpoint']) ->where('id', '!=', $healthCheck->id) ->exists(); if ($exists) { return back() ->withInput() ->withErrors(['endpoint' => 'This endpoint is already being monitored for this site.']); } $healthCheck->update([ 'endpoint' => $validated['endpoint'], 'expected_json_keys' => $expectedKeys, 'performance_threshold' => $validated['performance_threshold'] ?? null, 'is_active' => $validated['is_active'] ?? $healthCheck->is_active, ]); return redirect() ->route('health-checks.show', [$site, $healthCheck]) ->with('success', 'Health check updated successfully.'); } /** * Remove the specified health check from storage. */ public function destroy(Site $site, HealthCheck $healthCheck): RedirectResponse { // Ensure the user owns the site and the health check belongs to the site if ($site->user_id !== Auth::id() || $healthCheck->site_id !== $site->id) { abort(403, 'Unauthorized access to this health check.'); } $healthCheck->delete(); return redirect() ->route('health-checks.index', $site) ->with('success', 'Health check deleted successfully.'); } }