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\Controller; |
13
|
|
|
|
14
|
|
|
use OCA\Analytics\Db\StorageMapper; |
15
|
|
|
use OCA\Analytics\Service\ThresholdService; |
16
|
|
|
use OCP\AppFramework\Controller; |
17
|
|
|
use OCP\ILogger; |
18
|
|
|
use OCP\IRequest; |
19
|
|
|
|
20
|
|
|
class StorageController extends Controller |
21
|
|
|
{ |
22
|
|
|
private $logger; |
23
|
|
|
private $StorageMapper; |
24
|
|
|
private $ThresholdService; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
string $AppName, |
28
|
|
|
IRequest $request, |
29
|
|
|
ILogger $logger, |
30
|
|
|
StorageMapper $StorageMapper, |
31
|
|
|
ThresholdService $ThresholdService |
32
|
|
|
) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($AppName, $request); |
35
|
|
|
$this->logger = $logger; |
36
|
|
|
$this->StorageMapper = $StorageMapper; |
37
|
|
|
$this->ThresholdService = $ThresholdService; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the items for the selected category |
42
|
|
|
* |
43
|
|
|
* @NoAdminRequired |
44
|
|
|
* @param $datasetMetadata |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function read($datasetMetadata) |
48
|
|
|
{ |
49
|
|
|
$availableDimensions = array(); |
50
|
|
|
$header = array(); |
51
|
|
|
|
52
|
|
|
// output the dimensions available for filtering of this datasource |
53
|
|
|
// this needs to map the technical name to its displayname in the report |
54
|
|
|
$availableDimensions['dimension1'] = $datasetMetadata['dimension1']; |
55
|
|
|
$availableDimensions['dimension2'] = $datasetMetadata['dimension2']; |
56
|
|
|
|
57
|
|
|
// return the header texts of the data being transferred according to the current drilldown state selected by user |
58
|
|
|
$options = json_decode($datasetMetadata['filteroptions'], true); |
59
|
|
|
if (!isset($options['drilldown']['dimension1'])) $header[0] = $datasetMetadata['dimension1']; |
60
|
|
|
if (!isset($options['drilldown']['dimension2'])) $header[1] = $datasetMetadata['dimension2']; |
61
|
|
|
$header[6] = $datasetMetadata['value']; |
62
|
|
|
$header = array_values($header); |
63
|
|
|
|
64
|
|
|
$data = $this->StorageMapper->read($datasetMetadata['id'], $options); |
65
|
|
|
$data = array_values($data); |
66
|
|
|
foreach ($data as $key => $value) { |
67
|
|
|
$data[$key] = array_values($value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return empty($data) ? [ |
71
|
|
|
'dimensions' => $availableDimensions, |
72
|
|
|
'status' => 'nodata', |
73
|
|
|
'error' => 0 |
74
|
|
|
] : [ |
75
|
|
|
'header' => $header, |
76
|
|
|
'dimensions' => $availableDimensions, |
77
|
|
|
'data' => $data, |
78
|
|
|
'error' => 0 |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the items for the selected category |
84
|
|
|
* |
85
|
|
|
* @NoAdminRequired |
86
|
|
|
* @param int $datasetId |
87
|
|
|
* @param $dimension1 |
88
|
|
|
* @param $dimension2 |
89
|
|
|
* @param $value |
90
|
|
|
* @param string|null $user_id |
91
|
|
|
* @param null $bulkInsert |
|
|
|
|
92
|
|
|
* @return array |
93
|
|
|
* @throws \Exception |
94
|
|
|
*/ |
95
|
|
|
public function update(int $datasetId, $dimension1, $dimension2, $value, string $user_id = null, $bulkInsert = null) |
96
|
|
|
{ |
97
|
|
|
TODO: |
98
|
|
|
//dates in both columns |
99
|
|
|
$dimension2 = $this->convertGermanDateFormat($dimension2); |
100
|
|
|
//convert date into timestamp |
101
|
|
|
//$timestamp = $this->convertGermanDateFormat($timestamp); |
102
|
|
|
$value = $this->floatvalue($value); |
103
|
|
|
$validate = ''; |
104
|
|
|
$insert = $update = $error = $action = 0; |
|
|
|
|
105
|
|
|
|
106
|
|
|
if ($value !== false) { |
107
|
|
|
try { |
108
|
|
|
$action = $this->StorageMapper->create($datasetId, $dimension1, $dimension2, $value, $user_id, null, $bulkInsert); |
109
|
|
|
} catch (\Exception $e) { |
110
|
|
|
$error = 1; |
111
|
|
|
} |
112
|
|
|
if ($action === 'insert') $insert = 1; |
113
|
|
|
elseif ($action === 'update') $update = 1; |
114
|
|
|
} else { |
115
|
|
|
$error = 1; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($error === 0) $validate = $this->ThresholdService->validate($datasetId, $dimension1, $dimension2, $value); |
119
|
|
|
|
120
|
|
|
return [ |
121
|
|
|
'insert' => $insert, |
122
|
|
|
'update' => $update, |
123
|
|
|
'error' => $error, |
124
|
|
|
'validate' => $validate |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* delete data |
130
|
|
|
* |
131
|
|
|
* @NoAdminRequired |
132
|
|
|
* @param int $datasetId |
133
|
|
|
* @param $dimension1 |
134
|
|
|
* @param $dimension2 |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function delete(int $datasetId, $dimension1, $dimension2) |
138
|
|
|
{ |
139
|
|
|
return $this->StorageMapper->delete($datasetId, $dimension1, $dimension2); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Simulate delete data |
144
|
|
|
* |
145
|
|
|
* @NoAdminRequired |
146
|
|
|
* @param int $datasetId |
147
|
|
|
* @param $dimension1 |
148
|
|
|
* @param $dimension2 |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function deleteSimulate(int $datasetId, $dimension1, $dimension2) |
152
|
|
|
{ |
153
|
|
|
return $this->StorageMapper->deleteSimulate($datasetId, $dimension1, $dimension2); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private function floatvalue($val) |
157
|
|
|
{ |
158
|
|
|
$val = str_replace(",", ".", $val); |
159
|
|
|
$val = preg_replace('/\.(?=.*\.)/', '', $val); |
160
|
|
|
$val = preg_replace('/[^0-9-.]+/', '', $val); |
161
|
|
|
if (is_numeric($val)) { |
162
|
|
|
return number_format(floatval($val), 2, '.', ''); |
163
|
|
|
} else { |
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
private function convertGermanDateFormat($val) |
169
|
|
|
{ |
170
|
|
|
$fullString = explode(' ', $val); |
171
|
|
|
$dateLength = strlen($fullString[0]); |
172
|
|
|
$dateParts = explode('.', $fullString[0]); |
173
|
|
|
|
174
|
|
|
if ($dateLength >= 6 && $dateLength <= 10 && count($dateParts) === 3) { |
175
|
|
|
// is most likely a german date format 20.02.2020 |
176
|
|
|
$fullString[0] = $dateParts[2] . '-' . sprintf('%02d', $dateParts[1]) . '-' . sprintf('%02d', $dateParts[0]); |
177
|
|
|
$val = implode(' ', $fullString); |
178
|
|
|
} |
179
|
|
|
return $val; |
180
|
|
|
} |
181
|
|
|
} |