Add optimization and Telegram Bot functionality to admin maintenance page

This commit is contained in:
Matteo Gheza 2024-01-12 00:44:01 +01:00
parent 898c59104f
commit 3a4c475488
7 changed files with 256 additions and 9 deletions

View File

@ -131,6 +131,87 @@ class AdminController extends Controller
}
}
public function runOptimization() {
if(!request()->user()->hasPermission("admin-maintenance-update")) abort(401);
$commands = [
'config:cache',
'event:cache',
'route:cache'
];
foreach($commands as $command) {
Artisan::call($command);
}
return response()->json([
'message' => 'Optimization ran successfully'
]);
}
public function clearOptimization() {
if(!request()->user()->hasPermission("admin-maintenance-update")) abort(401);
Artisan::call('optimize:clear');
return response()->json([
'message' => 'Optimization cleared successfully'
]);
}
public function clearCache() {
if(!request()->user()->hasPermission("admin-maintenance-update")) abort(401);
Artisan::call('cache:clear');
return response()->json([
'message' => 'Cache cleared successfully'
]);
}
public function getTelegramBotDebugInfo() {
if(!request()->user()->hasPermission("admin-maintenance-update")) abort(401);
Artisan::call('telegraph:debug-webhook');
$output = Artisan::output();
// Convert the text to a query string format
$queryString = str_replace([': ', "\n"], ['=', '&'], $output);
// Parse the query string
parse_str($queryString, $result);
foreach($result as $key => &$value) {
$key = trim($key);
$value = trim($value);
if($value === "no") $value = false;
if($value === "si" || $value === "" || $value === "yes") $value = true;
}
unset($value);
return response()->json($result);
}
public function setTelegramWebhook() {
if(!request()->user()->hasPermission("admin-maintenance-update")) abort(401);
Artisan::call('telegraph:set-webhook');
return response()->json([
'message' => 'Webhook set successfully'
]);
}
public function unsetTelegramWebhook() {
if(!request()->user()->hasPermission("admin-maintenance-update")) abort(401);
Artisan::call('telegraph:unset-webhook');
return response()->json([
'message' => 'Webhook unset successfully'
]);
}
public function getPermissionsAndRoles() {
if(!request()->user()->hasPermission("admin-roles-read")) abort(401);
return response()->json([

View File

@ -56,13 +56,9 @@ return [
'http_client_requests' => env('SENTRY_BREADCRUMBS_HTTP_CLIENT_REQUESTS_ENABLED', true),
],
'before_send' => function (\Sentry\Event $event, ?\Sentry\EventHint $hint): ?\Sentry\Event {
if ($hint !== null && $hint->exception instanceof JsonException) {
return null;
}
return $event;
},
'ignore_exceptions' => [
JsonException::class,
],
// Performance monitoring specific configuration
'tracing' => [

View File

@ -102,6 +102,14 @@ Route::middleware('auth:sanctum')->group( function () {
Route::get('/admin/maintenanceMode', [AdminController::class, 'getMaintenanceMode']);
Route::post('/admin/maintenanceMode', [AdminController::class, 'updateMaintenanceMode']);
Route::post('/admin/runOptimization', [AdminController::class, 'runOptimization']);
Route::post('/admin/clearOptimization', [AdminController::class, 'clearOptimization']);
Route::post('/admin/clearCache', [AdminController::class, 'clearCache']);
Route::get('/admin/telegramBot/debug', [AdminController::class, 'getTelegramBotDebugInfo']);
Route::post('/admin/telegramBot/setWebhook', [AdminController::class, 'setTelegramWebhook']);
Route::post('/admin/telegramBot/unsetWebhook', [AdminController::class, 'unsetTelegramWebhook']);
Route::get('/admin/permissionsAndRoles', [AdminController::class, 'getPermissionsAndRoles']);
Route::post('/admin/roles', [AdminController::class, 'updateRoles']);
});

View File

@ -104,8 +104,41 @@
<button type="button" class="btn btn-lg btn-danger" (click)="updateMaintenanceMode(true)" [disabled]="isMaintenanceModeActive">{{ 'enable'|translate|ftitlecase }}</button>
<button type="button" class="btn btn-lg btn-warning" (click)="updateMaintenanceMode(false)" [disabled]="!isMaintenanceModeActive">{{ 'disable'|translate|ftitlecase }}</button>
</div>
<div class="btn-group-vertical mt-2 ps-5 pe-5">
<p class="btn-group-label">{{ 'admin.optimization'|translate|ftitlecase }}</p>
<button type="button" class="btn btn-lg btn-primary" (click)="runOptimization()">{{ 'admin.run_optimization'|translate|ftitlecase }}</button>
<button type="button" class="btn btn-lg btn-danger" (click)="clearOptimization()">{{ 'admin.clear_optimization'|translate|ftitlecase }}</button>
<button type="button" class="btn btn-lg btn-warning" (click)="clearCache()">{{ 'admin.clear_cache'|translate|ftitlecase }}</button>
</div>
</div>
<hr>
<div class="row">
<h4>{{ 'admin.telegram_bot'|translate }}</h4>
<div class="table-responsive ms-3 pe-5">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>{{ 'property'|translate|ftitlecase }}</th>
<th>{{ 'value'|translate|ftitlecase }}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let info of telegramBotInfoArray">
<td>{{ info[0] }}</td>
<td>{{ info[1] }}</td>
</tr>
</tbody>
</table>
</div>
<div class="btn-group-vertical mt-2 ps-5 pe-5">
<p class="btn-group-label">{{ 'admin.telegram_webhook'|translate }}</p>
<button type="button" class="btn btn-lg btn-danger" (click)="setTelegramBotWebhook()">{{ 'admin.telegram_webhook_set'|translate|ftitlecase }}</button>
<button type="button" class="btn btn-lg btn-warning" (click)="unsetTelegramBotWebhook()">{{ 'admin.telegram_webhook_unset'|translate|ftitlecase }}</button>
</div>
</div>
</div>
</div>
</div>

View File

@ -14,6 +14,9 @@ export class AdminMaintenanceComponent implements OnInit {
public isMaintenanceModeActive = false;
public telegramBotInfo: any | undefined = undefined;
public telegramBotInfoArray: any[] = [];
constructor(
private translateService: TranslateService,
private api: ApiClientService
@ -49,9 +52,24 @@ export class AdminMaintenanceComponent implements OnInit {
});
}
getTelegramBotDebugInfo() {
this.api.get('admin/telegramBot/debug').then((res: any) => {
this.telegramBotInfo = res;
this.telegramBotInfoArray = Object.entries(this.telegramBotInfo);
console.log(this.telegramBotInfo);
}).catch((err: any) => {
Swal.fire({
icon: 'error',
title: this.translateService.instant('error_title'),
text: err.message
});
});
}
ngOnInit(): void {
this.getDB();
this.getMaintenanceMode();
this.getTelegramBotDebugInfo();
}
runMigrations() {
@ -124,4 +142,89 @@ export class AdminMaintenanceComponent implements OnInit {
});
});
}
runOptimization() {
this.api.post('admin/runOptimization').then((res: any) => {
Swal.fire({
icon: 'success',
title: this.translateService.instant('success_title'),
text: this.translateService.instant('admin.run_optimization_success')
});
this.getDB();
}).catch((err: any) => {
Swal.fire({
icon: 'error',
title: this.translateService.instant('error_title'),
text: err.error.message
});
});
}
clearOptimization() {
this.api.post('admin/clearOptimization').then((res: any) => {
Swal.fire({
icon: 'success',
title: this.translateService.instant('success_title'),
text: this.translateService.instant('admin.clear_optimization_success')
});
this.getDB();
}).catch((err: any) => {
Swal.fire({
icon: 'error',
title: this.translateService.instant('error_title'),
text: err.error.message
});
});
}
clearCache() {
this.api.post('admin/clearCache').then((res: any) => {
Swal.fire({
icon: 'success',
title: this.translateService.instant('success_title'),
text: this.translateService.instant('admin.clear_cache_success')
});
this.getDB();
}).catch((err: any) => {
Swal.fire({
icon: 'error',
title: this.translateService.instant('error_title'),
text: err.error.message
});
});
}
setTelegramBotWebhook() {
this.api.post('admin/telegramBot/setWebhook').then((res: any) => {
Swal.fire({
icon: 'success',
title: this.translateService.instant('success_title'),
text: this.translateService.instant('admin.telegram_webhook_set_success')
});
this.getTelegramBotDebugInfo();
}).catch((err: any) => {
Swal.fire({
icon: 'error',
title: this.translateService.instant('error_title'),
text: err.error.message
});
});
}
unsetTelegramBotWebhook() {
this.api.post('admin/telegramBot/unsetWebhook').then((res: any) => {
Swal.fire({
icon: 'success',
title: this.translateService.instant('success_title'),
text: this.translateService.instant('admin.telegram_webhook_unset_success')
});
this.getTelegramBotDebugInfo();
}).catch((err: any) => {
Swal.fire({
icon: 'error',
title: this.translateService.instant('error_title'),
text: err.error.message
});
});
}
}

