perf: fix getTrafficRank slow query with index and batch loading
This commit is contained in:
@@ -481,19 +481,20 @@ class StatController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$result = [];
|
$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) {
|
foreach ($currentData as $data) {
|
||||||
$previousValue = isset($previousData[$data->id]) ? $previousData[$data->id]->value : 0;
|
$previousValue = isset($previousData[$data->id]) ? $previousData[$data->id]->value : 0;
|
||||||
$change = $previousValue > 0 ? round(($data->value - $previousValue) / $previousValue * 100, 1) : 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[] = [
|
$result[] = [
|
||||||
'id' => (string) $data->id,
|
'id' => (string) $data->id,
|
||||||
'name' => $name,
|
'name' => $names[$data->id] ?? ($type === 'node' ? "Node {$data->id}" : "User {$data->id}"),
|
||||||
'value' => $data->value, // Convert to GB
|
'value' => $data->value,
|
||||||
'previousValue' => $previousValue, // Convert to GB
|
'previousValue' => $previousValue,
|
||||||
'change' => $change,
|
'change' => $change,
|
||||||
'timestamp' => date('c', $endDate)
|
'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