Skip to content

Commit

Permalink
Update AP helpers, more efficently update post counts
Browse files Browse the repository at this point in the history
  • Loading branch information
dansup committed Feb 4, 2024
1 parent d3ff89e commit 7caed38
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
57 changes: 57 additions & 0 deletions app/Console/Commands/AccountPostCountStatUpdate.php
@@ -0,0 +1,57 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Services\AccountService;
use App\Services\Account\AccountStatService;
use App\Status;
use App\Profile;

class AccountPostCountStatUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:account-post-count-stat-update';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update post counts from recent activities';

/**
* Execute the console command.
*/
public function handle()
{
$ids = AccountStatService::getAllPostCountIncr();
if(!$ids || !count($ids)) {
return;
}
foreach($ids as $id) {
$acct = AccountService::get($id, true);
if(!$acct) {
AccountStatService::removeFromPostCount($id);
continue;
}
$statusCount = Status::whereProfileId($id)->count();
if($statusCount != $acct['statuses_count']) {
$profile = Profile::find($id);
if(!$profile) {
AccountStatService::removeFromPostCount($id);
continue;
}
$profile->status_count = $statusCount;
$profile->save();
AccountService::del($id);
}
AccountStatService::removeFromPostCount($id);
}
return;
}
}
3 changes: 2 additions & 1 deletion app/Console/Kernel.php
Expand Up @@ -38,7 +38,7 @@ protected function schedule(Schedule $schedule)
}

if(config('import.instagram.enabled')) {
$schedule->command('app:transform-imports')->everyFourMinutes();
$schedule->command('app:transform-imports')->everyTenMinutes();
$schedule->command('app:import-upload-garbage-collection')->hourlyAt(51);
$schedule->command('app:import-remove-deleted-accounts')->hourlyAt(37);
$schedule->command('app:import-upload-clean-storage')->twiceDailyAt(1, 13, 32);
Expand All @@ -49,6 +49,7 @@ protected function schedule(Schedule $schedule)
}
$schedule->command('app:notification-epoch-update')->weeklyOn(1, '2:21');
$schedule->command('app:hashtag-cached-count-update')->hourlyAt(25);
$schedule->command('app:account-post-count-stat-update')->everySixHours(25);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions app/Services/Account/AccountStatService.php
@@ -0,0 +1,26 @@
<?php

namespace App\Services\Account;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class AccountStatService
{
const REFRESH_CACHE_KEY = 'pf:services:accountstats:refresh:daily';

public static function incrementPostCount($pid)
{
return Redis::zadd(self::REFRESH_CACHE_KEY, $pid, $pid);
}

public static function removeFromPostCount($pid)
{
return Redis::zrem(self::REFRESH_CACHE_KEY, $pid);
}

public static function getAllPostCountIncr($limit = -1)
{
return Redis::zrange(self::REFRESH_CACHE_KEY, 0, $limit);
}
}
3 changes: 2 additions & 1 deletion app/Util/ActivityPub/Helpers.php
Expand Up @@ -43,6 +43,7 @@
use App\Jobs\ProfilePipeline\DecrementPostCount;
use App\Services\DomainService;
use App\Services\UserFilterService;
use App\Services\Account\AccountStatService;

class Helpers {

Expand Down Expand Up @@ -536,7 +537,7 @@ public static function storeStatus($url, $profile, $activity)
}
}

IncrementPostCount::dispatch($pid)->onQueue('low');
AccountStatService::incrementPostCount($pid);

if( $status->in_reply_to_id === null &&
in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
Expand Down

0 comments on commit 7caed38

Please sign in to comment.