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\Model; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Database\Model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Project Daily Stats. |
18
|
|
|
*/ |
19
|
|
|
class ProjectDailyStatsModel extends Model |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* SQL table name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const TABLE = 'project_daily_stats'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Update daily totals for the project. |
30
|
|
|
* |
31
|
|
|
* @param int $project_id Project id |
32
|
|
|
* @param string $date Record date (YYYY-MM-DD) |
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function updateTotals($project_id, $date) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$this->db->startTransaction(); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$lead_cycle_time = $this->averageLeadCycleTimeAnalytic->build($project_id); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$this->db->table(self::TABLE)->eq('day', $date)->eq('project_id', $project_id)->remove(); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$this->db->table(self::TABLE)->insert([ |
|
|
|
|
45
|
|
|
'day' => $date, |
46
|
|
|
'project_id' => $project_id, |
47
|
|
|
'avg_lead_time' => $lead_cycle_time['avg_lead_time'], |
48
|
|
|
'avg_cycle_time' => $lead_cycle_time['avg_cycle_time'], |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
$this->db->closeTransaction(); |
|
|
|
|
52
|
|
|
|
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get raw metrics for the project within a data range. |
58
|
|
|
* |
59
|
|
|
* @param int $project_id Project id |
60
|
|
|
* @param string $from Start date (ISO format YYYY-MM-DD) |
61
|
|
|
* @param string $to End date |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function getRawMetrics($project_id, $from, $to) |
66
|
|
|
{ |
67
|
|
|
$metrics = $this->db->table(self::TABLE) |
|
|
|
|
68
|
|
|
->columns('day', 'avg_lead_time', 'avg_cycle_time') |
69
|
|
|
->eq('project_id', $project_id) |
70
|
|
|
->gte('day', $from) |
71
|
|
|
->lte('day', $to) |
72
|
|
|
->asc('day') |
73
|
|
|
->findAll(); |
74
|
|
|
|
75
|
|
|
foreach ($metrics as &$metric) { |
76
|
|
|
$metric['avg_lead_time'] = (int) $metric['avg_lead_time']; |
77
|
|
|
$metric['avg_cycle_time'] = (int) $metric['avg_cycle_time']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $metrics; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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.