From 2101c3b6890db87da98c59ec408b306963b6bdb6 Mon Sep 17 00:00:00 2001 From: James Rhodes Date: Sat, 12 Jul 2014 11:42:32 +1000 Subject: [PATCH] Conduit APIs to start and stop tracking time in phrequent Summary: This adds methods to start and stop tracking any arbitrary PHID in phrequent. Currently, this uses copy-pasted code from PhrequentTrackController. I had to do this because the code to start/stop was not abstracted into a common class. Once the code to start/stop working is extracted into a re-usable class, the conduit API can use this as well. Test Plan: I called the functions with a PHID of a task and ensured that the fields in the phrequent database table was being updated correctly. Reviewers: skyronic, #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: maxhodak, erik.fercak, aran, epriestley, Korvin Maniphest Tasks: T3569, T3970 Differential Revision: https://secure.phabricator.com/D7326 --- src/__phutil_library_map__.php | 10 +++ .../conduit/ConduitAPI_phrequent_Method.php | 11 +++ .../ConduitAPI_phrequent_pop_Method.php | 49 +++++++++++++ .../ConduitAPI_phrequent_push_Method.php | 44 ++++++++++++ .../ConduitAPI_phrequent_tracking_Method.php | 45 ++++++++++++ .../controller/PhrequentTrackController.php | 40 +---------- .../editor/PhrequentTrackingEditor.php | 70 +++++++++++++++++++ .../phrequent/query/PhrequentSearchEngine.php | 2 +- .../phrequent/storage/PhrequentTimeBlock.php | 44 ++++++++++-- 9 files changed, 273 insertions(+), 42 deletions(-) create mode 100644 src/applications/phrequent/conduit/ConduitAPI_phrequent_Method.php create mode 100644 src/applications/phrequent/conduit/ConduitAPI_phrequent_pop_Method.php create mode 100644 src/applications/phrequent/conduit/ConduitAPI_phrequent_push_Method.php create mode 100644 src/applications/phrequent/conduit/ConduitAPI_phrequent_tracking_Method.php create mode 100644 src/applications/phrequent/editor/PhrequentTrackingEditor.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index d898002c79..2216f70dc8 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -225,6 +225,10 @@ phutil_register_library_map(array( 'ConduitAPI_phragment_Method' => 'applications/phragment/conduit/ConduitAPI_phragment_Method.php', 'ConduitAPI_phragment_getpatch_Method' => 'applications/phragment/conduit/ConduitAPI_phragment_getpatch_Method.php', 'ConduitAPI_phragment_queryfragments_Method' => 'applications/phragment/conduit/ConduitAPI_phragment_queryfragments_Method.php', + 'ConduitAPI_phrequent_Method' => 'applications/phrequent/conduit/ConduitAPI_phrequent_Method.php', + 'ConduitAPI_phrequent_pop_Method' => 'applications/phrequent/conduit/ConduitAPI_phrequent_pop_Method.php', + 'ConduitAPI_phrequent_push_Method' => 'applications/phrequent/conduit/ConduitAPI_phrequent_push_Method.php', + 'ConduitAPI_phrequent_tracking_Method' => 'applications/phrequent/conduit/ConduitAPI_phrequent_tracking_Method.php', 'ConduitAPI_phriction_Method' => 'applications/phriction/conduit/ConduitAPI_phriction_Method.php', 'ConduitAPI_phriction_edit_Method' => 'applications/phriction/conduit/ConduitAPI_phriction_edit_Method.php', 'ConduitAPI_phriction_history_Method' => 'applications/phriction/conduit/ConduitAPI_phriction_history_Method.php', @@ -2547,6 +2551,7 @@ phutil_register_library_map(array( 'PhrequentTimeBlockTestCase' => 'applications/phrequent/storage/__tests__/PhrequentTimeBlockTestCase.php', 'PhrequentTrackController' => 'applications/phrequent/controller/PhrequentTrackController.php', 'PhrequentTrackableInterface' => 'applications/phrequent/interface/PhrequentTrackableInterface.php', + 'PhrequentTrackingEditor' => 'applications/phrequent/editor/PhrequentTrackingEditor.php', 'PhrequentUIEventListener' => 'applications/phrequent/event/PhrequentUIEventListener.php', 'PhrequentUserTime' => 'applications/phrequent/storage/PhrequentUserTime.php', 'PhrequentUserTimeQuery' => 'applications/phrequent/query/PhrequentUserTimeQuery.php', @@ -2937,6 +2942,10 @@ phutil_register_library_map(array( 'ConduitAPI_phragment_Method' => 'ConduitAPIMethod', 'ConduitAPI_phragment_getpatch_Method' => 'ConduitAPI_phragment_Method', 'ConduitAPI_phragment_queryfragments_Method' => 'ConduitAPI_phragment_Method', + 'ConduitAPI_phrequent_Method' => 'ConduitAPIMethod', + 'ConduitAPI_phrequent_pop_Method' => 'ConduitAPI_phrequent_Method', + 'ConduitAPI_phrequent_push_Method' => 'ConduitAPI_phrequent_Method', + 'ConduitAPI_phrequent_tracking_Method' => 'ConduitAPI_phrequent_Method', 'ConduitAPI_phriction_Method' => 'ConduitAPIMethod', 'ConduitAPI_phriction_edit_Method' => 'ConduitAPI_phriction_Method', 'ConduitAPI_phriction_history_Method' => 'ConduitAPI_phriction_Method', @@ -5517,6 +5526,7 @@ phutil_register_library_map(array( 'PhrequentTimeBlock' => 'Phobject', 'PhrequentTimeBlockTestCase' => 'PhabricatorTestCase', 'PhrequentTrackController' => 'PhrequentController', + 'PhrequentTrackingEditor' => 'PhabricatorEditor', 'PhrequentUIEventListener' => 'PhabricatorEventListener', 'PhrequentUserTime' => array( diff --git a/src/applications/phrequent/conduit/ConduitAPI_phrequent_Method.php b/src/applications/phrequent/conduit/ConduitAPI_phrequent_Method.php new file mode 100644 index 0000000000..a822e0341f --- /dev/null +++ b/src/applications/phrequent/conduit/ConduitAPI_phrequent_Method.php @@ -0,0 +1,11 @@ + 'phid', + 'stopTime' => 'int', + 'note' => 'string' + ); + } + + public function defineReturnType() { + return 'phid'; + } + + public function defineErrorTypes() { + return array( + ); + } + + protected function execute(ConduitAPIRequest $request) { + $user = $request->getUser(); + $object_phid = $request->getValue('objectPHID'); + $timestamp = $request->getValue('stopTime'); + $note = $request->getValue('note'); + if ($timestamp === null) { + $timestamp = time(); + } + + $editor = new PhrequentTrackingEditor(); + + if (!$object_phid) { + return $editor->stopTrackingTop($user, $timestamp, $note); + } else { + return $editor->stopTracking($user, $object_phid, $timestamp, $note); + } + } + +} diff --git a/src/applications/phrequent/conduit/ConduitAPI_phrequent_push_Method.php b/src/applications/phrequent/conduit/ConduitAPI_phrequent_push_Method.php new file mode 100644 index 0000000000..03467494ac --- /dev/null +++ b/src/applications/phrequent/conduit/ConduitAPI_phrequent_push_Method.php @@ -0,0 +1,44 @@ + 'required phid', + 'startTime' => 'int' + ); + } + + public function defineReturnType() { + return 'phid'; + } + + public function defineErrorTypes() { + return array( + ); + } + + protected function execute(ConduitAPIRequest $request) { + $user = $request->getUser(); + $object_phid = $request->getValue('objectPHID'); + $timestamp = $request->getValue('startTime'); + if ($timestamp === null) { + $timestamp = time(); + } + + $editor = new PhrequentTrackingEditor(); + return $editor->startTracking($user, $object_phid, $timestamp); + } + +} diff --git a/src/applications/phrequent/conduit/ConduitAPI_phrequent_tracking_Method.php b/src/applications/phrequent/conduit/ConduitAPI_phrequent_tracking_Method.php new file mode 100644 index 0000000000..7372caeb20 --- /dev/null +++ b/src/applications/phrequent/conduit/ConduitAPI_phrequent_tracking_Method.php @@ -0,0 +1,45 @@ +getUser(); + + $times = id(new PhrequentUserTimeQuery()) + ->setViewer($user) + ->needPreemptingEvents(true) + ->withUserPHIDs(array($user->getPHID())) + ->execute(); + + $now = time(); + + $results = id(new PhrequentTimeBlock($times)) + ->getCurrentWorkStack($now); + + return array('data' => $results); + } + +} diff --git a/src/applications/phrequent/controller/PhrequentTrackController.php b/src/applications/phrequent/controller/PhrequentTrackController.php index b4b167550f..95e7c0d9f6 100644 --- a/src/applications/phrequent/controller/PhrequentTrackController.php +++ b/src/applications/phrequent/controller/PhrequentTrackController.php @@ -14,6 +14,7 @@ final class PhrequentTrackController public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); + $editor = new PhrequentTrackingEditor(); $phid = $this->phid; $handle = id(new PhabricatorHandleQuery()) @@ -61,9 +62,9 @@ final class PhrequentTrackController if (!$err) { if ($this->isStartingTracking()) { - $this->startTracking($user, $this->phid, $timestamp); + $editor->startTracking($user, $this->phid, $timestamp); } else if ($this->isStoppingTracking()) { - $this->stopTracking($user, $this->phid, $timestamp, $note); + $editor->stopTracking($user, $this->phid, $timestamp, $note); } return id(new AphrontRedirectResponse()); } @@ -108,39 +109,4 @@ final class PhrequentTrackController private function isStoppingTracking() { return $this->verb === 'stop'; } - - private function startTracking($user, $phid, $timestamp) { - $usertime = new PhrequentUserTime(); - $usertime->setDateStarted($timestamp); - $usertime->setUserPHID($user->getPHID()); - $usertime->setObjectPHID($phid); - $usertime->save(); - } - - private function stopTracking($user, $phid, $timestamp, $note) { - if (!PhrequentUserTimeQuery::isUserTrackingObject($user, $phid)) { - // Don't do anything, it's not being tracked. - return; - } - - $usertime_dao = new PhrequentUserTime(); - $conn = $usertime_dao->establishConnection('r'); - - queryfx( - $conn, - 'UPDATE %T usertime '. - 'SET usertime.dateEnded = %d, '. - 'usertime.note = %s '. - 'WHERE usertime.userPHID = %s '. - 'AND usertime.objectPHID = %s '. - 'AND usertime.dateEnded IS NULL '. - 'ORDER BY usertime.dateStarted, usertime.id DESC '. - 'LIMIT 1', - $usertime_dao->getTableName(), - $timestamp, - $note, - $user->getPHID(), - $phid); - } - } diff --git a/src/applications/phrequent/editor/PhrequentTrackingEditor.php b/src/applications/phrequent/editor/PhrequentTrackingEditor.php new file mode 100644 index 0000000000..fec91ef7cb --- /dev/null +++ b/src/applications/phrequent/editor/PhrequentTrackingEditor.php @@ -0,0 +1,70 @@ +setDateStarted($timestamp); + $usertime->setUserPHID($user->getPHID()); + $usertime->setObjectPHID($phid); + $usertime->save(); + + return $phid; + } + + public function stopTracking( + PhabricatorUser $user, + $phid, + $timestamp, + $note) { + + if (!PhrequentUserTimeQuery::isUserTrackingObject($user, $phid)) { + // Don't do anything, it's not being tracked. + return null; + } + + $usertime_dao = new PhrequentUserTime(); + $conn = $usertime_dao->establishConnection('r'); + + queryfx( + $conn, + 'UPDATE %T usertime '. + 'SET usertime.dateEnded = %d, '. + 'usertime.note = %s '. + 'WHERE usertime.userPHID = %s '. + 'AND usertime.objectPHID = %s '. + 'AND usertime.dateEnded IS NULL '. + 'ORDER BY usertime.dateStarted, usertime.id DESC '. + 'LIMIT 1', + $usertime_dao->getTableName(), + $timestamp, + $note, + $user->getPHID(), + $phid); + + return $phid; + } + + public function stopTrackingTop(PhabricatorUser $user, $timestamp, $note) { + $times = id(new PhrequentUserTimeQuery()) + ->setViewer($user) + ->withUserPHIDs(array($user->getPHID())) + ->withEnded(PhrequentUserTimeQuery::ENDED_NO) + ->setOrder(PhrequentUserTimeQuery::ORDER_STARTED_DESC) + ->execute(); + + if (count($times) === 0) { + // Nothing to stop tracking. + return null; + } + + $current = head($times); + + return $this->stopTracking( + $user, + $current->getObjectPHID(), + $timestamp, + $note); + } + +} diff --git a/src/applications/phrequent/query/PhrequentSearchEngine.php b/src/applications/phrequent/query/PhrequentSearchEngine.php index 6092659309..ddd5108468 100644 --- a/src/applications/phrequent/query/PhrequentSearchEngine.php +++ b/src/applications/phrequent/query/PhrequentSearchEngine.php @@ -186,7 +186,7 @@ final class PhrequentSearchEngine $usertime->getUserPHID() === $viewer->getPHID()) { $item->addAction( id(new PHUIListItemView()) - ->setIcon('fa-time-o') + ->setIcon('fa-stop') ->addSigil('phrequent-stop-tracking') ->setWorkflow(true) ->setRenderNameAsTooltip(true) diff --git a/src/applications/phrequent/storage/PhrequentTimeBlock.php b/src/applications/phrequent/storage/PhrequentTimeBlock.php index 45b3555fd2..a625e2d2f9 100644 --- a/src/applications/phrequent/storage/PhrequentTimeBlock.php +++ b/src/applications/phrequent/storage/PhrequentTimeBlock.php @@ -37,12 +37,12 @@ final class PhrequentTimeBlock extends Phobject { $timeline = array(); $timeline[] = array( 'event' => $event, - 'at' => $event->getDateStarted(), + 'at' => (int)$event->getDateStarted(), 'type' => 'start', ); $timeline[] = array( 'event' => $event, - 'at' => nonempty($event->getDateEnded(), $now), + 'at' => (int)nonempty($event->getDateEnded(), $now), 'type' => 'end', ); @@ -54,12 +54,12 @@ final class PhrequentTimeBlock extends Phobject { $same_object = ($preempt->getObjectPHID() == $base_phid); $timeline[] = array( 'event' => $preempt, - 'at' => $preempt->getDateStarted(), + 'at' => (int)$preempt->getDateStarted(), 'type' => $same_object ? 'start' : 'push', ); $timeline[] = array( 'event' => $preempt, - 'at' => nonempty($preempt->getDateEnded(), $now), + 'at' => (int)nonempty($preempt->getDateEnded(), $now), 'type' => $same_object ? 'end' : 'pop', ); } @@ -185,6 +185,42 @@ final class PhrequentTimeBlock extends Phobject { return $object_ranges; } + /** + * Returns the current list of work. + */ + public function getCurrentWorkStack($now, $include_inactive = false) { + $ranges = $this->getObjectTimeRanges($now); + + $results = array(); + foreach ($ranges as $phid => $blocks) { + $total = 0; + foreach ($blocks as $block) { + $total += $block[1] - $block[0]; + } + + $type = 'inactive'; + foreach ($blocks as $block) { + if ($block[1] === $now) { + if ($block[0] === $block[1]) { + $type = 'suspended'; + } else { + $type = 'active'; + } + break; + } + } + + if ($include_inactive || $type !== 'inactive') { + $results[] = array( + 'phid' => $phid, + 'time' => $total, + 'type' => $type); + } + } + + return $results; + } + /** * Merge a list of time ranges (pairs of `` epochs) so that no