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\CategoryManager; |
13
|
|
|
use app\components\JobFactory; |
14
|
|
|
use app\components\stats\diffs\MultiAccountsDiff; |
15
|
|
|
use app\components\stats\diffs\MultipleTagsDiff; |
16
|
|
|
use app\components\TagManager; |
17
|
|
|
use app\dictionaries\TrackerType; |
18
|
|
|
use app\models\Account; |
19
|
|
|
use app\models\Tag; |
20
|
|
|
use app\modules\admin\models\AccountSearch; |
21
|
|
|
use app\modules\admin\models\MonitoringForm; |
22
|
|
|
use app\modules\admin\models\TagSearch; |
23
|
|
|
use Carbon\Carbon; |
24
|
|
|
use Yii; |
25
|
|
|
use yii\filters\VerbFilter; |
26
|
|
|
use yii\helpers\StringHelper; |
27
|
|
|
use yii\helpers\Url; |
28
|
|
|
use yii\web\Controller; |
29
|
|
|
|
30
|
|
|
class MonitoringController extends Controller |
31
|
|
|
{ |
32
|
|
|
public function behaviors() |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
'verbs' => [ |
36
|
|
|
'class' => VerbFilter::class, |
37
|
|
|
'actions' => [ |
38
|
|
|
'create-account' => ['POST'], |
39
|
|
|
'delete-account' => ['POST'], |
40
|
|
|
'create-tag' => ['POST'], |
41
|
|
|
'delete-tag' => ['POST'], |
42
|
|
|
], |
43
|
|
|
], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function actionAccounts() |
48
|
|
|
{ |
49
|
|
|
$searchModel = new AccountSearch(); |
50
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
51
|
|
|
$dataProvider->query->andWhere(['account.monitoring' => 1]); |
52
|
|
|
|
53
|
|
|
$statsAttributes = [ |
54
|
|
|
'followed_by', |
55
|
|
|
'follows', |
56
|
|
|
'media', |
57
|
|
|
'er', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$dailyDiff = Yii::createObject([ |
61
|
|
|
'class' => MultiAccountsDiff::class, |
62
|
|
|
'accounts' => $dataProvider->models, |
63
|
|
|
'from' => Carbon::yesterday()->subDay(), |
64
|
|
|
'to' => Carbon::yesterday(), |
65
|
|
|
'statsAttributes' => $statsAttributes, |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$monthlyDiff = Yii::createObject([ |
69
|
|
|
'class' => MultiAccountsDiff::class, |
70
|
|
|
'accounts' => $dataProvider->models, |
71
|
|
|
'from' => Carbon::yesterday()->subMonth()->endOfMonth(), |
72
|
|
|
'to' => Carbon::yesterday(), |
73
|
|
|
'statsAttributes' => $statsAttributes, |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
return $this->render('accounts', [ |
77
|
|
|
'searchModel' => $searchModel, |
78
|
|
|
'dataProvider' => $dataProvider, |
79
|
|
|
'dailyDiff' => $dailyDiff, |
80
|
|
|
'monthlyDiff' => $monthlyDiff, |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function actionTags() |
85
|
|
|
{ |
86
|
|
|
$searchModel = new TagSearch(); |
87
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
88
|
|
|
$dataProvider->query->andWhere(['tag.monitoring' => 1]); |
89
|
|
|
|
90
|
|
|
$statsAttributes = [ |
91
|
|
|
'media', |
92
|
|
|
'likes', |
93
|
|
|
'comments', |
94
|
|
|
'min_likes', |
95
|
|
|
'max_likes', |
96
|
|
|
'min_comments', |
97
|
|
|
'max_comments', |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
$dailyDiff = Yii::createObject([ |
101
|
|
|
'class' => MultipleTagsDiff::class, |
102
|
|
|
'tags' => $dataProvider->models, |
103
|
|
|
'from' => Carbon::yesterday()->subDay(), |
104
|
|
|
'to' => Carbon::yesterday(), |
105
|
|
|
'statsAttributes' => $statsAttributes, |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
$monthlyDiff = Yii::createObject([ |
109
|
|
|
'class' => MultipleTagsDiff::class, |
110
|
|
|
'tags' => $dataProvider->models, |
111
|
|
|
'from' => Carbon::yesterday()->subMonth()->endOfMonth(), |
112
|
|
|
'to' => Carbon::yesterday(), |
113
|
|
|
'statsAttributes' => $statsAttributes, |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
return $this->render('tags', [ |
117
|
|
|
'searchModel' => $searchModel, |
118
|
|
|
'dataProvider' => $dataProvider, |
119
|
|
|
'dailyDiff' => $dailyDiff, |
120
|
|
|
'monthlyDiff' => $monthlyDiff, |
121
|
|
|
]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function actionCreateAccount() |
125
|
|
|
{ |
126
|
|
|
$form = new MonitoringForm(); |
127
|
|
|
$form->setScenario(TrackerType::ACCOUNT); |
128
|
|
|
|
129
|
|
|
if ($form->load(Yii::$app->request->post()) && $form->validate()) { |
130
|
|
|
$usernames = $this->normalizeTrackers($form->names); |
131
|
|
|
|
132
|
|
|
$accountManager = Yii::createObject(AccountManager::class); |
133
|
|
|
|
134
|
|
|
/** @var \yii\queue\Queue $queue */ |
135
|
|
|
$queue = Yii::$app->queue; |
136
|
|
|
|
137
|
|
|
foreach ($usernames as $username) { |
138
|
|
|
$account = $accountManager->startMonitoring($username); |
139
|
|
|
if (!$account->hasErrors()) { |
140
|
|
|
Yii::$app->session->setFlash('success', 'OK!'); |
|
|
|
|
141
|
|
|
|
142
|
|
|
$job = JobFactory::updateAccount($account); |
143
|
|
|
$queue->push($job); |
144
|
|
|
|
145
|
|
|
$categories = array_filter((array)$form->categories); |
146
|
|
|
if ($categories) { |
|
|
|
|
147
|
|
|
/** @var \app\models\User $identity */ |
148
|
|
|
$identity = Yii::$app->user->identity; |
149
|
|
|
$categoryManager = Yii::createObject(CategoryManager::class); |
150
|
|
|
$categoryManager->addToAccount($account, $categories, $identity); |
151
|
|
|
} |
152
|
|
|
} else { |
153
|
|
|
Yii::error('Validation error: ' . json_encode($account->errors), __METHOD__); |
154
|
|
|
Yii::$app->session->setFlash('error', "ERR! {$username}"); |
155
|
|
|
break; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->redirect(['monitoring/accounts', 'sort' => '-created_at']); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function actionCreateTag() |
165
|
|
|
{ |
166
|
|
|
$form = new MonitoringForm(); |
167
|
|
|
$form->setScenario(TrackerType::TAG); |
168
|
|
|
|
169
|
|
|
if ($form->load(Yii::$app->request->post()) && $form->validate()) { |
170
|
|
|
$names = $this->normalizeTrackers($form->names); |
171
|
|
|
|
172
|
|
|
$tagManager = Yii::createObject(TagManager::class); |
173
|
|
|
|
174
|
|
|
/** @var \yii\queue\Queue $queue */ |
175
|
|
|
$queue = Yii::$app->queue; |
176
|
|
|
|
177
|
|
|
foreach ($names as $name) { |
178
|
|
|
$tag = $tagManager->startMonitoring($name); |
179
|
|
|
if (!$tag->hasErrors()) { |
180
|
|
|
Yii::$app->session->setFlash('success', 'OK!'); |
181
|
|
|
$job = JobFactory::updateTag($tag); |
182
|
|
|
$queue->push($job); |
183
|
|
|
} else { |
184
|
|
|
Yii::error('Validation error: ' . json_encode($tag->errors), __METHOD__); |
185
|
|
|
Yii::$app->session->setFlash('error', "ERR! {$name}"); |
186
|
|
|
break; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this->redirect(['monitoring/tags', 'sort' => '-created_at']); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function actionDeleteAccount($id) |
196
|
|
|
{ |
197
|
|
|
$model = Account::findOne($id); |
198
|
|
|
$model->monitoring = 0; |
199
|
|
|
if ($model->save()) { |
200
|
|
|
Yii::$app->session->setFlash('success', 'OK!'); |
201
|
|
|
|
202
|
|
|
return Url::to(['monitoring/accounts']); |
203
|
|
|
} else { |
204
|
|
|
Yii::$app->session->setFlash('error', 'ERROR!'); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function actionDeleteTag($id) |
209
|
|
|
{ |
210
|
|
|
$model = Tag::findOne($id); |
211
|
|
|
$model->monitoring = 0; |
212
|
|
|
if ($model->save()) { |
213
|
|
|
Yii::$app->session->setFlash('success', 'OK!'); |
214
|
|
|
|
215
|
|
|
return Url::to(['monitoring/tags']); |
216
|
|
|
} else { |
217
|
|
|
Yii::$app->session->setFlash('error', 'ERROR!'); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string $trackers |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
|
|
private function normalizeTrackers(string $trackers): array |
226
|
|
|
{ |
227
|
|
|
$trackers = StringHelper::explode($trackers, ',', true, true); |
228
|
|
|
$trackers = array_unique($trackers); |
229
|
|
|
|
230
|
|
|
return $trackers; |
231
|
|
|
} |
232
|
|
|
} |
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.