|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Analytics |
|
4
|
|
|
* |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. See the LICENSE.md file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Marcel Scherello <[email protected]> |
|
9
|
|
|
* @copyright 2020 Marcel Scherello |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use OCA\Analytics\Datasource\DatasourceEvent; |
|
15
|
|
|
use OCA\Analytics\Datasource\ExternalFileService; |
|
16
|
|
|
use OCA\Analytics\Datasource\FileService; |
|
17
|
|
|
use OCA\Analytics\Datasource\GithubService; |
|
18
|
|
|
use OCA\Analytics\Datasource\JsonService; |
|
19
|
|
|
use OCA\Analytics\Datasource\RegexService; |
|
20
|
|
|
use OCP\AppFramework\Controller; |
|
21
|
|
|
use OCP\EventDispatcher\IEventDispatcher; |
|
22
|
|
|
use OCP\Files\NotFoundException; |
|
23
|
|
|
use OCP\IL10N; |
|
24
|
|
|
use OCP\ILogger; |
|
25
|
|
|
use OCP\IRequest; |
|
26
|
|
|
|
|
27
|
|
|
class DataSourceController extends Controller |
|
28
|
|
|
{ |
|
29
|
|
|
private $logger; |
|
30
|
|
|
private $GithubService; |
|
31
|
|
|
private $FileService; |
|
32
|
|
|
private $ExternalFileService; |
|
33
|
|
|
private $RegexService; |
|
34
|
|
|
private $JsonService; |
|
35
|
|
|
private $userId; |
|
36
|
|
|
/** @var IEventDispatcher */ |
|
37
|
|
|
protected $dispatcher; |
|
38
|
|
|
private $l10n; |
|
39
|
|
|
|
|
40
|
|
|
const DATASET_TYPE_GROUP = 0; |
|
41
|
|
|
const DATASET_TYPE_INTERNAL_FILE = 1; |
|
42
|
|
|
const DATASET_TYPE_INTERNAL_DB = 2; |
|
43
|
|
|
const DATASET_TYPE_GIT = 3; |
|
44
|
|
|
const DATASET_TYPE_EXTERNAL_FILE = 4; |
|
45
|
|
|
const DATASET_TYPE_REGEX = 5; |
|
46
|
|
|
const DATASET_TYPE_JSON = 6; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct( |
|
49
|
|
|
string $AppName, |
|
50
|
|
|
IRequest $request, |
|
51
|
|
|
$userId, |
|
52
|
|
|
ILogger $logger, |
|
53
|
|
|
GithubService $GithubService, |
|
54
|
|
|
FileService $FileService, |
|
55
|
|
|
RegexService $RegexService, |
|
56
|
|
|
JsonService $JsonService, |
|
57
|
|
|
ExternalFileService $ExternalFileService, |
|
58
|
|
|
IL10N $l10n, |
|
59
|
|
|
IEventDispatcher $dispatcher |
|
60
|
|
|
) |
|
61
|
|
|
{ |
|
62
|
|
|
parent::__construct($AppName, $request); |
|
63
|
|
|
$this->userId = $userId; |
|
64
|
|
|
$this->logger = $logger; |
|
65
|
|
|
$this->ExternalFileService = $ExternalFileService; |
|
66
|
|
|
$this->GithubService = $GithubService; |
|
67
|
|
|
$this->RegexService = $RegexService; |
|
68
|
|
|
$this->FileService = $FileService; |
|
69
|
|
|
$this->JsonService = $JsonService; |
|
70
|
|
|
$this->dispatcher = $dispatcher; |
|
71
|
|
|
$this->l10n = $l10n; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* get all datasets |
|
76
|
|
|
* |
|
77
|
|
|
* @NoAdminRequired |
|
78
|
|
|
*/ |
|
79
|
|
|
public function index() |
|
80
|
|
|
{ |
|
81
|
|
|
//$result[self::DATASET_TYPE_GROUP] = $this->l10n->t('No Data / Group'); |
|
82
|
|
|
//$result[self::DATASET_TYPE_INTERNAL_DB] = $this->l10n->t('Internal Database'); |
|
83
|
|
|
$result[self::DATASET_TYPE_INTERNAL_FILE] = $this->FileService->getName(); |
|
|
|
|
|
|
84
|
|
|
$result[self::DATASET_TYPE_GIT] = $this->GithubService->getName(); |
|
85
|
|
|
$result[self::DATASET_TYPE_EXTERNAL_FILE] = $this->ExternalFileService->getName(); |
|
86
|
|
|
$result[self::DATASET_TYPE_REGEX] = $this->RegexService->getName(); |
|
87
|
|
|
$result[self::DATASET_TYPE_JSON] = $this->JsonService->getName(); |
|
88
|
|
|
|
|
89
|
|
|
$result = $result + $this->getRegisteredDatasourceNames(); |
|
90
|
|
|
return $result; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* template for options & settings |
|
95
|
|
|
* |
|
96
|
|
|
* @NoAdminRequired |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getTemplates() |
|
100
|
|
|
{ |
|
101
|
|
|
$result[self::DATASET_TYPE_INTERNAL_FILE] = $this->FileService->getTemplate(); |
|
|
|
|
|
|
102
|
|
|
$result[self::DATASET_TYPE_GIT] = $this->GithubService->getTemplate(); |
|
103
|
|
|
$result[self::DATASET_TYPE_EXTERNAL_FILE] = $this->ExternalFileService->getTemplate(); |
|
104
|
|
|
$result[self::DATASET_TYPE_REGEX] = $this->RegexService->getTemplate(); |
|
105
|
|
|
$result[self::DATASET_TYPE_JSON] = $this->JsonService->getTemplate(); |
|
106
|
|
|
$result = $result + $this->getRegisteredDatasourceTemplates(); |
|
107
|
|
|
return $result; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the data from a datasource; |
|
112
|
|
|
* |
|
113
|
|
|
* @NoAdminRequired |
|
114
|
|
|
* @param int $datasource |
|
115
|
|
|
* @param $option |
|
116
|
|
|
* @return array|NotFoundException |
|
117
|
|
|
* @throws NotFoundException |
|
118
|
|
|
*/ |
|
119
|
|
|
public function read(int $datasource, $option) |
|
120
|
|
|
{ |
|
121
|
|
|
//$this->logger->debug('DataSourceController 66: Datasource Id: ' . $datasource . ', Option: ' . json_encode($option)); |
|
122
|
|
|
if ($datasource === self::DATASET_TYPE_INTERNAL_FILE) $result = $this->FileService->read($option); |
|
123
|
|
|
elseif ($datasource === self::DATASET_TYPE_GIT) $result = $this->GithubService->read($option); |
|
124
|
|
|
elseif ($datasource === self::DATASET_TYPE_EXTERNAL_FILE) $result = $this->ExternalFileService->read($option); |
|
125
|
|
|
elseif ($datasource === self::DATASET_TYPE_REGEX) $result = $this->RegexService->read($option); |
|
126
|
|
|
elseif ($datasource === self::DATASET_TYPE_JSON) $result = $this->JsonService->read($option); |
|
127
|
|
|
else $result = new NotFoundException(); |
|
128
|
|
|
|
|
129
|
|
|
return $result; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private function getRegisteredDatasourceTemplates() |
|
133
|
|
|
{ |
|
134
|
|
|
$event = new DatasourceEvent(); |
|
135
|
|
|
$this->dispatcher->dispatchTyped($event); |
|
136
|
|
|
|
|
137
|
|
|
$datasources = []; |
|
138
|
|
|
foreach ($event->getDataSources() as $class) { |
|
139
|
|
|
$uniqueId = $this->uniqueId(\OC::$server->get($class)->getId()); |
|
140
|
|
|
|
|
141
|
|
|
if (isset($datasources[$uniqueId])) { |
|
142
|
|
|
$this->logger->logException(new \InvalidArgumentException('Datasource with the same ID already registered: ' . \OC::$server->get($class)->getName()), ['level' => ILogger::INFO]); |
|
|
|
|
|
|
143
|
|
|
continue; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$datasources[$uniqueId] = \OC::$server->get($class)->getTemplate(); |
|
147
|
|
|
} |
|
148
|
|
|
return $datasources; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
private function getRegisteredDatasourceNames() |
|
152
|
|
|
{ |
|
153
|
|
|
$event = new DatasourceEvent(); |
|
154
|
|
|
$this->dispatcher->dispatchTyped($event); |
|
155
|
|
|
|
|
156
|
|
|
$datasources = []; |
|
157
|
|
|
foreach ($event->getDataSources() as $class) { |
|
158
|
|
|
$uniqueId = $this->uniqueId(\OC::$server->get($class)->getId()); |
|
159
|
|
|
$datasources[$uniqueId] = \OC::$server->get($class)->getName(); |
|
160
|
|
|
} |
|
161
|
|
|
return $datasources; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
private function uniqueId($id) |
|
165
|
|
|
{ |
|
166
|
|
|
return '99' . $id; |
|
167
|
|
|
} |
|
168
|
|
|
} |