1C-Bitrix 25.700.0
usagestat.php
См. документацию.
1<?php
2namespace Bitrix\Rest;
3
4use Bitrix\Main;
5use Bitrix\Main\DB\SqlQueryException;
6use Bitrix\Main\SystemException;
7
57class UsageStatTable extends Main\Entity\DataManager
58{
59
60 protected static $data = array();
61
67 public static function getTableName()
68 {
69 return 'b_rest_usage_stat';
70 }
71
77 public static function getMap()
78 {
79 return array(
80 'STAT_DATE' => array(
81 'data_type' => 'date',
82 'primary' => true
83 ),
84 'ENTITY_ID' => array(
85 'data_type' => 'integer',
86 'primary' => true
87 ),
88 'IS_SENT' => array(
89 'data_type' => 'boolean',
90 'values' => array(
91 'N',
92 'Y'
93 )
94 ),
95 'HOUR_0' => array(
96 'data_type' => 'integer',
97 'required' => true
98 ),
99 'HOUR_1' => array(
100 'data_type' => 'integer',
101 'required' => true
102 ),
103 'HOUR_2' => array(
104 'data_type' => 'integer',
105 'required' => true
106 ),
107 'HOUR_3' => array(
108 'data_type' => 'integer',
109 'required' => true
110 ),
111 'HOUR_4' => array(
112 'data_type' => 'integer',
113 'required' => true
114 ),
115 'HOUR_5' => array(
116 'data_type' => 'integer',
117 'required' => true
118 ),
119 'HOUR_6' => array(
120 'data_type' => 'integer',
121 'required' => true
122 ),
123 'HOUR_7' => array(
124 'data_type' => 'integer',
125 'required' => true
126 ),
127 'HOUR_8' => array(
128 'data_type' => 'integer',
129 'required' => true
130 ),
131 'HOUR_9' => array(
132 'data_type' => 'integer',
133 'required' => true
134 ),
135 'HOUR_10' => array(
136 'data_type' => 'integer',
137 'required' => true
138 ),
139 'HOUR_11' => array(
140 'data_type' => 'integer',
141 'required' => true
142 ),
143 'HOUR_12' => array(
144 'data_type' => 'integer',
145 'required' => true
146 ),
147 'HOUR_13' => array(
148 'data_type' => 'integer',
149 'required' => true
150 ),
151 'HOUR_14' => array(
152 'data_type' => 'integer',
153 'required' => true
154 ),
155 'HOUR_15' => array(
156 'data_type' => 'integer',
157 'required' => true
158 ),
159 'HOUR_16' => array(
160 'data_type' => 'integer',
161 'required' => true
162 ),
163 'HOUR_17' => array(
164 'data_type' => 'integer',
165 'required' => true
166 ),
167 'HOUR_18' => array(
168 'data_type' => 'integer',
169 'required' => true
170 ),
171 'HOUR_19' => array(
172 'data_type' => 'integer',
173 'required' => true
174 ),
175 'HOUR_20' => array(
176 'data_type' => 'integer',
177 'required' => true
178 ),
179 'HOUR_21' => array(
180 'data_type' => 'integer',
181 'required' => true
182 ),
183 'HOUR_22' => array(
184 'data_type' => 'integer',
185 'required' => true
186 ),
187 'HOUR_23' => array(
188 'data_type' => 'integer',
189 'required' => true
190 ),
191 'ENTITY' => array(
192 'data_type' => 'Bitrix\Rest\UsageEntityTable',
193 'reference' => array(
194 '=this.ENTITY_ID' => 'ref.ID',
195 ),
196 ),
197 );
198 }
199
200 public static function log(\CRestServer $server)
201 {
202 if (
203 \Bitrix\Rest\Integration\OAuthModule::isSupported()
204 && !defined('REST_FORCE_USAGE_STAT')
205 )
206 {
207 return;
208 }
209
210 if ($server->getClientId())
211 {
212 static::logMethod($server->getClientId(), $server->getMethod());
213 }
214 elseif ($server->getPasswordId())
215 {
216 static::logHookMethod($server->getPasswordId(), $server->getMethod());
217 }
218 }
219
220 public static function logHookMethod($passwordID, $methodName)
221 {
222 static::increment(UsageEntityTable::ENTITY_TYPE_WEBHOOK, $passwordID, UsageEntityTable::SUB_ENTITY_TYPE_METHOD, $methodName);
223 }
224
225 public static function logMethod($clientId, $methodName)
226 {
228 }
229
230 public static function logEvent($clientId, $eventName)
231 {
233 }
234
235 public static function logPlacement($clientId, $placementName)
236 {
238 }
239
240 public static function logRobot($clientId, $clientCode)
241 {
243 }
244
250 public static function logBizProc($clientId, string $clientCode): void
251 {
253 }
254
255 public static function logActivity($clientId, $clientCode)
256 {
258 }
259
260 public static function logConfiguration($clientId, $clientCode)
261 {
262 static::increment(
264 $clientId,
266 $clientCode
267 );
268 }
269
270 public static function logMessage($clientId, $messageType)
271 {
272 static::increment(
274 $clientId,
276 $messageType
277 );
278 }
279
280 public static function logLanding($clientId, $type, $count = 1)
281 {
282 static::incrementByCount(
284 $clientId,
286 $type,
287 $count
288 );
289 }
290
297 public static function logLandingKnowledge($clientId, string $type, int $count = 1)
298 {
299 static::incrementByCount(
301 $clientId,
303 $type,
304 $count
305 );
306 }
307
308 public static function logUserInterface($clientId, string $type, int $count = 1)
309 {
310 static::incrementByCount(
312 $clientId,
314 $type,
315 $count
316 );
317 }
318
319 public static function logAI(string $clientId, string $type): void
320 {
321 static::increment(
323 $clientId,
325 $type
326 );
327 }
328
329 public static function logBISuperset(string $clientId, string $type): void
330 {
331 static::increment(
333 $clientId,
335 $type
336 );
337 }
338
339 public static function logLandingWidget(string $clientId, string $type): void
340 {
341 static::increment(
343 $clientId,
345 $type
346 );
347 }
348
349 protected static function incrementByCount($entityType, $entityId, $subEntityType, $subEntityName, int $count)
350 {
351 $entityKey = static::getEntityKey($entityType, $entityId, $subEntityType, $subEntityName);
352 if (!isset(static::$data[$entityKey]))
353 {
354 static::$data[$entityKey] = 0;
355 }
356 static::$data[$entityKey] += $count;
357 }
358
359 protected static function increment($entityType, $entityId, $subEntityType, $subEntityName)
360 {
361 $entityKey = static::getEntityKey($entityType, $entityId, $subEntityType, $subEntityName);
362 if (!isset(static::$data[$entityKey]))
363 {
364 static::$data[$entityKey] = 0;
365 }
366 static::$data[$entityKey]++;
367 }
368
369 protected static function getEntityKey($entityType, $entityId, $subEntityType, $subEntityName)
370 {
371 return $entityType . "|" . $entityId . "|" . $subEntityType . "|" . $subEntityName;
372 }
373
374 public static function finalize()
375 {
376 if (
377 \Bitrix\Rest\Integration\OAuthModule::isSupported()
378 && !defined('REST_FORCE_USAGE_STAT')
379 )
380 {
381 return;
382 }
383
385 $helper = $connection->getSqlHelper();
386
387 $hour = intval(date('G'));
388 $curDateSql = new Main\Type\Date();
389
390 ksort(static::$data);
391 foreach (static::$data as $entityKey => $count)
392 {
393 list($entityType, $entityId, $subEntityType, $subEntityName) = explode("|", $entityKey, 4);
394 try
395 {
396 $statId = UsageEntityTable::register($entityType, $entityId, $subEntityType, $subEntityName);
397 }
398 catch (SqlQueryException $e)
399 {
400 $statId = false;
401 }
402
403 if ($statId)
404 {
405 $insertFields = [
406 'STAT_DATE' => $curDateSql,
407 'ENTITY_ID' => $statId,
408 'HOUR_'.$hour => $count,
409 ];
410 $updateFields = [
411 'HOUR_'.$hour => new Main\DB\SqlExpression('?#.?# + ?i', static::getTableName(), 'HOUR_' . $hour, $count)
412 ];
413
414 $queries = $helper->prepareMerge(static::getTableName(), [
415 'STAT_DATE',
416 'ENTITY_ID'
417 ], $insertFields, $updateFields);
418
419 foreach ($queries as $query)
420 {
421 $connection->queryExecute($query);
422 }
423 }
424 }
425
426 static::reset();
427 }
428
429 public static function reset()
430 {
431 static::$data = array();
432 }
433
438 public static function updateByFilter(array $filter, array $fields)
439 {
440 $entity = static::getEntity();
441 $sqlHelper = $entity->getConnection()->getSqlHelper();
442 $sqlTableName = static::getTableName();
443
444 $update = $sqlHelper->prepareUpdate($sqlTableName, $fields);
445 $where = Main\Entity\Query::buildFilterSql($entity, $filter);
446 if ($where <> '' && $update[0] <> '')
447 {
448 $sql = "UPDATE {$sqlTableName} SET $update[0] WHERE $where";
449 $entity->getConnection()->queryExecute($sql);
450 }
451 }
452
456 public static function deleteByFilter(array $filter)
457 {
458 $entity = static::getEntity();
459 $sqlTableName = static::getTableName();
460
461 $where = Main\Entity\Query::buildFilterSql($entity, $filter);
462 if ($where <> '')
463 {
464 $sql = "DELETE FROM {$sqlTableName} WHERE ".$where;
465 $entity->getConnection()->queryExecute($sql);
466 }
467 }
468
469 public static function cleanUpAgent()
470 {
471 $date = new Main\Type\DateTime();
472 $date->add("-60D");
473
474 static::deleteByFilter(array(
475 "<STAT_DATE" => $date,
476 "=IS_SENT" => "Y",
477 ));
478
479 return "\\Bitrix\\Rest\\UsageStatTable::cleanUpAgent();";
480 }
481
482 public static function sendAgent()
483 {
485 $helper = $connection->getSqlHelper();
486 $sqlTableName = static::getTableName();
487
488 $select = "
489 SELECT MIN(STAT_DATE) STAT_DATE_MIN
490 FROM {$sqlTableName}
491 WHERE IS_SENT = 'N'
492 AND (STAT_DATE < ".$helper->getCurrentDateFunction().")
493 ";
494 $result = $connection->query($select);
495 $date = $result->fetch();
496 if($date && $date["STAT_DATE_MIN"])
497 {
498 if (static::sendDateStat($date["STAT_DATE_MIN"]))
499 {
500 static::updateByFilter(array("=STAT_DATE" => $date["STAT_DATE_MIN"]), array("IS_SENT" => "Y"));
501 }
502 }
503 return "\\Bitrix\\Rest\\UsageStatTable::sendAgent();";
504 }
505
506 public static function sendDateStat($date)
507 {
508 $return = true;
509
510 $statList = static::getList(array(
511 "select" => array(
512 "ENTITY_ID" => "ENTITY_ID",
513 "ENTITY_TYPE" => "ENTITY.ENTITY_TYPE",
514 "ENTITY_CODE" => "ENTITY.ENTITY_CODE",
515 "SUB_ENTITY_TYPE" => "ENTITY.SUB_ENTITY_TYPE",
516 "SUB_ENTITY_NAME" => "ENTITY.SUB_ENTITY_NAME",
517 "STAT_DATE",
518 "HOUR_0",
519 "HOUR_1",
520 "HOUR_2",
521 "HOUR_3",
522 "HOUR_4",
523 "HOUR_5",
524 "HOUR_6",
525 "HOUR_7",
526 "HOUR_8",
527 "HOUR_9",
528 "HOUR_10",
529 "HOUR_11",
530 "HOUR_12",
531 "HOUR_13",
532 "HOUR_14",
533 "HOUR_15",
534 "HOUR_16",
535 "HOUR_17",
536 "HOUR_18",
537 "HOUR_19",
538 "HOUR_20",
539 "HOUR_21",
540 "HOUR_22",
541 "HOUR_23",
542 ),
543 "filter" => array(
544 "=STAT_DATE" => $date,
545 ),
546 ));
547
548 $usage = array();
549 while ($dayStat = $statList->fetch())
550 {
551 if ($dayStat["STAT_DATE"])
552 {
553 $dayStat["STAT_DATE"] = $dayStat["STAT_DATE"]->format("Y-m-d");
554 $dayStat['HOUR_TOTAL'] = 0;
555 for ($i = 0; $i < 24; $i++)
556 {
557 $dayStat['HOUR_TOTAL'] += (int)$dayStat['HOUR_' . $i];
558 unset($dayStat['HOUR_' . $i]);
559 }
560 $usage[] = $dayStat;
561 }
562 }
563
564 if ($usage)
565 {
566 if (!OAuthService::getEngine()?->isRegistered())
567 {
568 try
569 {
571 }
572 catch (SystemException)
573 {
574 }
575 }
576
577 if (OAuthService::getEngine()?->isRegistered())
578 {
579 $response = OAuthService::getEngine()?->getClient()->sendApplicationUsage($usage);
580 $return = is_array($response) && $response['result'] === true;
581 }
582 else
583 {
584 $return = false;
585 }
586 }
587
588 return $return;
589 }
590}
$connection
Определения actionsdefinitions.php:38
$count
Определения admin_tab.php:4
$type
Определения options.php:106
static getConnection($name="")
Определения application.php:638
Определения date.php:9
static getEngine()
Определения oauthservice.php:49
static register()
Определения oauthservice.php:59
static register($entityType, $entityId, $subEntityType, $subEntityName)
Определения usageentity.php:174
const ENTITY_TYPE_WEBHOOK
Определения usageentity.php:39
const SUB_ENTITY_TYPE_AI
Определения usageentity.php:52
const SUB_ENTITY_TYPE_ROBOT
Определения usageentity.php:44
const SUB_ENTITY_TYPE_BI_SUPERSET
Определения usageentity.php:53
const SUB_ENTITY_TYPE_CONFIGURATION
Определения usageentity.php:47
const SUB_ENTITY_TYPE_LANDING_KNOWLEDGE
Определения usageentity.php:50
const SUB_ENTITY_TYPE_METHOD
Определения usageentity.php:41
const SUB_ENTITY_TYPE_PLACEMENT
Определения usageentity.php:43
const SUB_ENTITY_TYPE_BIZ_PROC
Определения usageentity.php:45
const SUB_ENTITY_TYPE_SEND_MESSAGE
Определения usageentity.php:48
const ENTITY_TYPE_APPLICATION
Определения usageentity.php:38
const SUB_ENTITY_TYPE_UI
Определения usageentity.php:51
const SUB_ENTITY_TYPE_EVENT
Определения usageentity.php:42
const SUB_ENTITY_TYPE_LANDING
Определения usageentity.php:49
const SUB_ENTITY_TYPE_LANDING_WIDGET
Определения usageentity.php:54
const SUB_ENTITY_TYPE_ACTIVITY
Определения usageentity.php:46
static getEntityKey($entityType, $entityId, $subEntityType, $subEntityName)
Определения usagestat.php:369
static logBizProc($clientId, string $clientCode)
Определения usagestat.php:250
static logHookMethod($passwordID, $methodName)
Определения usagestat.php:220
static getMap()
Определения usagestat.php:77
static reset()
Определения usagestat.php:429
static logAI(string $clientId, string $type)
Определения usagestat.php:319
static logLanding($clientId, $type, $count=1)
Определения usagestat.php:280
static logActivity($clientId, $clientCode)
Определения usagestat.php:255
static log(\CRestServer $server)
Определения usagestat.php:200
static finalize()
Определения usagestat.php:374
static deleteByFilter(array $filter)
Определения usagestat.php:456
static updateByFilter(array $filter, array $fields)
Определения usagestat.php:438
static logLandingWidget(string $clientId, string $type)
Определения usagestat.php:339
static logMessage($clientId, $messageType)
Определения usagestat.php:270
static logBISuperset(string $clientId, string $type)
Определения usagestat.php:329
static sendAgent()
Определения usagestat.php:482
static $data
Определения usagestat.php:60
static logEvent($clientId, $eventName)
Определения usagestat.php:230
static sendDateStat($date)
Определения usagestat.php:506
static increment($entityType, $entityId, $subEntityType, $subEntityName)
Определения usagestat.php:359
static logConfiguration($clientId, $clientCode)
Определения usagestat.php:260
static logLandingKnowledge($clientId, string $type, int $count=1)
Определения usagestat.php:297
static cleanUpAgent()
Определения usagestat.php:469
static logUserInterface($clientId, string $type, int $count=1)
Определения usagestat.php:308
static incrementByCount($entityType, $entityId, $subEntityType, $subEntityName, int $count)
Определения usagestat.php:349
static logMethod($clientId, $methodName)
Определения usagestat.php:225
static logPlacement($clientId, $placementName)
Определения usagestat.php:235
static getTableName()
Определения usagestat.php:67
static logRobot($clientId, $clientCode)
Определения usagestat.php:240
getPasswordId()
Определения rest.php:367
getClientId()
Определения rest.php:362
getMethod()
Определения rest.php:372
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
$entity
$select
Определения iblock_catalog_list.php:194
$filter
Определения iblock_catalog_list.php:54
Определения handlers.php:8
$entityId
Определения payment.php:4
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$i
Определения factura.php:643
$response
Определения result.php:21
$clientId
Определения seo_client.php:18
$fields
Определения yandex_run.php:501