mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-18 04:21:27 +00:00
commit
ceb375d91d
5 changed files with 133 additions and 129 deletions
|
@ -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(),
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
|
@ -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' => [],
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -2,47 +2,43 @@
|
||||||
|
|
||||||
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
|
||||||
{
|
{
|
||||||
public static function get()
|
public static function get()
|
||||||
{
|
|
||||||
return array_merge(
|
|
||||||
self::recentData(),
|
|
||||||
self::additionalData(),
|
|
||||||
self::postsGraph()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function summary()
|
|
||||||
{
|
|
||||||
return array_merge(
|
|
||||||
self::recentData(),
|
|
||||||
self::additionalDataSummary(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function storage()
|
|
||||||
{
|
{
|
||||||
return Cache::remember('admin:dashboard:storage:stats', 120000, function() {
|
return array_merge(
|
||||||
|
self::recentData(),
|
||||||
|
self::additionalData(),
|
||||||
|
self::postsGraph()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function summary()
|
||||||
|
{
|
||||||
|
return array_merge(
|
||||||
|
self::recentData(),
|
||||||
|
self::additionalDataSummary(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function storage()
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
@ -77,97 +73,100 @@ class AdminStatsService
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function recentData()
|
protected static function recentData()
|
||||||
{
|
{
|
||||||
$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 [
|
|
||||||
'contact' => PrettyNumber::convert(Contact::whereNull('read_at')->count()),
|
|
||||||
'contact_monthly' => PrettyNumber::convert(Contact::whereNull('read_at')->where('created_at', '>', now()->subMonth())->count()),
|
|
||||||
'reports' => PrettyNumber::convert(Report::whereNull('admin_seen')->count()),
|
|
||||||
'reports_monthly' => PrettyNumber::convert(Report::whereNull('admin_seen')->where('created_at', '>', now()->subMonth())->count()),
|
|
||||||
];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static function additionalData()
|
return Cache::remember('admin:dashboard:home:data:v0:15min', $ttl, function () {
|
||||||
{
|
return [
|
||||||
$day = config('database.default') == 'pgsql' ? 'DATE_PART(\'day\',' : 'day(';
|
'contact' => PrettyNumber::convert(Contact::whereNull('read_at')->count()),
|
||||||
$ttl = now()->addHours(24);
|
'contact_monthly' => PrettyNumber::convert(Contact::whereNull('read_at')->where('created_at', '>', now()->subMonth())->count()),
|
||||||
return Cache::remember('admin:dashboard:home:data:v0:24hr', $ttl, function() use ($day) {
|
'reports' => PrettyNumber::convert(Report::whereNull('admin_seen')->count()),
|
||||||
return [
|
'reports_monthly' => PrettyNumber::convert(Report::whereNull('admin_seen')->where('created_at', '>', now()->subMonth())->count()),
|
||||||
'failedjobs' => PrettyNumber::convert(FailedJob::where('failed_at', '>=', \Carbon\Carbon::now()->subDay())->count()),
|
];
|
||||||
'statuses' => PrettyNumber::convert(Status::count()),
|
});
|
||||||
'statuses_monthly' => PrettyNumber::convert(Status::where('created_at', '>', now()->subMonth())->count()),
|
}
|
||||||
'profiles' => PrettyNumber::convert(Profile::count()),
|
|
||||||
'users' => PrettyNumber::convert(User::count()),
|
|
||||||
'users_monthly' => PrettyNumber::convert(User::where('created_at', '>', now()->subMonth())->count()),
|
|
||||||
'instances' => PrettyNumber::convert(Instance::count()),
|
|
||||||
'media' => PrettyNumber::convert(Media::count()),
|
|
||||||
'storage' => Media::sum('size'),
|
|
||||||
];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static function additionalDataSummary()
|
protected static function additionalData()
|
||||||
{
|
{
|
||||||
$ttl = now()->addHours(24);
|
$day = config('database.default') == 'pgsql' ? 'DATE_PART(\'day\',' : 'day(';
|
||||||
return Cache::remember('admin:dashboard:home:data-summary:v0:24hr', $ttl, function() {
|
$ttl = now()->addHours(24);
|
||||||
return [
|
|
||||||
'statuses' => PrettyNumber::convert(Status::count()),
|
|
||||||
'profiles' => PrettyNumber::convert(Profile::count()),
|
|
||||||
'users' => PrettyNumber::convert(User::count()),
|
|
||||||
'instances' => PrettyNumber::convert(Instance::count()),
|
|
||||||
];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static function postsGraph()
|
return Cache::remember('admin:dashboard:home:data:v0:24hr', $ttl, function () {
|
||||||
{
|
return [
|
||||||
$ttl = now()->addHours(12);
|
'failedjobs' => PrettyNumber::convert(FailedJob::where('failed_at', '>=', \Carbon\Carbon::now()->subDay())->count()),
|
||||||
return Cache::remember('admin:dashboard:home:data-postsGraph:v0.1:24hr', $ttl, function() {
|
'statuses' => PrettyNumber::convert(intval(StatusService::totalLocalStatuses())),
|
||||||
$gb = config('database.default') == 'pgsql' ? ['statuses.id', 'created_at'] : DB::raw('Date(created_at)');
|
'statuses_monthly' => PrettyNumber::convert(Status::where('created_at', '>', now()->subMonth())->count()),
|
||||||
$s = Status::selectRaw('Date(created_at) as date, count(statuses.id) as count')
|
'profiles' => PrettyNumber::convert(Profile::count()),
|
||||||
->where('created_at', '>=', now()->subWeek())
|
'users' => PrettyNumber::convert(User::count()),
|
||||||
->groupBy($gb)
|
'users_monthly' => PrettyNumber::convert(User::where('created_at', '>', now()->subMonth())->count()),
|
||||||
->orderBy('created_at', 'DESC')
|
'instances' => PrettyNumber::convert(Instance::count()),
|
||||||
->pluck('count', 'date');
|
'media' => PrettyNumber::convert(Media::count()),
|
||||||
|
'storage' => Media::sum('size'),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
$begin = now()->subWeek();
|
protected static function additionalDataSummary()
|
||||||
$end = now();
|
{
|
||||||
$interval = new DateInterval('P1D');
|
$ttl = now()->addHours(24);
|
||||||
$daterange = new DatePeriod($begin, $interval ,$end);
|
|
||||||
$dates = [];
|
|
||||||
foreach($daterange as $date){
|
|
||||||
$dates[$date->format("Y-m-d")] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
$dates = collect($dates)->merge($s);
|
return Cache::remember('admin:dashboard:home:data-summary:v0:24hr', $ttl, function () {
|
||||||
|
return [
|
||||||
|
'statuses' => PrettyNumber::convert(intval(StatusService::totalLocalStatuses())),
|
||||||
|
'profiles' => PrettyNumber::convert(Profile::count()),
|
||||||
|
'users' => PrettyNumber::convert(User::count()),
|
||||||
|
'instances' => PrettyNumber::convert(Instance::count()),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
$s = Status::selectRaw('Date(created_at) as date, count(statuses.id) as count')
|
protected static function postsGraph()
|
||||||
->where('created_at', '>=', now()->subWeeks(2))
|
{
|
||||||
->where('created_at', '<=', now()->subWeeks(1))
|
$ttl = now()->addHours(12);
|
||||||
->groupBy($gb)
|
|
||||||
->orderBy('created_at', 'DESC')
|
|
||||||
->pluck('count', 'date');
|
|
||||||
|
|
||||||
$begin = now()->subWeeks(2);
|
return Cache::remember('admin:dashboard:home:data-postsGraph:v0.1:24hr', $ttl, function () {
|
||||||
$end = now()->subWeeks(1);
|
$gb = config('database.default') == 'pgsql' ? ['statuses.id', 'created_at'] : DB::raw('Date(created_at)');
|
||||||
$interval = new DateInterval('P1D');
|
$s = Status::selectRaw('Date(created_at) as date, count(statuses.id) as count')
|
||||||
$daterange = new DatePeriod($begin, $interval ,$end);
|
->where('created_at', '>=', now()->subWeek())
|
||||||
$lw = [];
|
->groupBy($gb)
|
||||||
foreach($daterange as $date){
|
->orderBy('created_at', 'DESC')
|
||||||
$lw[$date->format("Y-m-d")] = 0;
|
->pluck('count', 'date');
|
||||||
}
|
|
||||||
|
|
||||||
$lw = collect($lw)->merge($s);
|
$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;
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
$dates = collect($dates)->merge($s);
|
||||||
'posts_this_week' => $dates->values(),
|
|
||||||
'posts_last_week' => $lw->values(),
|
|
||||||
];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$s = Status::selectRaw('Date(created_at) as date, count(statuses.id) as count')
|
||||||
|
->where('created_at', '>=', now()->subWeeks(2))
|
||||||
|
->where('created_at', '<=', now()->subWeeks(1))
|
||||||
|
->groupBy($gb)
|
||||||
|
->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(),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue