Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
rating.php
1<?php
10
14
20
21Loc::loadMessages(__FILE__);
22
27class 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}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getParam($name, $defaultValue=0)
Definition rating.php:272