StatsController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 45
c 3
b 0
f 0
dl 0
loc 114
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A updateAfterExpression() 0 3 1
A actionUpdate() 0 4 1
A actionUpdateTag() 0 15 2
A actionUpdateTags() 0 23 4
A actionUpdateAccounts() 0 21 3
A actionUpdateAccount() 0 15 2
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 11.01.2018
6
 */
7
8
namespace app\commands;
9
10
11
use app\components\JobFactory;
12
use app\models\Account;
13
use app\models\Tag;
14
use Yii;
15
use yii\console\Controller;
16
use yii\console\ExitCode;
17
use yii\db\Expression;
18
use yii\helpers\Console;
19
20
class StatsController extends Controller
21
{
22
    public function actionUpdate($force = 0)
23
    {
24
        $this->actionUpdateAccounts($force);
25
        $this->actionUpdateTags($force);
26
    }
27
28
    /**
29
     * Create update jobs.
30
     *
31
     * @param int $force ignore interval
32
     * @return int
33
     */
34
    public function actionUpdateAccounts($force = 0)
35
    {
36
        $query = Account::find()
37
            ->select('id')
38
            ->monitoring();
39
40
        if (!$force) {
41
            $query->andWhere(['or',
42
                $this->updateAfterExpression(),
43
                ['update_stats_after' => null],
44
            ]);
45
        }
46
47
        /** @var \yii\queue\Queue $queue */
48
        $queue = Yii::$app->queue;
49
        foreach ($query->column() as $accountId) {
50
            $queue->push(JobFactory::updateAccount($accountId));
51
        }
52
        $this->stdout("Accounts - OK!\n");
53
54
        return ExitCode::OK;
55
    }
56
57
    public function actionUpdateAccount($username)
58
    {
59
        $account = Account::findOne(['username' => $username]);
60
        if ($account === null) {
61
            $this->stdout("Account '$username' not found.\n", Console::FG_RED);
62
63
            return ExitCode::UNSPECIFIED_ERROR;
64
        }
65
66
        /** @var \yii\queue\Queue $queue */
67
        $queue = Yii::$app->queue;
68
        $queue->push(JobFactory::updateAccount($account->id));
69
        $this->stdout("OK!\n");
70
71
        return ExitCode::OK;
72
    }
73
74
    /**
75
     * Create update jobs.
76
     *
77
     * @param int $force ignore interval
78
     * @return int
79
     */
80
    public function actionUpdateTags($force = 0)
81
    {
82
        $query = Tag::find()
83
            ->select('id')
84
            ->monitoring();
85
86
        if (!$force) {
87
            if (!$force) {
88
                $query->andWhere(['or',
89
                    $this->updateAfterExpression(),
90
                    ['update_stats_after' => null],
91
                ]);
92
            }
93
        }
94
95
        /** @var \yii\queue\Queue $queue */
96
        $queue = Yii::$app->queue;
97
        foreach ($query->column() as $tagId) {
98
            $queue->push(JobFactory::updateTag($tagId));
99
        }
100
        $this->stdout("Tags - OK!\n");
101
102
        return ExitCode::OK;
103
    }
104
105
    /**
106
     * Update on run.
107
     *
108
     * @param $name
109
     * @return int
110
     */
111
    public function actionUpdateTag($name)
112
    {
113
        $tag = Tag::findOne(['name' => $name]);
114
        if ($tag === null) {
115
            $this->stdout("Tag '$name' not found.\n", Console::FG_RED);
116
117
            return ExitCode::UNSPECIFIED_ERROR;
118
        }
119
120
        /** @var \yii\queue\Queue $queue */
121
        $queue = Yii::$app->queue;
122
        $queue->push(JobFactory::updateTag($tag->id));
123
        $this->stdout("OK!\n");
124
125
        return ExitCode::OK;
126
    }
127
128
    /**
129
     * @return Expression
130
     */
131
    protected function updateAfterExpression(): Expression
132
    {
133
        return new Expression('DATE_FORMAT(update_stats_after, \'%Y-%m-%d %H\') < DATE_FORMAT(NOW(), \'%Y-%m-%d %H\')');
134
    }
135
}