/**
  * Get the current instance
  * @return tx_caretaker_InstanceNode
  */
 public function getInstance()
 {
     if ($this instanceof tx_caretaker_InstanceNode) {
         return $this;
     } else {
         if ($this->parent) {
             return $this->parent->getInstance();
         } else {
             return FALSE;
         }
     }
 }
 /**
  * Get the current instance
  * @return tx_caretaker_InstanceNode
  */
 public function getInstance()
 {
     if (is_a($this, 'tx_caretaker_InstanceNode')) {
         return $this;
     } else {
         if ($this->parent) {
             return $this->parent->getInstance();
         } else {
             return FALSE;
         }
     }
 }
 /**
  * Get the ResultRange for the given Aggregator and the timerange
  *
  * @param tx_caretaker_AbstractNode $node
  * @param integer $start_timestamp
  * @param integer $stop_timestamp
  * @return tx_caretaker_AggregatorResultRange
  */
 public function getRangeByNode($node, $start_timestamp, $stop_timestamp)
 {
     $result_range = new tx_caretaker_AggregatorResultRange($start_timestamp, $stop_timestamp);
     $instance = $node->getInstance();
     if ($instance) {
         $instanceUid = $instance->getUid();
     } else {
         $instanceUid = 0;
     }
     $nodeType = $node->getType();
     $nodeUid = $node->getUid();
     $base_condition = 'aggregator_uid=' . $nodeUid . ' AND aggregator_type="' . $nodeType . '" AND instance_uid=' . $instanceUid;
     $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_caretaker_aggregatorresult', $base_condition . ' AND tstamp >=' . $start_timestamp . ' AND tstamp <=' . $stop_timestamp, '', 'tstamp ASC');
     while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
         $result = $this->dbrow2instance($row);
         $result_range->addResult($result);
     }
     // add first value if needed
     $first = $result_range->getFirst();
     if (!$first || $first && $first->getTstamp() > $start_timestamp) {
         $GLOBALS['TYPO3_DB']->store_lastBuiltQuery = TRUE;
         $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_caretaker_aggregatorresult', $base_condition . ' AND tstamp <' . $start_timestamp, '', 'tstamp DESC', 1);
         if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
             $row['tstamp'] = $start_timestamp;
             $result = $this->dbrow2instance($row);
             $result_range->addResult($result);
         }
     }
     // add last value if needed
     $last = $result_range->getLast();
     if ($last && $last->getTstamp() < $stop_timestamp) {
         $real_last = new tx_caretaker_AggregatorResult($stop_timestamp, $last->getState(), $last->getNumUNDEFINED(), $last->getNumOK(), $last->getNumWARNING(), $last->getNumERROR(), $last->getMessage()->getText());
         $result_range->addResult($real_last);
     }
     return $result_range;
 }
 /**
  * @param tx_caretaker_AbstractNode $node
  * @param int $offset
  * @param int $limit
  * @return tx_caretaker_AggregatorResultRange
  */
 public function getResultRangeByNodeAndOffset($node, $offset = 0, $limit = 10)
 {
     $result_range = new tx_caretaker_AggregatorResultRange(NULL, NULL);
     $instance = $node->getInstance();
     if ($instance) {
         $instanceUid = $instance->getUid();
     } else {
         $instanceUid = 0;
     }
     $nodeType = $node->getType();
     $nodeUid = $node->getUid();
     $base_condition = 'aggregator_uid=' . $nodeUid . ' AND aggregator_type="' . $nodeType . '" AND instance_uid=' . $instanceUid;
     $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_caretaker_aggregatorresult', $base_condition, '', 'tstamp DESC', (int) $offset . ',' . (int) $limit);
     while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
         $result = $this->dbrow2instance($row);
         $result_range->addResult($result);
     }
     return $result_range;
 }
 /**
  * Get the latest Testresult for the given Instance and Test
  *
  * @param tx_caretaker_AbstractNode $testNode
  * @param $currentResult
  * @return tx_caretaker_TestResult
  */
 public function getPreviousDifferingResult($testNode, $currentResult)
 {
     $row = NULL;
     if ($testNode instanceof tx_caretaker_TestNode) {
         $testUID = $testNode->getUid();
         $instanceUID = $testNode->getInstance()->getUid();
         $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_caretaker_testresult', 'test_uid = ' . $testUID . ' AND instance_uid = ' . $instanceUID . ' AND result_status <> ' . $currentResult->getState() . ' AND tstamp < ' . $currentResult->getTimestamp(), 'tstamp DESC, uid DESC', '', '1');
         $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
     }
     if ($row) {
         $result = $this->dbrow2instance($row);
         return $result;
     } else {
         return new tx_caretaker_TestResult();
     }
 }