1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Data 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 2019 Marcel Scherello |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Controller; |
13
|
|
|
|
14
|
|
|
use OCA\Analytics\Activity\ActivityManager; |
15
|
|
|
use OCP\AppFramework\Controller; |
16
|
|
|
use OCP\AppFramework\Http\DataResponse; |
17
|
|
|
use OCP\AppFramework\Http\NotFoundResponse; |
18
|
|
|
use OCP\Files\NotFoundException; |
19
|
|
|
use OCP\ILogger; |
20
|
|
|
use OCP\IRequest; |
21
|
|
|
|
22
|
|
|
class DataLoadController extends Controller |
23
|
|
|
{ |
24
|
|
|
private $logger; |
25
|
|
|
private $StorageController; |
26
|
|
|
private $DataSourceController; |
27
|
|
|
private $userId; |
28
|
|
|
private $ActivityManager; |
29
|
|
|
private $DatasetController; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
string $AppName, |
33
|
|
|
IRequest $request, |
34
|
|
|
$userId, |
35
|
|
|
ILogger $logger, |
36
|
|
|
ActivityManager $ActivityManager, |
37
|
|
|
DataSourceController $DataSourceController, |
38
|
|
|
DatasetController $DatasetController, |
39
|
|
|
StorageController $StorageController |
40
|
|
|
) |
41
|
|
|
{ |
42
|
|
|
parent::__construct($AppName, $request); |
43
|
|
|
$this->userId = $userId; |
44
|
|
|
$this->logger = $logger; |
45
|
|
|
$this->StorageController = $StorageController; |
46
|
|
|
$this->ActivityManager = $ActivityManager; |
47
|
|
|
$this->DataSourceController = $DataSourceController; |
48
|
|
|
$this->DatasetController = $DatasetController; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* update data from input form |
53
|
|
|
* |
54
|
|
|
* @NoAdminRequired |
55
|
|
|
* @param int $datasetId |
56
|
|
|
* @param $dimension1 |
57
|
|
|
* @param $dimension2 |
58
|
|
|
* @param $dimension3 |
59
|
|
|
* @return DataResponse|NotFoundResponse |
60
|
|
|
*/ |
61
|
|
|
public function update(int $datasetId, $dimension1, $dimension2, $dimension3) |
62
|
|
|
{ |
63
|
|
|
//$this->NotificationManager->triggerNotification(NotificationManager::OBJECT_DATASET, $datasetId, NotificationManager::SUBJECT_THRESHOLD, ['subject' => 'Pflanze', 'rule' => 'Hum too low']); |
64
|
|
|
//disabled for the moment |
65
|
|
|
$datasetMetadata = $this->DatasetController->getOwnDataset($datasetId); |
66
|
|
|
if (!empty($datasetMetadata)) { |
67
|
|
|
$insert = $update = 0; |
68
|
|
|
$dimension3 = str_replace(',', '.', $dimension3); |
69
|
|
|
$action = $this->StorageController->update($datasetId, $dimension1, $dimension2, $dimension3); |
70
|
|
|
$insert = $insert + $action['insert']; |
71
|
|
|
$update = $update + $action['update']; |
72
|
|
|
|
73
|
|
|
$result = [ |
74
|
|
|
'insert' => $insert, |
75
|
|
|
'update' => $update |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD); |
79
|
|
|
return new DataResponse($result); |
80
|
|
|
} else { |
81
|
|
|
return new NotFoundResponse(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* update data from input form |
87
|
|
|
* |
88
|
|
|
* @NoAdminRequired |
89
|
|
|
* @param int $datasetId |
90
|
|
|
* @param $dimension1 |
91
|
|
|
* @param $dimension2 |
92
|
|
|
* @return DataResponse|NotFoundResponse |
93
|
|
|
*/ |
94
|
|
|
public function delete(int $datasetId, $dimension1, $dimension2) |
95
|
|
|
{ |
96
|
|
|
$datasetMetadata = $this->DatasetController->getOwnDataset($datasetId); |
97
|
|
|
if (!empty($datasetMetadata)) { |
98
|
|
|
$result = $this->StorageController->delete($datasetId, $dimension1, $dimension2); |
99
|
|
|
return new DataResponse($result); |
|
|
|
|
100
|
|
|
} else { |
101
|
|
|
return new NotFoundResponse(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Import clipboard data |
107
|
|
|
* |
108
|
|
|
* @NoAdminRequired |
109
|
|
|
* @param int $datasetId |
110
|
|
|
* @param $import |
111
|
|
|
* @return DataResponse|NotFoundResponse |
112
|
|
|
*/ |
113
|
|
|
public function importClipboard($datasetId, $import) |
114
|
|
|
{ |
115
|
|
|
$datasetMetadata = $this->DatasetController->getOwnDataset($datasetId); |
116
|
|
|
if (!empty($datasetMetadata)) { |
117
|
|
|
$insert = $update = 0; |
118
|
|
|
$delimiter = $this->detectDelimiter($import); |
119
|
|
|
$rows = str_getcsv($import, "\n"); |
120
|
|
|
|
121
|
|
|
foreach ($rows as &$row) { |
122
|
|
|
$row = str_getcsv($row, $delimiter); |
123
|
|
|
$row[2] = str_replace(',', '.', $row[2]); |
124
|
|
|
$action = $this->StorageController->update($datasetId, $row[0], $row[1], $row[2]); |
125
|
|
|
$insert = $insert + $action['insert']; |
126
|
|
|
$update = $update + $action['update']; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$result = [ |
130
|
|
|
'insert' => $insert, |
131
|
|
|
'update' => $update, |
132
|
|
|
'delimiter' => $delimiter |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_IMPORT); |
136
|
|
|
return new DataResponse($result); |
137
|
|
|
} else { |
138
|
|
|
return new NotFoundResponse(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function detectDelimiter($data) |
143
|
|
|
{ |
144
|
|
|
$delimiters = ["\t", ";", "|", ","]; |
145
|
|
|
$data_2 = null; |
146
|
|
|
$delimiter = $delimiters[0]; |
147
|
|
|
foreach ($delimiters as $d) { |
148
|
|
|
$firstRow = str_getcsv($data, "\n")[0]; |
149
|
|
|
$data_1 = str_getcsv($firstRow, $d); |
150
|
|
|
if (sizeof($data_1) > sizeof($data_2)) { |
151
|
|
|
$delimiter = $d; |
152
|
|
|
$data_2 = $data_1; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
return $delimiter; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Import data into dataset from an internal or external file |
160
|
|
|
* |
161
|
|
|
* @NoAdminRequired |
162
|
|
|
* @param int $datasetId |
163
|
|
|
* @param $path |
164
|
|
|
* @return DataResponse|NotFoundResponse |
165
|
|
|
* @throws NotFoundException |
166
|
|
|
*/ |
167
|
|
|
public function importFile(int $datasetId, $path) |
168
|
|
|
{ |
169
|
|
|
//$this->logger->error('DataLoadController 100:'.$datasetId. $path); |
170
|
|
|
$datasetMetadata = $this->DatasetController->getOwnDataset($datasetId); |
171
|
|
|
if (!empty($datasetMetadata)) { |
172
|
|
|
$insert = $update = 0; |
173
|
|
|
$datasetMetadata['type'] = DataSourceController::DATASET_TYPE_INTERNAL_FILE; |
174
|
|
|
$result = $this->DataSourceController->read($datasetMetadata, $path); |
175
|
|
|
|
176
|
|
|
foreach ($result['data'] as &$row) { |
177
|
|
|
$action = $this->StorageController->update($datasetId, $row['dimension1'], $row['dimension2'], $row['dimension3']); |
178
|
|
|
$insert = $insert + $action['insert']; |
179
|
|
|
$update = $update + $action['update']; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$result = [ |
183
|
|
|
'insert' => $insert, |
184
|
|
|
'update' => $update |
185
|
|
|
]; |
186
|
|
|
|
187
|
|
|
$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_IMPORT); |
188
|
|
|
return new DataResponse($result); |
189
|
|
|
} else { |
190
|
|
|
return new NotFoundResponse(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |