-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Job for the above table updates daily Bug:T383421
- Loading branch information
1 parent
7839f12
commit 5dae26b
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Wiki; | ||
use App\WikiDailyMetric; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
|
||
//This job is for the daily measurements of metrics per wikibases. | ||
//This is to help in understanding the purpose of active wikis. | ||
//More metrics should be added to the job as needed. | ||
class UpdateWikiMetricDailyJob implements ShouldQueue | ||
{ | ||
use Dispatchable; | ||
public int $timeout = 3600; | ||
|
||
protected Wiki $wiki; | ||
|
||
public function __construct($wiki) | ||
{ | ||
$this->wiki = $wiki; | ||
} | ||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$today = now()->format('Y-m-d'); | ||
$oldRecord = WikiDailyMetric::where('wiki_id', $this->wiki->id)->latest('date')->first(); | ||
$todayPageCount = $this->wiki->wikiSiteStats()->first()->pages ?? 0; | ||
$isDeleted = (bool)$this->wiki->deleted_at; | ||
if( !$oldRecord || $oldRecord->pages != $todayPageCount || !$oldRecord->is_deleted ) { | ||
WikiDailyMetric::create([ | ||
'id' => $this->wiki->id . '_' . date('Y-m-d'), | ||
'pages' => $todayPageCount, | ||
'is_deleted' => $isDeleted, | ||
'date' => $today, | ||
'wiki_id' => $this->wiki->id, | ||
]); | ||
|
||
\Log::info("New metric recorded for Wiki ID {$this->wiki->id}"); | ||
} else { | ||
\Log::info("Metric unchanged for Wiki ID {$this->wiki->id}, no new record added."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class WikiDailyMetric extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'wiki_daily_metrics'; | ||
|
||
protected $primaryKey = 'id'; | ||
|
||
public $incrementing = false; // Disable auto-increment | ||
|
||
protected $keyType = 'string'; | ||
|
||
protected $fillable = [ | ||
'id', | ||
'wiki_id', | ||
'date', | ||
'pages', | ||
'is_deleted', | ||
]; | ||
|
||
|
||
} |
32 changes: 32 additions & 0 deletions
32
database/migrations/2025_01_28_144045_wiki_daily_metric.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('wiki_daily_metrics', function (Blueprint $table) { | ||
$table->string('id')->primary(); | ||
$table->string('wiki_id'); | ||
$table->integer('pages'); | ||
$table->boolean('is_deleted'); | ||
$table->date('date'); | ||
$table->timestamps(); // Created at & Updated at | ||
$table->unique(['wiki_id', 'date']); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('wiki_daily_metrics'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Jobs; | ||
|
||
use App\Wiki; | ||
use Tests\TestCase; | ||
|
||
class UpdateWikiMetricDailyJobTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function dispatchJob() | ||
{ | ||
Queue::fake(); | ||
|
||
UpdateWikiMetricDailyJob::dispatch(); | ||
|
||
Queue::assertPushed(UpdateWikiMetricDailyJob::class); | ||
} | ||
|
||
public function successfullyAddRecords() | ||
{ | ||
$wiki = Wiki::factory()->create([ | ||
'domain' => 'thisfake.wikibase.cloud' | ||
]); | ||
(new UpdateWikiMetricDailyJob($wiki))->handle(); | ||
|
||
// Assert the metric is updated in the database | ||
$this->assertDatabaseHas('wiki_daily_metrics', [ | ||
'date' => now()->toDateString() | ||
]); | ||
} | ||
|
||
/** @test | ||
public function doesNotAddDuplicateRecordsWithOnlyDateChange() | ||
{ | ||
}*/ | ||
} | ||
|