From fd36f51a341d42ec2d191151bd7a14e918dfc637 Mon Sep 17 00:00:00 2001 From: Xinecraft Date: Sun, 12 Nov 2023 00:35:45 +0530 Subject: [PATCH] Fix SQL perf issue and log size issue --- .../Admin/ServerIntelController.php | 196 +++++++++--------- .../Api/ApiServerConsolelogController.php | 19 +- app/Listeners/UpsertPlayerOnSessionStart.php | 3 +- 3 files changed, 114 insertions(+), 104 deletions(-) diff --git a/app/Http/Controllers/Admin/ServerIntelController.php b/app/Http/Controllers/Admin/ServerIntelController.php index a83ce3c58..fbfebeeaf 100644 --- a/app/Http/Controllers/Admin/ServerIntelController.php +++ b/app/Http/Controllers/Admin/ServerIntelController.php @@ -8,18 +8,18 @@ use App\Models\MinecraftPlayer; use App\Models\MinecraftPlayerSession; use App\Models\MinecraftServerLiveInfo; +use App\Models\Server; use App\Models\ServerChatlog; use App\Models\ServerConsolelog; use App\Queries\Filters\FilterMultipleFields; use App\Utils\Helpers\MinecraftColorUtils; +use DB; use Illuminate\Http\Request; use Illuminate\Support\Carbon; -use Inertia\Inertia; -use App\Models\Server; use Illuminate\Support\Facades\Cache; +use Inertia\Inertia; use Spatie\QueryBuilder\AllowedFilter; use Spatie\QueryBuilder\QueryBuilder; -use DB; const RESPONSE_CACHE_SECONDS = 3600; // 1 hour @@ -55,12 +55,19 @@ public function overview(Request $request) $uniquePlayersCount = $uniquePlayers->count(); // Old & New Players Count + $uniquePlayersSubquery = MinecraftPlayerSession::select(['player_uuid']) + ->when($selectedServers, function ($query, $selectedServers) { + $query->whereIn('server_id', $selectedServers); + }) + ->where('created_at', '>=', now()->subWeek()) + ->distinct() + ->getQuery(); $oldPlayers = MinecraftPlayerSession::select(['player_uuid']) ->when($selectedServers, function ($query, $selectedServers) { $query->whereIn('server_id', $selectedServers); }) ->where('created_at', '<', now()->subWeek()) - ->whereIn('player_uuid', $uniquePlayers->pluck('player_uuid')) + ->whereIn('player_uuid', $uniquePlayersSubquery) ->distinct()->get(); $oldPlayersCount = $oldPlayers->count(); $newPlayersCount = $uniquePlayers->whereNotIn('player_uuid', $oldPlayers->pluck('player_uuid'))->count(); @@ -114,7 +121,6 @@ public function overview(Request $request) /** * End: LAST 7 DAYS STATS */ - $lastIntelReportedAt = Server::query() ->when($selectedServers, function ($query, $selectedServers) { $query->whereIn('id', $selectedServers); @@ -164,60 +170,60 @@ public function overviewNumbers(Request $request) $numbersData = []; // Total Players - $totalPlayers["last_24h"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->distinct()->count('player_uuid') ?: 0; - $totalPlayers["last_7days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->distinct()->count('player_uuid') ?: 0; - $totalPlayers["last_30days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->distinct()->count('player_uuid') ?: 0; - $totalPlayers["all_time"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->distinct()->count('player_uuid') ?: 0; - $numbersData["total_players"] = $totalPlayers; + $totalPlayers['last_24h'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->distinct()->count('player_uuid') ?: 0; + $totalPlayers['last_7days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->distinct()->count('player_uuid') ?: 0; + $totalPlayers['last_30days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->distinct()->count('player_uuid') ?: 0; + $totalPlayers['all_time'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->distinct()->count('player_uuid') ?: 0; + $numbersData['total_players'] = $totalPlayers; // Total Playtime - $totalPlaytime["last_24h"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->sum('play_time') ?: 0; - $totalPlaytime["last_7days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->sum('play_time') ?: 0; - $totalPlaytime["last_30days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->sum('play_time') ?: 0; - $totalPlaytime["all_time"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->sum('play_time') ?: 0; - $numbersData["total_playtime"] = $totalPlaytime; + $totalPlaytime['last_24h'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->sum('play_time') ?: 0; + $totalPlaytime['last_7days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->sum('play_time') ?: 0; + $totalPlaytime['last_30days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->sum('play_time') ?: 0; + $totalPlaytime['all_time'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->sum('play_time') ?: 0; + $numbersData['total_playtime'] = $totalPlaytime; // AFK Time - $totalAfkTime["last_24h"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->sum('afk_time') ?: 0; - $totalAfkTime["last_7days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->sum('afk_time') ?: 0; - $totalAfkTime["last_30days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->sum('afk_time') ?: 0; - $totalAfkTime["all_time"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->sum('afk_time') ?: 0; - $numbersData["total_afktime"] = $totalAfkTime; + $totalAfkTime['last_24h'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->sum('afk_time') ?: 0; + $totalAfkTime['last_7days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->sum('afk_time') ?: 0; + $totalAfkTime['last_30days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->sum('afk_time') ?: 0; + $totalAfkTime['all_time'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->sum('afk_time') ?: 0; + $numbersData['total_afktime'] = $totalAfkTime; // Total Sessions - $totalSessions["last_24h"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->count() ?: 0; - $totalSessions["last_7days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->count() ?: 0; - $totalSessions["last_30days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->count() ?: 0; - $totalSessions["all_time"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->distinct()->count() ?: 0; - $numbersData["total_sessions"] = $totalSessions; + $totalSessions['last_24h'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->count() ?: 0; + $totalSessions['last_7days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->count() ?: 0; + $totalSessions['last_30days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->count() ?: 0; + $totalSessions['all_time'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->distinct()->count() ?: 0; + $numbersData['total_sessions'] = $totalSessions; // Avg Session Playtime - $avgSessionPlaytime["last_24h"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('play_time') ?: 0; - $avgSessionPlaytime["last_7days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('play_time') ?: 0; - $avgSessionPlaytime["last_30days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('play_time') ?: 0; - $avgSessionPlaytime["all_time"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->avg('play_time') ?: 0; - $numbersData["avg_session_playtime"] = $avgSessionPlaytime; + $avgSessionPlaytime['last_24h'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('play_time') ?: 0; + $avgSessionPlaytime['last_7days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('play_time') ?: 0; + $avgSessionPlaytime['last_30days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('play_time') ?: 0; + $avgSessionPlaytime['all_time'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->avg('play_time') ?: 0; + $numbersData['avg_session_playtime'] = $avgSessionPlaytime; // Total Player Kills - $totalPlayerKills["last_24h"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->sum('player_kills') ?: 0; - $totalPlayerKills["last_7days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->sum('player_kills') ?: 0; - $totalPlayerKills["last_30days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->sum('player_kills') ?: 0; - $totalPlayerKills["all_time"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->sum('player_kills') ?: 0; - $numbersData["total_player_kills"] = $totalPlayerKills; + $totalPlayerKills['last_24h'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->sum('player_kills') ?: 0; + $totalPlayerKills['last_7days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->sum('player_kills') ?: 0; + $totalPlayerKills['last_30days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->sum('player_kills') ?: 0; + $totalPlayerKills['all_time'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->sum('player_kills') ?: 0; + $numbersData['total_player_kills'] = $totalPlayerKills; // Total Mob Kills - $totalMobKills["last_24h"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->sum('mob_kills') ?: 0; - $totalMobKills["last_7days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->sum('mob_kills') ?: 0; - $totalMobKills["last_30days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->sum('mob_kills') ?: 0; - $totalMobKills["all_time"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->sum('mob_kills') ?: 0; - $numbersData["total_mob_kills"] = $totalMobKills; + $totalMobKills['last_24h'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->sum('mob_kills') ?: 0; + $totalMobKills['last_7days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->sum('mob_kills') ?: 0; + $totalMobKills['last_30days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->sum('mob_kills') ?: 0; + $totalMobKills['all_time'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->sum('mob_kills') ?: 0; + $numbersData['total_mob_kills'] = $totalMobKills; // Total Deaths - $totalDeaths["last_24h"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->sum('deaths') ?: 0; - $totalDeaths["last_7days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->sum('deaths') ?: 0; - $totalDeaths["last_30days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->sum('deaths') ?: 0; - $totalDeaths["all_time"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->sum('deaths') ?: 0; - $numbersData["total_deaths"] = $totalDeaths; + $totalDeaths['last_24h'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->sum('deaths') ?: 0; + $totalDeaths['last_7days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->sum('deaths') ?: 0; + $totalDeaths['last_30days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->sum('deaths') ?: 0; + $totalDeaths['all_time'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->sum('deaths') ?: 0; + $numbersData['total_deaths'] = $totalDeaths; // Avg Sessions per Player $minecraftPlayerSessionTableName = app(MinecraftPlayerSession::class)->getTable(); @@ -244,7 +250,7 @@ public function overviewNumbers(Request $request) ->select('player_uuid', DB::raw('COUNT(*) as count')) ->groupBy('player_uuid') ->pluck('count')?->avg() ?: 0; - $numbersData["avg_session_per_player"] = $avgSessionsPerPlayer; + $numbersData['avg_session_per_player'] = $avgSessionsPerPlayer; return $numbersData; }); @@ -306,67 +312,67 @@ public function performanceNumbers(Request $request) // NumbersData - last 24 hours, last week, last month, 3 months. $numbersData = []; // Max Online Players - $maxPlayers["last_24h"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->max('online_players') ?: 0; - $maxPlayers["last_7days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->max('online_players') ?: 0; - $maxPlayers["last_30days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->max('online_players') ?: 0; - $maxPlayers["last_90days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->max('online_players') ?: 0; - $numbersData["max_players"] = $maxPlayers; + $maxPlayers['last_24h'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->max('online_players') ?: 0; + $maxPlayers['last_7days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->max('online_players') ?: 0; + $maxPlayers['last_30days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->max('online_players') ?: 0; + $maxPlayers['last_90days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->max('online_players') ?: 0; + $numbersData['max_players'] = $maxPlayers; // Lowest TPS - $lowTPS["last_24h"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->min('tps') ?: 0; - $lowTPS["last_7days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->min('tps') ?: 0; - $lowTPS["last_30days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->min('tps') ?: 0; - $lowTPS["last_90days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->min('tps') ?: 0; - $numbersData["low_tps"] = $lowTPS; + $lowTPS['last_24h'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->min('tps') ?: 0; + $lowTPS['last_7days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->min('tps') ?: 0; + $lowTPS['last_30days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->min('tps') ?: 0; + $lowTPS['last_90days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->min('tps') ?: 0; + $numbersData['low_tps'] = $lowTPS; // Avg CPU - $avgCPU["last_24h"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('cpu_load') ?: 0; - $avgCPU["last_7days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('cpu_load') ?: 0; - $avgCPU["last_30days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('cpu_load') ?: 0; - $avgCPU["last_90days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->avg('cpu_load') ?: 0; - $numbersData["avg_cpu"] = $avgCPU; + $avgCPU['last_24h'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('cpu_load') ?: 0; + $avgCPU['last_7days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('cpu_load') ?: 0; + $avgCPU['last_30days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('cpu_load') ?: 0; + $avgCPU['last_90days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->avg('cpu_load') ?: 0; + $numbersData['avg_cpu'] = $avgCPU; // Avg Ram - $avgMemory["last_24h"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('used_memory') ?: 0; - $avgMemory["last_7days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('used_memory') ?: 0; - $avgMemory["last_30days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('used_memory') ?: 0; - $avgMemory["last_90days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->avg('used_memory') ?: 0; - $numbersData["avg_memory"] = $avgMemory; + $avgMemory['last_24h'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('used_memory') ?: 0; + $avgMemory['last_7days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('used_memory') ?: 0; + $avgMemory['last_30days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('used_memory') ?: 0; + $avgMemory['last_90days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->avg('used_memory') ?: 0; + $numbersData['avg_memory'] = $avgMemory; // Avg Chunks - $avgChunks["last_24h"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('chunks_loaded') ?: 0; - $avgChunks["last_7days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('chunks_loaded') ?: 0; - $avgChunks["last_30days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('chunks_loaded') ?: 0; - $avgChunks["last_90days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->avg('chunks_loaded') ?: 0; - $numbersData["avg_chunks"] = $avgChunks; + $avgChunks['last_24h'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('chunks_loaded') ?: 0; + $avgChunks['last_7days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('chunks_loaded') ?: 0; + $avgChunks['last_30days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('chunks_loaded') ?: 0; + $avgChunks['last_90days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->avg('chunks_loaded') ?: 0; + $numbersData['avg_chunks'] = $avgChunks; // Min Free Disk - $minFreeDisk["last_24h"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->min('free_disk_in_kb') ?: 0; - $minFreeDisk["last_7days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->min('free_disk_in_kb') ?: 0; - $minFreeDisk["last_30days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->min('free_disk_in_kb') ?: 0; - $minFreeDisk["last_90days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->min('free_disk_in_kb') ?: 0; - $numbersData["min_free_disk"] = $minFreeDisk; + $minFreeDisk['last_24h'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->min('free_disk_in_kb') ?: 0; + $minFreeDisk['last_7days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->min('free_disk_in_kb') ?: 0; + $minFreeDisk['last_30days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->min('free_disk_in_kb') ?: 0; + $minFreeDisk['last_90days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->min('free_disk_in_kb') ?: 0; + $numbersData['min_free_disk'] = $minFreeDisk; // Total Restarts - $totalRestarts["last_24h"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->distinct()->count('server_session_id'); - $totalRestarts["last_7days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->distinct()->count('server_session_id'); - $totalRestarts["last_30days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->distinct()->count('server_session_id'); - $totalRestarts["last_90days"] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->distinct()->count('server_session_id'); - $numbersData["total_restarts"] = $totalRestarts; + $totalRestarts['last_24h'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->distinct()->count('server_session_id'); + $totalRestarts['last_7days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->distinct()->count('server_session_id'); + $totalRestarts['last_30days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->distinct()->count('server_session_id'); + $totalRestarts['last_90days'] = MinecraftServerLiveInfo::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->distinct()->count('server_session_id'); + $numbersData['total_restarts'] = $totalRestarts; // Player Avg Session Length - $playerAvgSessionLength["last_24h"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('play_time') ?: 0; - $playerAvgSessionLength["last_7days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('play_time') ?: 0; - $playerAvgSessionLength["last_30days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('play_time') ?: 0; - $playerAvgSessionLength["last_90days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->avg('play_time') ?: 0; - $numbersData["player_avg_session_length"] = $playerAvgSessionLength; + $playerAvgSessionLength['last_24h'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('play_time') ?: 0; + $playerAvgSessionLength['last_7days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('play_time') ?: 0; + $playerAvgSessionLength['last_30days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('play_time') ?: 0; + $playerAvgSessionLength['last_90days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->avg('play_time') ?: 0; + $numbersData['player_avg_session_length'] = $playerAvgSessionLength; // Player Avg AFK Time - $playerAvgAFKTime["last_24h"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('afk_time') ?: 0; - $playerAvgAFKTime["last_7days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('afk_time') ?: 0; - $playerAvgAFKTime["last_30days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('afk_time') ?: 0; - $playerAvgAFKTime["last_90days"] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->avg('afk_time') ?: 0; - $numbersData["player_avg_afk_time"] = $playerAvgAFKTime; + $playerAvgAFKTime['last_24h'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subHours(24))->avg('afk_time') ?: 0; + $playerAvgAFKTime['last_7days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subWeek())->avg('afk_time') ?: 0; + $playerAvgAFKTime['last_30days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonth())->avg('afk_time') ?: 0; + $playerAvgAFKTime['last_90days'] = MinecraftPlayerSession::whereIn('server_id', $selectedServers->pluck('id'))->where('created_at', '>=', now()->subMonths(3))->avg('afk_time') ?: 0; + $numbersData['player_avg_afk_time'] = $playerAvgAFKTime; return $numbersData; }); @@ -406,7 +412,7 @@ public function getPlayerPerCountry(Request $request) $query->when($selectedServers, function ($query, $selectedServers) { $query->whereIn('server_id', $selectedServers); }); - } + }, ])->get(); $data = $countries->map(function ($country) { return [ @@ -439,7 +445,6 @@ public function chatlog(Request $request) ->where('type', '!=', ServerType::Bungee()) ->get()->pluck('name', 'id'); - $selectedServers = $request->query('servers') ?? null; // list of selected server ids $chatHistory = QueryBuilder::for(ServerChatlog::class) ->when($selectedServers, function ($query, $selectedServers) { @@ -464,7 +469,7 @@ public function chatlog(Request $request) 'channel', 'type', 'created_at', - AllowedFilter::custom('q', new FilterMultipleFields(['data', 'causer_username', 'causer_uuid', 'type'])) + AllowedFilter::custom('q', new FilterMultipleFields(['data', 'causer_username', 'causer_uuid', 'type'])), ]) ->with(['server:id,name']) ->allowedSorts(['id', 'server_id', 'causer_uuid', 'causer_username', 'type', 'channel', 'created_at']) @@ -474,6 +479,7 @@ public function chatlog(Request $request) $chatHistory->getCollection()->transform(function ($item) { $item->data = MinecraftColorUtils::convertToHTML($item->data); + return $item; }); @@ -516,7 +522,7 @@ public function consolelog(Request $request) 'server_id', 'data', 'created_at', - AllowedFilter::custom('q', new FilterMultipleFields(['data'])) + AllowedFilter::custom('q', new FilterMultipleFields(['data'])), ]) ->with(['server:id,name']) ->allowedSorts(['id', 'server_id', 'created_at']) diff --git a/app/Http/Controllers/Api/ApiServerConsolelogController.php b/app/Http/Controllers/Api/ApiServerConsolelogController.php index 9b2bff5b1..dab26dd3b 100644 --- a/app/Http/Controllers/Api/ApiServerConsolelogController.php +++ b/app/Http/Controllers/Api/ApiServerConsolelogController.php @@ -13,19 +13,24 @@ public function store(Request $request): \Illuminate\Http\JsonResponse { $request->validate([ 'log' => 'required', - 'server_id' => 'required|exists:servers,id' + 'server_id' => 'required|exists:servers,id', ]); - $log = ServerConsolelog::create([ - 'server_id' => $request->server_id, - 'data' => $request->log - ]); + // Split each log into 50000 (<65535) bytes max (MySQL TEXT column limit) + $logs = str_split($request->log, 50000); + + foreach ($logs as $log) { + $log = ServerConsolelog::create([ + 'server_id' => $request->server_id, + 'data' => $log, + ]); - broadcast(new ServerConsolelogCreated($log)); + broadcast(new ServerConsolelogCreated($log)); + } return response()->json([ 'status' => 'success', - 'message' => 'Ok' + 'message' => 'Ok', ], 201); } } diff --git a/app/Listeners/UpsertPlayerOnSessionStart.php b/app/Listeners/UpsertPlayerOnSessionStart.php index 0b8e00acc..ee05b8e27 100644 --- a/app/Listeners/UpsertPlayerOnSessionStart.php +++ b/app/Listeners/UpsertPlayerOnSessionStart.php @@ -6,9 +6,8 @@ use App\Models\MinecraftPlayer; use App\Models\Player; use DB; -use Illuminate\Contracts\Queue\ShouldQueue; -class UpsertPlayerOnSessionStart implements ShouldQueue +class UpsertPlayerOnSessionStart { /** * Create the event listener.