Passed
Push — master ( 482dc5...d73db8 )
by Marcel
04:37 queued 14s
created

DatasetService::provider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\Service;
10
11
use OCA\Analytics\Activity\ActivityManager;
12
use OCA\Analytics\Controller\DatasourceController;
13
use OCA\Analytics\Db\DataloadMapper;
14
use OCA\Analytics\Db\DatasetMapper;
15
use OCA\Analytics\Db\ReportMapper;
16
use OCA\Analytics\Db\StorageMapper;
17
use OCP\AppFramework\Http\DataDownloadResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\DataDownloadResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use OCP\AppFramework\Http\DataResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\DataResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use OCP\DB\Exception;
0 ignored issues
show
Bug introduced by
The type OCP\DB\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use OCP\Files\IRootFolder;
0 ignored issues
show
Bug introduced by
The type OCP\Files\IRootFolder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use OCP\Files\NotFoundException;
0 ignored issues
show
Bug introduced by
The type OCP\Files\NotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use OCP\ITagManager;
0 ignored issues
show
Bug introduced by
The type OCP\ITagManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
class DatasetService {
26
	private $userId;
27
	private $logger;
28
	private $tagManager;
29
	private $ShareService;
30
	private $StorageMapper;
31
	private $DatasetMapper;
32
	private $ThresholdService;
33
	private $DataloadMapper;
34
	private $ActivityManager;
35
	private $rootFolder;
36
	private $VariableService;
37
	private $ReportMapper;
38
	private $contextChatManager;
39
40
	public function __construct(
41
		$userId,
42
		LoggerInterface $logger,
43
		ITagManager $tagManager,
44
		ShareService $ShareService,
45
		StorageMapper $StorageMapper,
46
		DatasetMapper $DatasetMapper,
47
		ThresholdService $ThresholdService,
48
		DataloadMapper $DataloadMapper,
49
		ActivityManager $ActivityManager,
50
		IRootFolder $rootFolder,
51
		VariableService $VariableService,
52
		ReportMapper $ReportMapper
53
	) {
54
		$this->userId = $userId;
55
		$this->logger = $logger;
56
		$this->tagManager = $tagManager;
57
		$this->ShareService = $ShareService;
58
		$this->ThresholdService = $ThresholdService;
59
		$this->StorageMapper = $StorageMapper;
60
		$this->DatasetMapper = $DatasetMapper;
61
		$this->DataloadMapper = $DataloadMapper;
62
		$this->ActivityManager = $ActivityManager;
63
		$this->rootFolder = $rootFolder;
64
		$this->VariableService = $VariableService;
65
		$this->ReportMapper = $ReportMapper;
66
	}
67
68
	/**
69
	 * get all datasets
70
	 *
71
	 * @return array
72
	 */
73
	public function index() {
74
		$ownDatasets = $this->DatasetMapper->index();
75
76
		// get data load indicators for icons shown in the advanced screen
77
		$dataloads = $this->DataloadMapper->getAllDataloadMetadata();
78
		foreach ($dataloads as $dataload) {
79
			$key = array_search($dataload['dataset'], array_column($ownDatasets, 'id'));
80
			if ($key !== '') {
81
				if ($dataload['schedules'] !== '' and $dataload['schedules'] !== null) {
82
					$dataload['schedules'] = 1;
83
				} else {
84
					$dataload['schedules'] = 0;
85
				}
86
				$ownDatasets[$key]['dataloads'] = $dataload['dataloads'];
87
				$ownDatasets[$key]['schedules'] = $dataload['schedules'];
88
			}
89
		}
90
91
		foreach ($ownDatasets as &$ownDataset) {
92
			$ownDataset['type'] = DatasourceController::DATASET_TYPE_INTERNAL_DB;
93
			$ownDataset['item_type'] = ShareService::SHARE_ITEM_TYPE_DATASET;
94
			$ownDataset = $this->VariableService->replaceTextVariables($ownDataset);
95
		}
96
97
		return $ownDatasets;
98
	}
99
100
	/**
101
	 * get own dataset details; used in external access like dataset controller
102
	 *
103
	 * @param int $datasetId
104
	 * @return array|bool
105
	 * @throws Exception
106
	 */
107
	public function readOwn(int $datasetId) {
108
		$ownDataset = $this->DatasetMapper->readOwn($datasetId);
109
		if (!empty($ownDataset)) {
110
			$ownDataset['permissions'] = \OCP\Constants::PERMISSION_UPDATE;
0 ignored issues
show
Bug introduced by
The type OCP\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
111
			$ownDataset['dataset'] = $ownDataset['id'];
112
		}
113
		return $ownDataset;
114
	}
115
116
	/**
117
	 * get  dataset details
118
	 *
119
	 * @param int $datasetId
120
	 * @return array|bool
121
	 * @throws Exception
122
	 */
123
	public function read(int $datasetId) {
124
		return $this->DatasetMapper->read($datasetId);
125
	}
126
127
	/**
128
	 * check if own report
129
	 *
130
	 * @param int $reportId
131
	 * @return bool
132
	 */
133
	public function isOwn(int $datasetId) {
134
		$ownDataset = $this->DatasetMapper->readOwn($datasetId);
135
		if (!empty($ownDataset)) {
136
			return true;
137
		} else {
138
			return false;
139
		}
140
	}
141
142
	/**
143
	 * get dataset status
144
	 *
145
	 * @param int $datasetId
146
	 * @return array|bool
147
	 * @throws Exception
148
	 */
149
	public function status(int $datasetId): array {
150
		$status = array();
151
		$status['reports'] = $this->ReportMapper->reportsForDataset($datasetId);
152
		$status['data'] = $this->StorageMapper->getRecordCount($datasetId);
153
		return $status;
154
	}
155
156
	/**
157
	 * create new dataset
158
	 *
159
	 * @param $name
160
	 * @param $dimension1
161
	 * @param $dimension2
162
	 * @param $value
163
	 * @return int
164
	 * @throws Exception
165
	 */
166
	public function create($name, $dimension1, $dimension2, $value) {
167
		$datasetId = $this->DatasetMapper->create($name, $dimension1, $dimension2, $value);
168
		$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
169
		return $datasetId;
170
	}
171
172
	/**
173
	 * get dataset details
174
	 *
175
	 * @param int $datasetId
176
	 * @param $name
177
	 * @param null $dimension1
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $dimension1 is correct as it would always require null to be passed?
Loading history...
178
	 * @param null $dimension2
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $dimension2 is correct as it would always require null to be passed?
Loading history...
179
	 * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
180
	 * @param null $subheader
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $subheader is correct as it would always require null to be passed?
Loading history...
181
	 * @param null $aiIndex
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $aiIndex is correct as it would always require null to be passed?
Loading history...
182
	 * @return bool
183
	 * @throws Exception
184
	 */
185
	public function update(int $datasetId, $name, $subheader, $dimension1, $dimension2, $value, $aiIndex) {
186
		return $this->DatasetMapper->update($datasetId, $name, $subheader, $dimension1, $dimension2, $value, $aiIndex);
187
	}
188
189
	/**
190
	 * Export Dataset
191
	 *
192
	 * @param int $datasetId
193
	 * @return DataDownloadResponse
194
	 * @throws Exception
195
	 */
196
	public function export(int $datasetId) {
197
		$result = array();
198
		$result['dataset'] = $this->DatasetMapper->read($datasetId);
199
		$result['dataload'] = $this->DataloadMapper->read($datasetId);
200
		$result['threshold'] = $this->ThresholdService->read($datasetId);
201
		$result['favorite'] = '';
202
203
		if ($result['dataset']['type'] === DatasourceController::DATASET_TYPE_INTERNAL_DB) {
204
			$result['data'] = $this->StorageMapper->read($datasetId);
205
		}
206
207
		unset($result['dataset']['id'], $result['dataset']['user_id'], $result['dataset']['user_id'], $result['dataset']['parent']);
208
		$data = json_encode($result);
209
		return new DataDownloadResponse($data, $result['dataset']['name'] . '.export.txt', 'text/plain; charset=utf-8');
210
	}
211
212
	/**
213
	 * Update the context chat provider
214
	 *
215
	 * @NoAdminRequired
216
	 * @param int $datasetId
217
	 * @return bool
218
	 */
219
	public function provider(int $datasetId) {
220
		if (class_exists('OCA\ContextChat\Public\ContentManager')) {
221
			$this->contextChatManager = \OC::$server->query('OCA\Analytics\ContextChat\ContextChatManager');
222
			$this->contextChatManager->submitContent($datasetId);
223
		}
224
		return true;
225
	}
226
227
	/**
228
	 * Delete Dataset and all depending objects
229
	 *
230
	 * @param int $datasetId
231
	 * @return bool
232
	 * @throws Exception
233
	 */
234
	public function delete(int $datasetId) {
235
		$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_DELETE);
236
		$this->DatasetMapper->delete($datasetId);
237
		$this->DataloadMapper->deleteByDataset($datasetId);
238
		$this->StorageMapper->deleteByDataset($datasetId);
239
		return true;
240
	}
241
242
	/**
243
	 * get dataset by user
244
	 *
245
	 * @param string $userId
246
	 * @return bool
247
	 * @throws Exception
248
	 */
249
	public function deleteByUser(string $userId) {
250
		$datasets = $this->DatasetMapper->indexByUser($userId);
251
		foreach ($datasets as $dataset) {
252
			$this->DatasetMapper->delete($dataset['id']);
253
			$this->DataloadMapper->deleteByDataset($dataset['id']);
254
			$this->StorageMapper->deleteByDataset($dataset['id']);
255
		}
256
		return true;
257
	}
258
259
}