示例#1
0
 public function setSign($sign)
 {
     if ($sign !== $this->sign) {
         return Filter::expression($this->column, $sign, $this->expression);
     }
     return $this;
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function render()
 {
     $repo = new AnnouncementIniRepository();
     $etag = $repo->getEtag();
     $cookie = new AnnouncementCookie();
     if ($cookie->getEtag() !== $etag) {
         $cookie->setEtag($etag);
         $cookie->setNextActive($repo->findNextActive());
         Icinga::app()->getResponse()->setCookie($cookie);
     }
     $acked = array();
     foreach ($cookie->getAcknowledged() as $hash) {
         $acked[] = Filter::expression('hash', '!=', $hash);
     }
     $acked = Filter::matchAll($acked);
     $announcements = $repo->findActive();
     $announcements->applyFilter($acked);
     if ($announcements->hasResult()) {
         $html = '<ul role="alert" id="announcements">';
         foreach ($announcements as $announcement) {
             $ackForm = new AcknowledgeAnnouncementForm();
             $ackForm->populate(array('hash' => $announcement->hash));
             $html .= '<li><div>' . $this->view()->escape($announcement->message) . '</div>' . $ackForm . '</li>';
         }
         $html .= '</ul>';
         return $html;
     }
     // Force container update on XHR
     return '<div style="display: none;"></div>';
 }
示例#3
0
 /**
  * Return the corresponding filter-object
  *
  * @returns Filter
  */
 public function getFilter()
 {
     $baseFilter = Filter::matchAny(Filter::expression('type', '=', 'hard_state'));
     if ($this->getValue('objecttype', 'hosts') === 'hosts') {
         $objectTypeFilter = Filter::expression('object_type', '=', 'host');
     } else {
         $objectTypeFilter = Filter::expression('object_type', '=', 'service');
     }
     $states = array('cnt_down_hard' => Filter::expression('state', '=', '1'), 'cnt_unreachable_hard' => Filter::expression('state', '=', '2'), 'cnt_up' => Filter::expression('state', '=', '0'), 'cnt_critical_hard' => Filter::expression('state', '=', '2'), 'cnt_warning_hard' => Filter::expression('state', '=', '1'), 'cnt_unknown_hard' => Filter::expression('state', '=', '3'), 'cnt_ok' => Filter::expression('state', '=', '0'));
     $state = $this->getValue('state', 'cnt_critical_hard');
     $stateFilter = $states[$state];
     if (in_array($state, array('cnt_ok', 'cnt_up'))) {
         return Filter::matchAll($objectTypeFilter, $stateFilter);
     }
     return Filter::matchAll($baseFilter, $objectTypeFilter, $stateFilter);
 }
 /**
  * {@inheritdoc}
  */
 public function onSuccess()
 {
     $cookie = new AnnouncementCookie();
     $repo = new AnnouncementIniRepository();
     $query = $repo->findActive();
     $filter = array();
     foreach ($cookie->getAcknowledged() as $hash) {
         $filter[] = Filter::expression('hash', '=', $hash);
     }
     $query->addFilter(Filter::matchAny($filter));
     $acknowledged = array();
     foreach ($query as $row) {
         $acknowledged[] = $row->hash;
     }
     $acknowledged[] = $this->getElement('hash')->getValue();
     $cookie->setAcknowledged($acknowledged);
     $this->getResponse()->setCookie($cookie);
     return true;
 }
 protected function applyChanges($changes)
 {
     $filter = $this->filter;
     $pairs = array();
     $addTo = null;
     $add = array();
     foreach ($changes as $k => $v) {
         if (preg_match('/^(column|value|sign|operator)((?:_new)?)_([\\d-]+)$/', $k, $m)) {
             if ($m[2] === '_new') {
                 if ($addTo !== null && $addTo !== $m[3]) {
                     throw new \Exception('F...U');
                 }
                 $addTo = $m[3];
                 $add[$m[1]] = $v;
             } else {
                 $pairs[$m[3]][$m[1]] = $v;
             }
         }
     }
     $operators = array();
     foreach ($pairs as $id => $fs) {
         if (array_key_exists('operator', $fs)) {
             $operators[$id] = $fs['operator'];
         } else {
             $f = $filter->getById($id);
             $f->setColumn($fs['column']);
             if ($f->getSign() !== $fs['sign']) {
                 if ($f->isRootNode()) {
                     $filter = $f->setSign($fs['sign']);
                 } else {
                     $filter->replaceById($id, $f->setSign($fs['sign']));
                 }
             }
             $f->setExpression($fs['value']);
         }
     }
     krsort($operators, version_compare(PHP_VERSION, '5.4.0') >= 0 ? SORT_NATURAL : SORT_REGULAR);
     foreach ($operators as $id => $operator) {
         $f = $filter->getById($id);
         if ($f->getOperatorName() !== $operator) {
             if ($f->isRootNode()) {
                 $filter = $f->setOperatorName($operator);
             } else {
                 $filter->replaceById($id, $f->setOperatorName($operator));
             }
         }
     }
     if ($addTo !== null) {
         if ($addTo === '0') {
             $filter = Filter::expression($add['column'], $add['sign'], $add['value']);
         } else {
             $parent = $filter->getById($addTo);
             $f = Filter::expression($add['column'], $add['sign'], $add['value']);
             if (isset($add['operator'])) {
                 switch ($add['operator']) {
                     case 'AND':
                         if ($parent->isExpression()) {
                             if ($parent->isRootNode()) {
                                 $filter = Filter::matchAll(clone $parent, $f);
                             } else {
                                 $filter = $filter->replaceById($addTo, Filter::matchAll(clone $parent, $f));
                             }
                         } else {
                             $parent->addFilter(Filter::matchAll($f));
                         }
                         break;
                     case 'OR':
                         if ($parent->isExpression()) {
                             if ($parent->isRootNode()) {
                                 $filter = Filter::matchAny(clone $parent, $f);
                             } else {
                                 $filter = $filter->replaceById($addTo, Filter::matchAny(clone $parent, $f));
                             }
                         } else {
                             $parent->addFilter(Filter::matchAny($f));
                         }
                         break;
                     case 'NOT':
                         if ($parent->isExpression()) {
                             if ($parent->isRootNode()) {
                                 $filter = Filter::not(Filter::matchAll($parent, $f));
                             } else {
                                 $filter = $filter->replaceById($addTo, Filter::not(Filter::matchAll($parent, $f)));
                             }
                         } else {
                             $parent->addFilter(Filter::not($f));
                         }
                         break;
                 }
             } else {
                 $parent->addFilter($f);
             }
         }
     }
     return $filter;
 }
示例#6
0
 public function flappingFilter()
 {
     return Filter::matchAny(Filter::expression('type', '=', 'flapping'), Filter::expression('type', '=', 'flapping_deleted'));
 }
示例#7
0
 /**
  * Add a where condition to the query by and
  *
  * The syntax of the condition and valid values are defined by the concrete backend-specific query implementation.
  *
  * @param   string  $condition
  * @param   mixed   $value
  *
  * @return  $this
  */
 public function where($condition, $value = null)
 {
     // TODO: more intelligence please
     $this->filter->addFilter(Filter::expression($condition, '=', $value));
     return $this;
 }
示例#8
0
 /**
  * Fetch all entries and forecasts by using the dataview associated with this timeline
  *
  * @return  array       The dataview's result
  */
 private function fetchResults()
 {
     $hookResults = array();
     foreach (Hook::all('timeline') as $timelineProvider) {
         $hookResults = array_merge($hookResults, $timelineProvider->fetchEntries($this->displayRange), $timelineProvider->fetchForecasts($this->forecastRange));
         foreach ($timelineProvider->getIdentifiers() as $identifier => $attributes) {
             if (!array_key_exists($identifier, $this->identifiers)) {
                 $this->identifiers[$identifier] = $attributes;
             }
         }
     }
     $query = $this->dataview;
     $filter = Filter::matchAll(Filter::where('type', array_keys($this->identifiers)), Filter::expression('timestamp', '<=', $this->displayRange->getStart()->getTimestamp()), Filter::expression('timestamp', '>', $this->displayRange->getEnd()->getTimestamp()));
     $query->applyFilter($filter);
     return array_merge($query->getQuery()->fetchAll(), $hookResults);
 }
 /**
  * Get the timestamp of the next active announcement
  *
  * @return  int|null
  */
 public function findNextActive()
 {
     $now = new DateTime();
     $query = $this->select(array('start', 'end'))->setFilter(Filter::matchAny(array(Filter::expression('start', '>', $now), Filter::expression('end', '>', $now))));
     $refresh = null;
     foreach ($query as $row) {
         $min = min($row->start->getTimestamp(), $row->end->getTimestamp());
         if ($refresh === null) {
             $refresh = $min;
         } else {
             $refresh = min($refresh, $min);
         }
     }
     return $refresh;
 }