diff --git a/src/applications/phrequent/storage/PhrequentTimeSlices.php b/src/applications/phrequent/storage/PhrequentTimeSlices.php --- a/src/applications/phrequent/storage/PhrequentTimeSlices.php +++ b/src/applications/phrequent/storage/PhrequentTimeSlices.php @@ -17,13 +17,15 @@ } 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 --- a/src/applications/phrequent/storage/__tests__/PhrequentTimeBlockTestCase.php +++ b/src/applications/phrequent/storage/__tests__/PhrequentTimeBlockTestCase.php @@ -282,6 +282,30 @@ $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;