View File

@ -33,7 +33,20 @@
"table": "table",
"rows": "rows",
"updates_and_maintenance_title": "Updates and maintenance",
"maintenance_mode_success": "Maintenance mode updated successfully"
"maintenance_mode_success": "Maintenance mode updated successfully",
"optimization": "optimization",
"run_optimization": "run optimization",
"run_optimization_success": "Optimization executed successfully",
"clear_optimization": "clear optimization",
"clear_optimization_success": "Optimization cleared successfully",
"clear_cache": "clear cache",
"clear_cache_success": "Cache cleared successfully",
"telegram_bot": "Telegram Bot",
"telegram_webhook": "Telegram Webhook",
"telegram_webhook_set": "Set Telegram Webhook",
"telegram_webhook_set_success": "Telegram Webhook set successfully",
"telegram_webhook_unset": "Unset Telegram Webhook",
"telegram_webhook_unset_success": "Telegram Webhook unset successfully"
},
"table": {
"yes_remove": "Yes, remove",

View File

@ -33,7 +33,20 @@
"table": "tabella",
"rows": "righe",
"updates_and_maintenance_title": "Aggiornamenti e manutenzione",
"maintenance_mode_success": "Modalità manutenzione aggiornata con successo"
"maintenance_mode_success": "Modalità manutenzione aggiornata con successo",
"optimization": "ottimizzazione",
"run_optimization": "esegui ottimizzazione",
"run_optimization_success": "Ottimizzazione eseguita con successo",
"clear_optimization": "rimuovi ottimizzazione",
"clear_optimization_success": "Ottimizzazione rimossa con successo",
"clear_cache": "svuota cache",
"clear_cache_success": "Cache svuotata con successo",
"telegram_bot": "Bot Telegram",
"telegram_webhook": "Webhook Telegram",
"telegram_webhook_set": "Imposta Webhook Telegram",
"telegram_webhook_set_success": "Webhook Telegram impostato con successo",
"telegram_webhook_unset": "Rimuovi Webhook Telegram",
"telegram_webhook_unset_success": "Webhook Telegram rimosso con successo"
},
"table": {
"yes_remove": "Si, rimuovi",