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

Add table and job for daily tracking and recording of wiki metrics #880

Merged
merged 7 commits into from
Feb 10, 2025

Conversation

rosalieper
Copy link
Contributor

@rosalieper rosalieper commented Jan 31, 2025

Added new table wiki_daily_metrics
Added a new Wiki.php class in the metric folder for wiki related metrics
Added a Job scheduled to run daily and update metrics in the wiki_daily_metric table.

Bug: T383421

@rosalieper rosalieper marked this pull request as draft January 31, 2025 14:38
@rosalieper rosalieper force-pushed the T383421 branch 3 times, most recently from bacc211 to 37591b6 Compare February 3, 2025 10:48
Copy link
Contributor

@tarrow tarrow left a comment

Choose a reason for hiding this comment

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

Just a few thoughts for you to while over; looks like a great start :)

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class UpdateWikiMetricDailyJob implements ShouldQueue
Copy link
Contributor

Choose a reason for hiding this comment

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

I get how we got here but I wonder if we can think of a way to name this or add a comment to make it clearer how this jobs differs from all of the other metric jobs

$isDeleted = (bool)$this->wiki->deleted_at;
if( !$oldRecord || $oldRecord->pages != $todayPageCount || !$oldRecord->is_deleted ) {
WikiDailyMetric::create([
'id' => $this->wiki->id . '_' . date('Y-m-d'),
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this is doing exactly what the ticket asked; I wondered for a while if it would be better to have a a primary key that is less of a natural key. See for example https://stackoverflow.com/a/718200

Perhaps you should ask Anton if he's really sure he wants this: for example is he certain he won't want to get two of these in a single day? Would there ever be an issue in merging or splitting wikis in the future?

OTOH I think this isn't a dealbreaker and we can probably live with it either way

{
$today = now()->format('Y-m-d');
$oldRecord = WikiDailyMetric::where('wiki_id', $this->wiki->id)->latest('date')->first();
$todayPageCount = $this->wiki->wikiSiteStats()->first()->pages ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

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

probably fine to just null this; to note this can happen if the wikiSiteStats update job hasn't run at all for this wiki yet

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure I understand. Do you mean I should set the value of $todayPageCount to null?

Copy link
Contributor

@tarrow tarrow Feb 4, 2025

Choose a reason for hiding this comment

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

After looking it's not fine to null this since pages claims to be an integer https://github.com/wbstack/api/pull/880/files#diff-516b6ba1298695ebbde7ccd7327da6954e9d29a05b82c3c51898d6fa7ad64a91R17

$wikis = Wiki::all();

foreach ($wikis as $wiki) {
dispatch(new UpdateWikiMetricDailyJob($wiki));
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably fine... I wonder if we're going to find that in production creating 1k+ jobs all at the same time is is problematic; how much worse would you find it to add a job then then spawns the other jobs over a period of time? Maybe we could even add a sleep to this closure here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One way to go about this would be to loop through the wikis in one job, instead of calling a job per wiki but my issue was how to test that. I wanted a way to inject the wiki in the job for testing.

WikiDailyMetric::create([
'id' => $this->wiki->id . '_' . date('Y-m-d'),
'pages' => $todayPageCount,
'id_deleted' => $isDeleted,
Copy link
Contributor

@tarrow tarrow Feb 4, 2025

Choose a reason for hiding this comment

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

I discovered trying to run it that this is a typo

Suggested change
'id_deleted' => $isDeleted,
'is_deleted' => $isDeleted,

Add Job for the above table updates daily

Bug:T383421
@rosalieper rosalieper changed the title Draft Commit Add table and job for daily tracking and recording of wiki metrics Feb 4, 2025
@rosalieper rosalieper marked this pull request as ready for review February 6, 2025 15:17
]);
}

/** @test*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/** @test*/

{
use RefreshDatabase;

/** @test */
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/** @test */

Copy link
Contributor

@tarrow tarrow left a comment

Choose a reason for hiding this comment

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

looks good to me; i'd remove these test annotations since we don't have them anywhere else.

Let's ship this thing and see how it works!

@rosalieper rosalieper merged commit 7b1f1de into main Feb 10, 2025
5 checks passed
@rosalieper rosalieper deleted the T383421 branch February 10, 2025 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants