Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
rating.php
1
<?php
9
namespace
Bitrix\Sender\Integration\Bitrix24\Limitation
;
10
11
use
Bitrix\Main\Config
;
12
use
Bitrix\Main\Type\Date
;
13
use
Bitrix\Main\Localization\Loc
;
14
15
use
Bitrix\Sender\Message
;
16
use
Bitrix\Sender\Dispatch\Semantics
;
17
use
Bitrix\Sender\Entity\Letter
;
18
use
Bitrix\Sender\Integration\Im\Notification
;
19
use
Bitrix\Sender\Internals\Model
;
20
21
Loc::loadMessages
(__FILE__);
22
27
class
Rating
28
{
30
protected
static
$base
= 1000;
31
37
public
static
function
regulate
()
38
{
39
$ratio = self::getRatio();
40
if
($ratio >= self::getBlockRate())
41
{
42
self::block();
43
}
44
}
45
51
public
static
function
calculate
()
52
{
53
$updateTimestamp = (int) self::getParam(
'date_update'
, 0);
54
$dateToday =
new
Date
();
55
if
($dateToday->getTimestamp() <= $updateTimestamp)
56
{
57
return
;
58
}
59
self::setParam(
'date_update'
, $dateToday->getTimestamp());
60
if
(!$updateTimestamp)
61
{
62
return
;
63
}
64
65
66
$yesterdayData = Model\DailyCounterTable::getRowByDate(1);
67
if
(!$yesterdayData)
68
{
69
return
;
70
}
71
72
// don't upgrade if error count more than 10% of sent
73
$countSent = (int) $yesterdayData[
'SENT_CNT'
];
74
$countError = (int) $yesterdayData[
'ERROR_CNT'
];
75
if
($countSent > 0 && ($countError / $countSent) > 0.1)
76
{
77
$countSent -= $countError;
78
}
79
80
$ratio = self::getRatio(1);
81
if
($ratio >= self::getBlockRate())
82
{
83
self::block();
84
}
85
elseif ($ratio >= self::getDownRate())
86
{
87
self::downgrade();
88
}
89
elseif ($countSent >= DailyLimit::instance()->getLimit())
90
{
91
self::upgrade();
92
}
93
}
94
100
public
static
function
upgrade
()
101
{
102
$limit = DailyLimit::instance()->getLimit();
103
$limit = $limit ?: self::getInitialLimit();
104
$limit = $limit * self::getLimitMultiplier();
105
if
($limit > self::getMaxLimit())
106
{
107
return
;
108
}
109
110
DailyLimit::instance()->setLimit($limit);
111
112
Notification::create()
113
->withMessage(self::getNotifyText(
'upgraded'
))
114
->toAllAuthors()
115
->send();
116
}
117
124
public
static
function
downgrade
($isNotify =
true
)
125
{
126
DailyLimit::instance()->setLimit(self::getInitialLimit());
127
128
if
($isNotify)
129
{
130
Notification::create()
131
->withMessage(self::getNotifyText(
'downgraded'
))
132
->toAllAuthors()
133
->send();
134
}
135
}
136
142
public
static
function
block
()
143
{
144
$letters = Model\LetterTable::getList(array(
145
'select'
=> array(
'ID'
),
146
'filter'
=> array(
147
'=STATUS'
=> Semantics::getWorkStates(),
148
'=MESSAGE_CODE'
=>
Message
\iBase::CODE_MAIL
149
)
150
));
151
152
$letter =
new
Letter
();
153
foreach
($letters as $letterData)
154
{
155
$letter->load($letterData[
'ID'
]);
156
if
(!$letter->getId())
157
{
158
continue
;
159
}
160
161
$state = $letter->getState();
162
if
($state->canPause())
163
{
164
$state->pause();
165
}
166
else
if
($state->canReady())
167
{
168
$state->ready();
169
}
170
else
if
($state->canStop())
171
{
172
$state->stop();
173
}
174
}
175
176
self::downgrade(
false
);
177
self::setParam(
'blocked'
,
'Y'
);
178
179
Notification::create()
180
->withMessage(self::getNotifyText(
'blocked'
))
181
->toAllAuthors()
182
->send();
183
}
184
190
public
static
function
isBlocked
()
191
{
192
return
self::getParam(
'blocked'
,
'N'
) ===
'Y'
;
193
}
194
200
public
static
function
getDownRate
()
201
{
202
return
round(self::getParam(
'down_over_abuses'
, 8) / self::$base, 3);
203
}
204
210
public
static
function
getBlockRate
()
211
{
212
$abusesPer1000Mails = self::getParam(
'block_over_abuses'
, 20);
213
return
round($abusesPer1000Mails / self::$base, 3);
214
}
215
221
public
static
function
getInitialLimit
()
222
{
223
return
(
int
) self::getParam(
'initial'
, 1000);
224
}
225
231
public
static
function
getMaxLimit
()
232
{
233
return
(
int
) self::getParam(
'max'
, 16000);
234
}
235
241
public
static
function
getLimitMultiplier
()
242
{
243
return
(
float
) self::getParam(
'multiplier'
, 2);
244
}
245
252
protected
static
function
getRatio
($daysLeft = 0)
253
{
254
$result = Model\DailyCounterTable::getRowByDate($daysLeft);
255
$sentCount = $result ? (int) $result[
'SENT_CNT'
] - (
int
) $result[
'ERROR_CNT'
] : 0;
256
$abuseCount = $result ? $result[
'ABUSE_CNT'
] : 0;
257
if
(!$sentCount || $sentCount < 200 || !$abuseCount)
258
{
259
return
0;
260
}
261
262
return
round($abuseCount / $sentCount, 3);
263
}
264
272
protected
static
function
getParam
($name, $defaultValue = 0)
273
{
274
return
Config\Option::get(
'sender'
,
"~r_limit_$name"
, $defaultValue);
275
}
276
284
protected
static
function
setParam
($name, $value)
285
{
286
Config\Option::set(
'sender'
,
"~r_limit_$name"
, $value);
287
}
288
295
public
static
function
getNotifyText
($code)
296
{
297
$code = mb_strtoupper($code);
298
return
Loc::getMessage
(
"SENDER_INTEGRATION_BITRIX24_RATING_{$code}1"
);
299
}
300
}
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\Type\Date
Definition
date.php:9
Bitrix\Sender\Dispatch\Semantics
Definition
semantics.php:19
Bitrix\Sender\Entity\Letter
Definition
letter.php:34
Bitrix\Sender\Entity\Message
Definition
message.php:27
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating
Definition
rating.php:28
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\upgrade
static upgrade()
Definition
rating.php:100
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\getNotifyText
static getNotifyText($code)
Definition
rating.php:295
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\getParam
static getParam($name, $defaultValue=0)
Definition
rating.php:272
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\isBlocked
static isBlocked()
Definition
rating.php:190
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\getDownRate
static getDownRate()
Definition
rating.php:200
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\getInitialLimit
static getInitialLimit()
Definition
rating.php:221
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\block
static block()
Definition
rating.php:142
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\getRatio
static getRatio($daysLeft=0)
Definition
rating.php:252
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\getMaxLimit
static getMaxLimit()
Definition
rating.php:231
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\getBlockRate
static getBlockRate()
Definition
rating.php:210
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\getLimitMultiplier
static getLimitMultiplier()
Definition
rating.php:241
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\setParam
static setParam($name, $value)
Definition
rating.php:284
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\$base
static $base
Definition
rating.php:30
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\regulate
static regulate()
Definition
rating.php:37
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\downgrade
static downgrade($isNotify=true)
Definition
rating.php:124
Bitrix\Sender\Integration\Bitrix24\Limitation\Rating\calculate
static calculate()
Definition
rating.php:51
Bitrix\Sender\Integration\Im\Notification
Definition
notification.php:20
Bitrix\Main\Config
Definition
configuration.php:2
Bitrix\Sender\Integration\Bitrix24\Limitation
Definition
dailylimit.php:9
Bitrix\Sender\Internals\Model
Definition
abuse.php:3
Bitrix\Sender\Message
Definition
adapter.php:9
modules
sender
lib
integration
bitrix24
limitation
rating.php
Создано системой
1.10.0