Skip to content

Commit

Permalink
Filtering options for the datastore's list command. (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmizzell authored Nov 25, 2019
1 parent ffdc2a8 commit 5e67749
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
58 changes: 45 additions & 13 deletions modules/custom/dkan_datastore/src/Drush.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,7 +17,7 @@ class Drush extends DrushCommands {
/**
* The datastore service.
*
* @var \Drupal\dkan_datastore\Service\Service
* @var \Drupal\dkan_datastore\Service
*/
protected $datastoreService;

Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion modules/custom/dkan_datastore/src/WebServiceApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5e67749

Please sign in to comment.