Move analytic logic to separate classes

This commit is contained in:
Frederic Guillot
2016-01-15 21:03:19 -05:00
parent 1bbd4faf56
commit c58478c0ab
13 changed files with 424 additions and 134 deletions

View File

@@ -10,82 +10,6 @@ namespace Kanboard\Model;
*/
class ProjectAnalytic extends Base
{
/**
* Get tasks repartition
*
* @access public
* @param integer $project_id Project id
* @return array
*/
public function getTaskRepartition($project_id)
{
$metrics = array();
$total = 0;
$columns = $this->board->getColumns($project_id);
foreach ($columns as $column) {
$nb_tasks = $this->taskFinder->countByColumnId($project_id, $column['id']);
$total += $nb_tasks;
$metrics[] = array(
'column_title' => $column['title'],
'nb_tasks' => $nb_tasks,
);
}
if ($total === 0) {
return array();
}
foreach ($metrics as &$metric) {
$metric['percentage'] = round(($metric['nb_tasks'] * 100) / $total, 2);
}
return $metrics;
}
/**
* Get users repartition
*
* @access public
* @param integer $project_id
* @return array
*/
public function getUserRepartition($project_id)
{
$metrics = array();
$total = 0;
$tasks = $this->taskFinder->getAll($project_id);
$users = $this->projectUserRole->getAssignableUsersList($project_id);
foreach ($tasks as $task) {
$user = isset($users[$task['owner_id']]) ? $users[$task['owner_id']] : $users[0];
$total++;
if (! isset($metrics[$user])) {
$metrics[$user] = array(
'nb_tasks' => 0,
'percentage' => 0,
'user' => $user,
);
}
$metrics[$user]['nb_tasks']++;
}
if ($total === 0) {
return array();
}
foreach ($metrics as &$metric) {
$metric['percentage'] = round(($metric['nb_tasks'] * 100) / $total, 2);
}
ksort($metrics);
return array_values($metrics);
}
/**
* Get the average lead and cycle time
*
@@ -179,49 +103,4 @@ class ProjectAnalytic extends Base
return $stats;
}
/**
* Get the time spent and estimated into each status
*
* @access public
* @param integer $project_id
* @return array
*/
public function getHoursByStatus($project_id)
{
$stats = array();
// Get the times related to each task
$tasks = $this->db
->table(Task::TABLE)
->columns('id', 'time_estimated', 'time_spent', 'is_active')
->eq('project_id', $project_id)
->desc('id')
->limit(1000)
->findAll();
// Init values
$stats['closed'] = array(
'time_spent' => 0,
'time_estimated' => 0,
);
$stats['open'] = array(
'time_spent' => 0,
'time_estimated' => 0,
);
// Add times spent and estimated to each status
foreach ($tasks as &$task) {
if ($task['is_active']) {
$stats['open']['time_estimated'] += $task['time_estimated'];
$stats['open']['time_spent'] += $task['time_spent'];
} else {
$stats['closed']['time_estimated'] += $task['time_estimated'];
$stats['closed']['time_spent'] += $task['time_spent'];
}
}
return $stats;
}
}