Passed
Push — master ( 912bd1...0b1a33 )
by Marcel
06:53
created

DatasetService::index()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 24
rs 9.2222
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 2021 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Service;
13
14
use OCA\Analytics\Activity\ActivityManager;
15
use OCA\Analytics\Controller\DatasourceController;
16
use OCA\Analytics\Db\DataloadMapper;
17
use OCA\Analytics\Db\DatasetMapper;
18
use OCA\Analytics\Db\StorageMapper;
19
use OCP\AppFramework\Http\DataDownloadResponse;
20
use OCP\AppFramework\Http\DataResponse;
21
use OCP\Files\IRootFolder;
22
use OCP\ITagManager;
23
use Psr\Log\LoggerInterface;
24
25
class DatasetService
26
{
27
    private $userId;
28
    private $logger;
29
    private $tagManager;
30
    private $ShareService;
31
    private $StorageMapper;
32
    private $DatasetMapper;
33
    private $ThresholdService;
34
    private $DataloadMapper;
35
    private $ActivityManager;
36
    private $rootFolder;
37
    private $VariableService;
38
39
    public function __construct(
40
        $userId,
41
        LoggerInterface $logger,
42
        ITagManager $tagManager,
43
        ShareService $ShareService,
44
        StorageMapper $StorageMapper,
45
        DatasetMapper $DatasetMapper,
46
        ThresholdService $ThresholdService,
47
        DataloadMapper $DataloadMapper,
48
        ActivityManager $ActivityManager,
49
        IRootFolder $rootFolder,
50
        VariableService $VariableService
51
    )
52
    {
53
        $this->userId = $userId;
54
        $this->logger = $logger;
55
        $this->tagManager = $tagManager;
56
        $this->ShareService = $ShareService;
57
        $this->ThresholdService = $ThresholdService;
58
        $this->StorageMapper = $StorageMapper;
59
        $this->DatasetMapper = $DatasetMapper;
60
        $this->DataloadMapper = $DataloadMapper;
61
        $this->ActivityManager = $ActivityManager;
62
        $this->rootFolder = $rootFolder;
63
        $this->VariableService = $VariableService;
64
    }
65
66
    /**
67
     * get all datasets
68
     *
69
     * @return DataResponse
70
     */
71
    public function index()
72
    {
73
        $ownDatasets = $this->DatasetMapper->index();
74
75
        // get dataload indicators for icons shown in the advanced screen
76
        $dataloads = $this->DataloadMapper->getAllDataloadMetadata();
77
        foreach ($dataloads as $dataload) {
78
            $key = array_search($dataload['dataset'], array_column($ownDatasets, 'id'));
79
            if ($key !== '') {
80
                if ($dataload['schedules'] !== '' and $dataload['schedules'] !== null) {
81
                    $dataload['schedules'] = 1;
82
                } else {
83
                    $dataload['schedules'] = 0;
84
                }
85
                $ownDatasets[$key]['dataloads'] = $dataload['dataloads'];
86
                $ownDatasets[$key]['schedules'] = $dataload['schedules'];
87
            }
88
        }
89
90
        foreach ($ownDatasets as &$ownDataset) {
91
            $ownDataset = $this->VariableService->replaceTextVariables($ownDataset);
92
        }
93
94
        return new DataResponse($ownDatasets);
95
    }
96
97
    /**
98
     * get own dataset details
99
     *
100
     * @param int $datasetId
101
     * @return array
102
     */
103
    public function read(int $datasetId)
104
    {
105
        $ownDataset = $this->DatasetMapper->read($datasetId);
106
        if (!empty($ownDataset)) {
107
            $ownDataset['permissions'] = \OCP\Constants::PERMISSION_UPDATE;
108
        }
109
        return $ownDataset;
110
    }
111
112
    /**
113
     * get own dataset details
114
     *
115
     * @param int $datasetId
116
     * @param string|null $user_id
117
     * @return array
118
     */
119
    public function getOwnDataset(int $datasetId, string $user_id = null)
120
    {
121
        $ownDataset = $this->DatasetMapper->read($datasetId, $user_id);
122
        if (!empty($ownDataset)) {
123
            $ownDataset['permissions'] = \OCP\Constants::PERMISSION_UPDATE;
124
            $ownDataset = $this->VariableService->replaceTextVariables($ownDataset);
125
        }
126
        return $ownDataset;
127
    }
128
129
    /**
130
     * create new dataset
131
     *
132
     * @param $name
133
     * @param $dimension1
134
     * @param $dimension2
135
     * @param $value
136
     * @return int
137
     * @throws \OCP\DB\Exception
138
     */
139
    public function create($name, $dimension1, $dimension2, $value)
140
    {
141
        //$this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
142
        return $this->DatasetMapper->create($name, $dimension1, $dimension2, $value);
143
    }
144
145
    /**
146
     * get dataset details
147
     *
148
     * @param int $datasetId
149
     * @param $name
150
     * @param $subheader
151
     * @param int $parent
152
     * @param int $type
153
     * @param $link
154
     * @param $visualization
155
     * @param $chart
156
     * @param $chartoptions
157
     * @param $dataoptions
158
     * @param $dimension1
159
     * @param $dimension2
160
     * @param $value
161
     * @return bool
162
     */
163
    public function update(int $datasetId, $name, $subheader, int $parent, int $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1 = null, $dimension2 = null, $value = null)
164
    {
165
        if ($type === DatasourceController::DATASET_TYPE_GROUP) {
166
            $parent = 0;
167
        }
168
        return $this->DatasetMapper->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $value);
169
    }
