Passed
Push — master ( 71a7b3...caa7e7 )
by Marcel
06:56
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
24
class DatasetController extends Controller
25
{
26
    private $logger;
27
    private $ShareController;
28
    private $StorageMapper;
29
    private $DatasetMapper;
30
    private $ThresholdMapper;
31
    private $DataloadMapper;
32
    private $ActivityManager;
33
34
    public function __construct(
35
        $appName,
36
        IRequest $request,
37
        ILogger $logger,
38
        ShareController $ShareController,
39
        StorageMapper $StorageMapper,
40
        DatasetMapper $DatasetMapper,
41
        ThresholdMapper $ThresholdMapper,
42
        DataloadMapper $DataloadMapper,
43
        ActivityManager $ActivityManager
44
    )
45
    {
46
        parent::__construct($appName, $request);
47
        $this->logger = $logger;
48
        $this->ShareController = $ShareController;
49
        $this->StorageMapper = $StorageMapper;
50
        $this->DatasetMapper = $DatasetMapper;
51
        $this->ThresholdMapper = $ThresholdMapper;
52
        $this->DataloadMapper = $DataloadMapper;
53
        $this->ActivityManager = $ActivityManager;
54
    }
55
56
    /**
57
     * get all datasets
58
     *
59
     * @NoAdminRequired
60
     */
61
    public function index()
62
    {
63
        $ownDatasets = $this->DatasetMapper->getDatasets();
64
        $sharedDatasets = $this->ShareController->getSharedDatasets();
65
        $ownDatasets = array_merge($ownDatasets, $sharedDatasets);
66
67
        $dataloads = $this->DataloadMapper->getAllDataloadMetadata();
68
        foreach ($dataloads as $dataload) {
69
            $key = array_search($dataload['dataset'], array_column($ownDatasets, 'id'));
70
            //$this->logger->debug($key);
71
            if ($key !== '') {
72
                if ($dataload['schedules'] !== '' and $dataload['schedules'] !== null) {
73
                    $dataload['schedules'] = 1;
74
                } else {
75
                    $dataload['schedules'] = 0;
76
                }
77
                $ownDatasets[$key]['dataloads'] = $dataload['dataloads'];
78
                $ownDatasets[$key]['schedules'] = $dataload['schedules'];
79
            }
80
        }
81
        return new DataResponse($ownDatasets);
82
    }
83
84
    /**
85
     * get own dataset details
86
     *
87
     * @NoAdminRequired
88
     * @param int $datasetId
89
     * @return array
90
     */
91
    public function read(int $datasetId)
92
    {
93
        return $this->getOwnDataset($datasetId);
94
    }
95
96
    /**
97
     * get own dataset details
98
     *
99
     * @NoAdminRequired
100
     * @param int $datasetId
101
     * @param string|null $user_id
102
     * @return array
103
     */
104
    public function getOwnDataset(int $datasetId, string $user_id = null)
105
    {
106
        return $this->DatasetMapper->getOwnDataset($datasetId, $user_id);
107
    }
108
109
    /**
110
     * create new dataset
111
     *
112
     * @NoAdminRequired
113
     * @param string $file
114
     * @param string $link
115
     * @return int
116
     */
117
    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

117
    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...
118
    {
119
        //$this->logger->error('datasetcontroller 82: '.$file);
120
        $this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
121
        $datasetId = $this->DatasetMapper->createDataset();
122
123
        if ($file === 'DEMO') {
124
            $name = 'Demo Report';
125
            $subheader = 'CSV Demo Data from GitHub';
126
            $parent = 0;
127
            $type = DataSourceController::DATASET_TYPE_EXTERNAL_FILE;
128
            $link = 'https://raw.githubusercontent.com/Rello/analytics/master/sample_data/sample1.csv';
129
            $visualization = 'ct';
130
            $chart = 'line';
131
            $this->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, '', '', '', '', '');
132
        } elseif ($file !== '') {
133
            $name = explode('.', end(explode('/', $file)))[0];
134
            $subheader = $file;
135
            $parent = 0;
136
            $type = DataSourceController::DATASET_TYPE_INTERNAL_FILE;
137
            $link = $file;
138
            $visualization = 'table';
139
            $chart = 'line';
140
            $this->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, '', '', '', '', '');
141
        }
142
        return $datasetId;
143
    }
144
145
    /**
146
     * Delete Dataset and all depending objects
147
     *
148
     * @NoAdminRequired
149
     * @param int $datasetId
150
     * @return bool
151
     */
152
    public function delete(int $datasetId)
153
    {
154
        $this->ShareController->deleteShareByDataset($datasetId);
155
        $this->StorageMapper->deleteDataByDataset($datasetId);
156
        $this->DatasetMapper->deleteDataset($datasetId);
157
        $this->ThresholdMapper->deleteThresholdByDataset($datasetId);
158
        $this->DataloadMapper->deleteDataloadByDataset($datasetId);
159
        $this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_DELETE);
160
        return true;
161
    }
162
163
    /**
164
     * get dataset details
165
     *
166
     * @NoAdminRequired
167
     * @param int $datasetId
168
     * @param $name
169
     * @param $subheader
170
     * @param int $parent
171
     * @param int $type
172
     * @param $link
173
     * @param $visualization
174
     * @param $chart
175
     * @param $chartoptions
176
     * @param $dataoptions
177
     * @param $dimension1
178
     * @param $dimension2
179
     * @param $dimension3
180
     * @return bool
181
     */
182
    public function update(int $datasetId, $name, $subheader, int $parent, int $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $dimension3)
183
    {
184
        if ($type === DataSourceController::DATASET_TYPE_GROUP) {
185
            $parent = 0;
186
        }
187
        return $this->DatasetMapper->updateDataset($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $dimension3);
188
    }
189
190
    /**
191
     * get dataset details
192
     *
193
     * @NoAdminRequired
194
     * @param int $datasetId
195
     * @param $chartoptions
196
     * @param $dataoptions
197
     * @return bool
198
     */
199
    public function updateOptions(int $datasetId, $chartoptions, $dataoptions)
200
    {
201
        return $this->DatasetMapper->updateDatasetOptions($datasetId, $chartoptions, $dataoptions);
202
    }
203
}