diff --git a/CHANGELOG.md b/CHANGELOG.md index c23f8f2..29ca038 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ CHANGELOG - Update minimal smartbooster/core-bundle requirements to have ProcessMonitor and ApiCallMonitor services ### Added -- Sonata admin template for generic fields such as : +- Sonata abstract monitoring admin for CRON and ApiCall +- Sonata admin template for generic fields : - `list_nl2br.html.twig` - `show_json.html.twig` - `show_process_logs.html.twig` diff --git a/src/Admin/Monitoring/AbstractApiCallAdmin.php b/src/Admin/Monitoring/AbstractApiCallAdmin.php new file mode 100644 index 0000000..c263603 --- /dev/null +++ b/src/Admin/Monitoring/AbstractApiCallAdmin.php @@ -0,0 +1,161 @@ +remove('create'); + $collection->remove('edit'); + $collection->remove('delete'); + } + + protected function configureDefaultSortValues(array &$sortValues): void + { + $sortValues[DatagridInterface::SORT_ORDER] = 'DESC'; + $sortValues[DatagridInterface::SORT_BY] = 'startedAt'; + } + + protected function configureDatagridFilters(DatagridMapper $filter): void + { + $filter + ->add('origin', null, [ + 'label' => 'label.origin', + 'show_filter' => true, + ]) + ->add('status', ChoiceFilter::class, [ + 'label' => 'label.result', + 'show_filter' => true, + 'field_type' => ChoiceType::class, + 'field_options' => [ + 'choices' => $this->getStatusChoices(), + 'choice_translation_domain' => false, + ], + ]) + ->add('startedAt', DateTimeRangeFilter::class, [ + 'label' => 'label.started_at', + 'show_filter' => true, + 'field_type' => DateTimeRangePickerType::class, + 'field_options' => [ + 'field_options_start' => ['format' => 'dd/MM/yyyy HH:mm'], + 'field_options_end' => ['format' => 'dd/MM/yyyy HH:mm'] + ] + ]) + ->add('type', ChoiceFilter::class, [ + 'label' => 'label.route', + 'show_filter' => true, + 'field_type' => ChoiceType::class, + 'field_options' => [ + 'choices' => $this->getRouteChoices(), + ], + ]) + ->add('routeUrl', null, [ + 'label' => 'label.route_url', + 'show_filter' => true, + ]) + ->add('statusCode', null, [ + 'label' => 'label.status_code', + ]) + ; + } + + protected function configureListFields(ListMapper $list): void + { + $list + ->add('id', null, ['label' => 'field.label_id']) + ->add('startedAt', null, ['label' => 'label.started_at']) + ->add('origin', null, ['label' => 'label.origin']) + ->add('statusCode', null, [ + 'label' => 'label.result', + 'template' => '@SmartSonata/admin/base_field/list_api_call_status_code.html.twig', + ]) + ->add('method', null, ['label' => 'label.method']) + ->add('routeUrl', null, ['label' => 'label.route_url']) + ->add('durationAsString', null, ['label' => 'label.duration']) + ->add('summary', null, [ + 'label' => 'label.summary', + 'template' => '@SmartSonata/admin/base_field/list_nl2br.html.twig', + ]) + ; + } + + protected function configureShowFields(ShowMapper $show): void + { + /** @var ApiCallInterface $subject */ + $subject = $this->getSubject(); + $show + ->with('general', ['label' => 'fieldset.label_general', 'class' => 'col-md-4']) + ->add('id', null, ['label' => 'field.label_id']) + ->add('type', FieldDescriptionInterface::TYPE_CHOICE, [ + 'label' => 'label.route', + 'choices' => array_flip($this->getRouteChoices()), + 'choice_translation_domain' => 'admin', + ]) + ->add('startedAt', null, ['label' => 'label.started_at']) + ->add('endedAt', null, ['label' => 'label.ended_at']) + ->add('durationAsString', null, ['label' => 'label.duration']) + ->add('statusAsString', FieldDescriptionInterface::TYPE_CHOICE, [ + 'label' => 'label.status', + 'choices' => array_flip($this->getStatusChoices()), + ]) + ->add('summary', null, ['label' => 'label.summary']) + ->end() + ->with('api_params', ['label' => 'label.api_params', 'class' => 'col-md-8']) + ->add('origin', null, ['label' => 'label.origin']) + ->add('statusCode', null, [ + 'label' => 'label.result', + 'template' => '@SmartSonata/admin/base_field/show_api_call_status_code.html.twig', + ]) + ->add('method', null, ['label' => 'label.method']) + ->add('routeUrl', null, ['label' => 'label.route_url']) + ->add('inputData', null, [ + 'label' => 'label.input_data', + 'template' => '@SmartSonata/admin/base_field/show_json.html.twig', + ]) + ->add( + 'outputResponse', + is_array($subject->getOutputResponse()) ? FieldDescriptionInterface::TYPE_ARRAY : FieldDescriptionInterface::TYPE_STRING, + [ + 'label' => 'label.output_response', + 'template' => '@SmartSonata/admin/base_field/show_json.html.twig', + ], + ) + ->end() + ->with('process_data', ['label' => 'label.process_data', 'class' => 'pull-right col-md-8']) + ->add('logs', null, [ + 'label' => 'label.logs', + 'template' => '@SmartSonata/admin/base_field/show_process_logs.html.twig', + ]) + ->add('data', null, [ + 'label' => 'label.process_json_data', + 'template' => '@SmartSonata/admin/base_field/show_json.html.twig', + ]) + ->end() + ; + } + + private function getStatusChoices(): array + { + return ProcessStatusEnum::casesByTrans($this->getTranslator(), true, 'messages'); + } + + abstract protected function getRouteChoices(): array; +} diff --git a/src/Admin/Monitoring/AbstractCronAdmin.php b/src/Admin/Monitoring/AbstractCronAdmin.php new file mode 100644 index 0000000..6b71b81 --- /dev/null +++ b/src/Admin/Monitoring/AbstractCronAdmin.php @@ -0,0 +1,138 @@ +remove('create'); + $collection->remove('edit'); + $collection->remove('delete'); + } + + protected function configureDefaultSortValues(array &$sortValues): void + { + $sortValues[DatagridInterface::SORT_ORDER] = 'DESC'; + $sortValues[DatagridInterface::SORT_BY] = 'startedAt'; + } + + protected function configureDatagridFilters(DatagridMapper $filter): void + { + $filter + ->add('type', ChoiceFilter::class, [ + 'label' => 'label.type', + 'show_filter' => true, + 'field_type' => ChoiceType::class, + 'field_options' => [ + 'choices' => $this->commandPoolHelper->getCronChoices(), + 'choice_translation_domain' => 'admin', + ], + ]) + ->add('status', ChoiceFilter::class, [ + 'label' => 'label.status', + 'show_filter' => true, + 'field_type' => ChoiceType::class, + 'field_options' => [ + 'choices' => $this->getStatusChoices(), + 'choice_translation_domain' => false, + ], + ]) + ->add('startedAt', DateTimeRangeFilter::class, [ + 'label' => 'label.started_at', + 'show_filter' => true, + 'field_type' => DateTimeRangePickerType::class, + 'field_options' => [ + 'field_options_start' => ['format' => 'dd/MM/yyyy HH:mm'], + 'field_options_end' => ['format' => 'dd/MM/yyyy HH:mm'] + ] + ]) + ; + } + + protected function configureListFields(ListMapper $list): void + { + $list + ->add('id', null, ['label' => 'field.label_id']) + ->addIdentifier('type', FieldDescriptionInterface::TYPE_CHOICE, [ + 'label' => 'label.type', + 'choices' => array_flip($this->commandPoolHelper->getCronChoices()), + 'choice_translation_domain' => 'admin', + 'sortable' => false, + ]) + ->add('startedAt', null, ['label' => 'label.started_at']) + ->add('durationAsString', null, ['label' => 'label.duration']) + ->add('status', null, [ + 'label' => 'label.status', + 'template' => '@SmartSonata/admin/base_field/list_process_status.html.twig', + ]) + ->add('summary', null, [ + 'label' => 'label.summary', + 'sortable' => false, + ]) + ; + } + + protected function configureShowFields(ShowMapper $show): void + { + $show + ->with('label_general', ['label' => 'fieldset.label_general', 'class' => 'col-md-4']) + ->add('id', null, ['label' => 'field.label_id']) + ->add('type', FieldDescriptionInterface::TYPE_CHOICE, [ + 'label' => 'label.type', + 'choices' => array_flip($this->commandPoolHelper->getCronChoices()), + 'choice_translation_domain' => 'admin', + ]) + ->add('startedAt', null, ['label' => 'label.started_at']) + ->add('endedAt', null, ['label' => 'label.ended_at']) + ->add('durationAsString', null, ['label' => 'label.duration']) + ->add('status', null, [ + 'label' => 'label.status', + 'template' => '@SmartSonata/admin/base_field/show_process_status.html.twig', + ]) + ->add('summary', null, ['label' => 'label.summary']) + ->end() + ->with('process_data', ['label' => 'label.process_data', 'class' => 'col-md-8']) + ->add('logs', null, [ + 'label' => 'label.logs', + 'template' => '@SmartSonata/admin/base_field/show_process_logs.html.twig', + ]) + ->add('data', null, [ + 'label' => 'label.process_json_data', + 'template' => '@SmartSonata/admin/base_field/show_json.html.twig', + ]) + ->end() + ; + } + + private function getStatusChoices(): array + { + return ProcessStatusEnum::casesByTrans($this->getTranslator(), true, 'messages'); + } + + #[Required] + public function setCommandPoolHelper(CommandPoolHelper $commandPoolHelper): void + { + $this->commandPoolHelper = $commandPoolHelper; + } +}