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