Passed
Push — master ( 2acc11...9ba60a )
by Marcel
02:36
created

DatasetController::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\Activity\ActivityManager;
15
use OCA\Analytics\Db\DataloadMapper;
16
use OCA\Analytics\Db\DatasetMapper;
17
use OCA\Analytics\Db\StorageMapper;
18
use OCA\Analytics\Db\ThresholdMapper;
19
use OCP\AppFramework\Controller;
20
use OCP\AppFramework\Http\DataResponse;
21
use OCP\ILogger;
22
use OCP\IRequest;
23
use OCP\ITagManager;
24
25
class DatasetController extends Controller
26
{
27
    private $logger;
28
    private $tagManager;
29
    private $ShareController;
30
    private $StorageMapper;
31
    private $DatasetMapper;
32
    private $ThresholdMapper;
33
    private $DataloadMapper;
34
    private $ActivityManager;
35
36
    public function __construct(
37
        $appName,
38
        IRequest $request,
39
        ILogger $logger,
40
        ITagManager $tagManager,
41
        ShareController $ShareController,
42
        StorageMapper $StorageMapper,
43
        DatasetMapper $DatasetMapper,
44
        ThresholdMapper $ThresholdMapper,
45
        DataloadMapper $DataloadMapper,
46
        ActivityManager $ActivityManager
47
    )
48
    {
49
        parent::__construct($appName, $request);
50
        $this->logger = $logger;
51
        $this->tagManager = $tagManager;
52
        $this->ShareController = $ShareController;
53
        $this->StorageMapper = $StorageMapper;
54
        $this->DatasetMapper = $DatasetMapper;
55
        $this->ThresholdMapper = $ThresholdMapper;
56
        $this->DataloadMapper = $DataloadMapper;
57
        $this->ActivityManager = $ActivityManager;
58
    }
59
60
    /**
61
     * get all datasets
62
     *
63
     * @NoAdminRequired
64
     */
65
    public function index()
66
    {
67
        $ownDatasets = $this->DatasetMapper->getDatasets();
68
        $sharedDatasets = $this->ShareController->getSharedDatasets();
69
        $ownDatasets = array_merge($ownDatasets, $sharedDatasets);
70
        $favorites = $this->tagManager->load('analytics')->getFavorites();
71
72
        $dataloads = $this->DataloadMapper->getAllDataloadMetadata();
73
        foreach ($dataloads as $dataload) {
74
            $key = array_search($dataload['dataset'], array_column($ownDatasets, 'id'));
75
            //$this->logger->debug($key);
76
            if ($key !== '') {
77
                if ($dataload['schedules'] !== '' and $dataload['schedules'] !== null) {
78
                    $dataload['schedules'] = 1;
79
                } else {
80
                    $dataload['schedules'] = 0;
81
                }
82
                $ownDatasets[$key]['dataloads'] = $dataload['dataloads'];
83
                $ownDatasets[$key]['schedules'] = $dataload['schedules'];
84
            }
85
        }
86
87
        foreach ($ownDatasets as &$ownDataset) {
88
            $hasTag = 0;
89
            if (is_array($favorites) and in_array($ownDataset['id'], $favorites)) {
90
                $hasTag = 1;
91
            }
92
            $ownDataset['favorite'] = $hasTag;
93
        }
94
95
        return new DataResponse($ownDatasets);
96
    }
97
98
    /**
99
     * get own dataset details
100
     *
101
     * @NoAdminRequired
102
     * @param int $datasetId
103
     * @return array
104
     */
105
    public function read(int $datasetId)
106
    {
107
        return $this->getOwnDataset($datasetId);
108
    }
109
110
    /**
111
     * get own dataset details
112
     *
113
     * @NoAdminRequired
114
     * @param int $datasetId
115
     * @param string|null $user_id
116
     * @return array
117
     */
118
    public function getOwnDataset(int $datasetId, string $user_id = null)
119
    {
120
        return $this->DatasetMapper->getOwnDataset($datasetId, $user_id);
121
    }
122
123
    /**
124
     * get own datasets which are marked as favorites
125
     *
126
     * @NoAdminRequired
127
     * @return array
128
     */
129
    public function getOwnFavoriteDatasets()
130
    {
131
        return $this->tagManager->load('analytics')->getFavorites();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->tagManager...ytics')->getFavorites() could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
132
    }
133
134
    /**
135
     * set/remove the favorite flag for a report
136
     *
137
     * @NoAdminRequired
138
     * @param int $datasetId
139
     * @param string $favorite
140
     * @return bool
141
     */
142
    public function setFavorite(int $datasetId, string $favorite)
143
    {
144
        if ($favorite === 'true') {
145
            $return = $this->tagManager->load('analytics')->addToFavorites($datasetId);
146
        } else {
147
            $return = $this->tagManager->load('analytics')->removeFromFavorites($datasetId);
148
        }
149
        return $return;
150
    }
151
152
    /**
153
     * create new dataset
154
     *
155
     * @NoAdminRequired
156
     * @param string $file
157
     * @param string $link
158
     * @return int
159
     */
160
    public function create($file = '', $link = '')
0 ignored issues
show
Unused Code introduced by
The parameter $link is not used and could be removed. ( Ignorable by Annotation )

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

160
    public function create($file = '', /** @scrutinizer ignore-unused */ $link = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
161
    {
162
        //$this->logger->error('datasetcontroller 82: '.$file);
163
        $this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
164
        $datasetId = $this->DatasetMapper->createDataset();
165
166
        if ($file === 'DEMO') {
167
            $name = 'Demo Report';
168
            $subheader = 'CSV Demo Data from GitHub';
169
            $parent = 0;
170
            $type = DataSourceController::DATASET_TYPE_EXTERNAL_FILE;
171
            $link = 'https://raw.githubusercontent.com/Rello/analytics/master/sample_data/sample1.csv';
172
            $visualization = 'ct';
173
            $chart = 'line';
174
            $this->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, '', '');
175
        } elseif ($file !== '') {
176
            $name = explode('.', end(explode('/', $file)))[0];
177
            $subheader = $file;
178
            $parent = 0;
179
            $type = DataSourceController::DATASET_TYPE_INTERNAL_FILE;
180
            $link = $file;
181
            $visualization = 'table';
182
            $chart = 'line';
183
            $this->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, '', '');
184
        }
185
        return $datasetId;
186
    }
