From 2f994d94e2d8049c4d15230693ff45e704240327 Mon Sep 17 00:00:00 2001 From: Xinecraft Date: Sat, 26 Oct 2024 15:32:16 +0530 Subject: [PATCH] truncate player punishment and more UX fixes for queue --- .../Controllers/Admin/DashboardController.php | 3 + .../Settings/DangerSettingController.php | 36 +++++- app/Jobs/TruncatePlayerIntelJob.php | 5 + app/Jobs/TruncatePlayerPunishmentJob.php | 37 ++++++ app/Jobs/TruncateServerChatlogsJob.php | 5 + app/Jobs/TruncateServerConsolelogsJob.php | 5 + app/Jobs/TruncateServerIntelJob.php | 5 + app/Jobs/TruncateShoutsJob.php | 5 + app/Providers/AppServiceProvider.php | 10 +- .../default/js/Pages/Admin/Dashboard.vue | 6 +- .../js/Pages/Admin/Setting/DangerSetting.vue | 114 ++++++++++++++++++ routes/web.php | 1 + 12 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 app/Jobs/TruncatePlayerPunishmentJob.php diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index c6dd1742f..3ccee45c5 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -12,6 +12,7 @@ use DB; use Illuminate\Http\Request; use Inertia\Inertia; +use Cache; class DashboardController extends Controller { @@ -65,6 +66,8 @@ public function index(Request $request) 'kpiPostCreatedForInterval' => $kpiPostCreatedForInterval, 'kpiTotalPostsPercent' => $kpiTotalPostsPercent, 'kpiTotalComments' => $kpiTotalComments, + + 'queueLastProcessed' => Cache::get('queue_last_processed'), ]; } diff --git a/app/Http/Controllers/Admin/Settings/DangerSettingController.php b/app/Http/Controllers/Admin/Settings/DangerSettingController.php index 59ff9ea95..a204b3615 100644 --- a/app/Http/Controllers/Admin/Settings/DangerSettingController.php +++ b/app/Http/Controllers/Admin/Settings/DangerSettingController.php @@ -2,8 +2,10 @@ namespace App\Http\Controllers\Admin\Settings; +use Illuminate\Support\Facades\Cache; use App\Http\Controllers\Controller; use App\Jobs\TruncatePlayerIntelJob; +use App\Jobs\TruncatePlayerPunishmentJob; use App\Jobs\TruncateServerChatlogsJob; use App\Jobs\TruncateServerConsolelogsJob; use App\Jobs\TruncateServerIntelJob; @@ -23,7 +25,16 @@ public function __construct() public function show(): \Inertia\Response { - return Inertia::render('Admin/Setting/DangerSetting'); + return Inertia::render('Admin/Setting/DangerSetting', [ + 'inProgressList' => [ + 'truncateShouts' => Cache::get('dangerzone::truncate_shouts'), + 'truncateConsolelogs' => Cache::get('dangerzone::truncate_consolelogs'), + 'truncateChatlogs' => Cache::get('dangerzone::truncate_chatlogs'), + 'truncatePlayerIntelData' => Cache::get('dangerzone::truncate_player_intel_data'), + 'truncateServerIntelData' => Cache::get('dangerzone::truncate_server_intel_data'), + 'truncatePlayerPunishments' => Cache::get('dangerzone::truncate_player_punishments'), + ] + ]); } public function truncateShouts(Request $request): \Illuminate\Http\RedirectResponse @@ -31,6 +42,8 @@ public function truncateShouts(Request $request): \Illuminate\Http\RedirectRespo Log::alert('TRUNCATE_SHOUTS', [ 'causer' => $request->user()->username, ]); + + Cache::put('dangerzone::truncate_shouts', now(), 3600 * 24); TruncateShoutsJob::dispatch(); return redirect()->back() @@ -42,6 +55,8 @@ public function truncateConsolelogs(Request $request): \Illuminate\Http\Redirect Log::alert('TRUNCATE_CONSOLELOGS', [ 'causer' => $request->user()->username, ]); + + Cache::put('dangerzone::truncate_consolelogs', now(), 3600 * 24); TruncateServerConsolelogsJob::dispatch(); return redirect()->back() @@ -53,6 +68,8 @@ public function truncateChatlogs(Request $request): \Illuminate\Http\RedirectRes Log::alert('TRUNCATE_CHATLOGS', [ 'causer' => $request->user()->username, ]); + + Cache::put('dangerzone::truncate_chatlogs', now(), 3600 * 24); TruncateServerChatlogsJob::dispatch(); return redirect()->back() @@ -64,6 +81,8 @@ public function truncatePlayerIntelData(Request $request): \Illuminate\Http\Redi Log::alert('TRUNCATE_PLAYER_INTEL', [ 'causer' => $request->user()->username, ]); + + Cache::put('dangerzone::truncate_player_intel_data', now(), 3600 * 24); TruncatePlayerIntelJob::dispatch(); return redirect()->back() @@ -75,9 +94,24 @@ public function truncateServerIntelData(Request $request): \Illuminate\Http\Redi Log::alert('TRUNCATE_SERVER_INTEL', [ 'causer' => $request->user()->username, ]); + + Cache::put('dangerzone::truncate_server_intel_data', now(), 3600 * 24); TruncateServerIntelJob::dispatch(); return redirect()->back() ->with(['toast' => ['type' => 'success', 'milliseconds' => 10000, 'title' => __('Queued Successfully! Server Analytics data will be deleted shortly. It may take upto 1 minute to complete.')]]); } + + public function truncatePlayerPunishments(Request $request): \Illuminate\Http\RedirectResponse + { + Log::alert('TRUNCATE_PLAYER_PUNISHMENTS', [ + 'causer' => $request->user()->username, + ]); + + Cache::put('dangerzone::truncate_player_punishments', now(), 3600 * 24); + TruncatePlayerPunishmentJob::dispatch(); + + return redirect()->back() + ->with(['toast' => ['type' => 'success', 'milliseconds' => 10000, 'title' => __('Queued Successfully! Player Punishments data will be deleted shortly. It may take upto few minute to complete.')]]); + } } diff --git a/app/Jobs/TruncatePlayerIntelJob.php b/app/Jobs/TruncatePlayerIntelJob.php index fc58d32b3..dcc0306ba 100644 --- a/app/Jobs/TruncatePlayerIntelJob.php +++ b/app/Jobs/TruncatePlayerIntelJob.php @@ -16,6 +16,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Cache; class TruncatePlayerIntelJob implements ShouldQueue { @@ -34,6 +35,8 @@ public function __construct() */ public function handle(): void { + Cache::put('dangerzone::truncate_player_intel_data', now(), 3600 * 24); + DB::transaction(function () { // Delete player intel. MinecraftPlayerDeath::query()->delete(); @@ -45,5 +48,7 @@ public function handle(): void MinecraftPlayer::query()->delete(); Player::query()->delete(); }, 3); + + Cache::forget('dangerzone::truncate_player_intel_data'); } } diff --git a/app/Jobs/TruncatePlayerPunishmentJob.php b/app/Jobs/TruncatePlayerPunishmentJob.php new file mode 100644 index 000000000..c60de5259 --- /dev/null +++ b/app/Jobs/TruncatePlayerPunishmentJob.php @@ -0,0 +1,37 @@ +delete(); + + Cache::forget('dangerzone::truncate_player_punishments'); + } +} diff --git a/app/Jobs/TruncateServerChatlogsJob.php b/app/Jobs/TruncateServerChatlogsJob.php index 2a3aefded..145889a35 100644 --- a/app/Jobs/TruncateServerChatlogsJob.php +++ b/app/Jobs/TruncateServerChatlogsJob.php @@ -8,6 +8,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Cache; class TruncateServerChatlogsJob implements ShouldQueue { @@ -26,6 +27,10 @@ public function __construct() */ public function handle(): void { + Cache::put('dangerzone::truncate_chatlogs', now(), 3600 * 24); + ServerChatlog::truncate(); + + Cache::forget('dangerzone::truncate_chatlogs'); } } diff --git a/app/Jobs/TruncateServerConsolelogsJob.php b/app/Jobs/TruncateServerConsolelogsJob.php index 8a03a55bb..eb43436da 100644 --- a/app/Jobs/TruncateServerConsolelogsJob.php +++ b/app/Jobs/TruncateServerConsolelogsJob.php @@ -8,6 +8,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Cache; class TruncateServerConsolelogsJob implements ShouldQueue { @@ -26,6 +27,10 @@ public function __construct() */ public function handle(): void { + Cache::put('dangerzone::truncate_consolelogs', now(), 3600 * 24); + ServerConsolelog::truncate(); + + Cache::forget('dangerzone::truncate_consolelogs'); } } diff --git a/app/Jobs/TruncateServerIntelJob.php b/app/Jobs/TruncateServerIntelJob.php index fff0210e8..9a69f7593 100644 --- a/app/Jobs/TruncateServerIntelJob.php +++ b/app/Jobs/TruncateServerIntelJob.php @@ -11,6 +11,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Cache; class TruncateServerIntelJob implements ShouldQueue { @@ -29,11 +30,15 @@ public function __construct() */ public function handle(): void { + Cache::put('dangerzone::truncate_server_intel_data', now(), 3600 * 24); + DB::transaction(function () { // Delete server intel. MinecraftWorldLiveInfo::query()->delete(); MinecraftServerWorld::query()->delete(); MinecraftServerLiveInfo::query()->delete(); }, 3); + + Cache::forget('dangerzone::truncate_server_intel_data'); } } diff --git a/app/Jobs/TruncateShoutsJob.php b/app/Jobs/TruncateShoutsJob.php index 4f57c5e87..97618279c 100644 --- a/app/Jobs/TruncateShoutsJob.php +++ b/app/Jobs/TruncateShoutsJob.php @@ -8,6 +8,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Cache; class TruncateShoutsJob implements ShouldQueue { @@ -26,6 +27,10 @@ public function __construct() */ public function handle(): void { + Cache::put('dangerzone::truncate_shouts', now(), 3600 * 24); + Shout::truncate(); + + Cache::forget('dangerzone::truncate_shouts'); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 3a6fed5b4..6682d14be 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,7 +3,10 @@ namespace App\Providers; use App\Models\User; +use Illuminate\Queue\Events\JobProcessed; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Queue; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\ServiceProvider; use Laravel\Pulse\Facades\Pulse; @@ -28,7 +31,7 @@ public function boot() { // Pulse::users(function ($ids) { - return User::findMany($ids)->map(fn ($user) => [ + return User::findMany($ids)->map(fn($user) => [ 'id' => $user->id, 'name' => $user->name, 'extra' => $user->email, @@ -46,5 +49,10 @@ public function boot() return $value; }); }); + + // Queue Status + Queue::after(function (JobProcessed $event) { + Cache::set('queue_last_processed', now()); + }); } } diff --git a/resources/default/js/Pages/Admin/Dashboard.vue b/resources/default/js/Pages/Admin/Dashboard.vue index 9d0c22c74..bfe8d243a 100644 --- a/resources/default/js/Pages/Admin/Dashboard.vue +++ b/resources/default/js/Pages/Admin/Dashboard.vue @@ -15,10 +15,12 @@ import PlayersPerServerMetricBox from '@/Shared/PlayersPerServerMetricBox.vue'; import PlayersPerCountryMetricBox from '@/Shared/PlayersPerCountryMetricBox.vue'; import NetworkTrendsMetricBox from '@/Shared/NetworkTrendsMetricBox.vue'; import { useAuthorizable } from '@/Composables/useAuthorizable'; +import { useHelpers } from '@/Composables/useHelpers'; import PlayersJoinAddressMetricBox from '@/Shared/PlayersJoinAddressMetricBox.vue'; import PlayersMinecraftVersionMetricBox from '@/Shared/PlayersMinecraftVersionMetricBox.vue'; const {can} = useAuthorizable(); +const {formatTimeAgoToNow} = useHelpers(); defineProps({ kpiTotalUsers: Number, @@ -39,6 +41,8 @@ defineProps({ kpiPostCreatedForInterval: Number, kpiTotalPostsPercent: Number, kpiTotalComments: Number, + + queueLastProcessed: String, }); @@ -122,7 +126,7 @@ defineProps({ change-desc="in last 7 days" :icon="FireIcon" icon-class="text-red-500 bg-red-100 dark:bg-red-500 dark:text-white" - description="Jobs failed to run." + :description="`Last Run: ${queueLastProcessed ? formatTimeAgoToNow(queueLastProcessed) : __('not yet')}`" /> diff --git a/resources/default/js/Pages/Admin/Setting/DangerSetting.vue b/resources/default/js/Pages/Admin/Setting/DangerSetting.vue index e2bf03c28..b7bf0f8b0 100644 --- a/resources/default/js/Pages/Admin/Setting/DangerSetting.vue +++ b/resources/default/js/Pages/Admin/Setting/DangerSetting.vue @@ -1,8 +1,15 @@ @@ -33,6 +40,20 @@ defineProps({

{{ __("Permanently delete all shouts from shoutbox.") }}

+ +
+

+ {{ + __("Action already in progress. Please wait before starting again.") + }} +

+

+ {{ __("Last Run: :at", { at: formatTimeAgoToNow(inProgressList?.truncateShouts) }) }} +

+
{{ __("Permanently delete all console logs of all servers.") }}

+ +
+

+ {{ + __("Action already in progress. Please wait before starting again.") + }} +

+

+ {{ __("Last Run: :at", { at: formatTimeAgoToNow(inProgressList?.truncateConsolelogs) }) }} +

+
{{ __("Permanently delete all chat history for all servers.") }}

+ +
+

+ {{ + __("Action already in progress. Please wait before starting again.") + }} +

+

+ {{ __("Last Run: :at", { at: formatTimeAgoToNow(inProgressList?.truncateChatlogs) }) }} +

+
{{ __("Permanently delete all player related stats for all servers. It will also unlink all players linked to user and then delete all player data.") }}

+ +
+

+ {{ + __("Action already in progress. Please wait before starting again.") + }} +

+

+ {{ __("Last Run: :at", { at: formatTimeAgoToNow(inProgressList?.truncatePlayerIntelData) }) }} +

+
{{ __("Permanently delete all the tracked Server Intel (Analytics) data for all servers. Eg: performance, activities data etc.") }}

+ +
+

+ {{ + __("Action already in progress. Please wait before starting again.") + }} +

+

+ {{ __("Last Run: :at", { at: formatTimeAgoToNow(inProgressList?.truncateServerIntelData) }) }} +

+
+ +
+
+

+ {{ __("Delete Player Punishments (BanWarden)") }} +

+

+ {{ __("Permanently delete all player punishments related data.") }} +

+ +
+

+ {{ + __("Action already in progress. Please wait before starting again.") + }} +

+

+ {{ __("Last Run: :at", { at: formatTimeAgoToNow(inProgressList?.truncatePlayerPunishments) }) }} +

+
+
+
+ + {{ __("Delete Player Punishments") }} + +
+
diff --git a/routes/web.php b/routes/web.php index 1a09375ee..14a3bf3dc 100644 --- a/routes/web.php +++ b/routes/web.php @@ -248,6 +248,7 @@ Route::delete('setting/danger/truncate-chatlogs', [\App\Http\Controllers\Admin\Settings\DangerSettingController::class, 'truncateChatlogs'])->name('setting.danger.truncate.chatlogs'); Route::delete('setting/danger/truncate-serverintel', [\App\Http\Controllers\Admin\Settings\DangerSettingController::class, 'truncatePlayerIntelData'])->name('setting.danger.truncate.playerintel'); Route::delete('setting/danger/truncate-playerintel', [\App\Http\Controllers\Admin\Settings\DangerSettingController::class, 'truncateServerIntelData'])->name('setting.danger.truncate.serverintel'); + Route::delete('setting/danger/truncate-playerpunishments', [\App\Http\Controllers\Admin\Settings\DangerSettingController::class, 'truncatePlayerPunishments'])->name('setting.danger.truncate.playerpunishments'); Route::get('poll', [\App\Http\Controllers\Admin\PollController::class, 'index'])->name('poll.index'); Route::get('poll/create', [\App\Http\Controllers\Admin\PollController::class, 'create'])->name('poll.create');