Christopher Johnson (WMDE) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/181938

Change subject: Adds ChartView
......................................................................

Adds ChartView

refactor and inject dependencies
remove parameters
addresses a few scrutinizer issues

Change-Id: I836dc4b8762f928f88ac391263d2a1756920095b
---
M src/__phutil_library_map__.php
M src/events/SprintUIEventListener.php
M src/query/SprintQuery.php
M src/storage/SprintBuildStats.php
M src/storage/SprintTransaction.php
A src/view/burndown/C3ChartView.php
M src/view/burndown/EventTableView.php
M src/view/burndown/HistoryTableView.php
M src/view/burndown/SprintDataView.php
M src/view/burndown/TasksTableView.php
10 files changed, 142 insertions(+), 142 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/phabricator/extensions/Sprint 
refs/changes/38/181938/1

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
index 7b4ed80..ba22c60 100644
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -14,6 +14,7 @@
     'BurndownDataDate' => 'util/BurndownDataDate.php',
     'BurndownDataDateTest' => 'tests/BurndownDataDateTest.php',
     'BurndownException' => 'exception/BurndownException.php',
+    'C3ChartView' => 'view/burndown/C3ChartView.php',
     'CeleritySprintResources' => 'celerity/CeleritySprintResources.php',
     'DateIterator' => 'tests/DateIterator.php',
     'EventTableView' => 'view/burndown/EventTableView.php',
diff --git a/src/events/SprintUIEventListener.php 
b/src/events/SprintUIEventListener.php
index c079535..74fc7f3 100644
--- a/src/events/SprintUIEventListener.php
+++ b/src/events/SprintUIEventListener.php
@@ -16,15 +16,16 @@
   }
 
   private function filterSprints ($phandles, $value){
+    $handles = array();
     if(is_array($phandles) && count($phandles)>0)
     {
       foreach($phandles as $handle) {
-             if (stripos($handle->getName(), $value) !== false) {
-                $newarray[$handle->getPHID()] = $phandles[$handle->getPHID()];
-             }
+        if (stripos($handle->getName(), $value) !== false) {
+            $handles[$handle->getPHID()] = $phandles[$handle->getPHID()];
+        }
       }
     }
-    return $newarray;
+    return $handles;
   }
 
   private function handlePropertyEvent($event) {
diff --git a/src/query/SprintQuery.php b/src/query/SprintQuery.php
index ae8b19a..eec22f2 100644
--- a/src/query/SprintQuery.php
+++ b/src/query/SprintQuery.php
@@ -30,7 +30,8 @@
   }
 
   public function getAuxFields() {
-    $field_list = PhabricatorCustomField::getObjectFields($this->project, 
PhabricatorCustomField::ROLE_EDIT);
+    $field_list = PhabricatorCustomField::getObjectFields($this->project,
+        PhabricatorCustomField::ROLE_EDIT);
     $field_list->setViewer($this->viewer);
     $field_list->readFieldsFromStorage($this->project);
     $aux_fields = $field_list->getFields();
@@ -59,6 +60,7 @@
   }
 
   public function getStoryPointsForTask($task_phid)  {
+    $points = null;
     $object = new ManiphestCustomFieldStorage();
     $corecustomfield = $object->loadRawDataWhere('objectPHID= %s', $task_phid);
     if (!empty($corecustomfield)) {
@@ -68,7 +70,7 @@
     } else {
       $points = 0;
     }
-     return $points;
+    return $points;
   }
 
   public function getXactions($tasks) {
@@ -156,7 +158,7 @@
     return $data;
  }
 
-  public function getTaskData($where) {
+  public function getTaskData() {
     $task_dao = new ManiphestCustomFieldStorage();
     $data = queryfx_all(
         $this->getCustomFieldConn(),
@@ -172,14 +174,15 @@
     // Load all edges of depends and depended on tasks
     $edges = id(new PhabricatorEdgeQuery())
         ->withSourcePHIDs(array_keys($tasks))
-        
->withEdgeTypes(array(PhabricatorEdgeConfig::TYPE_TASK_DEPENDS_ON_TASK, 
PhabricatorEdgeConfig::TYPE_TASK_DEPENDED_ON_BY_TASK))
+        ->withEdgeTypes(array(PhabricatorEdgeConfig::TYPE_TASK_DEPENDS_ON_TASK,
+            PhabricatorEdgeConfig::TYPE_TASK_DEPENDED_ON_BY_TASK))
         ->execute();
     return $edges;
   }
 
   public function getEvents($xactions) {
-    $scope_phids = array($this->project->getPHID());
-    $events = $this->extractEvents($xactions, $scope_phids);
+    $scope_phid = array($this->project->getPHID());
+    $events = $this->extractEvents($xactions, $scope_phid);
     return $events;
   }
 
@@ -257,24 +260,20 @@
     }
   }
 
