Passed
Push — master ( 7ba57e...634515 )
by Paweł
03:13
created

components/stats/TagDailyDiff.php (1 issue)

1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 19.06.2018
6
 */
7
8
namespace app\components\stats;
9
10
11
use app\components\stats\base\TagDiff;
12
use Carbon\Carbon;
13
use yii\db\Expression;
14
use yii\helpers\ArrayHelper;
15
16
/**
17
 * @inheritdoc
18
 *
19
 * @property array|\app\models\Tag[] $models
20
 */
21
class TagDailyDiff extends TagDiff
22
{
23
    public function initLastDiff()
24
    {
25
        $modelIds = $this->getModelIds();
26
        $max1Ids = $this->getLastStatsIds(null, null, $modelIds);
27
        $max2Ids = $this->getLastStatsBeforeIds(null, null, $modelIds, 'tag_stats.tag_id', 1, $max1Ids);
28
29
        $tagsStats = $this->getTagsStats($modelIds, ArrayHelper::merge($max1Ids, $max2Ids));
30
        $this->lastDiffCache = $this->prepareCache($tagsStats, 'tag_id');
31
    }
32
33
    /**
34
     * The difference between the data from subsequent days from a given date range.
35
     *
36
     * @param $olderDate
37
     * @param null $newerDate
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $newerDate is correct as it would always require null to be passed?
Loading history...
38
     */
39
    public function initDiff($olderDate, $newerDate = null)
40
    {
41
        $olderDate = (new Carbon($olderDate))->subDay()->startOfDay()->toDateTimeString();
42
        $newerDate = (new Carbon($newerDate))->endOfDay()->toDateTimeString();
43
44
        if (empty($this->diffCache)) {
45
            $tagIds = $this->getModelIds();
46
47
            //group by DATE Y-m-d
48
            $ids = $this->getLastStatsIds($olderDate, $newerDate, $tagIds, [
49
                'tag_id',
50
                new Expression('DATE(created_at)'),
51
            ]);
52
53
            $tagsStats = $this->getTagsStats($tagIds, $ids);
54
55
            $this->diffCache = $this->prepareCache($tagsStats, 'tag_id');
56
        }
57
    }
58
}