From 663a1c20e6ba0fbf65afcb43f0f48d34f21dcb53 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 5 Jul 2015 21:22:31 -0400 Subject: Add new analytic page: Average time spent into each column --- app/Controller/Analytic.php | 18 +++++++++- app/Controller/Task.php | 2 +- app/Helper/Dt.php | 4 +++ app/Model/ProjectAnalytic.php | 57 +++++++++++++++++++++++++++++- app/Model/TaskAnalytic.php | 13 +++---- app/Model/Transition.php | 4 +-- app/Template/analytic/avg_time_columns.php | 29 +++++++++++++++ app/Template/analytic/sidebar.php | 3 ++ app/Template/task/analytics.php | 18 ++++++---- 9 files changed, 128 insertions(+), 20 deletions(-) create mode 100644 app/Template/analytic/avg_time_columns.php (limited to 'app') diff --git a/app/Controller/Analytic.php b/app/Controller/Analytic.php index 2413ba92..010fda09 100644 --- a/app/Controller/Analytic.php +++ b/app/Controller/Analytic.php @@ -3,7 +3,7 @@ namespace Controller; /** - * Project Anaytic controller + * Project Analytic controller * * @package controller * @author Frederic Guillot @@ -26,6 +26,22 @@ class Analytic extends Base return $this->template->layout('analytic/layout', $params); } + /** + * Show average time spent by column + * + * @access public + */ + public function averageTimeByColumn() + { + $project = $this->getProject(); + + $this->response->html($this->layout('analytic/avg_time_columns', array( + 'project' => $project, + 'metrics' => $this->projectAnalytic->getAverageTimeSpentByColumn($project['id']), + 'title' => t('Average time spent into each column for "%s"', $project['name']), + ))); + } + /** * Show tasks distribution graph * diff --git a/app/Controller/Task.php b/app/Controller/Task.php index b6e4845f..2d4f783a 100644 --- a/app/Controller/Task.php +++ b/app/Controller/Task.php @@ -101,7 +101,7 @@ class Task extends Base 'task' => $task, 'lead_time' => $this->taskAnalytic->getLeadTime($task), 'cycle_time' => $this->taskAnalytic->getCycleTime($task), - 'column_averages' => $this->taskAnalytic->getAverageTimeByColumn($task), + 'time_spent_columns' => $this->taskAnalytic->getTimeSpentByColumn($task), ))); } diff --git a/app/Helper/Dt.php b/app/Helper/Dt.php index be595605..b338fdc8 100644 --- a/app/Helper/Dt.php +++ b/app/Helper/Dt.php @@ -21,6 +21,10 @@ class Dt extends \Core\Base */ public function duration($seconds) { + if ($seconds == 0) { + return 0; + } + $dtF = new DateTime("@0"); $dtT = new DateTime("@$seconds"); return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds'); diff --git a/app/Model/ProjectAnalytic.php b/app/Model/ProjectAnalytic.php index a663f921..f4e8af09 100644 --- a/app/Model/ProjectAnalytic.php +++ b/app/Model/ProjectAnalytic.php @@ -49,7 +49,7 @@ class ProjectAnalytic extends Base * Get users repartition * * @access public - * @param integer $project_id Project id + * @param integer $project_id * @return array */ public function getUserRepartition($project_id) @@ -87,4 +87,59 @@ class ProjectAnalytic extends Base return array_values($metrics); } + + /** + * Get the average time spent into each column + * + * @access public + * @param integer $project_id + * @return array + */ + public function getAverageTimeSpentByColumn($project_id) + { + $stats = array(); + $columns = $this->board->getColumnsList($project_id); + + // Get the time spent of the last move for each tasks + $tasks = $this->db + ->table(Task::TABLE) + ->columns('id', 'date_completed', 'date_moved', 'column_id') + ->eq('project_id', $project_id) + ->desc('id') + ->limit(1000) + ->findAll(); + + // Init values + foreach ($columns as $column_id => $column_title) { + $stats[$column_id] = array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => $column_title, + ); + } + + // Get time spent foreach task/column and take into account the last move + foreach ($tasks as &$task) { + $sums = $this->transition->getTimeSpentByTask($task['id']); + + if (! isset($sums[$task['column_id']])) { + $sums[$task['column_id']] = 0; + } + + $sums[$task['column_id']] += ($task['date_completed'] ?: time()) - $task['date_moved']; + + foreach ($sums as $column_id => $time_spent) { + $stats[$column_id]['count']++; + $stats[$column_id]['time_spent'] += $time_spent; + } + } + + // Calculate average for each column + foreach ($columns as $column_id => $column_title) { + $stats[$column_id]['average'] = (int) ($stats[$column_id]['time_spent'] / $stats[$column_id]['count']); + } + + return $stats; + } } diff --git a/app/Model/TaskAnalytic.php b/app/Model/TaskAnalytic.php index 41579c7d..33a645c1 100644 --- a/app/Model/TaskAnalytic.php +++ b/app/Model/TaskAnalytic.php @@ -45,21 +45,18 @@ class TaskAnalytic extends Base * @param array $task * @return array */ - public function getAverageTimeByColumn(array $task) + public function getTimeSpentByColumn(array $task) { $result = array(); $columns = $this->board->getColumnsList($task['project_id']); - $averages = $this->transition->getAverageTimeSpentByTask($task['id']); + $sums = $this->transition->getTimeSpentByTask($task['id']); foreach ($columns as $column_id => $column_title) { - $time_spent = 0; + $time_spent = isset($sums[$column_id]) ? $sums[$column_id] : 0; - if (empty($averages) && $task['column_id'] == $column_id) { - $time_spent = time() - $task['date_creation']; - } - else { - $time_spent = isset($averages[$column_id]) ? $averages[$column_id] : 0; + if ($task['column_id'] == $column_id) { + $time_spent += ($task['date_completed'] ?: time()) - $task['date_moved']; } $result[] = array( diff --git a/app/Model/Transition.php b/app/Model/Transition.php index 959b6aca..ac3fba54 100644 --- a/app/Model/Transition.php +++ b/app/Model/Transition.php @@ -39,13 +39,13 @@ class Transition extends Base } /** - * Get average time spent by task for each column + * Get time spent by task for each column * * @access public * @param integer $task_id * @return array */ - public function getAverageTimeSpentByTask($task_id) + public function getTimeSpentByTask($task_id) { return $this->db ->hashtable(self::TABLE) diff --git a/app/Template/analytic/avg_time_columns.php b/app/Template/analytic/avg_time_columns.php new file mode 100644 index 00000000..e74e7950 --- /dev/null +++ b/app/Template/analytic/avg_time_columns.php @@ -0,0 +1,29 @@ + + + +

+ +
+ +
+ + + + + + + + + + + + +
e($column['title']) ?>dt->duration($column['average']) ?>
+ +

+ +

+
+ diff --git a/app/Template/analytic/sidebar.php b/app/Template/analytic/sidebar.php index 2d1a7c96..1ffce710 100644 --- a/app/Template/analytic/sidebar.php +++ b/app/Template/analytic/sidebar.php @@ -13,5 +13,8 @@
  • url->link(t('Burndown chart'), 'analytic', 'burndown', array('project_id' => $project['id'])) ?>
  • +
  • + url->link(t('Average time into each column'), 'analytic', 'averageTimeByColumn', array('project_id' => $project['id'])) ?> +
  • \ No newline at end of file diff --git a/app/Template/task/analytics.php b/app/Template/task/analytics.php index dbee0e9c..3b1d2855 100644 --- a/app/Template/task/analytics.php +++ b/app/Template/task/analytics.php @@ -9,13 +9,14 @@ -

    +

    +
    - + - + @@ -25,8 +26,11 @@
    -
    \ No newline at end of file + + +asset->js('assets/js/vendor/d3.v3.min.js') ?> +asset->js('assets/js/vendor/c3.min.js') ?> \ No newline at end of file -- cgit v1.2.3
    e($column['title']) ?> dt->duration($column['time_spent']) ?>