From 5e6774913e927120af46434727f07e0aab60b856 Mon Sep 17 00:00:00 2001 From: fmizzell Date: Mon, 25 Nov 2019 11:33:38 -0600 Subject: [PATCH] Filtering options for the datastore's list command. (#258) --- modules/custom/dkan_datastore/src/Drush.php | 58 ++++++++++++++----- .../dkan_datastore/src/WebServiceApi.php | 2 +- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/modules/custom/dkan_datastore/src/Drush.php b/modules/custom/dkan_datastore/src/Drush.php index fd62e079a..34c0a53ef 100755 --- a/modules/custom/dkan_datastore/src/Drush.php +++ b/modules/custom/dkan_datastore/src/Drush.php @@ -3,6 +3,7 @@ namespace Drupal\dkan_datastore; use Consolidation\OutputFormatters\StructuredData\RowsOfFields; +use Consolidation\OutputFormatters\StructuredData\UnstructuredListData; use Drupal\dkan_data\ValueReferencer; use Drush\Commands\DrushCommands; @@ -16,7 +17,7 @@ class Drush extends DrushCommands { /** * The datastore service. * - * @var \Drupal\dkan_datastore\Service\Service + * @var \Drupal\dkan_datastore\Service */ protected $datastoreService; @@ -71,28 +72,59 @@ public function import($uuid, $deferred = FALSE) { * importerStatus: Importer * importerBytes: Processed * - * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields - * The importer list, organized into a RowsOfFields formatter object. + * @options format The format of the data. + * @options status Show imports of the given status. + * @options uuid-only Only the list of uuids. * * @command dkan-datastore:list */ - public function list() { + public function list($options = [ + 'format' => 'table', + 'status' => NULL, + 'uuid-only' => FALSE, + ]) { + $status = $options['status']; + $uuid_only = $options['uuid-only']; + $list = $this->datastoreService->list(); $rows = []; foreach ($list as $uuid => $item) { - $row = [ - 'uuid' => $uuid, - 'fileName' => $item->fileName, - 'fileFetcherStatus' => $item->fileFetcherStatus, - 'fileFetcherBytes' => \format_size($item->fileFetcherBytes) . " ($item->fileFetcherPercentDone%)", - 'importerStatus' => $item->importerStatus, - 'importerBytes' => \format_size($item->importerBytes) . " ($item->importerPercentDone%)", - ]; - $rows[] = $row; + $rows[] = $this->createRow($uuid, $item); + } + + if (!empty($status)) { + $rows = array_filter($rows, function ($row) use ($status) { + if ($row['fileFetcherStatus'] == $status || $row['importerStatus'] == $status) { + return TRUE; + } + return FALSE; + }); + } + + if ($uuid_only) { + foreach ($rows as $index => $row) { + $rows[$index] = $row['uuid']; + } + return new UnstructuredListData($rows); } + return new RowsOfFields($rows); } + /** + * Private. + */ + private function createRow($uuid, $item) { + return [ + 'uuid' => $uuid, + 'fileName' => $item->fileName, + 'fileFetcherStatus' => $item->fileFetcherStatus, + 'fileFetcherBytes' => \format_size($item->fileFetcherBytes) . " ($item->fileFetcherPercentDone%)", + 'importerStatus' => $item->importerStatus, + 'importerBytes' => \format_size($item->importerBytes) . " ($item->importerPercentDone%)", + ]; + } + /** * Drop. * diff --git a/modules/custom/dkan_datastore/src/WebServiceApi.php b/modules/custom/dkan_datastore/src/WebServiceApi.php index 3c4432c6b..43f6cb12a 100644 --- a/modules/custom/dkan_datastore/src/WebServiceApi.php +++ b/modules/custom/dkan_datastore/src/WebServiceApi.php @@ -105,7 +105,7 @@ private function importMultiple(array $resourceIds) { $responses = []; foreach ($resourceIds as $identifier) { try { - $results = $this->datastoreService->import($identifier, FALSE); + $results = $this->datastoreService->import($identifier, TRUE); $responses[$identifier] = $results; } catch (\Exception $e) {