Skip to content

Commit

Permalink
Add API endpoints to get list of available reports and reports details (
Browse files Browse the repository at this point in the history
#151)

* Add API endpoints to get list of available reports and reports details

* Add enpoint to get all data, remove metadate from detail endpoint
  • Loading branch information
ochorocho authored May 10, 2021
1 parent ac5f584 commit 0c3cf2f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
5 changes: 4 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
['name' => 'ApiData#preflighted_cors', 'url' => '/api/2.0/{path}', 'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']],
['name' => 'ApiData#addDataV2', 'url' => '/api/2.0/adddata/{datasetId}', 'verb' => 'POST'],
['name' => 'ApiData#deleteDataV2', 'url' => '/api/2.0/deletedata/{datasetId}', 'verb' => 'POST'],
['name' => 'ApiData#index', 'url' => '/api/2.0/dataset/list', 'verb' => 'GET'],
['name' => 'ApiData#detail', 'url' => '/api/2.0/dataset/{datasetId}/detail', 'verb' => 'GET'],
['name' => 'ApiData#data', 'url' => '/api/2.0/dataset/{datasetId}/data', 'verb' => 'GET'],

// wizard
['name' => 'wizard#dismiss', 'url' => '/wizard', 'verb' => 'POST'],
Expand All @@ -78,4 +81,4 @@
['name' => 'whatsNew#get', 'url' => '/whatsnew', 'verb' => 'GET'],
['name' => 'whatsNew#dismiss', 'url' => '/whatsnew', 'verb' => 'POST'],
]
];
];
73 changes: 71 additions & 2 deletions lib/Controller/ApiDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace OCA\Analytics\Controller;

use OCA\Analytics\Activity\ActivityManager;
use OCA\Analytics\Db\StorageMapper;
use OCA\Analytics\Service\DatasetService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
Expand All @@ -33,6 +34,7 @@ class ApiDataController extends ApiController
private $ActivityManager;
private $DatasetService;
private $StorageController;
private $StorageMapper;

public function __construct(
$appName,
Expand All @@ -41,7 +43,8 @@ public function __construct(
IUserSession $userSession,
ActivityManager $ActivityManager,
DatasetService $DatasetService,
StorageController $StorageController
StorageController $StorageController,
StorageMapper $StorageMapper
)
{
parent::__construct(
Expand All @@ -54,6 +57,7 @@ public function __construct(
$this->ActivityManager = $ActivityManager;
$this->DatasetService = $DatasetService;
$this->StorageController = $StorageController;
$this->StorageMapper = $StorageMapper;
}

/**
Expand Down Expand Up @@ -166,6 +170,71 @@ public function deleteDataV2(int $datasetId)
$message);
}

/**
* list datasets
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
* @return DataResponse
* @throws \Exception
*/
public function index() {
return $this->DatasetService->index();
}

/**
* read data of a dataset with additional information for table and series
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
* @return DataResponse
* @throws \Exception
*/
public function detail(int $datasetId)
{
$datasetMetadata = $this->DatasetService->getOwnDataset($datasetId);

if (!empty($datasetMetadata)) {
$allData = $this->StorageController->read($datasetMetadata);
$series = array_values(array_unique(array_map('array_shift', $allData['data'])));

return new DataResponse([
'header' => $allData['header'],
'dimensions' => $allData['dimensions'],
'series' => $series,
], HTTP::STATUS_OK);
} else {
return new DataResponse([
'message' => 'No metadata available for given $datasetId',
], HTTP::STATUS_OK);
}
}

/**
* get all data of a dataset and respect filter options
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
* @return DataResponse
* @throws \Exception
*/
public function data(int $datasetId)
{
$params = $this->request->getParams();
$datasetMetadata = $this->DatasetService->getOwnDataset($datasetId);

if (!empty($datasetMetadata)) {
$options = json_decode($params['filteroptions'], true);
$allData = $this->StorageMapper->read($datasetMetadata['id'], $options);

return new DataResponse($allData, HTTP::STATUS_OK);
} else {
return new DataResponse([
'message' => 'No data available for given $datasetId',
], HTTP::STATUS_OK);
}
}

/**
* derive if the parameter is technical or the free text description from the report
* @param $data
Expand Down Expand Up @@ -237,4 +306,4 @@ protected function requestResponse($success, $code = null, $message = null)
// curl -u Admin:2sroW-SxRcK-AmdsF-RYMJ5-CKSyf -d '{"delete":[{"dimension1": "a", "dimension2": "a"}]}' -X POST -H "Content-Type: application/json" http://nc18/nextcloud/apps/analytics/api/2.0/deletedata/158
// curl -u Admin:2sroW-SxRcK-AmdsF-RYMJ5-CKSyf -d '{"del":[{"dimension1": "a", "dimension2": "a"}]}' -X POST -H "Content-Type: application/json" http://nc18/nextcloud/apps/analytics/api/2.0/deletedata/158

}
}

0 comments on commit 0c3cf2f

Please sign in to comment.