124 lines
3.7 KiB
PHP
124 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Models\User;
|
|
use App\Models\Device;
|
|
use App\Models\Event;
|
|
use App\Models\Command;
|
|
use App\Models\Subscription;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Carbon\Carbon;
|
|
|
|
class AdminDashboard extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
public $statsDateRange = '30'; // days
|
|
|
|
// Activity monitoring
|
|
public $recentUsers = [];
|
|
public $recentEvents = [];
|
|
public $systemAlerts = [];
|
|
|
|
public function mount()
|
|
{
|
|
$this->loadRecentActivity();
|
|
}
|
|
|
|
public function loadRecentActivity()
|
|
{
|
|
// Load recent users
|
|
$this->recentUsers = User::latest()
|
|
->limit(5)
|
|
->get(['id', 'name', 'email', 'created_at', 'last_login_at', 'status']);
|
|
|
|
// Load recent critical events
|
|
$this->recentEvents = Event::with(['device.user'])
|
|
->whereIn('type', ['alarm', 'deviceOffline', 'deviceOverspeed', 'panic'])
|
|
->latest('event_time')
|
|
->limit(10)
|
|
->get();
|
|
|
|
// Load system alerts (could be from logs, failed commands, etc.)
|
|
$this->systemAlerts = [
|
|
[
|
|
'type' => 'warning',
|
|
'message' => 'High API usage detected',
|
|
'time' => now()->subMinutes(15),
|
|
],
|
|
[
|
|
'type' => 'info',
|
|
'message' => 'Scheduled maintenance completed',
|
|
'time' => now()->subHours(2),
|
|
],
|
|
];
|
|
}
|
|
|
|
public function getStatsProperty()
|
|
{
|
|
$days = (int) $this->statsDateRange;
|
|
$startDate = now()->subDays($days);
|
|
|
|
return [
|
|
'total_users' => User::count(),
|
|
'active_users' => User::where('status', 'active')->count(),
|
|
'new_users' => User::where('created_at', '>=', $startDate)->count(),
|
|
'total_devices' => Device::count(),
|
|
'online_devices' => Device::where('status', 'online')->count(),
|
|
'total_events' => Event::where('event_time', '>=', $startDate)->count(),
|
|
'critical_events' => Event::whereIn('type', ['alarm', 'deviceOffline', 'deviceOverspeed', 'panic'])
|
|
->where('event_time', '>=', $startDate)->count(),
|
|
'pending_commands' => Command::where('status', 'pending')->count(),
|
|
'active_subscriptions' => Subscription::where('status', 'active')->count(),
|
|
'revenue_this_month' => Subscription::where('status', 'active')
|
|
->whereMonth('created_at', now()->month)
|
|
->sum('price'),
|
|
];
|
|
}
|
|
|
|
public function getUserGrowthDataProperty()
|
|
{
|
|
$data = User::select(
|
|
DB::raw('DATE(created_at) as date'),
|
|
DB::raw('COUNT(*) as count')
|
|
)
|
|
->where('created_at', '>=', now()->subDays(30))
|
|
->groupBy('date')
|
|
->orderBy('date')
|
|
->get();
|
|
|
|
return [
|
|
'labels' => $data->pluck('date')->map(fn($date) => Carbon::parse($date)->format('M j'))->toArray(),
|
|
'data' => $data->pluck('count')->toArray(),
|
|
];
|
|
}
|
|
|
|
public function getDeviceStatusDataProperty()
|
|
{
|
|
$statusCounts = Device::select('status', DB::raw('COUNT(*) as count'))
|
|
->groupBy('status')
|
|
->get()
|
|
->pluck('count', 'status')
|
|
->toArray();
|
|
|
|
return [
|
|
'labels' => array_keys($statusCounts),
|
|
'data' => array_values($statusCounts),
|
|
];
|
|
}
|
|
|
|
public function refreshStats()
|
|
{
|
|
$this->loadRecentActivity();
|
|
session()->flash('success', 'Dashboard data refreshed!');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.admin-dashboard');
|
|
}
|
|
}
|