187
188
    /**
189
     * Delete Dataset and all depending objects
190
     *
191
     * @NoAdminRequired
192
     * @param int $datasetId
193
     * @return bool
194
     */
195
    public function delete(int $datasetId)
196
    {
197
        $this->ShareController->deleteShareByDataset($datasetId);
198
        $this->StorageMapper->deleteDataByDataset($datasetId);
199
        $this->DatasetMapper->deleteDataset($datasetId);
200
        $this->ThresholdMapper->deleteThresholdByDataset($datasetId);
201
        $this->DataloadMapper->deleteDataloadByDataset($datasetId);
202
        $this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_DELETE);
203
        return true;
204
    }
205
206
    /**
207
     * get dataset details
208
     *
209
     * @NoAdminRequired
210
     * @param int $datasetId
211
     * @param $name
212
     * @param $subheader
213
     * @param int $parent
214
     * @param int $type
215
     * @param $link
216
     * @param $visualization
217
     * @param $chart
218
     * @param $chartoptions
219
     * @param $dataoptions
220
     * @param $dimension1
221
     * @param $dimension2
222
     * @param $value
223
     * @return bool
224
     */
225
    public function update(int $datasetId, $name, $subheader, int $parent, int $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1 = null, $dimension2 = null, $value = null)
226
    {
227
        if ($type === DataSourceController::DATASET_TYPE_GROUP) {
228
            $parent = 0;
229
        }
230
        return $this->DatasetMapper->updateDataset($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $value);
231
    }
232
233
    /**
234
     * get dataset details
235
     *
236
     * @NoAdminRequired
237
     * @param int $datasetId
238
     * @param $chartoptions
239
     * @param $dataoptions
240
     * @param $filteroptions
241
     * @return bool
242
     */
243
    public function updateOptions(int $datasetId, $chartoptions, $dataoptions, $filteroptions)
244
    {
245
        return $this->DatasetMapper->updateDatasetOptions($datasetId, $chartoptions, $dataoptions, $filteroptions);
246
    }
247
}