1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-24 08:44:02 +00:00
pixelfed/app/Services/AdminStatsService.php

153 lines
4.9 KiB
PHP
Raw Normal View History

2020-01-23 05:15:11 +00:00
<?php
namespace App\Services;
use Cache;
2021-05-25 05:24:42 +00:00
use DB;
2020-01-23 05:15:11 +00:00
use App\Util\Lexer\PrettyNumber;
use App\{
Avatar,
2020-01-23 05:15:11 +00:00
Contact,
FailedJob,
Hashtag,
Instance,
Media,
Like,
Profile,
Report,
Status,
User
};
2021-05-25 05:24:42 +00:00
use \DateInterval;
use \DatePeriod;
2020-01-23 05:15:11 +00:00
class AdminStatsService
{
public static function get()
{
2021-05-25 05:24:42 +00:00
return array_merge(
self::recentData(),
self::additionalData(),
self::postsGraph()
);
2020-01-23 05:15:11 +00:00
}
public static function storage()
{
return Cache::remember('admin:dashboard:storage:stats', 120000, function() {
$res = [];
$res['last_updated'] = now()->format('c');
$avatars = Avatar::count();
$avatarsLocal = Avatar::whereNull('cdn_url')->count();
$res['avatar'] = [
'count' => $avatars,
'local_count' => $avatarsLocal,
'cloud_count' => ($avatars - $avatarsLocal),
'total_sum' => Avatar::sum('size')
];
$media = Media::count();
$mediaSum = Media::sum('size');
$mediaLocalSum = Media::whereNotNull('user_id')->sum('size');
$mediaLocal = Media::whereNotNull('user_id')->count();
$res['media'] = [
'count' => $media,
'local_count' => $mediaLocal,
'cloud_count' => ($media - $mediaLocal),
'total_sum' => $mediaSum,
'local_sum' => $mediaLocalSum,
'local_30d' => Media::whereNotNull('user_id')->where('created_at', '>', now()->subDays(30))->sum('size'),
'cloud_30d' => Media::whereNull('user_id')->where('created_at', '>', now()->subDays(30))->sum('size'),
'cloud_sum' => ($mediaSum - $mediaLocalSum),
'avg_size' => Media::avg('size'),
'avg_mime' => Media::groupBy('mime')->orderByRaw('COUNT(*) DESC')->first()->mime,
];
return $res;
});
}
2020-01-23 05:15:11 +00:00
protected static function recentData()
{
$day = config('database.default') == 'pgsql' ? 'DATE_PART(\'day\',' : 'day(';
2021-05-25 05:24:42 +00:00
$ttl = now()->addMinutes(15);
return Cache::remember('admin:dashboard:home:data:v0:15min', $ttl, function() use ($day) {
2020-01-23 05:15:11 +00:00
return [
2020-12-18 07:52:49 +00:00
'contact' => PrettyNumber::convert(Contact::whereNull('read_at')->count()),
2021-05-25 05:24:42 +00:00
'contact_monthly' => PrettyNumber::convert(Contact::whereNull('read_at')->where('created_at', '>', now()->subMonth())->count()),
2020-12-18 07:52:49 +00:00
'reports' => PrettyNumber::convert(Report::whereNull('admin_seen')->count()),
2021-05-25 05:24:42 +00:00
'reports_monthly' => PrettyNumber::convert(Report::whereNull('admin_seen')->where('created_at', '>', now()->subMonth())->count()),
2020-01-23 05:15:11 +00:00
];
});
}
protected static function additionalData()
{
$day = config('database.default') == 'pgsql' ? 'DATE_PART(\'day\',' : 'day(';
2021-05-25 05:24:42 +00:00
$ttl = now()->addHours(24);
return Cache::remember('admin:dashboard:home:data:v0:24hr', $ttl, function() use ($day) {
2020-01-23 05:15:11 +00:00
return [
2020-12-18 07:52:49 +00:00
'failedjobs' => PrettyNumber::convert(FailedJob::where('failed_at', '>=', \Carbon\Carbon::now()->subDay())->count()),
2020-12-18 09:58:00 +00:00
'statuses' => PrettyNumber::convert(Status::count()),
2021-05-25 05:24:42 +00:00
'statuses_monthly' => PrettyNumber::convert(Status::where('created_at', '>', now()->subMonth())->count()),
2020-12-18 07:52:49 +00:00
'profiles' => PrettyNumber::convert(Profile::count()),
'users' => PrettyNumber::convert(User::count()),
2021-05-25 05:24:42 +00:00
'users_monthly' => PrettyNumber::convert(User::where('created_at', '>', now()->subMonth())->count()),
2020-12-18 07:52:49 +00:00
'instances' => PrettyNumber::convert(Instance::count()),
'media' => PrettyNumber::convert(Media::count()),
'storage' => Media::sum('size'),
2020-01-23 05:15:11 +00:00
];
});
}
2021-05-25 05:24:42 +00:00
protected static function postsGraph()
{
$ttl = now()->addHours(12);
return Cache::remember('admin:dashboard:home:data-postsGraph:v0.1:24hr', $ttl, function() {
2021-09-27 17:00:48 +00:00
$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')
2021-05-25 05:24:42 +00:00
->where('created_at', '>=', now()->subWeek())
->groupBy($gb)
2021-05-25 05:24:42 +00:00
->orderBy('created_at', 'DESC')
->pluck('count', 'date');
$begin = now()->subWeek();
$end = now();
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$dates = [];
foreach($daterange as $date){
$dates[$date->format("Y-m-d")] = 0;
}
$dates = collect($dates)->merge($s);
2021-09-27 17:00:48 +00:00
$s = Status::selectRaw('Date(created_at) as date, count(statuses.id) as count')
2021-05-25 05:24:42 +00:00
->where('created_at', '>=', now()->subWeeks(2))
->where('created_at', '<=', now()->subWeeks(1))
->groupBy($gb)
2021-05-25 05:24:42 +00:00
->orderBy('created_at', 'DESC')
->pluck('count', 'date');
$begin = now()->subWeeks(2);
$end = now()->subWeeks(1);
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$lw = [];
foreach($daterange as $date){
$lw[$date->format("Y-m-d")] = 0;
}
$lw = collect($lw)->merge($s);
return [
'posts_this_week' => $dates->values(),
'posts_last_week' => $lw->values(),
];
});
}
}