storing analytics data

This commit is contained in:
2026-02-27 09:46:51 +01:00
parent 15b7b77d20
commit f0cca76eb3
57 changed files with 3478 additions and 466 deletions

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::table('artworks', function (Blueprint $table): void {
$table->float('trending_score_24h', 10, 4)->default(0)->after('is_approved')->index();
$table->float('trending_score_7d', 10, 4)->default(0)->after('trending_score_24h')->index();
$table->timestamp('last_trending_calculated_at')->nullable()->after('trending_score_7d');
});
}
public function down(): void
{
Schema::table('artworks', function (Blueprint $table): void {
$table->dropIndex(['trending_score_24h']);
$table->dropIndex(['trending_score_7d']);
$table->dropColumn(['trending_score_24h', 'trending_score_7d', 'last_trending_calculated_at']);
});
}
};

View File

@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* Adds sliding-window view and download counters to artwork_stats.
*
* These columns accumulate between scheduled resets:
* views_24h incremented on every view event; zeroed nightly at 03:30
* views_7d incremented on every view event; zeroed weekly on Monday 03:30
* downloads_24h recomputed nightly from artwork_downloads log (accurate)
* downloads_7d recomputed weekly from artwork_downloads log (accurate)
*
* TrendingService uses these instead of the all-time views/downloads totals.
*/
return new class extends Migration {
public function up(): void
{
Schema::table('artwork_stats', function (Blueprint $table) {
$table->unsignedBigInteger('views_24h')->default(0)->after('views');
$table->unsignedBigInteger('views_7d')->default(0)->after('views_24h');
$table->unsignedBigInteger('downloads_24h')->default(0)->after('downloads');
$table->unsignedBigInteger('downloads_7d')->default(0)->after('downloads_24h');
});
}
public function down(): void
{
Schema::table('artwork_stats', function (Blueprint $table) {
$table->dropColumn(['views_24h', 'views_7d', 'downloads_24h', 'downloads_7d']);
});
}
};

View File

@@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Unified activity feed events table.
*
* Event types: upload | comment | favorite | award | follow
* target_type: artwork | user
*/
public function up(): void
{
Schema::create('activity_events', function (Blueprint $table): void {
$table->id();
$table->unsignedBigInteger('actor_id')->index();
$table->string('type', 20)->index(); // upload|comment|favorite|award|follow
$table->string('target_type', 20)->index(); // artwork|user
$table->unsignedBigInteger('target_id')->index();
$table->json('meta')->nullable(); // extra context (category, tag, etc.)
$table->timestamp('created_at')->useCurrent()->index();
// Composite indexes for feed queries
$table->index(['type', 'created_at'], 'activity_events_type_created_idx');
$table->index(['actor_id', 'created_at'], 'activity_events_actor_created_idx');
$table->index(['target_type', 'target_id'], 'activity_events_target_idx');
$table->foreign('actor_id')->references('id')->on('users')->cascadeOnDelete();
});
}
public function down(): void
{
Schema::dropIfExists('activity_events');
}
};

View File

@@ -0,0 +1,51 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* Persistent view event log.
*
* Stores one row per view authenticated users get a user_id, guests are
* recorded with user_id = null. Enables:
* - "Recently viewed" per user
* - Exact windowed counts (replayable)
* - Unique-viewer counts per artwork
*
* Rows older than 90 days are pruned by the weekly
* skinbase:prune-view-events command.
*/
return new class extends Migration
{
public function up(): void
{
Schema::create('artwork_view_events', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('artwork_id');
$table->unsignedBigInteger('user_id')->nullable(); // null = guest
$table->timestamp('viewed_at')->useCurrent();
// Windowed aggregate queries: COUNT(*) WHERE artwork_id=? AND viewed_at>=?
$table->index(['artwork_id', 'viewed_at']);
// Per-user history: recent artworks viewed by a user
$table->index(['user_id', 'viewed_at']);
// Pruning: DELETE WHERE viewed_at < cutoff
$table->index('viewed_at');
$table->foreign('artwork_id')
->references('id')->on('artworks')
->cascadeOnDelete();
$table->foreign('user_id')
->references('id')->on('users')
->nullOnDelete();
});
}
public function down(): void
{
Schema::dropIfExists('artwork_view_events');
}
};