170
171
    /**
172
     * Import Dataset from File
173
     *
174
     * @param string|null $path
175
     * @param string|null $raw
176
     * @return int
177
     * @throws \OCP\Files\NotFoundException
178
     * @throws \OCP\Files\NotPermittedException
179
     */
180
    public function import(string $path = null, string $raw = null)
181
    {
182
        if ($path !== '') {
183
            $file = $this->rootFolder->getUserFolder($this->userId)->get($path);
184
            $data = $file->getContent();
0 ignored issues
show
Bug introduced by
The method getContent() does not exist on OCP\Files\Node. It seems like you code against a sub-type of OCP\Files\Node such as OCP\Files\File. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

184
            /** @scrutinizer ignore-call */ 
185
            $data = $file->getContent();
Loading history...
185
        } else if ($raw !== null) {
186
            $data = $raw;
187
        } else {
188
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
189
        }
190
        $data = json_decode($data, true);
191
192
        $dataset = $data['dataset'];
193
        isset($dataset['name']) ? $name = $dataset['name'] : $name = '';
194
        isset($dataset['subheader']) ? $subheader = $dataset['subheader'] : $subheader = '';
195
        $parent = 0;
196
        isset($dataset['type']) ? $type = $dataset['type'] : $type = null;
197
        isset($dataset['link']) ? $link = $dataset['link'] : $link = null;
198
        isset($dataset['visualization']) ? $visualization = $dataset['visualization'] : $visualization = null;
199
        isset($dataset['chart']) ? $chart = $dataset['chart'] : $chart = null;
200
        isset($dataset['chartoptions']) ? $chartoptions = $dataset['chartoptions'] : $chartoptions = null;
201
        isset($dataset['dataoptions']) ? $dataoptions = $dataset['dataoptions'] : $dataoptions = null;
202
        isset($dataset['filteroptions']) ? $filteroptions = $dataset['filteroptions'] : $filteroptions = null;
203
        isset($dataset['dimension1']) ? $dimension1 = $dataset['dimension1'] : $dimension1 = null;
204
        isset($dataset['dimension2']) ? $dimension2 = $dataset['dimension2'] : $dimension2 = null;
205
        isset($dataset['value']) ? $value = $dataset['value'] : $value = null;
206
207
        $datasetId = $this->DatasetMapper->create();
0 ignored issues
show
Bug introduced by
The call to OCA\Analytics\Db\DatasetMapper::create() has too few arguments starting with name. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

207
        /** @scrutinizer ignore-call */ 
208
        $datasetId = $this->DatasetMapper->create();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
208
        $this->DatasetMapper->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $value, $filteroptions);
