/** * Extracts a sorted range of TimePoint * * @param array $tags * @return array */ protected function getRange($tags) { $range = $this->timeLine->find($tags, TimePoint::TARGET_SERVER); TimePoint::sort($range); return $range; }
/** * Computes the total duration represented by the filtered TimePoints * @param string|array $tag A tag or a list of tags to filter * @param int $target The type of target TimePoint to filter * @param int $lastTimestamp An optional timestamp that will be utilized to close the last open range, if any * @return float Returns the total computed duration * @throws TimeException */ public function compute($tag = null, $target = TimePoint::TARGET_ALL, $lastTimestamp = 0) { // default value for the last timestamp if (!$lastTimestamp) { $lastTimestamp = microtime(true); } // either get all points or only a subset according to the provided criteria if (!$tag && $target == TimePoint::TARGET_ALL) { $points = $this->getPoints(); } else { $points = $this->find($tag, $target, TimePoint::TYPE_ALL); } // we need a ordered list of points TimePoint::sort($points); // gather points by ranges, relying on the points references $ranges = []; foreach ($points as $point) { $ranges[$point->getRef()][] = $point; } // compute the total duration by summing all gathered ranges // this loop can throw exceptions $duration = 0; foreach ($ranges as $range) { // the last range could be still open, so auto close it $nb = count($range); $last = $nb && $nb % 2 ? $range[$nb - 1] : null; if ($last && $last->getType() == TimePoint::TYPE_START) { $range[] = new TimePoint($last->getTags(), $lastTimestamp, TimePoint::TYPE_END, $last->getTarget()); } $duration += $this->computeRange($range); } return $duration; }
/** * Test TimePoint::sort() method * @dataProvider testSortProvider */ public function testSort(array $range, array $expectedResult) { $this->assertEquals($expectedResult, TimePoint::sort($range)); }