Passed
Push — master ( dfddba...5403dd )
by Marcel
03:03
created

DataloadService::replaceDimension()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 14
rs 9.6111
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\Service;
13
14
use Exception;
15
use OCA\Analytics\Activity\ActivityManager;
16
use OCA\Analytics\Controller\DatasourceController;
17
use OCA\Analytics\Controller\StorageController;
18
use OCA\Analytics\Db\DataloadMapper;
19
use OCP\AppFramework\Http\NotFoundResponse;
20
use OCP\Files\NotFoundException;
21
use OCP\IL10N;
22
use OCP\ILogger;
23
24
class DataloadService
25
{
26
    private $logger;
27
    private $StorageController;
28
    private $DatasourceController;
29
    private $ActivityManager;
30
    private $DatasetService;
31
    private $l10n;
32
    private $DataloadMapper;
33
34
    public function __construct(
35
        IL10N $l10n,
36
        ILogger $logger,
37
        ActivityManager $ActivityManager,
38
        DatasourceController $DatasourceController,
39
        DatasetService $DatasetService,
40
        StorageController $StorageController,
41
        DataloadMapper $DataloadMapper
42
    )
43
    {
44
        $this->l10n = $l10n;
45
        $this->logger = $logger;
46
        $this->StorageController = $StorageController;
47
        $this->ActivityManager = $ActivityManager;
48
        $this->DatasourceController = $DatasourceController;
49
        $this->DatasetService = $DatasetService;
50
        $this->DataloadMapper = $DataloadMapper;
51
    }
52
53
    // Dataloads
54
    // Dataloads
55
    // Dataloads
56
57
    /**
58
     * create a new dataload
59
     *
60
     * @param int $datasetId
61
     * @param int $datasourceId
62
     * @return int
63
     */
64
    public function create(int $datasetId, int $datasourceId)
65
    {
66
        return $this->DataloadMapper->create($datasetId, $datasourceId);
67
    }
68
69
    /**
70
     * get all dataloads for a dataset
71
     *
72
     * @param int $datasetId
73
     * @return array
74
     */
75
    public function read(int $datasetId)
76
    {
77
        return $this->DataloadMapper->read($datasetId);
78
    }
79
80
    /**
81
     * update dataload
82
     *
83
     * @param int $dataloadId
84
     * @param $name
85
     * @param $option
86
     * @param $schedule
87
     * @return bool
88
     */
89
    public function update(int $dataloadId, $name, $option, $schedule)
90
    {
91
        return $this->DataloadMapper->update($dataloadId, $name, $option, $schedule);
92
    }
93
94
    /**
95
     * delete a dataload
96
     *
97
     * @param int $dataloadId
98
     * @return bool
99
     */
100
    public function delete(int $dataloadId)
101
    {
102
        return $this->DataloadMapper->delete($dataloadId);
103
    }
104
105
    /**
106
     * execute all dataloads depending on their schedule
107
     * daily or hourly
108
     *
109
     * @param $schedule
110
     * @return void
111
     * @throws Exception
112
     */
113
    public function executeBySchedule($schedule)
114
    {
115
        $schedules = $this->DataloadMapper->getDataloadBySchedule($schedule);
116
        //$this->logger->debug('DataLoadController 145: execute schedule '.$schedule);
117
        foreach ($schedules as $dataload) {
118
            //$this->logger->debug('DataLoadController 147: execute dataload '.$dataload['id']);
119
            $this->execute($dataload['id']);
120
        }
121
    }
122
123
    /**
124
     * execute a dataload from datasource and store into dataset
125
     *
126
     * @param int $dataloadId
127
     * @return array
128
     * @throws Exception
129
     */
130
    public function execute(int $dataloadId)
131
    {
132
        $dataloadMetadata = $this->DataloadMapper->getDataloadById($dataloadId);
133
        $result = $this->getDataFromDatasource($dataloadId);
134
        $insert = $update = 0;
135
        $datasetId = $result['datasetId'];
136
        $option = json_decode($dataloadMetadata['option'], true);
137
138
        if (isset($option['delete']) and $option['delete'] === 'true') {
139
            $this->StorageController->delete($datasetId, '*', '*');
140
        }
141
        if ($result['error'] === 0) {
142
            foreach ($result['data'] as &$row) {
143
                if (count($row) === 2) {
144
                    // if datasource only delivers 2 colums, the value needs to be in the last one
145
                    $row[2] = $row[1];
146
                    $row[1] = null;
147
                }
148
                $action = $this->StorageController->update($datasetId, $row[0], $row[1], $row[2], $dataloadMetadata['user_id']);
149
                $insert = $insert + $action['insert'];
150
                $update = $update + $action['update'];
151
            }
152
        }
153
154
        $result = [
155
            'insert' => $insert,
156
            'update' => $update,
157
            'error' => $result['error'],
158
        ];
159
160
        if ($result['error'] === 0) $this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_DATALOAD, $dataloadMetadata['user_id']);
161
162
        return $result;
163
    }
164
165
    /**
166
     * get the data from datasource
167
     * to be used in simulation or execution
168
     *
169
     * @param int $dataloadId
170
     * @return array|NotFoundResponse
171
     * @throws NotFoundResponse|NotFoundException
172
     */
173
    public function getDataFromDatasource(int $dataloadId)
174
    {
175
        $dataloadMetadata = $this->DataloadMapper->getDataloadById($dataloadId);
176
        $datasetMetadata = $this->DatasetService->getOwnDataset($dataloadMetadata['dataset'], $dataloadMetadata['user_id']);
177
178
        if (!empty($datasetMetadata)) {
179
            $option = json_decode($dataloadMetadata['option'], true);
180
            $option['user_id'] = $dataloadMetadata['user_id'];
181
182
            //$this->logger->debug('DataLoadController 187: ' . $dataloadMetadata['option'] . '---' . json_encode($option));
183
            $result = $this->DatasourceController->read((int)$dataloadMetadata['datasource'], $option);
184
            $result['datasetId'] = $dataloadMetadata['dataset'];
185
186
            return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type OCP\Files\NotFoundException which is incompatible with the documented return type OCP\AppFramework\Http\NotFoundResponse|array.
Loading history...
187
        } else {
188
            return new NotFoundResponse();
189
        }
190
    }
191
}