Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalised first and lasted edited time to use revisions #854

Merged
merged 6 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions app/Jobs/UpdateWikiSiteStatsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Wiki;
use App\WikiSiteStats;
use Carbon\CarbonInterface;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -73,7 +74,7 @@ private function updateSiteStats (Wiki $wiki): void
});
}

private function getFirstEditedDate (Wiki $wiki): ?\Carbon\CarbonInterface
private function getFirstEditedDate (Wiki $wiki): ?CarbonInterface
{
$allRevisions = Http::withHeaders(['host' => $wiki->getAttribute('domain')])->get(
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php',
Expand Down Expand Up @@ -111,18 +112,38 @@ private function getFirstEditedDate (Wiki $wiki): ?\Carbon\CarbonInterface
return Carbon::parse($result);
}

private function getLastEditedDate (Wiki $wiki): ?\Carbon\CarbonInterface
private function getLastEditedDate (Wiki $wiki): ?CarbonInterface
{
$recentChangesInfo = Http::withHeaders(['host' => $wiki->getAttribute('domain')])->get(
$allRevisions = Http::withHeaders(['host' => $wiki->getAttribute('domain')])->get(
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php',
[
'action' => 'query',
'format' => 'json',
'list' => 'allrevisions',
'formatversion' => 2,
'arvlimit' => 1,
'arvprop' => 'ids',
'arvexcludeuser' => 'PlatformReservedUser',
'arvdir' => 'older',
],
);
$lastRevision = data_get($allRevisions->json(), 'query.allrevisions.0.revisions.0.revid');
if (!$lastRevision) {
return null;
}

$revisionInfo = Http::withHeaders(['host' => $wiki->getAttribute('domain')])->get(
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php',
[
'action' => 'query',
'list' => 'recentchanges',
'format' => 'json',
'rcexcludeuser' => 'PlatformReservedUser',
'prop' => 'revisions',
'rvprop' => 'timestamp',
'formatversion' => 2,
'revids' => $lastRevision,
],
);
$result = data_get($recentChangesInfo->json(), 'query.recentchanges.0.timestamp');
$result = data_get($revisionInfo->json(), 'query.pages.0.revisions.0.timestamp');
if (!$result) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use App\Jobs\UpdateWikiSiteStatsJob;
use App\WikiLifecycleEvents;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
WikiLifecycleEvents::all()->map->delete();
m90 marked this conversation as resolved.
Show resolved Hide resolved
(new UpdateWikiSiteStatsJob())->handle();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this run synchronously and waits until all jobs for all wikis are finished or will it just dispatch a set of jobs into the queue and then move on? Asking because I would think calling handle on a job is a wee bit of an anti pattern, but maybe we just need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this run synchronously and waits until all jobs for all wikis are finished

It does this exactly; runs them synchronously and if they fail the migration will fail. You think it would be better to just dispatch it to the queue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think that the migration runs in a transaction, and this means we have a transaction open for a very long time. But maybe that's ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I would guess it be open for best part of a minute+. I've made the job dispatchable and instead dispatched it.

To paste from my local setup this of 50 Wikis from ~10s to 32ms to run the migration

}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
Loading
Loading