From c0585b7a34ed7eb4fc65e59498053f3cfd6853d5 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 7 Aug 2014 17:05:14 -0700 Subject: [PATCH] Fix Phrequent duration accounting Summary: Fixes T5705. This was just derp; instead of returning the duration of the first slice, return the duration of all the slices. Test Plan: Added unit tests. Saw reasonable results in the UI. Reviewers: btrahan, hach-que Reviewed By: hach-que Subscribers: epriestley Maniphest Tasks: T5705 Differential Revision: https://secure.phabricator.com/D10184 --- .../phrequent/storage/PhrequentTimeSlices.php | 6 +++-- .../__tests__/PhrequentTimeBlockTestCase.php | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) 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;