Issues (496)

lib/Service/ThresholdService.php (3 issues)

1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\Service;
10
11
use OCA\Analytics\Db\ReportMapper;
12
use OCA\Analytics\Db\ThresholdMapper;
13
use OCA\Analytics\Notification\NotificationManager;
14
use Psr\Log\LoggerInterface;
0 ignored issues
show
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use OCP\IL10N;
0 ignored issues
show
The type OCP\IL10N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
class ThresholdService {
18
	private $logger;
19
	private $ThresholdMapper;
20
	private $ReportMapper;
21
	private $NotificationManager;
22
	private $VariableService;
23
	private $l10n;
24
25
	public function __construct(
26
		LoggerInterface $logger,
27
		ThresholdMapper $ThresholdMapper,
28
		NotificationManager $NotificationManager,
29
		ReportMapper $ReportMapper,
30
		VariableService $VariableService,
31
		IL10N $l10n
32
	) {
33
		$this->logger = $logger;
34
		$this->ThresholdMapper = $ThresholdMapper;
35
		$this->NotificationManager = $NotificationManager;
36
		$this->ReportMapper = $ReportMapper;
37
		$this->VariableService = $VariableService;
38
		$this->l10n = $l10n;
39
	}
40
41
	/**
42
	 * read all thresholds for a dataset
43
	 *
44
	 * @param int $reportId
45
	 * @return array
46
	 */
47
	public function read(int $reportId) {
48
		$thresholds = $this->ThresholdMapper->getThresholdsByReport($reportId);
49
		return $this->VariableService->replaceThresholdsVariables($thresholds);
50
	}
51
52
	/**
53
	 * read all thresholds for a dataset without any replaced text variables
54
	 *
55
	 * @param int $reportId
56
	 * @return array
57
	 */
58
	public function readRaw(int $reportId) {
59
		return $this->ThresholdMapper->getThresholdsByReport($reportId);
60
	}
61
62
	/**
63
	 * create new threshold for dataset
64
	 *
65
	 * @param int $reportId
66
	 * @param $dimension1
67
	 * @param $option
68
	 * @param $value
69
	 * @param int $severity
70
	 * @return int
71
	 */
72
	public function create(int $reportId, $dimension1, $option, $value, int $severity) {
73
		$value = $this->floatvalue($value);
74
		return $this->ThresholdMapper->create($reportId, $dimension1, $value, $option, $severity);
75
	}
76
77
	private function floatvalue($val) {
78
		// if value is a 3 digit comma number with one leading zero like 0,111, it should not go through the 1000 separator removal
79
		if (preg_match('/(?<=\b0)\,(?=\d{3}\b)/', $val) === 0 && preg_match('/(?<=\b0)\.(?=\d{3}\b)/', $val) === 0) {
80
			// remove , as 1000 separator
81
			$val = preg_replace('/(?<=\d)\,(?=\d{3}\b)/', '', $val);
82
			// remove . as 1000 separator
83
			$val = preg_replace('/(?<=\d)\.(?=\d{3}\b)/', '', $val);
84
		}
85
		// convert remaining comma to decimal point
86
		$val = str_replace(",", ".", $val);
87
		if (is_numeric($val)) {
88
			return number_format(floatval($val), 2, '.', '');
89
		} else {
90
			return false;
91
		}
92
	}
93
94
	/**
95
	 * Delete threshold
96
	 *
97
	 * @param int $thresholdId
98
	 * @return bool
99
	 */
100
	public function delete(int $thresholdId) {
101
		$this->ThresholdMapper->deleteThreshold($thresholdId);
102
		return true;
103
	}
104
105
	/**
106
	 * validate notification thresholds per report
107
	 *
108
	 * @param int $reportId
109
	 * @param $dimension1
110
	 * @param $dimension2
111
	 * @param $value
112
	 * @param int $insert
113
	 * @return string
114
	 * @throws \Exception
115
	 */
116
	public function validate(int $reportId, $dimension1, $dimension2, $value, int $insert = 0) {
0 ignored issues
show
The parameter $dimension2 is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

116
	public function validate(int $reportId, $dimension1, /** @scrutinizer ignore-unused */ $dimension2, $value, int $insert = 0) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
		$result = '';
118
		$thresholds = $this->ThresholdMapper->getSevOneThresholdsByReport($reportId);
119
		$datasetMetadata = $this->ReportMapper->read($reportId);
120
121
		foreach ($thresholds as $threshold) {
122
			if ($threshold['dimension1'] === $dimension1 or $threshold['dimension1'] === '*') {
123
				if ($threshold['option'] === 'new' && $insert != 0) {
124
					$this->NotificationManager->triggerNotification(NotificationManager::SUBJECT_THRESHOLD, $reportId, $threshold['id'], [
125
						'report' => $datasetMetadata['name'],
126
						'subject' => $dimension1,
127
						'rule' => $this->l10n->t('new record'),
128
						'value' => ''
129
					], $threshold['user_id']);
130
					$result = 'Threshold value met';
131
				} elseif (version_compare(floatval($value), floatval($threshold['value']), $threshold['option'])) {
132
					$this->NotificationManager->triggerNotification(NotificationManager::SUBJECT_THRESHOLD, $reportId, $threshold['id'], [
133
						'report' => $datasetMetadata['name'],
134
						'subject' => $dimension1,
135
						'rule' => $threshold['option'],
136
						'value' => $threshold['value']
137
					], $threshold['user_id']);
138
					$result = 'Threshold value met';
139
				}
140
			}
141
		}
142
		return $result;
143
	}
144
}