summaryrefslogtreecommitdiff
path: root/app/Model/ProjectDailyStats.php
blob: 7ec1ee2cb0c0235a0e1dc20a08ac5b2f4048fc58 (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
<?php

namespace Kanboard\Model;

use PicoDb\Database;

/**
 * Project Daily Stats
 *
 * @package  model
 * @author   Frederic Guillot
 */
class ProjectDailyStats extends Base
{
    /**
     * SQL table name
     *
     * @var string
     */
    const TABLE = 'project_daily_stats';

    /**
     * Update daily totals for the project
     *
     * @access public
     * @param  integer    $project_id    Project id
     * @param  string     $date          Record date (YYYY-MM-DD)
     * @return boolean
     */
    public function updateTotals($project_id, $date)
    {
        $this->db->startTransaction();

        $lead_cycle_time = $this->projectAnalytic->getAverageLeadAndCycleTime($project_id);

        $exists = $this->db->table(ProjectDailyStats::TABLE)
            ->eq('day', $date)
            ->eq('project_id', $project_id)
            ->exists();

        if ($exists) {
            $this->db->table(ProjectDailyStats::TABLE)
                ->eq('project_id', $project_id)
                ->eq('day', $date)
                ->update(array(
                    'avg_lead_time' => $lead_cycle_time['avg_lead_time'],
                    'avg_cycle_time' => $lead_cycle_time['avg_cycle_time'],
                ));
        } else {
            $this->db->table(ProjectDailyStats::TABLE)->insert(array(
                'day' => $date,
                'project_id' => $project_id,
                'avg_lead_time' => $lead_cycle_time['avg_lead_time'],
                'avg_cycle_time' => $lead_cycle_time['avg_cycle_time'],
            ));
        }

        $this->db->closeTransaction();

        return true;
    }

    /**
     * Get raw metrics for the project within a data range
     *
     * @access public
     * @param  integer    $project_id    Project id
     * @param  string     $from          Start date (ISO format YYYY-MM-DD)
     * @param  string     $to            End date
     * @return array
     */
    public function getRawMetrics($project_id, $from, $to)
    {
        return $this->db->table(self::TABLE)
                        ->columns('day', 'avg_lead_time', 'avg_cycle_time')
                        ->eq(self::TABLE.'.project_id', $project_id)
                        ->gte('day', $from)
                        ->lte('day', $to)
                        ->asc(self::TABLE.'.day')
                        ->findAll();
    }
}