Upload beautify
This commit is contained in:
138
tests/Feature/Analytics/FeedAnalyticsTest.php
Normal file
138
tests/Feature/Analytics/FeedAnalyticsTest.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Artwork;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('stores feed analytics events with required dimensions', function () {
|
||||
$user = User::factory()->create();
|
||||
$artwork = Artwork::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson('/api/analytics/feed', [
|
||||
'event_type' => 'feed_click',
|
||||
'artwork_id' => $artwork->id,
|
||||
'position' => 3,
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source' => 'personalized',
|
||||
'dwell_seconds' => 27,
|
||||
]);
|
||||
|
||||
$response->assertOk()->assertJson(['success' => true]);
|
||||
|
||||
$this->assertDatabaseHas('feed_events', [
|
||||
'user_id' => $user->id,
|
||||
'artwork_id' => $artwork->id,
|
||||
'event_type' => 'feed_click',
|
||||
'position' => 3,
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source' => 'personalized',
|
||||
'dwell_seconds' => 27,
|
||||
]);
|
||||
});
|
||||
|
||||
it('aggregates daily feed analytics with ctr save-rate and dwell buckets', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$artworkA = Artwork::factory()->create();
|
||||
$artworkB = Artwork::factory()->create();
|
||||
|
||||
$metricDate = now()->subDay()->toDateString();
|
||||
|
||||
DB::table('feed_events')->insert([
|
||||
[
|
||||
'event_date' => $metricDate,
|
||||
'event_type' => 'feed_impression',
|
||||
'user_id' => $user->id,
|
||||
'artwork_id' => $artworkA->id,
|
||||
'position' => 1,
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source' => 'personalized',
|
||||
'dwell_seconds' => null,
|
||||
'occurred_at' => now()->subDay(),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
],
|
||||
[
|
||||
'event_date' => $metricDate,
|
||||
'event_type' => 'feed_impression',
|
||||
'user_id' => $user->id,
|
||||
'artwork_id' => $artworkB->id,
|
||||
'position' => 2,
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source' => 'personalized',
|
||||
'dwell_seconds' => null,
|
||||
'occurred_at' => now()->subDay(),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
],
|
||||
[
|
||||
'event_date' => $metricDate,
|
||||
'event_type' => 'feed_click',
|
||||
'user_id' => $user->id,
|
||||
'artwork_id' => $artworkA->id,
|
||||
'position' => 1,
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source' => 'personalized',
|
||||
'dwell_seconds' => 3,
|
||||
'occurred_at' => now()->subDay(),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
],
|
||||
[
|
||||
'event_date' => $metricDate,
|
||||
'event_type' => 'feed_click',
|
||||
'user_id' => $user->id,
|
||||
'artwork_id' => $artworkB->id,
|
||||
'position' => 2,
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source' => 'personalized',
|
||||
'dwell_seconds' => 35,
|
||||
'occurred_at' => now()->subDay(),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
],
|
||||
]);
|
||||
|
||||
DB::table('user_discovery_events')->insert([
|
||||
'event_id' => '33333333-3333-3333-3333-333333333333',
|
||||
'user_id' => $user->id,
|
||||
'artwork_id' => $artworkA->id,
|
||||
'category_id' => null,
|
||||
'event_type' => 'favorite',
|
||||
'event_version' => 'event-v1',
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'weight' => 1,
|
||||
'event_date' => $metricDate,
|
||||
'occurred_at' => now()->subDay(),
|
||||
'meta' => null,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$this->artisan('analytics:aggregate-feed', ['--date' => $metricDate])->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('feed_daily_metrics', [
|
||||
'metric_date' => $metricDate,
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source' => 'personalized',
|
||||
'impressions' => 2,
|
||||
'clicks' => 2,
|
||||
'saves' => 1,
|
||||
'dwell_0_5' => 1,
|
||||
'dwell_30_120' => 1,
|
||||
]);
|
||||
|
||||
$metric = DB::table('feed_daily_metrics')
|
||||
->where('metric_date', $metricDate)
|
||||
->where('algo_version', 'clip-cosine-v1')
|
||||
->where('source', 'personalized')
|
||||
->first();
|
||||
|
||||
expect((float) $metric->ctr)->toBe(1.0);
|
||||
expect((float) $metric->save_rate)->toBe(0.5);
|
||||
});
|
||||
93
tests/Feature/Analytics/FeedEvaluationCommandsTest.php
Normal file
93
tests/Feature/Analytics/FeedEvaluationCommandsTest.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
it('evaluates feed weights for all algos', function () {
|
||||
$metricDate = now()->subDay()->toDateString();
|
||||
|
||||
DB::table('feed_daily_metrics')->insert([
|
||||
[
|
||||
'metric_date' => $metricDate,
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source' => 'personalized',
|
||||
'impressions' => 100,
|
||||
'clicks' => 20,
|
||||
'saves' => 6,
|
||||
'ctr' => 0.2,
|
||||
'save_rate' => 0.3,
|
||||
'dwell_0_5' => 4,
|
||||
'dwell_5_30' => 8,
|
||||
'dwell_30_120' => 5,
|
||||
'dwell_120_plus' => 3,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
],
|
||||
[
|
||||
'metric_date' => $metricDate,
|
||||
'algo_version' => 'clip-cosine-v2',
|
||||
'source' => 'personalized',
|
||||
'impressions' => 100,
|
||||
'clicks' => 22,
|
||||
'saves' => 8,
|
||||
'ctr' => 0.22,
|
||||
'save_rate' => 0.36,
|
||||
'dwell_0_5' => 3,
|
||||
'dwell_5_30' => 9,
|
||||
'dwell_30_120' => 6,
|
||||
'dwell_120_plus' => 4,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
],
|
||||
]);
|
||||
|
||||
$this->artisan('analytics:evaluate-feed-weights', ['--from' => $metricDate, '--to' => $metricDate])
|
||||
->assertSuccessful();
|
||||
});
|
||||
|
||||
it('compares baseline and candidate feed algos', function () {
|
||||
$metricDate = now()->subDay()->toDateString();
|
||||
|
||||
DB::table('feed_daily_metrics')->insert([
|
||||
[
|
||||
'metric_date' => $metricDate,
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source' => 'personalized',
|
||||
'impressions' => 100,
|
||||
'clicks' => 20,
|
||||
'saves' => 6,
|
||||
'ctr' => 0.2,
|
||||
'save_rate' => 0.3,
|
||||
'dwell_0_5' => 4,
|
||||
'dwell_5_30' => 8,
|
||||
'dwell_30_120' => 5,
|
||||
'dwell_120_plus' => 3,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
],
|
||||
[
|
||||
'metric_date' => $metricDate,
|
||||
'algo_version' => 'clip-cosine-v2',
|
||||
'source' => 'personalized',
|
||||
'impressions' => 100,
|
||||
'clicks' => 24,
|
||||
'saves' => 10,
|
||||
'ctr' => 0.24,
|
||||
'save_rate' => 0.416,
|
||||
'dwell_0_5' => 3,
|
||||
'dwell_5_30' => 8,
|
||||
'dwell_30_120' => 7,
|
||||
'dwell_120_plus' => 6,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
],
|
||||
]);
|
||||
|
||||
$this->artisan('analytics:compare-feed-ab', [
|
||||
'baseline' => 'clip-cosine-v1',
|
||||
'candidate' => 'clip-cosine-v2',
|
||||
'--from' => $metricDate,
|
||||
'--to' => $metricDate,
|
||||
])->assertSuccessful();
|
||||
});
|
||||
72
tests/Feature/Analytics/SimilarArtworkAnalyticsTest.php
Normal file
72
tests/Feature/Analytics/SimilarArtworkAnalyticsTest.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Artwork;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('stores similar artwork analytics events', function () {
|
||||
$author = User::factory()->create();
|
||||
|
||||
$source = Artwork::factory()->create(['user_id' => $author->id]);
|
||||
$similar = Artwork::factory()->create(['user_id' => $author->id]);
|
||||
|
||||
$response = $this->postJson('/api/analytics/similar-artworks', [
|
||||
'event_type' => 'click',
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source_artwork_id' => $source->id,
|
||||
'similar_artwork_id' => $similar->id,
|
||||
'position' => 2,
|
||||
]);
|
||||
|
||||
$response->assertOk()->assertJson(['success' => true]);
|
||||
|
||||
$this->assertDatabaseHas('similar_artwork_events', [
|
||||
'event_type' => 'click',
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source_artwork_id' => $source->id,
|
||||
'similar_artwork_id' => $similar->id,
|
||||
'position' => 2,
|
||||
]);
|
||||
});
|
||||
|
||||
it('aggregates daily analytics counts by algo version', function () {
|
||||
DB::table('similar_artwork_events')->insert([
|
||||
[
|
||||
'event_date' => now()->subDay()->toDateString(),
|
||||
'event_type' => 'impression',
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source_artwork_id' => Artwork::factory()->create()->id,
|
||||
'similar_artwork_id' => null,
|
||||
'position' => null,
|
||||
'items_count' => 8,
|
||||
'occurred_at' => now()->subDay(),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
],
|
||||
[
|
||||
'event_date' => now()->subDay()->toDateString(),
|
||||
'event_type' => 'click',
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'source_artwork_id' => Artwork::factory()->create()->id,
|
||||
'similar_artwork_id' => Artwork::factory()->create()->id,
|
||||
'position' => 1,
|
||||
'items_count' => null,
|
||||
'occurred_at' => now()->subDay(),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
],
|
||||
]);
|
||||
|
||||
$this->artisan('analytics:aggregate-similar-artworks', ['--date' => now()->subDay()->toDateString()])
|
||||
->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('similar_artwork_daily_metrics', [
|
||||
'metric_date' => now()->subDay()->toDateString(),
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'impressions' => 1,
|
||||
'clicks' => 1,
|
||||
]);
|
||||
});
|
||||
Reference in New Issue
Block a user