Passed
Push — master ( 8cf58c...3a8f57 )
by Paweł
02:46
created

StatsController::updateAfterExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\console\Controller;
15
use yii\console\ExitCode;
16
use yii\db\Expression;
17
use yii\helpers\Console;
18
19
class StatsController extends Controller
20
{
21
    /**
22
     * Create update jobs.
23
     *
24
     * @param int $force ignore interval
25
     * @return int
26
     */
27
    public function actionUpdateTags($force = 0)
28
    {
29
        $query = Tag::find()
30
            ->select('id')
31
            ->monitoring();
32
33
        if (!$force) {
34
            if (!$force) {
35
                $query->andWhere(['or',
36
                    $this->updateAfterExpression(),
37
                    ['update_stats_after' => null],
38
                ]);
39
            }
40
        }
41
42
        /** @var \yii\queue\Queue $queue */
43
        $queue = \Yii::$app->queue;
44
        foreach ($query->column() as $tagId) {
45
            $queue->push(JobFactory::createTagUpdate($tagId));
46
        }
47
        $this->stdout("OK!\n");
48
49
        return ExitCode::OK;
50
    }
51
52
    /**
53
     * Update on run.
54
     *
55
     * @param $name
56
     * @return int
57
     */
58
    public function actionUpdateTag($name)
59
    {
60
        $tag = Tag::findOne(['name' => $name]);
61
        if ($tag === null) {
62
            $this->stdout("Tag '$name' not found.\n", Console::FG_RED);
63
64
            return ExitCode::UNSPECIFIED_ERROR;
65
        }
66
67
        /** @var \yii\queue\Queue $queue */
68
        $queue = \Yii::$app->queue;
69
        $queue->push(JobFactory::createTagUpdate($tag->id));
70
        $this->stdout("OK!\n");
71
72
        return ExitCode::OK;
73
    }
74
75
    /**
76
     * Create update jobs.
77
     *
78
     * @param int $force ignore interval
79
     * @return int
80
     */
81
    public function actionUpdateAccounts($force = 0)
82
    {
83
        $query = Account::find()
84
            ->select('id')
85
            ->monitoring();
86
87
        if (!$force) {
88
            $query->andWhere(['or',
89
                $this->updateAfterExpression(),
90
                ['update_stats_after' => null],
91
            ]);
92
        }
93
94
        /** @var \yii\queue\Queue $queue */
95
        $queue = \Yii::$app->queue;
96
        foreach ($query->column() as $accountId) {
97
            $queue->push(JobFactory::createAccountUpdate($accountId));
98
        }
99
        $this->stdout("OK!\n");
100
101
        return ExitCode::OK;
102
    }
103
104
    public function actionUpdateAccount($username)
105
    {
106
        $account = Account::findOne(['username' => $username]);
107
        if ($account === null) {
108
            $this->stdout("Account '$username' not found.\n", Console::FG_RED);
109
110
            return ExitCode::UNSPECIFIED_ERROR;
111
        }
112
113
        /** @var \yii\queue\Queue $queue */
114
        $queue = \Yii::$app->queue;
115
        $queue->push(JobFactory::createAccountUpdate($account->id));
116
        $this->stdout("OK!\n");
117
118
        return ExitCode::OK;
119
    }
120
121
    /**
122
     * @return Expression
123
     */
124
    protected function updateAfterExpression(): Expression
125
    {
126
        return new Expression('DATE_FORMAT(update_stats_after, \'%Y-%m-%d %H\') < DATE_FORMAT(NOW(), \'%Y-%m-%d %H\')');
127
    }
128
}