diff --git a/app/Console/Commands/StartStatJobs.php b/app/Console/Commands/StartStatJobs.php new file mode 100644 index 0000000..2a06a3e --- /dev/null +++ b/app/Console/Commands/StartStatJobs.php @@ -0,0 +1,35 @@ +afterResponse(); + return 0; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 6d23f9c..1985a09 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,6 +2,7 @@ namespace App\Console; +use App\Jobs\StartStatJobs; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -23,6 +24,8 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { + // stats job on night + $schedule->job(StartStatJobs::class)->dailyAt('03:30'); } /** diff --git a/app/Jobs/StartStatJobs.php b/app/Jobs/StartStatJobs.php new file mode 100644 index 0000000..456bf04 --- /dev/null +++ b/app/Jobs/StartStatJobs.php @@ -0,0 +1,17 @@ +onQueue('stats')->delay(rand(0, 90)); + } + } +} diff --git a/app/Jobs/StatJob.php b/app/Jobs/StatJob.php new file mode 100644 index 0000000..3ff9583 --- /dev/null +++ b/app/Jobs/StatJob.php @@ -0,0 +1,78 @@ +brand = $brand; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $stats = new BrandStat([ + 'active' => CustomerLogin::whereBrandId($this->brand->id) + ->whereDate('last_connection', '<', Carbon::now()->subMonths(6))->count(), + 'inactive' => CustomerLogin::whereBrandId($this->brand->id) + ->whereDate('last_connection', '>', Carbon::now()->subMonths(6))->count(), + 'new_accounts' => CustomerLogin::whereBrandId($this->brand->id) + ->whereDate('created_at', '<', Carbon::now()->subMonths(6))->count(), + 'old_accounts' => CustomerLogin::whereBrandId($this->brand->id) + ->whereDate('created_at', '>', Carbon::now()->subMonths(6))->count(), + 'total_accounts' => CustomerLogin::whereBrandId($this->brand->id)->count(), + 'total_opt_in' => CustomerInfo::whereBrandId($this->brand->id) + ->whereOptIn(true)->count() + SingleOptin::whereBrandId($this->brand->id)->count() + ]); + $stats->brand()->associate($this->brand); + $stats->saveOrFail(); + } + + /** + * Get the middleware the job should pass through. + * + * @return array + */ + public function middleware() + { + // dispatch randomly overlaping jobs if overlapping + return [(new WithoutOverlapping(self::JOBKEY))->releaseAfter(rand(20, 90))]; + } +} diff --git a/app/Models/BrandStat.php b/app/Models/BrandStat.php new file mode 100644 index 0000000..8b12948 --- /dev/null +++ b/app/Models/BrandStat.php @@ -0,0 +1,36 @@ + 'date' + ]; + + protected $fillable = [ + 'active', 'inactive', + 'old_accounts', 'new_accounts', + 'total_accounts', 'total_opt_in' + ]; + + protected static function booted() + { + static::creating(function (BrandStat $stat) { + $stat->created_at = Carbon::now(); + }); + } + + public function brand() + { + return $this->belongsTo(Brand::class); + } +} diff --git a/database/migrations/2021_10_29_110018_create_brand_stats_table.php b/database/migrations/2021_10_29_110018_create_brand_stats_table.php new file mode 100644 index 0000000..57e53c6 --- /dev/null +++ b/database/migrations/2021_10_29_110018_create_brand_stats_table.php @@ -0,0 +1,52 @@ +unsignedInteger('active'); + + // Le nombre de comptes inactifs (dernière connexion supérieure à 6 mois) + $table->unsignedInteger('inactive'); + + // Le nombre d’anciens comptes (date de création supérieure à 6 mois + $table->unsignedInteger('old_accounts'); + + // Le nombre de nouveaux comptes (date de création inférieure à 6 mois) + $table->unsignedInteger('new_accounts'); + + // Le nombre de comptes total + $table->unsignedInteger('total_accounts'); + + // Le nombre d’optins (single optin compris) + $table->unsignedInteger('total_opt_in'); + + $table->timestamp('created_at')->nullable(); + + $table->foreignId('brand_id')->constrained('brands'); + + $table->index(['brand_id', 'created_at']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('brand_stats'); + } +}