/** * Check if trigger meet conditions to be fired * * @access public * @param Trigger $trigger * @param array $serverData * @param int $msDelay * @return boolean */ public function shouldTriggerBeFired(Trigger $trigger, array $serverData, $msDelay) { $this->checkIsComparatorValid(); if (!$this->notificationLogService->hasNotificationDelayExpired($trigger->getId(), $serverData['server_id'], $msDelay)) { return false; } $strategy = new StrategyContext($trigger->getType()); return $strategy->compare($trigger, $serverData, $this->serviceRepository, $this->percentageHelper, $this->comparator); }
/** * Compare trigger value to $serverData[$servicename] * * @param Monitor\Model\Trigger $trigger * @param array $serverData * @param \Doctrine\ORM\EntityRepository $serviceRepository * @param \Monitor\Utils\PercentageHelper $percentageHelper * @param \Monitor\Contract\Notification\Trigger\Comparator\ComparatorInterface $comparator */ public function compare(Trigger $trigger, array $serverData, EntityRepository $serviceRepository, PercentageHelper $percentageHelper, ComparatorInterface $comparator) { if (!isset($serverData[$trigger->getServiceName()])) { return false; } if ($comparator->compare($trigger, $serverData[$trigger->getServiceName()])) { return true; } return false; }
/** * Prepare notification * * @access private * @param Trigger $trigger * @param array $serverData * @return \Monitor\Notification\Notification */ private function prepareNotification(Trigger $trigger, array $serverData) { $notificationId = $trigger->getNotificationId(); $notification = $this->getNotificationById($notificationId); if (!$notification) { throw new \Exception('Notification not found'); } //merge server data and trigger properties so we can use them in fulfilling notification message $data = array_merge($serverData, $trigger->toArray()); $this->parseNotification($notification, $data); return $notification; }
/** * Compare trigger value to service percentage * * @param Monitor\Model\Trigger $trigger * @param array $serverData * @param \Doctrine\ORM\EntityRepository $serviceRepository * @param \Monitor\Utils\PercentageHelper $percentageHelper * @param \Monitor\Contract\Notification\Trigger\Comparator\ComparatorInterface $comparator */ public function compare(Trigger $trigger, array $serverData, EntityRepository $serviceRepository, PercentageHelper $percentageHelper, ComparatorInterface $comparator) { $service = $serviceRepository->findOneBy(['name' => $trigger->getServiceName()]); if (!$service) { //$log->error('cant find service') return false; } $serviceCompare = $percentageHelper->getServicePercentage($serverData, $service); if ($comparator->compare($trigger, $serviceCompare)) { return true; } return false; }
/** * Comparing trigger's value * * @access public * @param \Monitor\Model\Trigger $trigger * @param $value * @return bool */ public function compare(Trigger $trigger, $value) { switch ($trigger->getOperator()) { case ">": return $this->moreThan($value, $trigger->getValue()); case "<": return !$this->moreThan($value, $trigger->getValue()); case "=": return $this->equal($value, $trigger->getValue()); case "!=": return !$this->equal($value, $trigger->getValue()); } return false; }