Skip to content

Commit

Permalink
Fix bugs in Intel data
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinecraft committed Mar 7, 2024
1 parent 075e834 commit 2802805
Show file tree
Hide file tree
Showing 164 changed files with 1,332 additions and 1,300 deletions.
62 changes: 34 additions & 28 deletions app/Http/Controllers/Admin/GraphController.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,22 +257,38 @@ public function getServerPerformanceOverTime(Request $request)

$fromDate = $request->query('from_date') ?? now()->subWeek();
$toDate = $request->query('to_date') ?? now();
$query = DB::table('minecraft_server_live_infos')
->selectRaw("ROUND(UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(created_at, '%Y-%m-%d %H:'), LPAD((MINUTE(created_at) DIV 5) * 5, 2, '0'), ':00')) * 1000) AS created_at_5min")
$subquery = DB::table('minecraft_server_live_infos')
->selectRaw('ROUND(UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(created_at, "%Y-%m-%d %H:"), LPAD((MINUTE(created_at) DIV 5) * 5, 2, "0"), ":00")) * 1000) AS created_at_5min')
->selectRaw('MAX(online_players) AS online_players')
->selectRaw('MIN(tps) AS tps')
->selectRaw('MAX(cpu_load) AS cpu_load')
->selectRaw('MAX(used_memory) AS used_memory')
->selectRaw('MAX(chunks_loaded) AS chunks_loaded')
->groupBy('created_at_5min')
->orderBy('created_at_5min')
->selectRaw('server_id')
->where('created_at', '>=', $fromDate)
->where('created_at', '<=', $toDate)
->whereIn('server_id', $servers->pluck('id'));
->whereIn('server_id', $servers->pluck('id'))
->groupBy('created_at_5min', 'server_id');

$query = DB::table(DB::raw("({$subquery->toSql()}) as performance_per_server"))
->mergeBindings($subquery)
->select(
'performance_per_server.created_at_5min',
DB::raw('SUM(performance_per_server.online_players) AS online_players'),
DB::raw('AVG(performance_per_server.tps) AS tps'),
DB::raw('AVG(performance_per_server.cpu_load) AS cpu_load'),
DB::raw('SUM(performance_per_server.used_memory) AS used_memory'),
DB::raw('SUM(performance_per_server.chunks_loaded) AS chunks_loaded')
)
->groupBy('performance_per_server.created_at_5min')
->orderBy('performance_per_server.created_at_5min', 'ASC');

$sqlData = $query->get();
$sqlData = $sqlData->map(function ($item) {
$item->created_at_5min = (int) $item->created_at_5min;
$item->used_memory = round($item->used_memory / 1024); // Convert to MB
$item->tps = round($item->tps, 2);
$item->cpu_load = round($item->cpu_load, 2);

return array_values(get_object_vars($item));
})->toArray();
Expand Down Expand Up @@ -305,19 +321,9 @@ public function getServerOnlineActivityOverTime(Request $request)
$fromDate = $request->query('from_date') ?? now()->subMonth();
$toDate = $request->query('to_date') ?? now();
$subquery = DB::table('minecraft_server_live_infos')
->selectRaw('
ROUND(
UNIX_TIMESTAMP(
CONCAT(
DATE_FORMAT(created_at, "%Y-%m-%d %H:"),
LPAD((MINUTE(created_at) DIV 5) * 5, 2, "0"),
":00"
)
) * 1000
) AS created_at_5min,
MAX(online_players) AS max_online_players,
server_id
')
->selectRaw('ROUND(UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(created_at, "%Y-%m-%d %H:"),LPAD((MINUTE(created_at) DIV 5) * 5, 2, "0"),":00")) * 1000) AS created_at_5min')
->selectRaw('MAX(online_players) AS max_online_players')
->selectRaw('server_id')
->where('created_at', '>=', $fromDate)
->where('created_at', '<=', $toDate)
->whereIn('server_id', $servers->pluck('id'))
Expand Down Expand Up @@ -363,18 +369,18 @@ public function getPlayerMinecraftVersions(Request $request)
$servers = Server::where('type', '!=', ServerType::Bungee())->get();
}

$data = DB::table('minecraft_player_sessions')
->selectRaw('COUNT(*) AS count, minecraft_version')
$data = DB::table('minecraft_players')
->selectRaw('COUNT(DISTINCT player_uuid) AS count, last_minecraft_version')
->whereIn('server_id', $servers->pluck('id'))
->groupBy('minecraft_version')
->groupBy('last_minecraft_version')
->orderBy('count', 'desc')
->when($request->query('top'), function ($query, $top) {
return $query->limit($top);
})
->get()
->map(function ($item) {
return [
'name' => $item->minecraft_version ?? __('Unknown'),
'name' => $item->last_minecraft_version ?? __('Unknown'),
'value' => $item->count,
];
});
Expand Down Expand Up @@ -402,18 +408,18 @@ public function getPlayerJoinAddresses(Request $request)
$servers = Server::where('type', '!=', ServerType::Bungee())->get();
}