-  public function extractEvents($xactions, array $scope_phids) {
+  public function extractEvents($xactions, $scope_phid) {
     assert_instances_of($xactions, 'ManiphestTransaction');
-
-    $scope_phids = array_fuse($scope_phids);
 
     $events = array();
     foreach ($xactions as $xaction) {
       $old = $xaction->getOldValue();
       $new = $xaction->getNewValue();
 
+      $event_type = $this->setXActionEventType ($xaction, $old, $new, 
$scope_phid);
 
-      $event_type = $this->setXActionEventType ($xaction, $old, $new, 
$scope_phids);
-
-      // If we found some kind of events that we care about, stick it in the
-      // list of events.
       if ($event_type !== null) {
         $events[] = array(
             'transactionPHID' => $xaction->getPHID(),
+            'objectPHID' => $xaction->getObjectPHID(),
             'epoch' => $xaction->getDateCreated(),
             'key'   => $xaction->getMetadataValue('customfield:key'),
             'type'  => $event_type,
@@ -283,7 +282,6 @@
       }
     }
 
-    // Sort all events chronologically.
     $events = isort($events, 'epoch');
 
     return $events;
diff --git a/src/storage/SprintBuildStats.php b/src/storage/SprintBuildStats.php
index 7ecfc2f..95a66e7 100644
--- a/src/storage/SprintBuildStats.php
+++ b/src/storage/SprintBuildStats.php
@@ -235,4 +235,20 @@
     }
     return $data;
   }
+
+  public function transposeArray($array) {
+    $transposed_array = array();
+    if ($array) {
+      foreach ($array as $row_key => $row) {
+        if (is_array($row) && !empty($row)) {
+          foreach ($row as $column_key => $element) {
+            $transposed_array[$column_key][$row_key] = $element;
+          }
+        } else {
+          $transposed_array[0][$row_key] = $row;
+        }
+      }
+    }
+    return $transposed_array;
+  }
 }
diff --git a/src/storage/SprintTransaction.php 
b/src/storage/SprintTransaction.php
index 2603013..7fa63d5 100644
--- a/src/storage/SprintTransaction.php
+++ b/src/storage/SprintTransaction.php
@@ -5,8 +5,10 @@
   private $viewer;
   private $task_points = array();
   private $tasks;
+  private $task_created;
   private $task_statuses = array();
   private $task_in_sprint = array();
+  private $taskpoints;
 
   public function setViewer ($viewer) {
     $this->viewer = $viewer;
@@ -23,7 +25,7 @@
     return $this;
   }
 
