Files
Infra-Management/app/Http/Controllers/DashboardController.php
I-Fun c649f055d8 feat: initialize Infra Manager with React, Tailwind CSS, and Inertia.js
- Added main application entry point (app.jsx) and bootstrap file for React setup.
- Created Wiki index page (Index.jsx) for documentation and file management.
- Implemented utility functions for formatting and status handling in utils.js.
- Set up Blade template (app.blade.php) for Inertia.js integration.
- Defined API routes for metrics and terminal access in api.php.
- Established console commands for scheduled tasks in console.php.
- Created web routes for various application features including authentication, hypervisors, containers, cloud accounts, security, backups, and wiki management in web.php.
- Developed setup scripts for Windows (setup.ps1) and Linux (setup.sh) environments.
- Configured Tailwind CSS for styling with custom themes and animations in tailwind.config.js.
- Set up Vite for asset bundling and development server in vite.config.js.
2026-04-07 20:29:32 +07:00

69 lines
2.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Alert;
use App\Models\CloudAccount;
use App\Models\Container;
use App\Models\DockerEndpoint;
use App\Models\Hypervisor;
use App\Models\SecurityScore;
use App\Models\VirtualMachine;
use Inertia\Inertia;
use Inertia\Response;
class DashboardController extends Controller
{
public function index(): Response
{
$hypervisors = Hypervisor::select('id', 'name', 'type', 'status', 'last_sync_at')->get();
$vmStats = VirtualMachine::selectRaw('
COUNT(*) as total,
SUM(CASE WHEN status = "running" THEN 1 ELSE 0 END) as running,
SUM(CASE WHEN status = "stopped" THEN 1 ELSE 0 END) as stopped,
AVG(cpu_usage) as avg_cpu,
AVG(memory_usage) as avg_memory
')->first();
$containerStats = Container::selectRaw('
COUNT(*) as total,
SUM(CASE WHEN state = "running" THEN 1 ELSE 0 END) as running,
SUM(CASE WHEN state = "exited" THEN 1 ELSE 0 END) as stopped
')->first();
$endpoints = DockerEndpoint::select('id', 'name', 'type', 'status')->get();
$cloudAccounts = CloudAccount::select('id', 'name', 'provider', 'status')->get();
$criticalAlerts = Alert::where('status', 'firing')->where('severity', 'critical')->count();
$warningAlerts = Alert::where('status', 'firing')->where('severity', 'warning')->count();
$recentAlerts = Alert::with('rule')
->where('status', 'firing')
->orderByDesc('created_at')
->limit(5)
->get();
$avgSecurityScore = SecurityScore::avg('score') ?? 0;
return Inertia::render('Dashboard/Index', [
'stats' => [
'hypervisors' => $hypervisors->count(),
'hypervisors_online' => $hypervisors->where('status', 'online')->count(),
'vms' => (int) ($vmStats->total ?? 0),
'vms_running' => (int) ($vmStats->running ?? 0),
'vms_stopped' => (int) ($vmStats->stopped ?? 0),
'avg_cpu' => round((float) ($vmStats->avg_cpu ?? 0), 1),
'avg_memory' => round((float) ($vmStats->avg_memory ?? 0), 1),
'containers' => (int) ($containerStats->total ?? 0),
'containers_running' => (int) ($containerStats->running ?? 0),
'cloud_accounts' => $cloudAccounts->count(),
'critical_alerts' => $criticalAlerts,
'warning_alerts' => $warningAlerts,
'security_score' => round($avgSecurityScore, 1),
],
'hypervisors' => $hypervisors,
'endpoints' => $endpoints,
'cloud_accounts' => $cloudAccounts,
'recent_alerts' => $recentAlerts,
]);
}
}