/**
  * Add a new test result range
  * @param tx_caretaker_TestResultRange $testResultRange
  * @param string $title
  */
 public function addTestResultrange(tx_caretaker_TestResultRange $testResultRange, $title)
 {
     $this->testResultRanges[] = $testResultRange;
     $this->testResultRangeTitles[] = $title;
     if (!$this->getStartTimestamp() || $this->getStartTimestamp() > $testResultRange->getStartTimestamp()) {
         $this->setStartTimestamp($testResultRange->getStartTimestamp());
     }
     if (!$this->getEndTimestamp() || $this->getEndTimestamp() < $testResultRange->getEndTimestamp()) {
         $this->setEndTimestamp($testResultRange->getEndTimestamp());
     }
     if (!$this->getMinValue() || $this->getMinValue() > $testResultRange->getMinValue()) {
         $this->setMinValue($testResultRange->getMinValue());
     }
     if (!$this->getMaxValue() || $this->getMaxValue() < $testResultRange->getMaxValue()) {
         $this->setMaxValue($testResultRange->getMaxValue());
     }
     $this->init();
 }
 /**
  * draw the chart-foreground into the given chart image
  * @param resource $image
  */
 protected function drawChartImageForeground(&$image)
 {
     $colorBg = imagecolorallocatealpha($image, 0, 0, 255, 100);
     $color = imagecolorallocate($image, 0, 0, 255);
     $lastX = NULL;
     $lastY = NULL;
     $bgPoints = array();
     $feLines = array();
     /** @var tx_caretaker_TestResult $testResult */
     foreach ($this->testResultRange as $testResult) {
         $newX = intval($this->transformX($testResult->getTimestamp()));
         $newY = intval($this->transformY($testResult->getValue()));
         if ($lastX !== NULL) {
             // bg
             $bgPoints[] = $lastX;
             $bgPoints[] = $lastY;
             $bgPoints[] = $newX;
             $bgPoints[] = $lastY;
             $bgPoints[] = $newX;
             $bgPoints[] = $newY;
             // fe
             $feLines[] = array($lastX, $lastY, $newX, $lastY);
             $feLines[] = array($newX, $lastY, $newX, $newY);
         }
         $lastX = $newX;
         $lastY = $newY;
     }
     $bgPoints[] = intval($this->transformX($this->testResultRange->getLast()->getTimestamp()));
     $bgPoints[] = intval($this->transformY(0));
     $bgPoints[] = intval($this->transformX($this->testResultRange->getFirst()->getTimestamp()));
     $bgPoints[] = intval($this->transformY(0));
     // draw filled chart background
     if (count($bgPoints) > 7) {
         imagefilledpolygon($image, $bgPoints, count($bgPoints) / 2, $colorBg);
     }
     // draw line
     if (count($feLines) > 1) {
         foreach ($feLines as $line) {
             imageline($image, $line[0], $line[1], $line[2], $line[3], $color);
         }
     }
 }
 function test_getAverageValue()
 {
     $tr = new tx_caretaker_TestResultRange(500, 1000);
     $tr->addResult(new tx_caretaker_TestResult(500, tx_caretaker_Constants::state_ok, 10, ''));
     $tr->addResult(new tx_caretaker_TestResult(750, tx_caretaker_Constants::state_ok, 20, ''));
     $tr->addResult(new tx_caretaker_TestResult(1000, tx_caretaker_Constants::state_ok, 20, ''));
     $this->assertEquals($tr->getAverageValue(500, 1000), 15, 'average value fails');
     $tr = new tx_caretaker_TestResultRange(500, 1000);
     $tr->addResult(new tx_caretaker_TestResult(500, tx_caretaker_Constants::state_ok, 10, ''));
     $tr->addResult(new tx_caretaker_TestResult(750, tx_caretaker_Constants::state_ok, 20, ''));
     $this->assertEquals($tr->getAverageValue(), 10, 'average value fails');
     $tr = new tx_caretaker_TestResultRange(500, 1000);
     $tr->addResult(new tx_caretaker_TestResult(800, tx_caretaker_Constants::state_ok, 10, ''));
     $tr->addResult(new tx_caretaker_TestResult(900, tx_caretaker_Constants::state_ok, 20, ''));
     $tr->addResult(new tx_caretaker_TestResult(1000, tx_caretaker_Constants::state_ok, 20, ''));
     $this->assertEquals($tr->getAverageValue(), 15, 'average value fails');
     $tr = new tx_caretaker_TestResultRange(500, 1000);
     $tr->addResult(new tx_caretaker_TestResult(500, tx_caretaker_Constants::state_ok, 10, ''));
     $tr->addResult(new tx_caretaker_TestResult(900, tx_caretaker_Constants::state_ok, 20, ''));
     $tr->addResult(new tx_caretaker_TestResult(1000, tx_caretaker_Constants::state_ok, 20, ''));
     $this->assertEquals($tr->getAverageValue(), 12, 'average value fails');
     $tr = new tx_caretaker_TestResultRange(500, 1000);
     $tr->addResult(new tx_caretaker_TestResult(750, tx_caretaker_Constants::state_ok, 10, ''));
     $this->assertEquals($tr->getAverageValue(), 0, 'average value fails');
 }
 /**
  * Get the ResultRange for the given Instance Test and the timerange
  *
  * @param tx_caretaker_TestNode $testNode
  * @param integer $start_timestamp
  * @param integer $stop_timestamp
  * @param boolean $graph By default the result range is created for the graph, so the last result is added again at the end
  * @return tx_caretaker_TestResultRange
  */
 public function getRangeByNode(tx_caretaker_TestNode $testNode, $start_timestamp, $stop_timestamp, $graph = true)
 {
     $testUID = $testNode->getUid();
     $instanceUID = $testNode->getInstance()->getUid();
     $result_range = new tx_caretaker_TestResultRange($start_timestamp, $stop_timestamp);
     $base_condition = 'test_uid=' . $testUID . ' AND instance_uid=' . $instanceUID . ' ';
     $GLOBALS['TYPO3_DB']->store_lastBuiltQuery = TRUE;
     $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_caretaker_testresult', $base_condition . 'AND tstamp >=' . $start_timestamp . ' AND tstamp <=' . $stop_timestamp, '', 'tstamp ASC');
     $last = 0;
     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_testresult', $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, 'first');
         }
     }
     // add last value if needed
     $last = $result_range->getLast();
     if ($last && $last->getTstamp() < $stop_timestamp) {
         if ($graph) {
             $real_last = new tx_caretaker_TestResult($stop_timestamp, $last->getState(), $last->getValue(), $last->getMessage()->getText(), $last->getSubMessages());
             $result_range->addResult($real_last);
         }
     }
     return $result_range;
 }