Update cache

This commit is contained in:
Daniel Supernault 2024-06-12 02:58:25 -06:00
parent 17b449d5ed
commit 6e0d1ef41b
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1
5 changed files with 133 additions and 129 deletions

View file

@ -1649,7 +1649,7 @@ class ApiV1Controller extends Controller
$stats = Cache::remember('api:v1:instance-data:stats', 43200, function () { $stats = Cache::remember('api:v1:instance-data:stats', 43200, function () {
return [ return [
'user_count' => User::count(), 'user_count' => User::count(),
'status_count' => Status::whereNull('uri')->count(), 'status_count' => StatusService::totalLocalStatuses(),
'domain_count' => Instance::count(), 'domain_count' => Instance::count(),
]; ];
}); });

View file

@ -4,8 +4,9 @@ namespace App\Http\Controllers\Api;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\{Profile, Status, User}; use App\{Profile, Instance, Status, User};
use Cache; use Cache;
use App\Services\StatusService;
class InstanceApiController extends Controller { class InstanceApiController extends Controller {
@ -40,11 +41,8 @@ class InstanceApiController extends Controller {
'urls' => [], 'urls' => [],
'stats' => [ 'stats' => [
'user_count' => User::count(), 'user_count' => User::count(),
'status_count' => Status::whereNull('uri')->count(), 'status_count' => StatusService::totalLocalStatuses(),
'domain_count' => Profile::whereNotNull('domain') 'domain_count' => Instance::count()
->groupBy('domain')
->pluck('domain')
->count()
], ],
'thumbnail' => '', 'thumbnail' => '',
'languages' => [], 'languages' => [],

View file

@ -5,7 +5,7 @@ namespace App\Http\Controllers;
use App\Page; use App\Page;
use App\Profile; use App\Profile;
use App\Services\FollowerService; use App\Services\FollowerService;
use App\Status; use App\Services\StatusService;
use App\User; use App\User;
use App\Util\ActivityPub\Helpers; use App\Util\ActivityPub\Helpers;
use App\Util\Localization\Localization; use App\Util\Localization\Localization;
@ -60,7 +60,7 @@ class SiteController extends Controller
{ {
return Cache::remember('site.about_v2', now()->addMinutes(15), function () { return Cache::remember('site.about_v2', now()->addMinutes(15), function () {
$user_count = number_format(User::count()); $user_count = number_format(User::count());
$post_count = number_format(Status::count()); $post_count = number_format(StatusService::totalLocalStatuses());
$rules = config_cache('app.rules') ? json_decode(config_cache('app.rules'), true) : null; $rules = config_cache('app.rules') ? json_decode(config_cache('app.rules'), true) : null;
return view('site.about', compact('rules', 'user_count', 'post_count'))->render(); return view('site.about', compact('rules', 'user_count', 'post_count'))->render();

View file

@ -2,24 +2,20 @@
namespace App\Services; namespace App\Services;
use Cache; use App\Avatar;
use DB; use App\Contact;
use App\FailedJob;
use App\Instance;
use App\Media;
use App\Profile;
use App\Report;
use App\Status;
use App\User;
use App\Util\Lexer\PrettyNumber; use App\Util\Lexer\PrettyNumber;
use App\{ use Cache;
Avatar, use DateInterval;
Contact, use DatePeriod;
FailedJob, use DB;
Hashtag,
Instance,
Media,
Like,
Profile,
Report,
Status,
User
};
use \DateInterval;
use \DatePeriod;
class AdminStatsService class AdminStatsService
{ {
@ -42,7 +38,7 @@ class AdminStatsService
public static function storage() public static function storage()
{ {
return Cache::remember('admin:dashboard:storage:stats', 120000, function() { return Cache::remember('admin:dashboard:storage:stats', 120000, function () {
$res = []; $res = [];
$res['last_updated'] = str_replace('+00:00', 'Z', now()->format(DATE_RFC3339_EXTENDED)); $res['last_updated'] = str_replace('+00:00', 'Z', now()->format(DATE_RFC3339_EXTENDED));
@ -53,7 +49,7 @@ class AdminStatsService
'count' => $avatars, 'count' => $avatars,
'local_count' => $avatarsLocal, 'local_count' => $avatarsLocal,
'cloud_count' => ($avatars - $avatarsLocal), 'cloud_count' => ($avatars - $avatarsLocal),
'total_sum' => Avatar::sum('size') 'total_sum' => Avatar::sum('size'),
]; ];
$media = Media::count(); $media = Media::count();
@ -81,7 +77,8 @@ class AdminStatsService
{ {
$day = config('database.default') == 'pgsql' ? 'DATE_PART(\'day\',' : 'day('; $day = config('database.default') == 'pgsql' ? 'DATE_PART(\'day\',' : 'day(';
$ttl = now()->addMinutes(15); $ttl = now()->addMinutes(15);
return Cache::remember('admin:dashboard:home:data:v0:15min', $ttl, function() use ($day) {
return Cache::remember('admin:dashboard:home:data:v0:15min', $ttl, function () {
return [ return [
'contact' => PrettyNumber::convert(Contact::whereNull('read_at')->count()), 'contact' => PrettyNumber::convert(Contact::whereNull('read_at')->count()),
'contact_monthly' => PrettyNumber::convert(Contact::whereNull('read_at')->where('created_at', '>', now()->subMonth())->count()), 'contact_monthly' => PrettyNumber::convert(Contact::whereNull('read_at')->where('created_at', '>', now()->subMonth())->count()),
@ -95,10 +92,11 @@ class AdminStatsService
{ {
$day = config('database.default') == 'pgsql' ? 'DATE_PART(\'day\',' : 'day('; $day = config('database.default') == 'pgsql' ? 'DATE_PART(\'day\',' : 'day(';
$ttl = now()->addHours(24); $ttl = now()->addHours(24);
return Cache::remember('admin:dashboard:home:data:v0:24hr', $ttl, function() use ($day) {
return Cache::remember('admin:dashboard:home:data:v0:24hr', $ttl, function () {
return [ return [
'failedjobs' => PrettyNumber::convert(FailedJob::where('failed_at', '>=', \Carbon\Carbon::now()->subDay())->count()), 'failedjobs' => PrettyNumber::convert(FailedJob::where('failed_at', '>=', \Carbon\Carbon::now()->subDay())->count()),
'statuses' => PrettyNumber::convert(Status::count()), 'statuses' => PrettyNumber::convert(intval(StatusService::totalLocalStatuses())),
'statuses_monthly' => PrettyNumber::convert(Status::where('created_at', '>', now()->subMonth())->count()), 'statuses_monthly' => PrettyNumber::convert(Status::where('created_at', '>', now()->subMonth())->count()),
'profiles' => PrettyNumber::convert(Profile::count()), 'profiles' => PrettyNumber::convert(Profile::count()),
'users' => PrettyNumber::convert(User::count()), 'users' => PrettyNumber::convert(User::count()),
@ -113,9 +111,10 @@ class AdminStatsService
protected static function additionalDataSummary() protected static function additionalDataSummary()
{ {
$ttl = now()->addHours(24); $ttl = now()->addHours(24);
return Cache::remember('admin:dashboard:home:data-summary:v0:24hr', $ttl, function() {
return Cache::remember('admin:dashboard:home:data-summary:v0:24hr', $ttl, function () {
return [ return [
'statuses' => PrettyNumber::convert(Status::count()), 'statuses' => PrettyNumber::convert(intval(StatusService::totalLocalStatuses())),
'profiles' => PrettyNumber::convert(Profile::count()), 'profiles' => PrettyNumber::convert(Profile::count()),
'users' => PrettyNumber::convert(User::count()), 'users' => PrettyNumber::convert(User::count()),
'instances' => PrettyNumber::convert(Instance::count()), 'instances' => PrettyNumber::convert(Instance::count()),
@ -126,7 +125,8 @@ class AdminStatsService
protected static function postsGraph() protected static function postsGraph()
{ {
$ttl = now()->addHours(12); $ttl = now()->addHours(12);
return Cache::remember('admin:dashboard:home:data-postsGraph:v0.1:24hr', $ttl, function() {
return Cache::remember('admin:dashboard:home:data-postsGraph:v0.1:24hr', $ttl, function () {
$gb = config('database.default') == 'pgsql' ? ['statuses.id', 'created_at'] : DB::raw('Date(created_at)'); $gb = config('database.default') == 'pgsql' ? ['statuses.id', 'created_at'] : DB::raw('Date(created_at)');
$s = Status::selectRaw('Date(created_at) as date, count(statuses.id) as count') $s = Status::selectRaw('Date(created_at) as date, count(statuses.id) as count')
->where('created_at', '>=', now()->subWeek()) ->where('created_at', '>=', now()->subWeek())
@ -137,10 +137,10 @@ class AdminStatsService
$begin = now()->subWeek(); $begin = now()->subWeek();
$end = now(); $end = now();
$interval = new DateInterval('P1D'); $interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end); $daterange = new DatePeriod($begin, $interval, $end);
$dates = []; $dates = [];
foreach($daterange as $date){ foreach ($daterange as $date) {
$dates[$date->format("Y-m-d")] = 0; $dates[$date->format('Y-m-d')] = 0;
} }
$dates = collect($dates)->merge($s); $dates = collect($dates)->merge($s);
@ -155,10 +155,10 @@ class AdminStatsService
$begin = now()->subWeeks(2); $begin = now()->subWeeks(2);
$end = now()->subWeeks(1); $end = now()->subWeeks(1);
$interval = new DateInterval('P1D'); $interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end); $daterange = new DatePeriod($begin, $interval, $end);
$lw = []; $lw = [];
foreach($daterange as $date){ foreach ($daterange as $date) {
$lw[$date->format("Y-m-d")] = 0; $lw[$date->format('Y-m-d')] = 0;
} }
$lw = collect($lw)->merge($s); $lw = collect($lw)->merge($s);
@ -169,5 +169,4 @@ class AdminStatsService
]; ];
}); });
} }
} }

View file

@ -191,4 +191,11 @@ class StatusService
BookmarkService::get($pid, $id) : BookmarkService::get($pid, $id) :
false; false;
} }
public static function totalLocalStatuses()
{
return Cache::remember(self::CACHE_KEY . 'totalpub', 14400, function() {
return Status::whereNull('url')->count();
});
}
} }