From 0569d2baa6b5e1cbb8f690cf8b3bb7aab6b1f9da Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Tue, 18 Jul 2023 13:07:28 +0300 Subject: [PATCH 01/10] UHF-8636: debug page item for indexed items --- src/Plugin/DebugDataItem/SearchApiIndex.php | 98 +++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/Plugin/DebugDataItem/SearchApiIndex.php diff --git a/src/Plugin/DebugDataItem/SearchApiIndex.php b/src/Plugin/DebugDataItem/SearchApiIndex.php new file mode 100644 index 00000000..60b1efcb --- /dev/null +++ b/src/Plugin/DebugDataItem/SearchApiIndex.php @@ -0,0 +1,98 @@ +entityTypeManager = $container->get('entity_type.manager'); + return $instance; + } + + /** + * {@inheritdoc} + */ + public function collect(): array { + $data = ['indexes' => []]; + + // @todo: Check if search api is enabled. + try { + $indexes = $this->entityTypeManager + ->getStorage('search_api_index') + ->loadMultiple(); + } + catch(\Exception $e) { + return $data; + } + + if ($indexes) { + /** @var \Drupal\search_api\IndexInterface $index */ + foreach($indexes as $index) { + $tracker = $index->getTrackerInstance(); + + $result = $this->resolveResult( + $tracker->getIndexedItemsCount(), + $tracker->getTotalItemsCount() + ); + + $data['indexes'] = [$index->getServerId() => $result]; + } + } + + return $data; + } + + + /** + * Resolve return value based on index status. + * + * @param int $indexed + * Amount of up-to-date items in index. + * @param int $total + * Maximum amount of items in index. + * + * @return string + * Status. + */ + private function resolveResult(int $indexed, int $total): string { + if ($indexed == 0 || $total == 0) { + return 'indexing or index rebuild required'; + } + + if ($indexed === $total) { + return 'Index up to date'; + } + + return "$indexed/$total"; + } + +} From bdb09ffaacd52667a4b0dab2e4401da36b0dabae Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Tue, 18 Jul 2023 13:33:28 +0300 Subject: [PATCH 02/10] UHF-8636: code fixes --- src/Plugin/DebugDataItem/SearchApiIndex.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Plugin/DebugDataItem/SearchApiIndex.php b/src/Plugin/DebugDataItem/SearchApiIndex.php index 60b1efcb..a3b045ba 100644 --- a/src/Plugin/DebugDataItem/SearchApiIndex.php +++ b/src/Plugin/DebugDataItem/SearchApiIndex.php @@ -1,4 +1,4 @@ - []]; - // @todo: Check if search api is enabled. + // @todo Check if search api is enabled. try { $indexes = $this->entityTypeManager ->getStorage('search_api_index') ->loadMultiple(); } - catch(\Exception $e) { + catch (\Exception $e) { return $data; } if ($indexes) { /** @var \Drupal\search_api\IndexInterface $index */ - foreach($indexes as $index) { + foreach ($indexes as $index) { $tracker = $index->getTrackerInstance(); $result = $this->resolveResult( @@ -71,7 +69,6 @@ public function collect(): array { return $data; } - /** * Resolve return value based on index status. * From 57edd690a850f3bf9e5e52df885d232eddad8037 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Wed, 19 Jul 2023 09:57:52 +0300 Subject: [PATCH 03/10] UHF-8636: add cluster status to the response --- src/Plugin/DebugDataItem/SearchApiIndex.php | 31 +++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Plugin/DebugDataItem/SearchApiIndex.php b/src/Plugin/DebugDataItem/SearchApiIndex.php index a3b045ba..0731ecc5 100644 --- a/src/Plugin/DebugDataItem/SearchApiIndex.php +++ b/src/Plugin/DebugDataItem/SearchApiIndex.php @@ -5,6 +5,7 @@ namespace Drupal\helfi_api_base\Plugin\DebugDataItem; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\helfi_api_base\DebugDataItemPluginBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -27,12 +28,20 @@ class SearchApiIndex extends DebugDataItemPluginBase implements ContainerFactory */ protected EntityTypeManagerInterface $entityTypeManager; + /** + * Module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler + */ + protected ModuleHandlerInterface $moduleHandler; + /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { $instance = new static($configuration, $plugin_id, $plugin_definition); $instance->entityTypeManager = $container->get('entity_type.manager'); + $instance->moduleHandler = $container->get('module_handler'); return $instance; } @@ -42,11 +51,22 @@ public static function create(ContainerInterface $container, array $configuratio public function collect(): array { $data = ['indexes' => []]; - // @todo Check if search api is enabled. + if ( + !$this->moduleHandler->moduleExists('search_api') || + !$this->moduleHandler->moduleExists('elasticsearch_connector') + ) { + return $data; + } + try { $indexes = $this->entityTypeManager ->getStorage('search_api_index') ->loadMultiple(); + + $clusterManager = \Drupal::service('elasticsearch_connector.cluster_manager'); + $clientManager = \Drupal::service('elasticsearch_connector.client_manager'); + $clusters = $clusterManager->loadAllClusters(FALSE); + $cluster = reset($clusters); } catch (\Exception $e) { return $data; @@ -62,10 +82,17 @@ public function collect(): array { $tracker->getTotalItemsCount() ); - $data['indexes'] = [$index->getServerId() => $result]; + $client = $clientManager->getClientForCluster($cluster); + $cluster_status = $client->getClusterInfo()['health']['status'] ?? FALSE; + + $data['indexes'][] = [ + $index->getServerId() => $result, + 'cluster_status' => $cluster_status, + ]; } } + return $data; } From 9f0b7e4b03763a3e4fd46318533af315fac6d25f Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Wed, 19 Jul 2023 10:04:12 +0300 Subject: [PATCH 04/10] UHF-8636: code fixes --- src/Plugin/DebugDataItem/SearchApiIndex.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Plugin/DebugDataItem/SearchApiIndex.php b/src/Plugin/DebugDataItem/SearchApiIndex.php index 0731ecc5..3f6be624 100644 --- a/src/Plugin/DebugDataItem/SearchApiIndex.php +++ b/src/Plugin/DebugDataItem/SearchApiIndex.php @@ -29,7 +29,7 @@ class SearchApiIndex extends DebugDataItemPluginBase implements ContainerFactory protected EntityTypeManagerInterface $entityTypeManager; /** - * Module handler. + * The module handler. * * @var \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler */ @@ -92,7 +92,6 @@ public function collect(): array { } } - return $data; } From 1ddd53c1df361c166dc0bb1e53cbc2159af625a7 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Wed, 19 Jul 2023 10:07:59 +0300 Subject: [PATCH 05/10] UHF-8636: code fixes --- src/Plugin/DebugDataItem/SearchApiIndex.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/DebugDataItem/SearchApiIndex.php b/src/Plugin/DebugDataItem/SearchApiIndex.php index 3f6be624..306407b1 100644 --- a/src/Plugin/DebugDataItem/SearchApiIndex.php +++ b/src/Plugin/DebugDataItem/SearchApiIndex.php @@ -29,7 +29,7 @@ class SearchApiIndex extends DebugDataItemPluginBase implements ContainerFactory protected EntityTypeManagerInterface $entityTypeManager; /** - * The module handler. + * The module handler interface. * * @var \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler */ From dff946c56199c6ba83d6ddaa2c97868545987969 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Wed, 19 Jul 2023 10:14:41 +0300 Subject: [PATCH 06/10] UHF-8636: remove variable name from docblock --- src/Plugin/DebugDataItem/SearchApiIndex.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugin/DebugDataItem/SearchApiIndex.php b/src/Plugin/DebugDataItem/SearchApiIndex.php index 306407b1..bc4ca624 100644 --- a/src/Plugin/DebugDataItem/SearchApiIndex.php +++ b/src/Plugin/DebugDataItem/SearchApiIndex.php @@ -29,9 +29,9 @@ class SearchApiIndex extends DebugDataItemPluginBase implements ContainerFactory protected EntityTypeManagerInterface $entityTypeManager; /** - * The module handler interface. + * The module handler. * - * @var \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler + * @var \Drupal\Core\Extension\ModuleHandlerInterface */ protected ModuleHandlerInterface $moduleHandler; From 28cc4dc077ecb170b8b5116372d3ab0079904016 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Tue, 25 Jul 2023 14:02:18 +0300 Subject: [PATCH 07/10] UHF-8636: inject --- src/Plugin/DebugDataItem/SearchApiIndex.php | 29 +++++++++------------ 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/Plugin/DebugDataItem/SearchApiIndex.php b/src/Plugin/DebugDataItem/SearchApiIndex.php index bc4ca624..2cc2465c 100644 --- a/src/Plugin/DebugDataItem/SearchApiIndex.php +++ b/src/Plugin/DebugDataItem/SearchApiIndex.php @@ -5,11 +5,11 @@ namespace Drupal\helfi_api_base\Plugin\DebugDataItem; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\helfi_api_base\DebugDataItemPluginBase; use Symfony\Component\DependencyInjection\ContainerInterface; + /** * Plugin implementation of the debug_data_item. * @@ -28,20 +28,19 @@ class SearchApiIndex extends DebugDataItemPluginBase implements ContainerFactory */ protected EntityTypeManagerInterface $entityTypeManager; - /** - * The module handler. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected ModuleHandlerInterface $moduleHandler; - /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { $instance = new static($configuration, $plugin_id, $plugin_definition); $instance->entityTypeManager = $container->get('entity_type.manager'); - $instance->moduleHandler = $container->get('module_handler'); + if ( + $container->has('elasticsearch_connector.cluster_manager') && + $container->has('elasticsearch_connector.client_manager') + ) { + $instance->clusterManager = $container->get('elasticsearch_connector.cluster_manager'); + $instance->clientManager = $container->get('elasticsearch_connector.client_manager'); + } return $instance; } @@ -52,8 +51,8 @@ public function collect(): array { $data = ['indexes' => []]; if ( - !$this->moduleHandler->moduleExists('search_api') || - !$this->moduleHandler->moduleExists('elasticsearch_connector') + !property_exists($this, 'clusterManager') || + !property_exists($this, 'clientManager') ) { return $data; } @@ -63,9 +62,7 @@ public function collect(): array { ->getStorage('search_api_index') ->loadMultiple(); - $clusterManager = \Drupal::service('elasticsearch_connector.cluster_manager'); - $clientManager = \Drupal::service('elasticsearch_connector.client_manager'); - $clusters = $clusterManager->loadAllClusters(FALSE); + $clusters = $this->clusterManager->loadAllClusters(FALSE); $cluster = reset($clusters); } catch (\Exception $e) { @@ -82,11 +79,11 @@ public function collect(): array { $tracker->getTotalItemsCount() ); - $client = $clientManager->getClientForCluster($cluster); + $client = $this->clientManager->getClientForCluster($cluster); $cluster_status = $client->getClusterInfo()['health']['status'] ?? FALSE; $data['indexes'][] = [ - $index->getServerId() => $result, + $index->getOriginalId() => $result, 'cluster_status' => $cluster_status, ]; } From d8bce7c81bb648d42179e3170d5cc0ecc847eccb Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Tue, 25 Jul 2023 14:15:43 +0300 Subject: [PATCH 08/10] UHF-8636: code fixes --- src/Plugin/DebugDataItem/SearchApiIndex.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Plugin/DebugDataItem/SearchApiIndex.php b/src/Plugin/DebugDataItem/SearchApiIndex.php index 2cc2465c..d62f5f43 100644 --- a/src/Plugin/DebugDataItem/SearchApiIndex.php +++ b/src/Plugin/DebugDataItem/SearchApiIndex.php @@ -9,7 +9,6 @@ use Drupal\helfi_api_base\DebugDataItemPluginBase; use Symfony\Component\DependencyInjection\ContainerInterface; - /** * Plugin implementation of the debug_data_item. * From a191cd42ca97ea5abb487bad0a62e151a299d269 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Tue, 25 Jul 2023 14:49:38 +0300 Subject: [PATCH 09/10] UHF-8636: added parameters for dynamic variables --- src/Plugin/DebugDataItem/SearchApiIndex.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Plugin/DebugDataItem/SearchApiIndex.php b/src/Plugin/DebugDataItem/SearchApiIndex.php index d62f5f43..5f71694c 100644 --- a/src/Plugin/DebugDataItem/SearchApiIndex.php +++ b/src/Plugin/DebugDataItem/SearchApiIndex.php @@ -27,6 +27,20 @@ class SearchApiIndex extends DebugDataItemPluginBase implements ContainerFactory */ protected EntityTypeManagerInterface $entityTypeManager; + /** + * The cluster manager. + * + * @var null + */ + protected $clusterManager = null; + + /** + * The client manager. + * + * @var null + */ + protected $clientManager = null; + /** * {@inheritdoc} */ From 25a539750d195b7a78069a9cdd64ad6ea467e9d8 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Tue, 25 Jul 2023 14:52:26 +0300 Subject: [PATCH 10/10] UHF-8636: code fixes --- src/Plugin/DebugDataItem/SearchApiIndex.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugin/DebugDataItem/SearchApiIndex.php b/src/Plugin/DebugDataItem/SearchApiIndex.php index 5f71694c..9d297b9e 100644 --- a/src/Plugin/DebugDataItem/SearchApiIndex.php +++ b/src/Plugin/DebugDataItem/SearchApiIndex.php @@ -32,14 +32,14 @@ class SearchApiIndex extends DebugDataItemPluginBase implements ContainerFactory * * @var null */ - protected $clusterManager = null; + protected $clusterManager = NULL; /** * The client manager. * * @var null */ - protected $clientManager = null; + protected $clientManager = NULL; /** * {@inheritdoc}