Passed
Push — master ( 6b49ff...b93f48 )
by Paweł
05:24
created

AccountDiff::getAccountsStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 20.06.2018
6
 */
7
8
namespace app\components\stats\base;
9
10
11
use app\models\AccountStats;
12
use yii\db\Expression;
13
14
abstract class AccountDiff extends Diff
15
{
16
    protected $statsAttributes = [
17
        'media',
18
        'followed_by',
19
        'follows',
20
        'er',
21
    ];
22
23
    protected function getLastStatsIds($olderDate, $newerDate, $accountIds, $groupBy = 'account_stats.account_id', $ignoredIds = null)
24
    {
25
        $q = AccountStats::find()->cache(5)
26
            ->select(new Expression('MAX(id) as id'))
27
            ->andWhere(['account_id' => $accountIds])
28
            ->andFilterWhere(['>=', 'created_at', $olderDate])
29
            ->andFilterWhere(['<=', 'created_at', $newerDate])
30
            ->andFilterWhere(['not', ['id' => $ignoredIds]])
31
            ->groupBy($groupBy);
32
33
        return $q->column();
34
    }
35
36
    protected function getLastStatsBeforeIds($olderDate, $newerDate, $accountIds, $groupBy = 'account_stats.account_id', $beforeInDays = 1, $ignoredIds = null)
37
    {
38
        $q = AccountStats::find()
39
            ->select(new Expression('MAX(id) as id'))
40
            ->andWhere(['account_stats.account_id' => $accountIds])
41
            ->andFilterWhere(['>=', 'created_at', $olderDate])
42
            ->andFilterWhere(['<=', 'created_at', $newerDate])
43
            ->andFilterWhere(['not', ['account_stats.account_id' => $ignoredIds]])
44
            ->leftJoin([
45
                'as_max' => AccountStats::find()
46
                    ->select([
47
                        'account_id',
48
                        new Expression('MAX(created_at) as created_at'),
49
                    ])
50
                    ->andWhere(['account_id' => $accountIds])
51
                    ->groupBy('account_id'),
52
            ], 'account_stats.account_id=as_max.account_id')
53
            ->andWhere(['<', 'account_stats.created_at', new Expression(sprintf('DATE_SUB(DATE_SUB(DATE(as_max.created_at), interval %d DAY), interval 1 second)', $beforeInDays - 1))])
54
            ->groupBy($groupBy);
55
56
        return $q->column();
57
    }
58
59
    /**
60
     * @param $accountIds
61
     * @param $ids
62
     * @return array
63
     */
64
    protected function getAccountsStats($accountIds, $ids)
65
    {
66
        $accountsStats = AccountStats::find()
67
            ->select([
68
                'account_stats.*',
69
                new Expression('DATE(created_at) as created_at'),
70
            ])
71
            ->andWhere(['account_id' => $accountIds])
72
            ->andWhere(['id' => $ids])
73
            ->orderBy('account_id ASC, id ASC')
74
            ->asArray()
75
            ->all();
76
77
        return $accountsStats;
78
    }
79
}