209
210
        foreach ($data['dataload'] as $dataload) {
211
            isset($dataload['datasource']) ? $datasource = $dataload['datasource'] : $datasource = null;
212
            isset($dataload['name']) ? $name = $dataload['name'] : $name = null;
213
            isset($dataload['option']) ? $option = $dataload['option'] : $option = null;
214
            $schedule = null;
215
216
            $dataloadId = $this->DataloadMapper->create($datasetId, $datasource);
217
            $this->DataloadMapper->update($dataloadId, $name, $option, $schedule);
218
        }
219
220
        foreach ($data['threshold'] as $threshold) {
221
            isset($threshold['dimension1']) ? $dimension1 = $threshold['dimension1'] : $dimension1 = null;
222
            isset($threshold['value']) ? $value = $threshold['value'] : $value = null;
223
            isset($threshold['option']) ? $option = $threshold['option'] : $option = null;
224
            isset($threshold['severity']) ? $severity = $threshold['severity'] : $severity = null;
225
            $this->ThresholdService->create($datasetId, $dimension1, $option, $value, $severity);
226
        }
227
228
        foreach ($data['data'] as $dData) {
229
            isset($dData[0]) ? $dimension1 = $dData[0] : $dimension1 = null;
230
            isset($dData[1]) ? $dimension2 = $dData[1] : $dimension2 = null;
231
            isset($dData[2]) ? $value = $dData[2] : $value = null;
232
            $this->StorageMapper->create($datasetId, $dimension1, $dimension2, $value);
233
        }
234
235
        if (isset($data['favorite'])) {
236
            $this->setFavorite($datasetId, $data['favorite']);
0 ignored issues
show
Bug introduced by
The method setFavorite() does not exist on OCA\Analytics\Service\DatasetService. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

236
            $this->/** @scrutinizer ignore-call */ 
237
                   setFavorite($datasetId, $data['favorite']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
237
        }
238
239
        return $datasetId;
240
    }
241
242
    /**
243
     * Export Dataset
244
     *
245
     * @param int $datasetId
246
     * @return DataDownloadResponse
247
     */
248
    public function export(int $datasetId)
249
    {
250
        $result = array();
251
        $result['dataset'] = $this->DatasetMapper->read($datasetId);
252
        $result['dataload'] = $this->DataloadMapper->read($datasetId);
253
        $result['threshold'] = $this->ThresholdService->read($datasetId);
254
        $result['favorite'] = '';
255
256
        if ($result['dataset']['type'] === DatasourceController::DATASET_TYPE_INTERNAL_DB) {
257
            $result['data'] = $this->StorageMapper->read($datasetId);
258
        }
259
260
        unset($result['dataset']['id'], $result['dataset']['user_id'], $result['dataset']['user_id'], $result['dataset']['parent']);
261
        $data = json_encode($result);
262
        return new DataDownloadResponse($data, $result['dataset']['name'] . '.export.txt', 'text/plain; charset=utf-8');
263
    }
264
265
    /**
266
     * Delete Dataset and all depending objects
267
     *
268
     * @param int $datasetId
269
     * @return bool
270
     */
271
    public function delete(int $datasetId)
272
    {
273
        $this->ShareService->deleteShareByReport($datasetId);
274
        $this->StorageMapper->deleteByDataset($datasetId);
275
        $this->DatasetMapper->delete($datasetId);
276
        $this->ThresholdService->deleteThresholdByReport($datasetId);
277
        $this->DataloadMapper->deleteDataloadByDataset($datasetId);
278
        $this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_DELETE);
279
        $this->setFavorite($datasetId, 'false');
280
        return true;
281
    }
282
283
    /**
284
     * get dataset details
285
     *
286
     * @param int $datasetId
287
     * @param $chartoptions
288
     * @param $dataoptions
289
     * @param $filteroptions
290
     * @return bool
291
     */
292
    public function updateOptions(int $datasetId, $chartoptions, $dataoptions, $filteroptions)
293
    {
294
        return $this->DatasetMapper->updateOptions($datasetId, $chartoptions, $dataoptions, $filteroptions);
295
    }
296
}