Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
stat.php
1<?php
2namespace Bitrix\Rest;
3
5
61class StatTable extends Main\Entity\DataManager
62{
63 const STORE_PERIOD = 5184000; // 60*24*3600
64
65 protected static $data = array();
66 protected static $dataPassword = array();
67
73 public static function getTableName()
74 {
75 return 'b_rest_stat';
76 }
77
83 public static function getMap()
84 {
85 return array(
86 'STAT_DATE' => array(
87 'data_type' => 'date',
88 'primary' => true,
89 ),
90 'APP_ID' => array(
91 'data_type' => 'integer',
92 'primary' => true,
93 ),
94 'METHOD_ID' => array(
95 'data_type' => 'integer',
96 'primary' => true,
97 ),
98 'PASSWORD_ID' => array(
99 'data_type' => 'integer',
100 'primary' => true,
101 ),
102 'HOUR_0' => array(
103 'data_type' => 'integer',
104 'required' => true,
105 ),
106 'HOUR_1' => array(
107 'data_type' => 'integer',
108 'required' => true,
109 ),
110 'HOUR_2' => array(
111 'data_type' => 'integer',
112 'required' => true,
113 ),
114 'HOUR_3' => array(
115 'data_type' => 'integer',
116 'required' => true,
117 ),
118 'HOUR_4' => array(
119 'data_type' => 'integer',
120 'required' => true,
121 ),
122 'HOUR_5' => array(
123 'data_type' => 'integer',
124 'required' => true,
125 ),
126 'HOUR_6' => array(
127 'data_type' => 'integer',
128 'required' => true,
129 ),
130 'HOUR_7' => array(
131 'data_type' => 'integer',
132 'required' => true,
133 ),
134 'HOUR_8' => array(
135 'data_type' => 'integer',
136 'required' => true,
137 ),
138 'HOUR_9' => array(
139 'data_type' => 'integer',
140 'required' => true,
141 ),
142 'HOUR_10' => array(
143 'data_type' => 'integer',
144 'required' => true,
145 ),
146 'HOUR_11' => array(
147 'data_type' => 'integer',
148 'required' => true,
149 ),
150 'HOUR_12' => array(
151 'data_type' => 'integer',
152 'required' => true,
153 ),
154 'HOUR_13' => array(
155 'data_type' => 'integer',
156 'required' => true,
157 ),
158 'HOUR_14' => array(
159 'data_type' => 'integer',
160 'required' => true,
161 ),
162 'HOUR_15' => array(
163 'data_type' => 'integer',
164 'required' => true,
165 ),
166 'HOUR_16' => array(
167 'data_type' => 'integer',
168 'required' => true,
169 ),
170 'HOUR_17' => array(
171 'data_type' => 'integer',
172 'required' => true,
173 ),
174 'HOUR_18' => array(
175 'data_type' => 'integer',
176 'required' => true,
177 ),
178 'HOUR_19' => array(
179 'data_type' => 'integer',
180 'required' => true,
181 ),
182 'HOUR_20' => array(
183 'data_type' => 'integer',
184 'required' => true,
185 ),
186 'HOUR_21' => array(
187 'data_type' => 'integer',
188 'required' => true,
189 ),
190 'HOUR_22' => array(
191 'data_type' => 'integer',
192 'required' => true,
193 ),
194 'HOUR_23' => array(
195 'data_type' => 'integer',
196 'required' => true,
197 ),
198 'APP' => array(
199 'data_type' => 'Bitrix\Rest\StatAppTable',
200 'reference' => array(
201 '=this.APP_ID' => 'ref.APP_ID',
202 ),
203 ),
204 'METHOD' => array(
205 'data_type' => 'Bitrix\Rest\StatMethodTable',
206 'reference' => array(
207 '=this.METHOD_ID' => 'ref.ID',
208 ),
209 ),
210 'PASSWORD' => array(
211 'data_type' => '\Bitrix\Rest\APAuth\PasswordTable',
212 'reference' => array(
213 '=this.PASSWORD_ID' => 'ref.ID',
214 ),
215 ),
216 );
217 }
218
219 public static function log(\CRestServer $server)
220 {
221 if(Main\ModuleManager::isModuleInstalled('oauth'))
222 {
223 return;
224 }
225
226 if($server->getClientId())
227 {
228 static::logMethod($server->getClientId(), $server->getMethod());
229 }
230 elseif($server->getPasswordId())
231 {
232 static::logApMethod($server->getPasswordId(), $server->getMethod());
233 }
234 }
235
236 public static function logApMethod($passwordID, $methodName)
237 {
238 static::addApToLog($passwordID, $methodName, StatMethodTable::METHOD_TYPE_METHOD);
239 }
240
241 public static function logMethod($clientId, $methodName)
242 {
243 static::addToLog($clientId, $methodName, StatMethodTable::METHOD_TYPE_METHOD);
244 }
245
246 public static function logEvent($clientId, $eventName)
247 {
248 static::addToLog($clientId, $eventName, StatMethodTable::METHOD_TYPE_EVENT);
249 }
250
251 public static function logPlacement($clientId, $placementName)
252 {
253 static::addToLog($clientId, $placementName, StatMethodTable::METHOD_TYPE_PLACEMENT);
254 }
255
256 public static function logRobot($clientId)
257 {
258 static::addToLog($clientId, 'ROBOT', StatMethodTable::METHOD_TYPE_ROBOT);
259 }
260
261 public static function logActivity($clientId)
262 {
263 static::addToLog($clientId, 'ACTIVITY', StatMethodTable::METHOD_TYPE_ACTIVITY);
264 }
265
266 protected static function addApToLog($passwordID, $methodName, $methodType)
267 {
268 if (!isset(static::$dataPassword[$passwordID]))
269 {
270 static::$dataPassword[$passwordID] = array();
271 }
272
273 if (!isset(static::$dataPassword[$passwordID][$methodName]))
274 {
275 static::$dataPassword[$passwordID][$methodName] = 0;
276 }
277
278 static::$dataPassword[$passwordID][$methodName]++;
279 }
280
281 protected static function addToLog($clientId, $methodName, $methodType)
282 {
283 if (!isset(static::$data[$clientId]))
284 {
285 static::$data[$clientId] = array();
286 }
287
288 if (!isset(static::$data[$clientId][$methodName]))
289 {
290 static::$data[$clientId][$methodName] = 0;
291 }
292
293 static::$data[$clientId][$methodName]++;
294 }
295
296 public static function finalize()
297 {
298 if(Main\ModuleManager::isModuleInstalled('oauth'))
299 {
300 return;
301 }
302
303 $connection = Main\Application::getConnection();
304 $helper = $connection->getSqlHelper();
305
306 $hour = intval(date('G'));
307 $curDateSql = new Main\Type\Date();
308 if(count(static::$data) > 0)
309 {
310 foreach(static::$data as $clientId => $stat)
311 $appInfo = AppTable::getByClientId($clientId);
312 {
313 if($appInfo)
314 {
315 StatAppTable::register($appInfo);
316 foreach($stat as $methodName => $count)
317 {
318 $methodId = StatMethodTable::getId($methodName);
319 if (!$methodId)
320 {
321 continue;
322 }
323
324 $insertFields = array(
325 'STAT_DATE' => $curDateSql,
326 'APP_ID' => $appInfo['ID'],
327 'METHOD_ID' => $methodId,
328 'HOUR_'.$hour => $count,
329 'PASSWORD_ID' => 0
330 );
331
332 $updateFields = array(
333 'HOUR_'.$hour => new Main\DB\SqlExpression('?#+?i', 'HOUR_'.$hour, $count)
334 );
335
336 $queries = $helper->prepareMerge(
337 static::getTableName(),
338 array('DATE', 'APP_ID', 'METHOD_ID'),
339 $insertFields,
340 $updateFields
341 );
342
343 foreach($queries as $query)
344 {
345 $connection->queryExecute($query);
346 }
347 }
348 }
349 }
350 }
351
352 if(count(static::$dataPassword) > 0)
353 {
354 foreach(static::$dataPassword as $passwordID => $stat)
355 {
356
357 foreach ($stat as $methodName => $count)
358 {
359 $methodId = StatMethodTable::getId($methodName);
360 if (!$methodId)
361 {
362 continue;
363 }
364
365 $insertFields = array(
366 'STAT_DATE' => $curDateSql,
367 'PASSWORD_ID' => $passwordID,
368 'METHOD_ID' => $methodId,
369 'HOUR_' . $hour => $count,
370 'APP_ID' => 0
371 );
372
373 $updateFields = array(
374 'HOUR_'.$hour => new Main\DB\SqlExpression('?#+?i', 'HOUR_'.$hour, $count)
375 );
376
377 $queries = $helper->prepareMerge(
378 static::getTableName(),
379 array('DATE', 'APP_ID', 'METHOD_ID'),
380 $insertFields,
381 $updateFields
382 );
383
384 foreach($queries as $query)
385 {
386 $connection->queryExecute($query);
387 }
388 }
389 }
390 }
391
392 static::reset();
393 }
394
395 public static function reset()
396 {
397 static::$data = array();
398 }
399
403 public static function deleteByFilter(array $filter)
404 {
405 $entity = static::getEntity();
406 $sqlTableName = static::getTableName();
407
408 $where = Main\Entity\Query::buildFilterSql($entity, $filter);
409 if($where <> '')
410 {
411 $sql = "DELETE FROM {$sqlTableName} WHERE ".$where;
412 $entity->getConnection()->queryExecute($sql);
413 }
414 }
415
420 public static function updateByFilter(array $filter, array $fields)
421 {
422 $entity = static::getEntity();
423 $sqlHelper = $entity->getConnection()->getSqlHelper();
424 $sqlTableName = static::getTableName();
425
426 $update = $sqlHelper->prepareUpdate($sqlTableName, $fields);
427 $where = Main\Entity\Query::buildFilterSql($entity, $filter);
428 if($where <> '' && $update[0] <> '')
429 {
430 $sql = "UPDATE {$sqlTableName} SET $update[0] WHERE $where";
431 $entity->getConnection()->queryExecute($sql);
432 }
433 }
434
435 public static function cleanUpAgent()
436 {
437 $date = new Main\Type\DateTime();
438 $date->add("-60D");
439
440 static::deleteByFilter(array(
441 "<STAT_DATE" => $date,
442 ));
443
444 return "\\Bitrix\\Rest\\StatTable::cleanUpAgent();";
445 }
446}
static isModuleInstalled($moduleName)
static getByClientId($clientId)
Definition app.php:929
static register($appInfo)
Definition statapp.php:87
static getId($methodName)
static getMap()
Definition stat.php:83
static addApToLog($passwordID, $methodName, $methodType)
Definition stat.php:266
static log(\CRestServer $server)
Definition stat.php:219
static logActivity($clientId)
Definition stat.php:261
static addToLog($clientId, $methodName, $methodType)
Definition stat.php:281
static finalize()
Definition stat.php:296
static deleteByFilter(array $filter)
Definition stat.php:403
static updateByFilter(array $filter, array $fields)
Definition stat.php:420
static $dataPassword
Definition stat.php:66
static logEvent($clientId, $eventName)
Definition stat.php:246
static logApMethod($passwordID, $methodName)
Definition stat.php:236
static cleanUpAgent()
Definition stat.php:435
static logMethod($clientId, $methodName)
Definition stat.php:241
static logPlacement($clientId, $placementName)
Definition stat.php:251
static logRobot($clientId)
Definition stat.php:256
static getTableName()
Definition stat.php:73