Update AdminApiController, add instance stats endpoint

This commit is contained in:
Daniel Supernault 2023-06-20 00:34:02 -06:00
parent ba58aaba36
commit 89c3710d3c
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 334 additions and 274 deletions

View file

@ -23,6 +23,7 @@ use App\Services\AdminStatsService;
use App\Services\ConfigCacheService; use App\Services\ConfigCacheService;
use App\Services\InstanceService; use App\Services\InstanceService;
use App\Services\ModLogService; use App\Services\ModLogService;
use App\Services\SnowflakeService;
use App\Services\StatusService; use App\Services\StatusService;
use App\Services\NetworkTimelineService; use App\Services\NetworkTimelineService;
use App\Services\NotificationService; use App\Services\NotificationService;
@ -599,4 +600,62 @@ class AdminApiController extends Controller
return new AdminInstance($instance); return new AdminInstance($instance);
} }
public function getAllStats(Request $request)
{
abort_if(!$request->user(), 404);
abort_unless($request->user()->is_admin === 1, 404);
if($request->has('refresh')) {
Cache::forget('admin-api:instance-all-stats_v1');
}
return Cache::remember('admin-api:instance-all-stats-v1', 1209600, function() {
$days = range(0, 6);
$res = [
'cached_at' => now()->format('c'),
];
$minStatusId = SnowflakeService::byDate(now()->subDays(8));
foreach($days as $day) {
$label = now()->subDays($day)->format('D');
$labelShort = substr($label, 0, 1);
$res['users']['days'][] = [
'date' => now()->subDays($day)->format('M j Y'),
'label_full' => $label,
'label' => $labelShort,
'count' => User::whereDate('created_at', now()->subDays($day))->count()
];
$res['posts']['days'][] = [
'date' => now()->subDays($day)->format('M j Y'),
'label_full' => $label,
'label' => $labelShort,
'count' => Status::where('id', '>', $minStatusId)->whereNull('uri')->whereDate('created_at', now()->subDays($day))->count()
];
$res['instances']['days'][] = [
'date' => now()->subDays($day)->format('M j Y'),
'label_full' => $label,
'label' => $labelShort,
'count' => Instance::whereDate('created_at', now()->subDays($day))->count()
];
}
$res['users']['total'] = DB::table('users')->count();
$res['users']['min'] = collect($res['users']['days'])->min('count');
$res['users']['max'] = $res['users']['total'];
$res['users']['change'] = $res['users']['total'] - $res['users']['min'];
$res['posts']['total'] = DB::table('statuses')->whereNull('uri')->count();
$res['posts']['min'] = collect($res['posts']['days'])->min('count');
$res['posts']['max'] = collect($res['posts']['days'])->max('count');
$res['posts']['change'] = $res['posts']['total'] - $res['posts']['min'];
$res['instances']['total'] = DB::table('instances')->count();
$res['instances']['min'] = collect($res['instances']['days'])->min('count');
$res['instances']['max'] = collect($res['instances']['days'])->max('count');
$res['instances']['change'] = $res['instances']['total'] - $res['instances']['min'];
return $res;
});
}
} }

View file

@ -200,21 +200,21 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
}); });
Route::group(['prefix' => 'live'], function() use($middleware) { Route::group(['prefix' => 'live'], function() use($middleware) {
Route::post('create_stream', 'LiveStreamController@createStream')->middleware($middleware); // Route::post('create_stream', 'LiveStreamController@createStream')->middleware($middleware);
Route::post('stream/edit', 'LiveStreamController@editStream')->middleware($middleware); // Route::post('stream/edit', 'LiveStreamController@editStream')->middleware($middleware);
Route::get('active/list', 'LiveStreamController@getActiveStreams')->middleware($middleware); // Route::get('active/list', 'LiveStreamController@getActiveStreams')->middleware($middleware);
Route::get('accounts/stream', 'LiveStreamController@getUserStream')->middleware($middleware); // Route::get('accounts/stream', 'LiveStreamController@getUserStream')->middleware($middleware);
Route::get('accounts/stream/guest', 'LiveStreamController@getUserStreamAsGuest'); // Route::get('accounts/stream/guest', 'LiveStreamController@getUserStreamAsGuest');
Route::delete('accounts/stream', 'LiveStreamController@deleteStream')->middleware($middleware); // Route::delete('accounts/stream', 'LiveStreamController@deleteStream')->middleware($middleware);
Route::get('chat/latest', 'LiveStreamController@getLatestChat')->middleware($middleware); // Route::get('chat/latest', 'LiveStreamController@getLatestChat')->middleware($middleware);
Route::post('chat/message', 'LiveStreamController@addChatComment')->middleware($middleware); // Route::post('chat/message', 'LiveStreamController@addChatComment')->middleware($middleware);
Route::post('chat/delete', 'LiveStreamController@deleteChatComment')->middleware($middleware); // Route::post('chat/delete', 'LiveStreamController@deleteChatComment')->middleware($middleware);
Route::post('chat/ban-user', 'LiveStreamController@banChatUser')->middleware($middleware); // Route::post('chat/ban-user', 'LiveStreamController@banChatUser')->middleware($middleware);
Route::post('chat/pin', 'LiveStreamController@pinChatComment')->middleware($middleware); // Route::post('chat/pin', 'LiveStreamController@pinChatComment')->middleware($middleware);
Route::post('chat/unpin', 'LiveStreamController@unpinChatComment')->middleware($middleware); // Route::post('chat/unpin', 'LiveStreamController@unpinChatComment')->middleware($middleware);
Route::get('config', 'LiveStreamController@getConfig'); // Route::get('config', 'LiveStreamController@getConfig');
Route::post('broadcast/publish', 'LiveStreamController@clientBroadcastPublish'); // Route::post('broadcast/publish', 'LiveStreamController@clientBroadcastPublish')->middleware($middleware);
Route::post('broadcast/finish', 'LiveStreamController@clientBroadcastFinish'); // Route::post('broadcast/finish', 'LiveStreamController@clientBroadcastFinish')->middleware($middleware);
}); });
Route::group(['prefix' => 'admin'], function() use($middleware) { Route::group(['prefix' => 'admin'], function() use($middleware) {
@ -235,6 +235,7 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
Route::get('instances/get', 'Api\AdminApiController@getInstance')->middleware($middleware); Route::get('instances/get', 'Api\AdminApiController@getInstance')->middleware($middleware);
Route::post('instances/moderate', 'Api\AdminApiController@moderateInstance')->middleware($middleware); Route::post('instances/moderate', 'Api\AdminApiController@moderateInstance')->middleware($middleware);
Route::post('instances/refresh-stats', 'Api\AdminApiController@refreshInstanceStats')->middleware($middleware); Route::post('instances/refresh-stats', 'Api\AdminApiController@refreshInstanceStats')->middleware($middleware);
Route::get('instance/stats', 'Api\AdminApiController@getAllStats')->middleware($middleware);
}); });
Route::group(['prefix' => 'landing/v1'], function() use($middleware) { Route::group(['prefix' => 'landing/v1'], function() use($middleware) {