Skip to content

Commit 14ec44d

Browse files
committed
Add admin instance stats collector
1 parent 0d4fe62 commit 14ec44d

4 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\Comment;
6+
use App\Models\CommentReply;
7+
use App\Models\Follower;
8+
use App\Models\Instance;
9+
use App\Models\Profile;
10+
use App\Models\Report;
11+
use App\Models\Video;
12+
use Illuminate\Console\Command;
13+
14+
class InstanceStatsCollectorCommand extends Command
15+
{
16+
protected $signature = 'app:instance-stats-collector-command';
17+
18+
protected $description = 'Collect statistics for instances that need updating';
19+
20+
public function handle()
21+
{
22+
$count = 0;
23+
$now = now();
24+
25+
Instance::query()
26+
->where(function ($query) {
27+
$query->whereNull('stats_last_collected_at')
28+
->orWhere('stats_last_collected_at', '<', now()->subDay());
29+
})
30+
->limit(250)
31+
->lazyById()
32+
->each(function ($instance) use (&$count, $now) {
33+
$instance->user_count = Profile::whereDomain($instance->domain)->count();
34+
$instance->video_count = Video::join('profiles', 'videos.profile_id', '=', 'profiles.id')
35+
->where('profiles.domain', $instance->domain)
36+
->count();
37+
$instance->comment_count = Comment::join('profiles', 'comments.profile_id', '=', 'profiles.id')
38+
->where('profiles.domain', $instance->domain)
39+
->count();
40+
$instance->reply_count = CommentReply::join('profiles', 'comment_replies.profile_id', '=', 'profiles.id')
41+
->where('profiles.domain', $instance->domain)
42+
->count();
43+
$instance->follower_count = Follower::join('profiles', 'followers.following_id', '=', 'profiles.id')
44+
->where('profiles.domain', $instance->domain)
45+
->count();
46+
$instance->following_count = Follower::join('profiles', 'followers.profile_id', '=', 'profiles.id')
47+
->where('profiles.domain', $instance->domain)
48+
->count();
49+
$instance->report_count = Report::join('profiles', 'reports.reported_profile_id', '=', 'profiles.id')
50+
->where('profiles.domain', $instance->domain)
51+
->count();
52+
$instance->stats_last_collected_at = $now;
53+
$instance->save();
54+
55+
$count++;
56+
});
57+
58+
$this->info("Collected stats for {$count} instances");
59+
}
60+
}

app/Models/Instance.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* @property int $failure_count
3030
* @property \Illuminate\Support\Carbon|null $created_at
3131
* @property \Illuminate\Support\Carbon|null $updated_at
32+
* @property \Illuminate\Support\Carbon|null $stats_last_collected_at
3233
*
3334
* @method static \Illuminate\Database\Eloquent\Builder<static>|Instance newModelQuery()
3435
* @method static \Illuminate\Database\Eloquent\Builder<static>|Instance newQuery()
@@ -84,6 +85,7 @@ class Instance extends Model
8485
'admin_notes',
8586
'allow_video_posts',
8687
'allow_videos_in_fyf',
88+
'stats_last_collected_at',
8789
];
8890

8991
protected $casts = [
@@ -95,6 +97,7 @@ class Instance extends Model
9597
'last_failure_at' => 'datetime',
9698
'failure_count' => 'integer',
9799
'federation_state' => 'integer',
100+
'stats_last_collected_at' => 'datetime',
98101
];
99102

100103
/* Federation State
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('instances', function (Blueprint $table) {
15+
$table->timestamp('stats_last_collected_at')->nullable()->index();
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('instances', function (Blueprint $table) {
25+
$table->dropColumn('stats_last_collected_at');
26+
});
27+
}
28+
};

routes/console.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Schedule::command('passport:purge --revoked')->daily()->onOneServer();
1414
Schedule::command('version:check --force')->twiceDaily(9, 17)->withoutOverlapping()->runInBackground()->onOneServer();
1515
Schedule::command('videos:retry-failed')->everyThirtyMinutes()->onOneServer();
16+
Schedule::command('app:instance-stats-collector-command')->hourlyAt(20)->onOneServer();
1617

1718
if (config('loops.admin_dashboard.autoUpdate')) {
1819
Schedule::command('admin:refresh-dashboard-30d')->everyThirtyMinutes()->onOneServer();

0 commit comments

Comments
 (0)