Skip to content

Commit

Permalink
Update AnalyticsDatasource.php
Browse files Browse the repository at this point in the history
Signed-off-by: Rello <Rello@users.noreply.github.com>
  • Loading branch information
Rello authored Sep 20, 2023
1 parent e91b988 commit 0d3bda0
Showing 1 changed file with 78 additions and 49 deletions.
127 changes: 78 additions & 49 deletions lib/Datasource/AnalyticsDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,36 @@
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 V1Api $api;
private ViewService $viewService;
private V1Api $api;

protected ?string $userId;

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 All @@ -55,68 +62,90 @@ public function getId(): int {
return 55;
}

/**
* available options of the data source
*
* return needs to be an array and can consist of many fields.
* every field needs to provide the following format
* id *mandatory* = name of the option for the readData
* name *mandatory* = displayed name of the input field in the UI
* type *optional* = 'tf' to create a dropdown. Values need to be provided in the placeholder separated with "/".
* placeholder *mandatory* = help text for the input field in the UI
* for type=tf:
* e.g. "true/false"
* if value/text pairs are required for the dropdown/option, the values need to be separated with "-" in addition.
* e.g. "eq-equal/gt-greater"
* to avoid translation of the technical strings, separate them
* 'true-' - $this->l10n->t('Yes').'/false-'.$this->l10n->t('No')
*
* example:
* {['id' => 'datatype', 'name' => 'Type of Data', 'type' => 'tf', 'placeholder' => 'adaptation/absolute']}
*
* @return array
* @throws InternalError
*/
/**
* available options of the data source
*
* return needs to be an array and can consist of many fields.
* every field needs to provide the following format
* id *mandatory* = name of the option for the readData
* name *mandatory* = displayed name of the input field in the UI
* type *optional* = 'tf' to create a dropdown. Values need to be provided in the placeholder separated with "/".
* placeholder *mandatory* = help text for the input field in the UI
* for type=tf:
* e.g. "true/false"
* if value/text pairs are required for the dropdown/option, the values need to be separated with "-" in addition.
* e.g. "eq-equal/gt-greater"
* to avoid translation of the technical strings, separate them
* 'true-' - $this->l10n->t('Yes').'/false-'.$this->l10n->t('No')
*
* example:
* {['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;
}

/**
* Read the Data
*
* return needs to be an array
* [
* 'header' => $header, //array('column header 1', 'column header 2','column header 3')
* 'dimensions' => array_slice($header, 0, count($header) - 1),
* 'data' => $data,
* 'error' => 0, // INT 0 = no error
* ]
*
* @param array $option
* @return array available options of the data source
* @throws InternalError
* @throws PermissionError
*/
/**
* Read the Data
*
* return needs to be an array
* [
* 'header' => $header, //array('column header 1', 'column header 2','column header 3')
* 'dimensions' => array_slice($header, 0, count($header) - 1),
* 'data' => $data,
* 'error' => 0, // INT 0 = no error
* ]
*
* @param array $option
* @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) {
// its a table
$data = $this->api->getData($ids[0], null, null, $this->userId);
} elseif (count($ids) === 2) {
// its a view
$data = $this->api->getData($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 Down

0 comments on commit 0d3bda0

Please sign in to comment.