Skip to content

Commit

Permalink
Merge pull request #877 from nextcloud/Rello-analytics
Browse files Browse the repository at this point in the history
Extract selection option labels for Analytics
  • Loading branch information
blizzz committed Jul 3, 2024
2 parents 0002099 + 54fc70d commit 81342ba
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
* @author Marcel Scherello <analytics@scherello.de>
*/

namespace OCA\Tables\Datasource;
namespace OCA\Tables\Analytics;

use OCA\Analytics\Datasource\IDatasource;
use OCA\Tables\Api\V1Api;
use OCA\Tables\Errors\InternalError;
use OCA\Tables\Errors\NotFoundError;
use OCA\Tables\Errors\PermissionError;
use OCA\Tables\Service\ColumnService;
use OCA\Tables\Service\RowService;
use OCA\Tables\Service\TableService;
use OCA\Tables\Service\ViewService;
use OCP\AppFramework\Db\DoesNotExistException;
Expand All @@ -28,7 +29,8 @@ class AnalyticsDatasource implements IDatasource {
private IL10N $l10n;
private TableService $tableService;
private ViewService $viewService;
private V1Api $api;
private RowService $rowService;
private ColumnService $columnService;

protected ?string $userId;

Expand All @@ -37,14 +39,16 @@ public function __construct(
LoggerInterface $logger,
TableService $tableService,
ViewService $viewService,
V1Api $api,
ColumnService $columnService,
RowService $rowService,
?string $userId
) {
$this->l10n = $l10n;
$this->logger = $logger;
$this->tableService = $tableService;
$this->viewService = $viewService;
$this->api = $api;
$this->columnService = $columnService;
$this->rowService = $rowService;
$this->userId = $userId;
}

Expand Down Expand Up @@ -141,10 +145,10 @@ public function readData($option): array {

if (count($ids) === 1) {
// it's a table
$data = $this->api->getData((int) $ids[0], null, null, $this->userId);
$data = $this->getData((int)$ids[0], null, null);
} elseif (count($ids) === 2) {
// it's a view
$data = $this->api->getData((int) $ids[1], null, null, $this->userId, 'view');
$data = $this->getData((int)$ids[1], null, null, 'view');
}

// extract the header from the first row
Expand Down Expand Up @@ -172,13 +176,69 @@ public function readData($option): array {
}
unset($rows);

return [
'header' => $header,
'dimensions' => array_slice($header, 0, count($header) - 1),
'data' => $data,
//'rawdata' => $data,
'error' => 0,
];
return ['header' => $header, 'dimensions' => array_slice($header, 0, count($header) - 1), 'data' => $data, //'rawdata' => $data,
'error' => 0,];
}

/**
* Get the data from the table backend
* this was taken over from the former API
*
* @param int $nodeId
* @param int|null $limit
* @param int|null $offset
* @param string|null $nodeType
* @return array
* @throws DoesNotExistException
* @throws InternalError
* @throws MultipleObjectsReturnedException
* @throws NotFoundError
* @throws PermissionError
*/
private function getData(int $nodeId, ?int $limit, ?int $offset, ?string $nodeType = null): array {
if ($nodeType === 'view') {
$columns = $this->columnService->findAllByView($nodeId, $this->userId);
$rows = $this->rowService->findAllByView($nodeId, $this->userId, $limit, $offset);
} else {
// if no nodeType is provided, the old table selection is used to not break anything
$columns = $this->columnService->findAllByTable($nodeId, null, $this->userId);
$rows = $this->rowService->findAllByTable($nodeId, $this->userId, $limit, $offset);
}

$data = [];

// first line contains the titles
$header = [];
foreach ($columns as $column) {
$header[] = $column->getTitle();
}
$data[] = $header;

// now add the rows
foreach ($rows as $row) {
$rowData = $row->getData();
$line = [];
foreach ($columns as $column) {
$value = '';
foreach ($rowData as $datum) {
if ($datum['columnId'] === $column->getId()) {
// if column type selection, the corresponding labels need to be fetched
if ($column->getType() === 'selection') {
foreach ($column->getSelectionOptionsArray() as $option) {
if ($option['id'] === $datum['value']) {
$value = $option['label'];
}
}
} else {
$value = $datum['value'];
}
}
}
$line[] = $value;
}
$data[] = $line;
}
return $data;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Listener/AnalyticsDatasourceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace OCA\Tables\Listener;

use OCA\Analytics\Datasource\DatasourceEvent;
use OCA\Tables\Datasource\AnalyticsDatasource;
use OCA\Tables\Analytics\AnalyticsDatasource;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;

Expand Down
2 changes: 1 addition & 1 deletion tests/stub.phpstub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

// reference for: use OCA\Analytics\Datasource\DatasourceEvent;
// reference for: use OCA\Analytics\Analytics\DatasourceEvent;
namespace OCA\Analytics\Datasource {
class DatasourceEvent extends \OCP\EventDispatcher\Event {
abstract public function registerDatasource(string $datasource): void {}
Expand Down

0 comments on commit 81342ba

Please sign in to comment.