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\Db\DatasetMapper; |
15
|
|
|
use OCA\Analytics\Db\ThresholdMapper; |
16
|
|
|
use OCA\Analytics\Notification\NotificationManager; |
17
|
|
|
use OCP\AppFramework\Controller; |
18
|
|
|
use OCP\ILogger; |
19
|
|
|
use OCP\IRequest; |
20
|
|
|
|
21
|
|
|
class ThresholdController extends Controller |
22
|
|
|
{ |
23
|
|
|
private $logger; |
24
|
|
|
private $ThresholdMapper; |
25
|
|
|
private $DatasetMapper; |
26
|
|
|
private $NotificationManager; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
$appName, |
30
|
|
|
IRequest $request, |
31
|
|
|
ILogger $logger, |
32
|
|
|
ThresholdMapper $ThresholdMapper, |
33
|
|
|
NotificationManager $NotificationManager, |
34
|
|
|
DatasetMapper $DatasetMapper |
35
|
|
|
) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($appName, $request); |
38
|
|
|
$this->logger = $logger; |
39
|
|
|
$this->ThresholdMapper = $ThresholdMapper; |
40
|
|
|
$this->NotificationManager = $NotificationManager; |
41
|
|
|
$this->DatasetMapper = $DatasetMapper; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* read all thresholds for a dataset |
46
|
|
|
* |
47
|
|
|
* @NoAdminRequired |
48
|
|
|
* @param int $datasetId |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function read(int $datasetId) |
52
|
|
|
{ |
53
|
|
|
return $this->ThresholdMapper->getThresholdsByDataset($datasetId); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* create new threshold for dataset |
58
|
|
|
* |
59
|
|
|
* @NoAdminRequired |
60
|
|
|
* @param int $datasetId |
61
|
|
|
* @param $dimension1 |
62
|
|
|
* @param $option |
63
|
|
|
* @param $dimension3 |
64
|
|
|
* @param int $severity |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
|
|
public function create(int $datasetId, $dimension1, $option, $dimension3, int $severity) |
68
|
|
|
{ |
69
|
|
|
$dimension3 = $this->floatvalue($dimension3); |
70
|
|
|
return $this->ThresholdMapper->createThreshold($datasetId, $dimension1, $dimension3, $option, $severity); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Delete threshold for dataset |
75
|
|
|
* |
76
|
|
|
* @NoAdminRequired |
77
|
|
|
* @param int $thresholdId |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function delete(int $thresholdId) |
81
|
|
|
{ |
82
|
|
|
$this->ThresholdMapper->deleteThreshold($thresholdId); |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* validate threshold |
88
|
|
|
* |
89
|
|
|
* @NoAdminRequired |
90
|
|
|
* @param int $datasetId |
91
|
|
|
* @param $dimension1 |
92
|
|
|
* @param $dimension2 |
93
|
|
|
* @param $dimension3 |
94
|
|
|
* @return string |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
|
|
public function validate(int $datasetId, $dimension1, $dimension2, $dimension3) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$result = ''; |
100
|
|
|
$thresholds = $this->ThresholdMapper->getSevOneThresholdsByDataset($datasetId); |
101
|
|
|
$datasetMetadata = $this->DatasetMapper->getDatasetOptions($datasetId); |
102
|
|
|
|
103
|
|
|
foreach ($thresholds as $threshold) { |
104
|
|
|
//$this->logger->error('ThresholdController 104: ' . $threshold['dimension3'].'==='.$threshold['option'].'==='.$dimension3); |
105
|
|
|
if ($threshold['dimension1'] === $dimension1 or $threshold['dimension1'] === '*') { |
106
|
|
|
if (version_compare($dimension3, $threshold['dimension3'], $threshold['option'])) { |
107
|
|
|
$this->NotificationManager->triggerNotification(NotificationManager::SUBJECT_THRESHOLD, $datasetId, $threshold['id'], ['report' => $datasetMetadata['name'], 'subject' => $dimension1, 'rule' => $threshold['option'], 'value' => $threshold['dimension3']], $datasetMetadata['user_id']); |
108
|
|
|
$result = 'Threshold value met'; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function floatvalue($val) |
116
|
|
|
{ |
117
|
|
|
$val = str_replace(",", ".", $val); |
118
|
|
|
$val = preg_replace('/\.(?=.*\.)/', '', $val); |
119
|
|
|
$val = preg_replace('/[^0-9-.]+/', '', $val); |
120
|
|
|
if (is_numeric($val)) { |
121
|
|
|
return number_format(floatval($val), 2, '.', ''); |
122
|
|
|
} else { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.