diff options
Diffstat (limited to 'app/Model/ProjectAnalytic.php')
-rw-r--r-- | app/Model/ProjectAnalytic.php | 94 |
1 files changed, 93 insertions, 1 deletions
diff --git a/app/Model/ProjectAnalytic.php b/app/Model/ProjectAnalytic.php index a663f921..1ee8a405 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,96 @@ class ProjectAnalytic extends Base return array_values($metrics); } + + /** + * Get the average lead and cycle time + * + * @access public + * @param integer $project_id + * @return array + */ + public function getAverageLeadAndCycleTime($project_id) + { + $stats = array( + 'count' => 0, + 'total_lead_time' => 0, + 'total_cycle_time' => 0, + 'avg_lead_time' => 0, + 'avg_cycle_time' => 0, + ); + + $tasks = $this->db + ->table(Task::TABLE) + ->columns('date_completed', 'date_creation', 'date_started') + ->eq('project_id', $project_id) + ->desc('id') + ->limit(1000) + ->findAll(); + + foreach ($tasks as &$task) { + $stats['count']++; + $stats['total_lead_time'] += ($task['date_completed'] ?: time()) - $task['date_creation']; + $stats['total_cycle_time'] += empty($task['date_started']) ? 0 : ($task['date_completed'] ?: time()) - $task['date_started']; + } + + $stats['avg_lead_time'] = (int) ($stats['total_lead_time'] / $stats['count']); + $stats['avg_cycle_time'] = (int) ($stats['total_cycle_time'] / $stats['count']); + + return $stats; + } + + /** + * 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; + } } |