diff --git a/src/applications/phrequent/storage/PhrequentTimeSlices.php b/src/applications/phrequent/storage/PhrequentTimeSlices.php index 30c4345720..b19f857149 100644 --- a/src/applications/phrequent/storage/PhrequentTimeSlices.php +++ b/src/applications/phrequent/storage/PhrequentTimeSlices.php @@ -17,13 +17,15 @@ final class PhrequentTimeSlices extends Phobject { } public function getDuration($now) { + $total = 0; foreach ($this->ranges as $range) { if ($range[1] === null) { - return $now - $range[0]; + $total += $now - $range[0]; } else { - return $range[1] - $range[0]; + $total += $range[1] - $range[0]; } } + return $total; } public function getIsOngoing() { diff --git a/src/applications/phrequent/storage/__tests__/PhrequentTimeBlockTestCase.php b/src/applications/phrequent/storage/__tests__/PhrequentTimeBlockTestCase.php index d42560090e..b2e062a8e8 100644 --- a/src/applications/phrequent/storage/__tests__/PhrequentTimeBlockTestCase.php +++ b/src/applications/phrequent/storage/__tests__/PhrequentTimeBlockTestCase.php @@ -282,6 +282,30 @@ final class PhrequentTimeBlockTestCase extends PhabricatorTestCase { $ranges); } + public function testSumTimeSlices() { + // This block has multiple closed slices. + $block = new PhrequentTimeBlock( + array( + $this->newEvent('T1', 3456, 4456)->attachPreemptingEvents(array()), + $this->newEvent('T1', 8000, 9000)->attachPreemptingEvents(array()), + )); + + $this->assertEqual( + 2000, + $block->getTimeSpentOnObject('T1', 10000)); + + // This block has an open slice. + $block = new PhrequentTimeBlock( + array( + $this->newEvent('T1', 3456, 4456)->attachPreemptingEvents(array()), + $this->newEvent('T1', 8000, null)->attachPreemptingEvents(array()), + )); + + $this->assertEqual( + 3000, + $block->getTimeSpentOnObject('T1', 10000)); + } + private function newEvent($object_phid, $start_time, $end_time) { static $id = 0;