Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update V1Api.php #590

Merged
merged 6 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions lib/Api/V1Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace OCA\Tables\Api;

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 OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;

class V1Api {
private RowService $rowService;
Expand All @@ -19,17 +22,30 @@ public function __construct(ColumnService $columnService, RowService $rowService
}

/**
* @param int $tableId
* @param int $nodeId
* @param int|null $limit
* @param int|null $offset
* @param string|null $userId
* @param string|null $nodeType
* @return array
* @throws DoesNotExistException
* @throws InternalError
* @throws MultipleObjectsReturnedException
* @throws NotFoundError
* @throws PermissionError
*/
public function getData(int $tableId, ?int $limit, ?int $offset): array {
$columns = $this->columnService->findAllByTable($tableId);

$rows = $this->rowService->findAllByTable($tableId, $this->userId, $limit, $offset);
public function getData(int $nodeId, ?int $limit, ?int $offset, ?string $userId, ?string $nodeType = null): array {
if ($userId) {
$this->userId = $userId;
}
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 = [];

Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/Api1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public function deleteColumn(int $columnId): DataResponse {
*/
public function indexTableRowsSimple(int $tableId, ?int $limit, ?int $offset): DataResponse {
return $this->handleError(function () use ($tableId, $limit, $offset) {
return $this->v1Api->getData($tableId, $limit, $offset);
return $this->v1Api->getData($tableId, $limit, $offset, $this->userId, 'table');
});
}

Expand Down
51 changes: 40 additions & 11 deletions lib/Datasource/AnalyticsDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
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\TableService;
use OCA\Tables\Service\ViewService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\IL10N;
use Psr\Log\LoggerInterface;

class AnalyticsDatasource implements IDatasource {
private LoggerInterface $logger;
private IL10N $l10n;
private TableService $tableService;
private ViewService $viewService;
private V1Api $api;

protected ?string $userId;
Expand All @@ -31,12 +36,14 @@ public function __construct(
IL10N $l10n,
LoggerInterface $logger,
TableService $tableService,
ViewService $viewService,
V1Api $api,
string $userId
?string $userId
) {
$this->l10n = $l10n;
$this->logger = $logger;
$this->tableService = $tableService;
$this->viewService = $viewService;
$this->api = $api;
$this->userId = $userId;
}
Expand All @@ -45,7 +52,7 @@ public function __construct(
* @return string Display Name of the datasource
*/
public function getName(): string {
return $this->l10n->t('Nextcloud tables');
return $this->l10n->t('Nextcloud Tables');
}

/**
Expand Down Expand Up @@ -75,25 +82,36 @@ public function getId(): int {
* {['id' => 'datatype', 'name' => 'Type of Data', 'type' => 'tf', 'placeholder' => 'adaptation/absolute']}
*
* @return array
* @throws DoesNotExistException
* @throws InternalError
* @throws MultipleObjectsReturnedException
* @throws NotFoundError
* @throws PermissionError
*/
public function getTemplate(): array {
$tableString = '';
$template = [];
$tables = [];

// get all tables for the current user and concatenate the placeholder string
// get all tables for the user
if ($this->userId) {
$tables = $this->tableService->findAll($this->userId);
} else {
$tables = [];
}

// concatenate the option-string. The format is tableId:viewId-title
foreach ($tables as $table) {
$tableString = $tableString . $table->jsonSerialize()['id'] . '-' . $table->jsonSerialize()['title'] . '/';
$tableString = $tableString . $table->getId() . '-' . $table->getTitle() . '/';
// get all views per table
$views = $this->viewService->findAll($this->tableService->find($table->getId()));
foreach ($views as $view) {
$tableString = $tableString . $table->getId() . ':' . $view->getId() . '-' . $view->getTitle() . '/';
}
}

// add the tables to a dropdown in the data source settings
$template[] = ['id' => 'tableId', 'name' => $this->l10n->t('Select table'), 'type' => 'tf', 'placeholder' => $tableString];
$template[] = ['id' => 'columns', 'name' => $this->l10n->t('Select columns'), 'placeholder' => $this->l10n->t('e.g. 1,2,4 or leave empty'), 'type' => 'columnPicker'];
$template[] = ['id' => 'timestamp', 'name' => $this->l10n->t('Timestamp of data load'), 'placeholder' => 'false-' . $this->l10n->t('No') . '/true-' . $this->l10n->t('Yes'), 'type' => 'tf'];
return $template;
}

Expand All @@ -112,11 +130,22 @@ public function getTemplate(): array {
* @return array available options of the data source
* @throws InternalError
* @throws PermissionError
* @throws NotFoundError
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function readData($option): array {
// get the data for the selected table
$tableId = $option['tableId'];
$data = $this->api->getData($tableId, null, null);
// get the ids which come in the format tableId:viewId
$ids = explode(':', $option['tableId']);
$this->userId = $option['user_id'];

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

// extract the header from the first row
$header = $data[0];
Expand Down Expand Up @@ -147,7 +176,7 @@ public function readData($option): array {
'header' => $header,
'dimensions' => array_slice($header, 0, count($header) - 1),
'data' => $data,
'rawdata' => $data,
//'rawdata' => $data,
'error' => 0,
];
}
Expand All @@ -162,7 +191,7 @@ public function readData($option): array {
private function minimizeRow(array $selectedColumns, array $row): array {
$rowMinimized = [];
foreach ($selectedColumns as $selectedColumn) {
if ((int) $selectedColumn) {
if ((int)$selectedColumn) {
$rowMinimized[] = $row[$selectedColumn - 1];
} else {
$rowMinimized[] = $selectedColumn;
Expand Down
12 changes: 7 additions & 5 deletions lib/Service/ColumnService.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,18 @@ public function findAllByTable(int $tableId, ?int $viewId = null, ?string $userI

/**
* @param int $viewId
* @param string|null $userId
* @return array
* @throws DoesNotExistException
* @throws InternalError
* @throws MultipleObjectsReturnedException
* @throws NotFoundError
* @throws PermissionError
*/
public function findAllByView(int $viewId): array {
public function findAllByView(int $viewId, ?string $userId = null): array {
try {
// No need to check for columns outside the view since they cannot be addressed
$view = $this->viewService->find($viewId, true);
$view = $this->viewService->find($viewId, true, $userId);
$viewColumnIds = $view->getColumnsArray();
$viewColumns = [];
foreach ($viewColumnIds as $viewColumnId) {
Expand Down Expand Up @@ -391,12 +392,13 @@ public function delete(int $id, bool $skipRowCleanup = false, ?string $userId =
}

/**
* @param int $tableId
* @param int $viewId
* @param int|null $tableId
* @param int|null $viewId
* @param array $titles example ['Test column 1', 'And so on', '3rd column title']
* @param string|null $userId
* @param bool $createUnknownColumns
* @param int $countCreatedColumns
* @param int $countMatchingColumns
* @return array with column object or null for given columns
* @throws DoesNotExistException
* @throws InternalError
Expand All @@ -411,7 +413,7 @@ public function findOrCreateColumnsByTitleForTableAsArray(?int $tableId, ?int $v
if($userId === null) {
$userId = $this->userId;
}
$allColumns = $viewId !== null ? $this->findAllByView($viewId) : $this->findAllByTable($tableId);
$allColumns = $viewId !== null ? $this->findAllByView($viewId, $userId) : $this->findAllByTable($tableId, null, $userId);
$i = -1;
foreach ($titles as $title) {
$i++;
Expand Down
Loading