$data = DB::table('minecraft_player_sessions')
->selectRaw('COUNT(*) AS count, join_address')
$data = DB::table('minecraft_players')
->selectRaw('COUNT(DISTINCT player_uuid) AS count, last_join_address')
->whereIn('server_id', $servers->pluck('id'))
->groupBy('join_address')
->groupBy('last_join_address')
->orderBy('count', 'desc')
->when($request->query('top'), function ($query, $top) {
return $query->limit($top);
})
->get()
->map(function ($item) {
return [
'name' => $item->join_address ?? __('Unknown'),
'name' => $item->last_join_address ?? __('Unknown'),
'value' => $item->count,
];
});
Expand Down Expand Up @@ -444,7 +450,7 @@ public function getPlayerJoinAddressesOverTime(Request $request)
$query = DB::table('minecraft_player_sessions')
->selectRaw('UNIX_TIMESTAMP(DATE(created_at)) AS unix_day')
->selectRaw('join_address')
->selectRaw('COUNT(*) AS count')
->selectRaw('COUNT(DISTINCT player_uuid) AS count')
->groupBy(['join_address', 'unix_day'])
->orderBy('unix_day')
->where('created_at', '>=', $fromDate)
Expand Down Expand Up @@ -519,7 +525,7 @@ public function getPlayerMinecraftVersionsOverTime(Request $request)
$query = DB::table('minecraft_player_sessions')
->selectRaw('UNIX_TIMESTAMP(DATE(created_at)) AS unix_day')
->selectRaw('minecraft_version')
->selectRaw('COUNT(*) AS count')
->selectRaw('COUNT(DISTINCT player_uuid) AS count')
->groupBy(['minecraft_version', 'unix_day'])
->orderBy('unix_day')
->where('created_at', '>=', $fromDate)
Expand Down
29 changes: 14 additions & 15 deletions app/Http/Controllers/Admin/ServerIntelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,8 @@ public function overview(Request $request)
->where('first_seen_at', '>=', now()->subWeek())
->distinct()->count('player_uuid');

// Peek Online Players
$peekOnlinePlayersCount = MinecraftServerLiveInfo::query()
->when($selectedServers, function ($query, $selectedServers) {
$query->whereIn('server_id', $selectedServers);
})
->where('created_at', '>=', now()->subWeek())
->max('online_players') ?: 0;
// Peek Online Players Count
$peekOnlinePlayersCount = MinecraftServerLiveInfo::getOnlinePlayersCount($selectedServers, now()->subWeek());

// Avg TPS
$averageTps = MinecraftServerLiveInfo::select(['tps'])
Expand Down Expand Up @@ -309,10 +304,10 @@ 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;
$maxPlayers['last_24h'] = MinecraftServerLiveInfo::getOnlinePlayersCount($selectedServers->pluck('id'), now()->subHours(24)) ?: 0;
$maxPlayers['last_7days'] = MinecraftServerLiveInfo::getOnlinePlayersCount($selectedServers->pluck('id'), now()->subWeek()) ?: 0;
$maxPlayers['last_30days'] = MinecraftServerLiveInfo::getOnlinePlayersCount($selectedServers->pluck('id'), now()->subMonth()) ?: 0;
$maxPlayers['last_90days'] = MinecraftServerLiveInfo::getOnlinePlayersCount($selectedServers->pluck('id'), now()->subMonths(3)) ?: 0;
$numbersData['max_players'] = $maxPlayers;

// Lowest TPS
Expand Down Expand Up @@ -406,11 +401,15 @@ public function getPlayerPerCountry(Request $request)

$countries = Country::withCount([
'minecraftPlayers' => function ($query) use ($selectedServers) {
$query->when($selectedServers, function ($query, $selectedServers) {
$query->whereIn('server_id', $selectedServers);
});
$query->select(DB::raw('COUNT(DISTINCT(player_uuid))'))
->when($selectedServers, function ($query, $selectedServers) {
$query->whereIn('server_id', $selectedServers);
});
},
])->get();
])
->orderBy('minecraft_players_count', 'desc')
->get();

$data = $countries->map(function ($country) {
return [
'name' => $country->name,
Expand Down
28 changes: 28 additions & 0 deletions app/Models/MinecraftServerLiveInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,37 @@

namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class MinecraftServerLiveInfo extends BaseModel
{
use HasFactory;

public static function getOnlinePlayersCount($serverIds, $endDate)
{
$subquery = DB::table('minecraft_server_live_infos')
->selectRaw('ROUND(UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(created_at, "%Y-%m-%d %H:"),LPAD((MINUTE(created_at) DIV 5) * 5, 2, "0"),":00")) * 1000) AS created_at_5min')
->selectRaw('MAX(online_players) AS max_online_players')
->selectRaw('server_id')
->when($endDate, function ($query, $endDate) {
return $query->where('created_at', '>=', $endDate);
})
->when($serverIds, function ($query, $serverIds) {
return $query->whereIn('server_id', $serverIds);
})
->groupBy('created_at_5min', 'server_id');

$query = DB::table(DB::raw("({$subquery->toSql()}) as online_players_per_server"))
->mergeBindings($subquery)
->select('online_players_per_server.created_at_5min', DB::raw('SUM(online_players_per_server.max_online_players) AS total_max_online_players'))
->groupBy('online_players_per_server.created_at_5min')
->orderBy('online_players_per_server.created_at_5min', 'ASC');

$max = DB::table(DB::raw("({$query->toSql()}) as online_players"))
->mergeBindings($query)
->max('online_players.total_max_online_players');

return (int) $max;
}
}
2 changes: 1 addition & 1 deletion config/pulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
|
*/

'enabled' => env('PULSE_ENABLED', true),
'enabled' => env('PULSE_ENABLED', false),

/*
|--------------------------------------------------------------------------
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2802805

Please sign in to comment.