|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jalle19\StatusManager\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Jalle19\StatusManager\Database\InstanceQuery; |
|
6
|
|
|
use Jalle19\StatusManager\Database\SubscriptionQuery; |
|
7
|
|
|
use Jalle19\StatusManager\Database\UserQuery; |
|
8
|
|
|
use Jalle19\StatusManager\Message\AbstractMessage; |
|
9
|
|
|
use Jalle19\StatusManager\Message\Handler\HandlerInterface; |
|
10
|
|
|
use Jalle19\StatusManager\Message\Request\MostActiveWatchersRequest; |
|
11
|
|
|
use Jalle19\StatusManager\Message\Request\PopularChannelsRequest; |
|
12
|
|
|
use Jalle19\StatusManager\Message\Request\StatisticsRequest; |
|
13
|
|
|
use Jalle19\StatusManager\Message\Response\MostActiveWatchersResponse; |
|
14
|
|
|
use Jalle19\StatusManager\Message\Response\PopularChannelsResponse; |
|
15
|
|
|
use Jalle19\tvheadend\exception\RequestFailedException; |
|
16
|
|
|
use Propel\Runtime\ActiveQuery\Criteria; |
|
17
|
|
|
use Propel\Runtime\Exception\PropelException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class StatisticsManager |
|
21
|
|
|
* @package Jalle19\StatusManager\Manager |
|
22
|
|
|
* @copyright Copyright © Sam Stenvall 2016- |
|
23
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class StatisticsManager extends AbstractManager implements HandlerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritdoc |
|
30
|
|
|
*/ |
|
31
|
|
|
public function handleMessage(AbstractMessage $message) |
|
32
|
|
|
{ |
|
33
|
|
|
// Wrap database exceptions in the more generic RequestFailedException |
|
34
|
|
|
try |
|
35
|
|
|
{ |
|
36
|
|
|
switch ($message->getType()) |
|
37
|
|
|
{ |
|
38
|
|
|
case AbstractMessage::TYPE_POPULAR_CHANNELS_REQUEST: |
|
39
|
|
|
/* @var PopularChannelsRequest $message */ |
|
40
|
|
|
return new PopularChannelsResponse($message, $this->getPopularChannels( |
|
41
|
|
|
$message->getInstanceName(), |
|
42
|
|
|
$message->getUserName(), |
|
43
|
|
|
$message->getLimit(), |
|
44
|
|
|
$message->getTimeInterval())); |
|
45
|
|
|
case AbstractMessage::TYPE_MOST_ACTIVE_WATCHERS_REQUEST: |
|
46
|
|
|
/* @var MostActiveWatchersRequest $message */ |
|
47
|
|
|
return new MostActiveWatchersResponse($message, $this->getMostActiveWatchers( |
|
48
|
|
|
$message->getInstanceName(), |
|
49
|
|
|
$message->getLimit(), |
|
50
|
|
|
$message->getTimeInterval())); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
catch (PropelException $e) |
|
54
|
|
|
{ |
|
55
|
|
|
throw new RequestFailedException('A database error occured: ' . $e->getMessage()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $instanceName |
|
64
|
|
|
* @param string|null $userName |
|
65
|
|
|
* @param int|null $limit |
|
66
|
|
|
* @param string $timeInterval |
|
67
|
|
|
* |
|
68
|
|
|
* @return array the popular channels |
|
69
|
|
|
*/ |
|
70
|
|
|
private function getPopularChannels($instanceName, $userName, $limit, $timeInterval) |
|
71
|
|
|
{ |
|
72
|
|
|
// Find the instance and the user |
|
73
|
|
|
$instance = InstanceQuery::create()->findOneByName($instanceName); |
|
74
|
|
|
$user = UserQuery::create()->findOneByName($userName); |
|
75
|
|
|
|
|
76
|
|
|
// Find the subscriptions |
|
77
|
|
|
$query = SubscriptionQuery::create()->getPopularChannelsQuery($instance, $user); |
|
78
|
|
|
$query = $this->filterByLimit($query, $limit); |
|
79
|
|
|
$query = $this->filterBySubscriptionStopped($timeInterval, $query); |
|
80
|
|
|
$query = $this->filterIgnoredUsers($instanceName, $query->useUserQuery())->endUse(); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
return $query->find()->getData(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $instanceName |
|
88
|
|
|
* @param int $limit |
|
89
|
|
|
* @param string $timeInterval |
|
90
|
|
|
* |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
|
|
private function getMostActiveWatchers($instanceName, $limit, $timeInterval) |
|
94
|
|
|
{ |
|
95
|
|
|
$instance = InstanceQuery::create()->findOneByName($instanceName); |
|
96
|
|
|
$query = UserQuery::create()->getMostActiveWatchersQuery($instance); |
|
97
|
|
|
|
|
98
|
|
|
$query = $this->filterByLimit($query, $limit); |
|
99
|
|
|
$query = $this->filterBySubscriptionStopped($timeInterval, $query->useSubscriptionQuery())->endUse(); |
|
100
|
|
|
$query = $this->filterIgnoredUsers($instanceName, $query); |
|
101
|
|
|
|
|
102
|
|
|
return $query->find()->getData(); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param mixed $query a query instance |
|
108
|
|
|
* @param int|null $limit |
|
109
|
|
|
* |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
private function filterByLimit($query, $limit) |
|
113
|
|
|
{ |
|
114
|
|
|
if ($limit !== null) |
|
115
|
|
|
$query->limit($limit); |
|
116
|
|
|
|
|
117
|
|
|
return $query; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $timeInterval |
|
123
|
|
|
* @param SubscriptionQuery $query |
|
124
|
|
|
* |
|
125
|
|
|
* @return SubscriptionQuery |
|
126
|
|
|
*/ |
|
127
|
|
|
private function filterBySubscriptionStopped($timeInterval, SubscriptionQuery $query) |
|
128
|
|
|
{ |
|
129
|
|
|
if ($timeInterval !== StatisticsRequest::TIME_INTERVAL_ALL_TIME) |
|
130
|
|
|
{ |
|
131
|
|
|
$query = $query->filterByStopped([ |
|
132
|
|
|
'min' => $this->getTimeIntervalTimestamp($timeInterval), |
|
133
|
|
|
]); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $query; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $instanceName |
|
142
|
|
|
* @param UserQuery $query |
|
143
|
|
|
* |
|
144
|
|
|
* @return $this|UserQuery |
|
145
|
|
|
*/ |
|
146
|
|
|
private function filterIgnoredUsers($instanceName, UserQuery $query) |
|
147
|
|
|
{ |
|
148
|
|
|
$instance = $this->configuration->getInstanceByName($instanceName); |
|
149
|
|
|
$ignoredUsers = $instance->getIgnoredUsers(); |
|
150
|
|
|
|
|
151
|
|
|
foreach ($ignoredUsers as $ignoredUser) |
|
152
|
|
|
$query = $query->filterByName($ignoredUser, Criteria::NOT_EQUAL); |
|
153
|
|
|
|
|
154
|
|
|
return $query; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $timeInterval |
|
160
|
|
|
* |
|
161
|
|
|
* @return int |
|
162
|
|
|
*/ |
|
163
|
|
|
private function getTimeIntervalTimestamp($timeInterval) |
|
164
|
|
|
{ |
|
165
|
|
|
$dateTime = new \DateTime(); |
|
166
|
|
|
|
|
167
|
|
|
switch ($timeInterval) |
|
168
|
|
|
{ |
|
169
|
|
|
case StatisticsRequest::TIME_INTERVAL_LAST_MONTH: |
|
170
|
|
|
$dateTime = $dateTime->modify('-1 month'); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $dateTime->getTimestamp(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
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.