From 635e2d03092bf09ca760d2269c569e42313ec7cd Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Mon, 7 Jun 2021 11:20:11 -0400 Subject: [PATCH] Add a FarmMetricsBlock that includes asset and log record counts by type. --- .../install/block.block.farm_metrics.yml | 19 +++ modules/ui/metrics/css/metrics_block.css | 4 + .../ui/metrics/farm_ui_metrics.libraries.yml | 4 + .../src/Plugin/Block/FarmMetricsBlock.php | 141 ++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 modules/ui/metrics/config/install/block.block.farm_metrics.yml create mode 100644 modules/ui/metrics/css/metrics_block.css create mode 100644 modules/ui/metrics/farm_ui_metrics.libraries.yml create mode 100644 modules/ui/metrics/src/Plugin/Block/FarmMetricsBlock.php diff --git a/modules/ui/metrics/config/install/block.block.farm_metrics.yml b/modules/ui/metrics/config/install/block.block.farm_metrics.yml new file mode 100644 index 00000000..bd1997de --- /dev/null +++ b/modules/ui/metrics/config/install/block.block.farm_metrics.yml @@ -0,0 +1,19 @@ +langcode: en +status: false +dependencies: + module: + - farm_ui_metrics + theme: + - gin +id: farm_metrics +theme: gin +region: content +weight: 0 +provider: null +plugin: farm_metrics_block +settings: + id: farm_metrics_block + label: 'Metrics' + provider: farm_ui_metrics + label_display: '0' +visibility: { } diff --git a/modules/ui/metrics/css/metrics_block.css b/modules/ui/metrics/css/metrics_block.css new file mode 100644 index 00000000..d80d2c65 --- /dev/null +++ b/modules/ui/metrics/css/metrics_block.css @@ -0,0 +1,4 @@ +.metrics-container { + display: flex; + flex-flow: row wrap; +} diff --git a/modules/ui/metrics/farm_ui_metrics.libraries.yml b/modules/ui/metrics/farm_ui_metrics.libraries.yml new file mode 100644 index 00000000..a1e58ace --- /dev/null +++ b/modules/ui/metrics/farm_ui_metrics.libraries.yml @@ -0,0 +1,4 @@ +metrics_block: + css: + theme: + css/metrics_block.css: { } diff --git a/modules/ui/metrics/src/Plugin/Block/FarmMetricsBlock.php b/modules/ui/metrics/src/Plugin/Block/FarmMetricsBlock.php new file mode 100644 index 00000000..0a080a74 --- /dev/null +++ b/modules/ui/metrics/src/Plugin/Block/FarmMetricsBlock.php @@ -0,0 +1,141 @@ +database = $database; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('database'), + $container->get('entity_type.bundle.info') + ); + } + + /** + * {@inheritdoc} + */ + public function build() { + $output = []; + + // Create a container for asset metrics. + $output['asset'] = [ + '#markup' => '' . Link::createFromRoute('Assets', 'view.farm_asset.page')->toString() . '', + 'metrics' => [ + '#type' => 'container', + '#attributes' => [ + 'class' => 'assets metrics-container', + ], + ], + ]; + $metrics = $this->getEntityMetrics('asset'); + foreach ($metrics as $metric) { + $output['asset']['metrics'][] = [ + '#markup' => $metric, + ]; + } + $output['#cache']['tags'] = ['asset_list']; + + // Create a section for log metrics. + $output['log'] = [ + '#markup' => '' . Link::createFromRoute('Logs', 'view.farm_log.page')->toString() . '', + 'metrics' => [ + '#type' => 'container', + '#attributes' => [ + 'class' => 'logs metrics-container', + ], + ], + ]; + $metrics = $this->getEntityMetrics('log'); + foreach ($metrics as $metric) { + $output['log']['metrics'][] = [ + '#markup' => $metric, + ]; + } + $output['#cache']['tags'] = ['log_list']; + + // Attach CSS. + $output['#attached']['library'][] = 'farm_ui_metrics/metrics_block'; + + // Return the output. + return $output; + } + + /** + * Gather metrics for rendering in the block. + * + * @param string $entity_type + * The entity type machine name. + * + * @return array + * Returns an array of metric information. + */ + protected function getEntityMetrics($entity_type) { + $metrics = []; + + // Load bundles. + $bundles = $this->bundleInfo->getBundleInfo($entity_type); + + // Count records by type. + foreach ($bundles as $bundle => $bundle_info) { + $count = $this->database->query('SELECT COUNT(*) FROM {' . $entity_type . '_field_data} WHERE type=:type', [':type' => $bundle])->fetchField(); + $metrics[] = Link::createFromRoute($bundle_info['label'] . ': ' . $count, 'view.farm_asset.page_type', ['arg_0' => $bundle], ['attributes' => ['class' => 'metric button']])->toString(); + } + + return $metrics; + } + +}