-  public function buildDailyData($events, $before, $start, $end, $dates, 
$xactions) {
+  public function parseEvents($events, $before, $start, $end, $dates, 
$xactions) {
 
     $sprintpoints = id(new SprintPoints())
         ->setTaskPoints($this->taskpoints);
@@ -31,15 +33,14 @@
     foreach ($events as $event) {
       $date = null;
       $xaction = $xactions[$event['transactionPHID']];
-      $xaction_date = $xaction->getDateCreated();
-      $task_phid = $xaction->getObjectPHID();
+      $xaction_date = $event['epoch'];
+      $task_phid = $event['objectPHID'];
 
       $points = $sprintpoints->getTaskPoints($task_phid);
 
-        $date = phabricator_format_local_time($xaction_date, $this->viewer, 'D 
M j');
+      $date = phabricator_format_local_time($xaction_date, $this->viewer, 'D M 
j');
 
-        if ($xaction_date < $start) {
-
+      if ($xaction_date < $start) {
           switch ($event['type']) {
             case "task-add":
               $this->AddTasksBefore($before, $dates);
@@ -67,19 +68,14 @@
           continue;
         }
 
-        // Determine which date to attach this data to
-
         if ($xaction_date > $start && $xaction_date < $end) {
 
           switch ($event['type']) {
             case "create":
  //            $this->AddTaskCreated($task_phid);
-              // Will be accounted for by "task-add" when the project is added
-              // But we still include it so it shows on the Events list
               break;
             case "task-add":
-              // A task was added to the sprint
-             $this->AddTasksToday($date, $dates);
+              $this->AddTasksToday($date, $dates);
               $this->AddTaskInSprint($task_phid);
               break;
             case "task-remove":
@@ -92,13 +88,12 @@
             case "reopen":
               // A task was reopened, subtract from done
               $this->ReopenedTasksToday($date, $dates);
-             $this->ReopenedPointsToday($date, $points, $dates);
+              $this->ReopenedPointsToday($date, $points, $dates);
               break;
             case "points":
               $old_point_value = $xaction->getOldValue();
               $new_point_value = $xaction->getNewValue();
-                $this->changePoints($date, $task_phid, $new_point_value, 
$old_point_value, $dates);
-//                $this->closePoints($date, $task_phid, $points, 
$old_point_value, $dates);
+              $this->changePoints($date, $task_phid, $new_point_value, 
$old_point_value, $dates);
               break;
           }
         }
@@ -130,11 +125,6 @@
     return $dates;
   }
 
-  private function ReopenedPointsBefore($before, $points, $dates) {
-    $before->setPointsReopenedBefore($points);
-    return $dates;
-  }
-
   private function AddTasksToday($date, $dates) {
     $dates[$date]->setTasksAddedToday();
     return $dates;
@@ -150,24 +140,9 @@
     return $dates;
   }
 
-  private function AddPointsBefore($before, $points, $dates) {
-    $before->setPointsAddedBefore($points);
-    return $dates;
-  }
-
-  private function ClosePointsBefore($before, $points, $dates) {
-    $before->setPointsClosedBefore($points);
-    return $dates;
-  }
-
   private function ChangePointsBefore($before, $new_point_value,  
$old_point_value) {
     $points = $new_point_value - $old_point_value;
     $before->setPointsAddedBefore($points);
-  }
-
-  private function AddPointsToday($date, $points, $dates) {
-    $dates[$date]->setPointsAddedToday($points);
-    return $dates;
   }
 
   private function ClosePointsToday($date, $points, $dates) {
@@ -193,20 +168,12 @@
     return $dates;
   }
 
-  private function closePoints($date, $task_phid, $points, $old_point_value, 
$dates)
-  {
-    // If the task is closed, adjust completed points as well
-    if (isset($this->task_statuses[$task_phid]) && 
$this->task_statuses[$task_phid] == 'closed') {
-      $this->task_points[$task_phid] = $points - $old_point_value;
-      $dates[$date]->setPointsClosedToday($this->task_points[$task_phid]);
-    }
-  }
-
   private function AddTaskCreated($task_phid) {
     $this->task_created = $task_phid;
   }
 
   function getxActionValue($mapping, $keys) {
+    $output_arr = array();
     foreach($keys as $key) {
       $output_arr[] = $mapping[$key];
     }
diff --git a/src/view/burndown/C3ChartView.php 
b/src/view/burndown/C3ChartView.php
new file mode 100644
index 0000000..69ed5c2
--- /dev/null
+++ b/src/view/burndown/C3ChartView.php
@@ -0,0 +1,56 @@
+<?php
+
+final class C3ChartView {
+  private $data;
+  private $project;
+  private $timeseries;
+
+  public function setChartData($chart_data) {
+    $this->data = $chart_data;
+    return $this;
+  }
+
+  public function setTimeSeries($timeseries) {
+    $this->timeseries = $timeseries;
+    return $this;
+  }
+
+  public function setProject($project) {
+    $this->project = $project;
+    return $this;
+  }
+
+  public function buildC3Chart() {
+    $totalpoints = $this->data[0];
+    $remainingpoints = $this->data[1];
+    $idealpoints = $this->data[2];
+    $pointstoday = $this->data[3];
+    $timeseries = $this->timeseries;
+
+    require_celerity_resource('d3', 'sprint');
+    require_celerity_resource('c3-css', 'sprint');
+    require_celerity_resource('c3', 'sprint');
+
+    $id = 'chart';
+    Javelin::initBehavior('c3-chart', array(
+        'hardpoint' => $id,
+        'timeseries' => $timeseries,
+        'totalpoints' => $totalpoints,
+        'remainingpoints' => $remainingpoints,
+        'idealpoints' => $idealpoints,
+        'pointstoday' => $pointstoday
+    ), 'sprint');
+
+    $chart = id(new PHUIObjectBoxView())
+        ->setHeaderText(pht('Burndown for ' . $this->project->getName()))
+        ->appendChild(phutil_tag('div',
+            array(
+                'id' => 'chart',
+                'style' => 'width: 100%; height:400px'
+            ), ''));
+
+    return $chart;
+  }
+
+
+}
\ No newline at end of file
diff --git a/src/view/burndown/EventTableView.php 
b/src/view/burndown/EventTableView.php
index 6aed0bf..e836497 100644
--- a/src/view/burndown/EventTableView.php
+++ b/src/view/burndown/EventTableView.php
@@ -4,6 +4,8 @@
   private $project;
   private $viewer;
   private $request;
+  private $events;
+  private $tasks;
 
   public function setProject ($project) {
     $this->project = $project;
@@ -15,15 +17,25 @@
     return $this;
   }
 
+  public function setEvents ($events) {
+    $this->events = $events;
+    return $this;
+  }
+
+  public function setTasks ($tasks) {
+    $this->tasks = $tasks;
+    return $this;
+  }
+
   public function setRequest ($request) {
     $this->request =  $request;
     return $this;
   }
 
-  public function buildEventTable($events, $xactions, $tasks, $start, $end) {
+  public function buildEventTable($start, $end) {
     $order = $this->request->getStr('ord', 'name');
     list($order, $reverse) = AphrontTableView::parseSort($order);
-    $rows = $this->buildEventsTree($events, $xactions, $tasks,  $start, $end,
+    $rows = $this->buildEventsTree($start, $end,
         $order, $reverse);
     $table = id(new AphrontTableView($rows))
         ->setHeaders(
@@ -67,16 +79,15 @@
     return $box;
   }
 
-  private function buildEventsTree ($events, $xactions, $tasks,  $start, $end,
+  private function buildEventsTree ($start, $end,
                                     $order, $reverse) {
 
     $rows = array();
-    foreach ($events as $event) {
-      $xaction = $xactions[$event['transactionPHID']];
-      $xaction_date = $xaction->getDateCreated();
+    foreach ($this->events as $event) {
+      $xaction_date = $event['epoch'];
       if ($xaction_date > $start && $xaction_date < $end) {
-        $task_phid = $xaction->getObjectPHID();
-        $task = $tasks[$task_phid];
+        $task_phid = $event['objectPHID'];
+        $task = $this->tasks[$task_phid];
         $rows[] = array(
             $event['epoch'],
             phabricator_datetime($event['epoch'], $this->viewer),
diff --git a/src/view/burndown/HistoryTableView.php 
b/src/view/burndown/HistoryTableView.php
index db5e7c3..3a46426 100644
--- a/src/view/burndown/HistoryTableView.php
+++ b/src/view/burndown/HistoryTableView.php
@@ -1,11 +1,10 @@
 <?php
 
-final class HistoryTableView
-{
+final class HistoryTableView {
 
-  public function buildHistoryTable($before)
-  {
-        $bdata[] = array(
+  public function buildHistoryTable($before) {
+    $bdata = array();
+    $bdata[] = array(
             $before->getTasksAddedBefore(),
             $before->getTasksReopenedBefore(),
             $before->getTasksClosedBefore(),
diff --git a/src/view/burndown/SprintDataView.php 
b/src/view/burndown/SprintDataView.php
index c1a0c72..d0d8701 100644
--- a/src/view/burndown/SprintDataView.php
+++ b/src/view/burndown/SprintDataView.php
@@ -41,11 +41,16 @@
         ->setViewer($this->viewer)
         ->setPHID($this->project->getPHID());
 
-    $this->taskpoints = 
$query->getTaskData(SprintConstants::CUSTOMFIELD_INDEX);
+    $this->taskpoints = $query->getTaskData();
     $tasks = $query->getTasks();
     $this->tasks = mpull($tasks, null, 'getPHID');
 
-    $chart = $this->buildC3Chart($query);
+    $chart_data = $this->buildChartDataSet($query);
+    $chart_view = id(new C3ChartView())
+        ->setChartData($chart_data)
+        ->setProject($this->project)
+        ->setTimeSeries($this->timeseries);
+    $chart = $chart_view->buildC3Chart();
 
     $tasks_table_view = id(new TasksTableView())
         ->setProject($this->project)
@@ -54,21 +59,25 @@
         ->setTasks($this->tasks)
         ->setTaskPoints($this->taskpoints)
         ->setQuery($query);
-
     $tasks_table = $tasks_table_view->buildTasksTable();
+
     $pie = $this->buildC3Pie();
     $history_table_view = new HistoryTableView();
     $history_table = $history_table_view->buildHistoryTable($this->before);
+
     $sprint_table_view = new SprintTableView();
     $sprint_table = $sprint_table_view->buildSprintTable($this->sprint_data,
         $this->before);
+
     $event_table_view = id(new EventTableView())
         ->setProject($this->project)
         ->setViewer($this->viewer)
-        ->setRequest($this->request);
-    $event_table = $event_table_view->buildEventTable($this->events,
-        $this->xactions,
-        $this->tasks, $this->start, $this->end);
+        ->setRequest($this->request)
+        ->setEvents($this->events)
+        ->setTasks($this->tasks);
+    $event_table = $event_table_view->buildEventTable(
+        $this->start, $this->end);
+
     return array($chart, $tasks_table, $pie, $history_table, $sprint_table,
         $event_table);
   }
@@ -97,64 +106,13 @@
         ->setTasks($this->tasks)
         ->setTaskPoints($this->taskpoints);
 
-    $sprint_xaction->buildStatArrays();
-
-    $dates = $sprint_xaction->buildDailyData($this->events, $this->before,
+    $dates = $sprint_xaction->parseEvents($this->events, $this->before,
         $this->start, $this->end, $dates, $this->xactions, $this->project);
 
     $this->sprint_data = $stats->setSprintData($dates, $this->before);
     $data = $stats->buildDataSet($this->sprint_data);
-    $data = $this->transposeArray($data);
+    $data = $stats->transposeArray($data);
     return $data;
-  }
-
-  private function transposeArray($array) {
-    $transposed_array = array();
-    if ($array) {
-      foreach ($array as $row_key => $row) {
-        if (is_array($row) && !empty($row)) {
-          foreach ($row as $column_key => $element) {
-            $transposed_array[$column_key][$row_key] = $element;
-          }
-        } else {
-          $transposed_array[0][$row_key] = $row;
-        }
-      }
-    }
-    return $transposed_array;
-  }
-
-  private function buildC3Chart($query) {
-    $data = $this->buildChartDataSet($query);
-    $totalpoints = $data[0];
-    $remainingpoints = $data[1];
-    $idealpoints = $data[2];
-    $pointstoday = $data[3];
-    $timeseries = $this->timeseries;
-
-    require_celerity_resource('d3', 'sprint');
-    require_celerity_resource('c3-css', 'sprint');
-    require_celerity_resource('c3', 'sprint');
-
-    $id = 'chart';
-    Javelin::initBehavior('c3-chart', array(
-        'hardpoint' => $id,
-        'timeseries' => $timeseries,
-        'totalpoints' => $totalpoints,
-        'remainingpoints' => $remainingpoints,
-        'idealpoints' => $idealpoints,
-        'pointstoday' => $pointstoday
-    ), 'sprint');
-
-    $chart = id(new PHUIObjectBoxView())
-        ->setHeaderText(pht('Burndown for ' . $this->project->getName()))
-        ->appendChild(phutil_tag('div',
-            array(
-                'id' => 'chart',
-                'style' => 'width: 100%; height:400px'
-            ), ''));
-
-    return $chart;
   }
 
   private function buildC3Pie() {
diff --git a/src/view/burndown/TasksTableView.php 
b/src/view/burndown/TasksTableView.php
index 322ade3..b863d23 100644
--- a/src/view/burndown/TasksTableView.php
+++ b/src/view/burndown/TasksTableView.php
@@ -39,13 +39,6 @@
     return $this;
   }
 
-  public function getTaskOpenStatusSum () {
-    return $this->task_open_status_sum;
-  }
-
-  public function getTaskClosedStatusSum () {
-    return $this->task_closed_status_sum;
-  }
   /**
    * Format the tasks data for display on the page.
    *

-- 
To view, visit https://gerrit.wikimedia.org/r/181938
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I836dc4b8762f928f88ac391263d2a1756920095b
Gerrit-PatchSet: 1
Gerrit-Project: phabricator/extensions/Sprint
Gerrit-Branch: master
Gerrit-Owner: Christopher Johnson (WMDE) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to