feat(wg): wg_traffic_samples table + model

feat/v1-foundation
boban 2026-06-20 23:59:11 +02:00
parent eff836a1fb
commit ae4937654d
3 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,18 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class WgTrafficSample extends Model
{
public $timestamps = false;
protected $guarded = [];
protected $casts = [
'rx' => 'integer',
'tx' => 'integer',
'sampled_at' => 'datetime',
];
}

View File

@ -0,0 +1,26 @@
<?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::create('wg_traffic_samples', function (Blueprint $table) {
$table->id();
$table->string('peer_pubkey');
$table->string('peer_name')->nullable();
$table->unsignedBigInteger('rx')->default(0);
$table->unsignedBigInteger('tx')->default(0);
$table->timestamp('sampled_at')->index();
$table->index(['peer_pubkey', 'sampled_at']);
});
}
public function down(): void
{
Schema::dropIfExists('wg_traffic_samples');
}
};

View File

@ -0,0 +1,23 @@
<?php
namespace Tests\Feature;
use App\Models\WgTrafficSample;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class WgTrafficSampleTest extends TestCase
{
use RefreshDatabase;
public function test_persists_and_casts_a_sample(): void
{
$s = WgTrafficSample::create([
'peer_pubkey' => 'abc', 'peer_name' => 'laptop', 'rx' => 1000, 'tx' => 2000, 'sampled_at' => now(),
]);
$this->assertDatabaseHas('wg_traffic_samples', ['peer_pubkey' => 'abc', 'rx' => 1000]);
$this->assertInstanceOf(Carbon::class, $s->fresh()->sampled_at);
}
}