|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created for IG Monitoring. |
|
4
|
|
|
* User: jakim <[email protected]> |
|
5
|
|
|
* Date: 19.01.2018 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace app\modules\admin\controllers; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use app\components\AccountManager; |
|
12
|
|
|
use app\components\JobFactory; |
|
13
|
|
|
use app\components\stats\AccountDailyDiff; |
|
14
|
|
|
use app\components\stats\AccountMonthlyDiff; |
|
15
|
|
|
use app\components\stats\TagDailyDiff; |
|
16
|
|
|
use app\components\stats\TagMonthlyDiff; |
|
17
|
|
|
use app\components\TagManager; |
|
18
|
|
|
use app\dictionaries\TrackerType; |
|
19
|
|
|
use app\models\Account; |
|
20
|
|
|
use app\models\Tag; |
|
21
|
|
|
use app\modules\admin\models\MonitoringForm; |
|
22
|
|
|
use app\modules\admin\models\AccountSearch; |
|
23
|
|
|
use app\modules\admin\models\TagSearch; |
|
24
|
|
|
use yii\filters\VerbFilter; |
|
25
|
|
|
use yii\helpers\StringHelper; |
|
26
|
|
|
use yii\helpers\Url; |
|
27
|
|
|
use yii\web\Controller; |
|
28
|
|
|
|
|
29
|
|
|
class MonitoringController extends Controller |
|
30
|
|
|
{ |
|
31
|
|
|
public function behaviors() |
|
32
|
|
|
{ |
|
33
|
|
|
return [ |
|
34
|
|
|
'verbs' => [ |
|
35
|
|
|
'class' => VerbFilter::class, |
|
36
|
|
|
'actions' => [ |
|
37
|
|
|
'create-account' => ['POST'], |
|
38
|
|
|
'delete-account' => ['POST'], |
|
39
|
|
|
'create-tag' => ['POST'], |
|
40
|
|
|
'delete-tag' => ['POST'], |
|
41
|
|
|
], |
|
42
|
|
|
], |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function actionAccounts() |
|
47
|
|
|
{ |
|
48
|
|
|
$searchModel = new AccountSearch(); |
|
49
|
|
|
$dataProvider = $searchModel->search(\Yii::$app->request->queryParams); |
|
50
|
|
|
$dataProvider->query->andWhere(['account.monitoring' => 1]); |
|
51
|
|
|
|
|
52
|
|
|
$dailyDiff = \Yii::createObject([ |
|
53
|
|
|
'class' => AccountDailyDiff::class, |
|
54
|
|
|
'models' => $dataProvider->models, |
|
55
|
|
|
]); |
|
56
|
|
|
$dailyDiff->initLastDiff(); |
|
57
|
|
|
|
|
58
|
|
|
$monthlyDiff = \Yii::createObject([ |
|
59
|
|
|
'class' => AccountMonthlyDiff::class, |
|
60
|
|
|
'models' => $dataProvider->models, |
|
61
|
|
|
]); |
|
62
|
|
|
$monthlyDiff->initLastDiff(); |
|
63
|
|
|
|
|
64
|
|
|
return $this->render('accounts', [ |
|
65
|
|
|
'searchModel' => $searchModel, |
|
66
|
|
|
'dataProvider' => $dataProvider, |
|
67
|
|
|
'dailyDiff' => $dailyDiff, |
|
68
|
|
|
'monthlyDiff' => $monthlyDiff, |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function actionTags() |
|
73
|
|
|
{ |
|
74
|
|
|
$searchModel = new TagSearch(); |
|
75
|
|
|
$dataProvider = $searchModel->search(\Yii::$app->request->queryParams); |
|
76
|
|
|
$dataProvider->query->andWhere(['tag.monitoring' => 1]); |
|
77
|
|
|
|
|
78
|
|
|
$dailyDiff = \Yii::createObject([ |
|
79
|
|
|
'class' => TagDailyDiff::class, |
|
80
|
|
|
'models' => $dataProvider->models, |
|
81
|
|
|
]); |
|
82
|
|
|
$dailyDiff->initLastDiff(); |
|
83
|
|
|
|
|
84
|
|
|
$monthlyDiff = \Yii::createObject([ |
|
85
|
|
|
'class' => TagMonthlyDiff::class, |
|
86
|
|
|
'models' => $dataProvider->models, |
|
87
|
|
|
]); |
|
88
|
|
|
$monthlyDiff->initLastDiff(); |
|
89
|
|
|
|
|
90
|
|
|
return $this->render('tags', [ |
|
91
|
|
|
'searchModel' => $searchModel, |
|
92
|
|
|
'dataProvider' => $dataProvider, |
|
93
|
|
|
'dailyDiff' => $dailyDiff, |
|
94
|
|
|
'monthlyDiff' => $monthlyDiff, |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function actionCreateAccount() |
|
99
|
|
|
{ |
|
100
|
|
|
$form = new MonitoringForm(); |
|
101
|
|
|
$form->setScenario(TrackerType::ACCOUNT); |
|
102
|
|
|
|
|
103
|
|
|
if ($form->load(\Yii::$app->request->post()) && $form->validate()) { |
|
104
|
|
|
$usernames = $this->normalizeTrackers($form->names); |
|
105
|
|
|
|
|
106
|
|
|
$accountManager = \Yii::createObject(AccountManager::class); |
|
107
|
|
|
|
|
108
|
|
|
/** @var \yii\queue\Queue $queue */ |
|
109
|
|
|
$queue = \Yii::$app->queue; |
|
110
|
|
|
|
|
111
|
|
|
foreach ($usernames as $username) { |
|
112
|
|
|
$account = $accountManager->monitor($username, $form->proxy_id, $form->proxy_tag_id); |
|
113
|
|
|
$account->disabled = 0; |
|
114
|
|
|
if (!$account->hasErrors()) { |
|
115
|
|
|
\Yii::$app->session->setFlash('success', 'OK!'); |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
$job = JobFactory::createAccountUpdate($account); |
|
118
|
|
|
$queue->push($job); |
|
119
|
|
|
|
|
120
|
|
|
$tags = array_filter((array)$form->tags); |
|
121
|
|
|
if ($tags) { |
|
|
|
|
|
|
122
|
|
|
$tagManager = \Yii::createObject(TagManager::class); |
|
123
|
|
|
$tagManager->saveForAccount($account, $tags, \Yii::$app->user->id); |
|
124
|
|
|
} |
|
125
|
|
|
} else { |
|
126
|
|
|
\Yii::error('Validation error: ' . json_encode($account->errors), __METHOD__); |
|
127
|
|
|
\Yii::$app->session->setFlash('error', "ERR! {$username}"); |
|
128
|
|
|
break; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $this->redirect(['monitoring/accounts', 'sort' => '-created_at']); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function actionCreateTag() |
|
138
|
|
|
{ |
|
139
|
|
|
$form = new MonitoringForm(); |
|
140
|
|
|
$form->setScenario(TrackerType::TAG); |
|
141
|
|
|
|
|
142
|
|
|
if ($form->load(\Yii::$app->request->post()) && $form->validate()) { |
|
143
|
|
|
$names = $this->normalizeTrackers($form->names); |
|
144
|
|
|
|
|
145
|
|
|
$tagManager = \Yii::createObject(TagManager::class); |
|
146
|
|
|
|
|
147
|
|
|
/** @var \yii\queue\Queue $queue */ |
|
148
|
|
|
$queue = \Yii::$app->queue; |
|
149
|
|
|
|
|
150
|
|
|
foreach ($names as $name) { |
|
151
|
|
|
$tag = $tagManager->monitor($name, $form->proxy_id, $form->proxy_tag_id); |
|
152
|
|
|
if (!$tag->hasErrors()) { |
|
153
|
|
|
\Yii::$app->session->setFlash('success', 'OK!'); |
|
154
|
|
|
$job = JobFactory::createTagUpdate($tag); |
|
155
|
|
|
$queue->push($job); |
|
156
|
|
|
} else { |
|
157
|
|
|
\Yii::error('Validation error: ' . json_encode($tag->errors), __METHOD__); |
|
158
|
|
|
\Yii::$app->session->setFlash('error', "ERR! {$name}"); |
|
159
|
|
|
break; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $this->redirect(['monitoring/tags', 'sort' => '-created_at']); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function actionDeleteAccount($id) |
|
169
|
|
|
{ |
|
170
|
|
|
$model = Account::findOne($id); |
|
171
|
|
|
$model->monitoring = 0; |
|
|
|
|
|
|
172
|
|
|
if ($model->save()) { |
|
173
|
|
|
\Yii::$app->session->setFlash('success', 'OK!'); |
|
174
|
|
|
|
|
175
|
|
|
return Url::to(['monitoring/accounts']); |
|
176
|
|
|
} else { |
|
177
|
|
|
\Yii::$app->session->setFlash('error', 'ERROR!'); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function actionDeleteTag($id) |
|
182
|
|
|
{ |
|
183
|
|
|
$model = Tag::findOne($id); |
|
184
|
|
|
$model->monitoring = 0; |
|
185
|
|
|
if ($model->save()) { |
|
186
|
|
|
\Yii::$app->session->setFlash('success', 'OK!'); |
|
187
|
|
|
|
|
188
|
|
|
return Url::to(['monitoring/tags']); |
|
189
|
|
|
} else { |
|
190
|
|
|
\Yii::$app->session->setFlash('error', 'ERROR!'); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param string $trackers |
|
196
|
|
|
* @return array |
|
197
|
|
|
*/ |
|
198
|
|
|
private function normalizeTrackers(string $trackers): array |
|
199
|
|
|
{ |
|
200
|
|
|
$trackers = StringHelper::explode($trackers, ',', true, true); |
|
201
|
|
|
$trackers = array_unique($trackers); |
|
202
|
|
|
|
|
203
|
|
|
return $trackers; |
|
204
|
|
|
} |
|
205
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.