* validate notification thresholds per report * * @param int $reportId * @param $dimension1 * @param $dimension2 * @param $value * @param int $insert * @return string * @throws \Exception */
(int $reportId, $dimension1, $dimension2, $value, int $insert = 0)
| 203 | * @throws \Exception |
| 204 | */ |
| 205 | public function validate(int $reportId, $dimension1, $dimension2, $value, int $insert = 0) { |
| 206 | $result = null; |
| 207 | $thresholds = $this->ThresholdMapper->getSevOneThresholdsByReport($reportId); |
| 208 | $thresholds = $this->VariableService->replaceThresholdsVariables($thresholds); |
| 209 | $datasetMetadata = $this->ReportMapper->read($reportId); |
| 210 | |
| 211 | foreach ($thresholds as $threshold) { |
| 212 | if (isset($datasetMetadata['user_id']) && $threshold['user_id'] !== $datasetMetadata['user_id']) { |
| 213 | continue; |
| 214 | } |
| 215 | $dimIndex = intval($threshold['dimension']); |
| 216 | switch ($dimIndex) { |
| 217 | case 0: |
| 218 | $compare = $dimension1; |
| 219 | $subject = $datasetMetadata['dimension1']; |
| 220 | break; |
| 221 | case 1: |
| 222 | $compare = $dimension2; |
| 223 | $subject = $datasetMetadata['dimension2']; |
| 224 | break; |
| 225 | default: |
| 226 | $compare = $value; |
| 227 | $subject = $datasetMetadata['value']; |
| 228 | } |
| 229 | |
| 230 | if ($threshold['option'] === 'new' && $insert != 0) { |
| 231 | $this->NotificationManager->triggerNotification(NotificationManager::SUBJECT_THRESHOLD, $reportId, $threshold['id'], [ |
| 232 | 'report' => $datasetMetadata['name'], |
| 233 | 'subject' => $subject, |
| 234 | 'rule' => $this->l10n->t('new record'), |
| 235 | 'value' => '' |
| 236 | ], $threshold['user_id']); |
| 237 | $result = 'Threshold value met'; |
| 238 | } else { |
| 239 | $option = strtoupper($threshold['option']); |
| 240 | |
| 241 | // map legacy symbolic options to the new textual ones |
| 242 | $legacyMap = [ |
| 243 | '=' => 'EQ', |
| 244 | '>' => 'GT', |
| 245 | '<' => 'LT', |
| 246 | '>=' => 'GE', |
| 247 | '<=' => 'LE', |
| 248 | '!=' => 'NE', |
| 249 | ]; |
| 250 | if (isset($legacyMap[$option])) { |
| 251 | $option = $legacyMap[$option]; |
| 252 | } |
| 253 | |
| 254 | switch ($option) { |
| 255 | case 'EQ': |
| 256 | $comparison = $this->compareValues($compare, $threshold['target']) === 0; |
| 257 | break; |
| 258 | case 'NE': |
| 259 | $comparison = $this->compareValues($compare, $threshold['target']) !== 0; |
| 260 | break; |
| 261 | case 'GT': |
| 262 | $comparison = $this->compareValues($compare, $threshold['target']) > 0; |