summaryrefslogtreecommitdiff
path: root/app/Model/ProjectAnalytic.php
blob: 34d152d1287b009a58adab48e45a192e1ff5e78a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php

namespace Kanboard\Model;

/**
 * Project analytic model
 *
 * @package  model
 * @author   Frederic Guillot
 */
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->projectPermission->getMemberList($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
     *
     * @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'] = $stats['count'] > 0 ? (int) ($stats['total_lead_time'] / $stats['count']) : 0;
        $stats['avg_cycle_time'] = $stats['count'] > 0 ? (int) ($stats['total_cycle_time'] / $stats['count']) : 0;

        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) {
                if (isset($stats[$column_id])) {
                    $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'] = $stats[$column_id]['count'] > 0 ? (int) ($stats[$column_id]['time_spent'] / $stats[$column_id]['count']) : 0;
        }

        return $stats;
    }
}