1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Analytic; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
use Jitamin\Model\TaskModel; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Average Lead and Cycle Time. |
19
|
|
|
*/ |
20
|
|
|
class AverageLeadCycleTimeAnalytic extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Build report. |
24
|
|
|
* |
25
|
|
|
* @param int $project_id Project id |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function build($project_id) |
30
|
|
|
{ |
31
|
|
|
$stats = [ |
32
|
|
|
'count' => 0, |
33
|
|
|
'total_lead_time' => 0, |
34
|
|
|
'total_cycle_time' => 0, |
35
|
|
|
'avg_lead_time' => 0, |
36
|
|
|
'avg_cycle_time' => 0, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
$tasks = $this->getTasks($project_id); |
40
|
|
|
|
41
|
|
|
foreach ($tasks as &$task) { |
42
|
|
|
$stats['count']++; |
43
|
|
|
$stats['total_lead_time'] += $this->calculateLeadTime($task); |
44
|
|
|
$stats['total_cycle_time'] += $this->calculateCycleTime($task); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$stats['avg_lead_time'] = $this->calculateAverage($stats, 'total_lead_time'); |
48
|
|
|
$stats['avg_cycle_time'] = $this->calculateAverage($stats, 'total_cycle_time'); |
49
|
|
|
|
50
|
|
|
return $stats; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Calculate average. |
55
|
|
|
* |
56
|
|
|
* @param array &$stats |
57
|
|
|
* @param string $field |
58
|
|
|
* |
59
|
|
|
* @return float |
60
|
|
|
*/ |
61
|
|
|
private function calculateAverage(array &$stats, $field) |
62
|
|
|
{ |
63
|
|
|
if ($stats['count'] > 0) { |
64
|
|
|
return (int) ($stats[$field] / $stats['count']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return 0; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Calculate lead time. |
72
|
|
|
* |
73
|
|
|
* @param array &$task |
74
|
|
|
* |
75
|
|
|
* @return int |
76
|
|
|
*/ |
77
|
|
|
private function calculateLeadTime(array &$task) |
78
|
|
|
{ |
79
|
|
|
$end = $task['date_completed'] ?: time(); |
80
|
|
|
$start = $task['date_creation']; |
81
|
|
|
|
82
|
|
|
return $end - $start; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Calculate cycle time. |
87
|
|
|
* |
88
|
|
|
* @param array &$task |
89
|
|
|
* |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
private function calculateCycleTime(array &$task) |
93
|
|
|
{ |
94
|
|
|
$end = (int) $task['date_completed'] ?: time(); |
95
|
|
|
$start = (int) $task['date_started']; |
96
|
|
|
|
97
|
|
|
// Start date can be in the future when defined with the Gantt chart |
98
|
|
|
if ($start > 0 && $end > $start) { |
99
|
|
|
return $end - $start; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return 0; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the 1000 last created tasks. |
107
|
|
|
* |
108
|
|
|
* @param int $project_id |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
private function getTasks($project_id) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
return $this->db |
|
|
|
|
115
|
|
|
->table(TaskModel::TABLE) |
116
|
|
|
->columns('date_completed', 'date_creation', 'date_started') |
117
|
|
|
->eq('project_id', $project_id) |
118
|
|
|
->desc('id') |
119
|
|
|
->limit(1000) |
120
|
|
|
->findAll(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.