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 0d3bda0 commit 3714f7b
Showing 1 changed file with 110 additions and 103 deletions.
213 changes: 110 additions & 103 deletions lib/Datasource/AnalyticsDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,48 @@
use OCP\IL10N;
use Psr\Log\LoggerInterface;

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

protected ?string $userId;
protected ?string $userId;

public function __construct(
IL10N $l10n,
LoggerInterface $logger,
TableService $tableService,
public function __construct(
IL10N $l10n,
LoggerInterface $logger,
TableService $tableService,
ViewService $viewService,
V1Api $api,
?string $userId
) {
$this->l10n = $l10n;
$this->logger = $logger;
$this->tableService = $tableService;
V1Api $api,
?string $userId
)
{
$this->l10n = $l10n;
$this->logger = $logger;
$this->tableService = $tableService;
$this->viewService = $viewService;
$this->api = $api;
$this->userId = $userId;
}

/**
* @return string Display Name of the datasource
*/
public function getName(): string {
return $this->l10n->t('Nextcloud Tables');
}

/**
* @return int 2 digit unique datasource id
*/
public function getId(): int {
return 55;
}
$this->api = $api;
$this->userId = $userId;
}

/**
* @return string Display Name of the datasource
*/
public function getName(): string
{
return $this->l10n->t('Nextcloud Tables');
}

/**
* @return int 2 digit unique datasource id
*/
public function getId(): int
{
return 55;
}

/**
* available options of the data source
Expand Down Expand Up @@ -88,32 +92,33 @@ public function getId(): int {
* @throws NotFoundError
* @throws PermissionError
*/
public function getTemplate(): array {
$tableString = '';
$template = [];
public function getTemplate(): array
{
$tableString = '';
$template = [];
$tables = [];

// get all tables for the user
if ($this->userId) {
$tables = $this->tableService->findAll($this->userId);
}
// get all tables for the user
if ($this->userId) {
$tables = $this->tableService->findAll($this->userId);
}

// concatenate the option-string. The format is tableId:viewId-title
foreach ($tables as $table) {
$tableString = $tableString . $table->getId() . '-' . $table->getTitle() . '/';
foreach ($tables as $table) {
$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;
}
// 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
Expand All @@ -134,8 +139,9 @@ public function getTemplate(): array {
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function readData($option): array {
// get the ids which come in the format tableId:viewId
public function readData($option): array
{
// get the ids which come in the format tableId:viewId
$ids = explode(':', $option['tableId']);
$this->userId = $option['user_id'];

Expand All @@ -144,59 +150,60 @@ public function readData($option): array {
$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');
$data = $this->api->getData($ids[1], null, null, $this->userId, 'view');
}

// extract the header from the first row
$header = $data[0];
// continue with the data without header
$rows = array_slice($data, 1);

// get the selected columns from the data source options
$selectedColumns = [];
if (isset($option['columns']) && strlen($option['columns']) > 0) {
$selectedColumns = str_getcsv($option['columns']);
}

// extract the header from the first row
$header = $data[0];
// continue with the data without header
$rows = array_slice($data, 1);

// get the selected columns from the data source options
$selectedColumns = [];
if (isset($option['columns']) && strlen($option['columns']) > 0) {
$selectedColumns = str_getcsv($option['columns']);
}

$data = [];
if (count($selectedColumns) !== 0) {
// if dedicated columns are selected, the others are removed
$header = $this->minimizeRow($selectedColumns, $header);
foreach ($rows as $row) {
$data[] = $this->minimizeRow($selectedColumns, $row);
}
} else {
foreach ($rows as $row) {
$data[] = $row;
}
}
unset($rows);

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

/**
* filter only the selected columns in the given sequence
*
* @param array $selectedColumns
* @param array $row
* @return array
*/
private function minimizeRow(array $selectedColumns, array $row): array {
$rowMinimized = [];
foreach ($selectedColumns as $selectedColumn) {
if ((int) $selectedColumn) {
$rowMinimized[] = $row[$selectedColumn - 1];
} else {
$rowMinimized[] = $selectedColumn;
}
}
return $rowMinimized;
}
$data = [];
if (count($selectedColumns) !== 0) {
// if dedicated columns are selected, the others are removed
$header = $this->minimizeRow($selectedColumns, $header);
foreach ($rows as $row) {
$data[] = $this->minimizeRow($selectedColumns, $row);
}
} else {
foreach ($rows as $row) {
$data[] = $row;
}
}
unset($rows);

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

/**
* filter only the selected columns in the given sequence
*
* @param array $selectedColumns
* @param array $row
* @return array
*/
private function minimizeRow(array $selectedColumns, array $row): array
{
$rowMinimized = [];
foreach ($selectedColumns as $selectedColumn) {
if ((int)$selectedColumn) {
$rowMinimized[] = $row[$selectedColumn - 1];
} else {
$rowMinimized[] = $selectedColumn;
}
}
return $rowMinimized;
}
}

0 comments on commit 3714f7b

Please sign in to comment.