mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-23 19:37:35 +08:00
perf: fix getTrafficRank slow query with index and batch loading
This commit is contained in:
@@ -481,19 +481,20 @@ class StatController extends Controller
|
||||
}
|
||||
|
||||
$result = [];
|
||||
$ids = $currentData->pluck('id');
|
||||
$names = $type === 'node'
|
||||
? Server::whereIn('id', $ids)->pluck('name', 'id')
|
||||
: User::whereIn('id', $ids)->pluck('email', 'id');
|
||||
|
||||
foreach ($currentData as $data) {
|
||||
$previousValue = isset($previousData[$data->id]) ? $previousData[$data->id]->value : 0;
|
||||
$change = $previousValue > 0 ? round(($data->value - $previousValue) / $previousValue * 100, 1) : 0;
|
||||
|
||||
$name = $type === 'node'
|
||||
? optional(Server::find($data->id))->name ?? "Node {$data->id}"
|
||||
: optional(User::find($data->id))->email ?? "User {$data->id}";
|
||||
|
||||
$result[] = [
|
||||
'id' => (string) $data->id,
|
||||
'name' => $name,
|
||||
'value' => $data->value, // Convert to GB
|
||||
'previousValue' => $previousValue, // Convert to GB
|
||||
'name' => $names[$data->id] ?? ($type === 'node' ? "Node {$data->id}" : "User {$data->id}"),
|
||||
'value' => $data->value,
|
||||
'previousValue' => $previousValue,
|
||||
'change' => $change,
|
||||
'timestamp' => date('c', $endDate)
|
||||
];
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('v2_stat_user', function (Blueprint $table) {
|
||||
$table->index(['record_at', 'user_id'], 'idx_stat_user_record_user');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('v2_stat_user', function (Blueprint $table) {
|
||||
$table->dropIndex('idx_stat_user_record_user');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user