Skip to content

Commit

Permalink
Add WikiDailyMetric table
Browse files Browse the repository at this point in the history
Add Job for the above table updates daily

Bug:T383421
  • Loading branch information
rosalieper committed Feb 4, 2025
1 parent 7839f12 commit 5dae26b
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use App\Jobs\RequeuePendingQsBatchesJob;
use App\Jobs\SandboxCleanupJob;
use App\Jobs\PollForMediaWikiJobsJob;
use App\Jobs\UpdateWikiMetricDailyJob;
use App\Jobs\UpdateWikiSiteStatsJob;
use App\Jobs\SendEmptyWikiNotificationsJob;
use App\Jobs\CreateQueryserviceBatchesJob;
use App\Jobs\FailStalledEntityImportsJob;
use App\Jobs\UpdateQueryserviceAllowList;
use App\Wiki;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand Down Expand Up @@ -60,6 +62,14 @@ protected function schedule(Schedule $schedule): void
$schedule->job(new SendEmptyWikiNotificationsJob)->dailyAt('21:00');

$schedule->job(new UpdateQueryserviceAllowList)->weeklyOn(Schedule::MONDAY, '01:00');

$schedule->call(function () {
$wikis = Wiki::all();

foreach ($wikis as $wiki) {
dispatch(new UpdateWikiMetricDailyJob($wiki));
}
})->dailyAt('23:00');
}

/**
Expand Down
47 changes: 47 additions & 0 deletions app/Jobs/UpdateWikiMetricDailyJob.php
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.");
}
}
}
29 changes: 29 additions & 0 deletions app/WikiDailyMetric.php
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 database/migrations/2025_01_28_144045_wiki_daily_metric.php
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');
}
};
40 changes: 40 additions & 0 deletions tests/Jobs/UpdateWikiMetricDailyJobTest.php
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()
{
}*/
}

0 comments on commit 5dae26b

Please sign in to comment.