Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usagestat.php
1<?php
2namespace Bitrix\Rest;
3
6
56class UsageStatTable extends Main\Entity\DataManager
57{
58
59 protected static $data = array();
60
66 public static function getTableName()
67 {
68 return 'b_rest_usage_stat';
69 }
70
76 public static function getMap()
77 {
78 return array(
79 'STAT_DATE' => array(
80 'data_type' => 'date',
81 'primary' => true
82 ),
83 'ENTITY_ID' => array(
84 'data_type' => 'integer',
85 'primary' => true
86 ),
87 'IS_SENT' => array(
88 'data_type' => 'boolean',
89 'values' => array(
90 'N',
91 'Y'
92 )
93 ),
94 'HOUR_0' => array(
95 'data_type' => 'integer',
96 'required' => true
97 ),
98 'HOUR_1' => array(
99 'data_type' => 'integer',
100 'required' => true
101 ),
102 'HOUR_2' => array(
103 'data_type' => 'integer',
104 'required' => true
105 ),
106 'HOUR_3' => array(
107 'data_type' => 'integer',
108 'required' => true
109 ),
110 'HOUR_4' => array(
111 'data_type' => 'integer',
112 'required' => true
113 ),
114 'HOUR_5' => array(
115 'data_type' => 'integer',
116 'required' => true
117 ),
118 'HOUR_6' => array(
119 'data_type' => 'integer',
120 'required' => true
121 ),
122 'HOUR_7' => array(
123 'data_type' => 'integer',
124 'required' => true
125 ),
126 'HOUR_8' => array(
127 'data_type' => 'integer',
128 'required' => true
129 ),
130 'HOUR_9' => array(
131 'data_type' => 'integer',
132 'required' => true
133 ),
134 'HOUR_10' => array(
135 'data_type' => 'integer',
136 'required' => true
137 ),
138 'HOUR_11' => array(
139 'data_type' => 'integer',
140 'required' => true
141 ),
142 'HOUR_12' => array(
143 'data_type' => 'integer',
144 'required' => true
145 ),
146 'HOUR_13' => array(
147 'data_type' => 'integer',
148 'required' => true
149 ),
150 'HOUR_14' => array(
151 'data_type' => 'integer',
152 'required' => true
153 ),
154 'HOUR_15' => array(
155 'data_type' => 'integer',
156 'required' => true
157 ),
158 'HOUR_16' => array(
159 'data_type' => 'integer',
160 'required' => true
161 ),
162 'HOUR_17' => array(
163 'data_type' => 'integer',
164 'required' => true
165 ),
166 'HOUR_18' => array(
167 'data_type' => 'integer',
168 'required' => true
169 ),
170 'HOUR_19' => array(
171 'data_type' => 'integer',
172 'required' => true
173 ),
174 'HOUR_20' => array(
175 'data_type' => 'integer',
176 'required' => true
177 ),
178 'HOUR_21' => array(
179 'data_type' => 'integer',
180 'required' => true
181 ),
182 'HOUR_22' => array(
183 'data_type' => 'integer',
184 'required' => true
185 ),
186 'HOUR_23' => array(
187 'data_type' => 'integer',
188 'required' => true
189 ),
190 'ENTITY' => array(
191 'data_type' => 'Bitrix\Rest\UsageEntityTable',
192 'reference' => array(
193 '=this.ENTITY_ID' => 'ref.ID',
194 ),
195 ),
196 );
197 }
198
199 public static function log(\CRestServer $server)
200 {
201 if (Main\ModuleManager::isModuleInstalled('oauth'))
202 {
203 return;
204 }
205
206 if ($server->getClientId())
207 {
208 static::logMethod($server->getClientId(), $server->getMethod());
209 }
210 elseif ($server->getPasswordId())
211 {
212 static::logHookMethod($server->getPasswordId(), $server->getMethod());
213 }
214 }
215
216 public static function logHookMethod($passwordID, $methodName)
217 {
218 static::increment(UsageEntityTable::ENTITY_TYPE_WEBHOOK, $passwordID, UsageEntityTable::SUB_ENTITY_TYPE_METHOD, $methodName);
219 }
220
221 public static function logMethod($clientId, $methodName)
222 {
224 }
225
226 public static function logEvent($clientId, $eventName)
227 {
229 }
230
231 public static function logPlacement($clientId, $placementName)
232 {
234 }
235
236 public static function logRobot($clientId, $clientCode)
237 {
239 }
240
246 public static function logBizProc($clientId, string $clientCode): void
247 {
249 }
250
251 public static function logActivity($clientId, $clientCode)
252 {
254 }
255
256 public static function logConfiguration($clientId, $clientCode)
257 {
258 static::increment(
260 $clientId,
262 $clientCode
263 );
264 }
265
266 public static function logMessage($clientId, $messageType)
267 {
268 static::increment(
270 $clientId,
272 $messageType
273 );
274 }
275
276 public static function logLanding($clientId, $type, $count = 1)
277 {
278 static::incrementByCount(
280 $clientId,
282 $type,
283 $count
284 );
285 }
286
293 public static function logLandingKnowledge($clientId, string $type, int $count = 1)
294 {
295 static::incrementByCount(
297 $clientId,
299 $type,
300 $count
301 );
302 }
303
304 public static function logUserInterface($clientId, string $type, int $count = 1)
305 {
306 static::incrementByCount(
308 $clientId,
310 $type,
311 $count
312 );
313 }
314
315 public static function logBISuperset(string $clientId, string $type): void
316 {
317 static::increment(
319 $clientId,
321 $type
322 );
323 }
324
325 protected static function incrementByCount($entityType, $entityId, $subEntityType, $subEntityName, int $count)
326 {
327 $entityKey = static::getEntityKey($entityType, $entityId, $subEntityType, $subEntityName);
328 if (!isset(static::$data[$entityKey]))
329 {
330 static::$data[$entityKey] = 0;
331 }
332 static::$data[$entityKey] += $count;
333 }
334
335 protected static function increment($entityType, $entityId, $subEntityType, $subEntityName)
336 {
337 $entityKey = static::getEntityKey($entityType, $entityId, $subEntityType, $subEntityName);
338 if (!isset(static::$data[$entityKey]))
339 {
340 static::$data[$entityKey] = 0;
341 }
342 static::$data[$entityKey]++;
343 }
344
345 protected static function getEntityKey($entityType, $entityId, $subEntityType, $subEntityName)
346 {
347 return $entityType . "|" . $entityId . "|" . $subEntityType . "|" . $subEntityName;
348 }
349
350 public static function finalize()
351 {
352 if (Main\ModuleManager::isModuleInstalled('oauth'))
353 {
354 return;
355 }
356
357 $connection = Main\Application::getConnection();
358 $helper = $connection->getSqlHelper();
359
360 $hour = intval(date('G'));
361 $curDateSql = new Main\Type\Date();
362
363 ksort(static::$data);
364 foreach (static::$data as $entityKey => $count)
365 {
366 list($entityType, $entityId, $subEntityType, $subEntityName) = explode("|", $entityKey, 4);
367 try
368 {
369 $statId = UsageEntityTable::register($entityType, $entityId, $subEntityType, $subEntityName);
370 }
371 catch (SqlQueryException $e)
372 {
373 $statId = false;
374 }
375
376 if ($statId)
377 {
378 $insertFields = array(
379 'STAT_DATE' => $curDateSql,
380 'ENTITY_ID' => $statId,
381 'HOUR_'.$hour => $count,
382 );
383 $updateFields = array(
384 'HOUR_'.$hour => new Main\DB\SqlExpression('?#+?i', 'HOUR_'.$hour, $count)
385 );
386
387 $queries = $helper->prepareMerge(static::getTableName(), array(
388 'STAT_DATE',
389 'ENTITY_ID'
390 ), $insertFields, $updateFields);
391
392 foreach ($queries as $query)
393 {
394 $connection->queryExecute($query);
395 }
396 }
397 }
398
399 static::reset();
400 }
401
402 public static function reset()
403 {
404 static::$data = array();
405 }
406
411 public static function updateByFilter(array $filter, array $fields)
412 {
413 $entity = static::getEntity();
414 $sqlHelper = $entity->getConnection()->getSqlHelper();
415 $sqlTableName = static::getTableName();
416
417 $update = $sqlHelper->prepareUpdate($sqlTableName, $fields);
418 $where = Main\Entity\Query::buildFilterSql($entity, $filter);
419 if ($where <> '' && $update[0] <> '')
420 {
421 $sql = "UPDATE {$sqlTableName} SET $update[0] WHERE $where";
422 $entity->getConnection()->queryExecute($sql);
423 }
424 }
425
429 public static function deleteByFilter(array $filter)
430 {
431 $entity = static::getEntity();
432 $sqlTableName = static::getTableName();
433
434 $where = Main\Entity\Query::buildFilterSql($entity, $filter);
435 if ($where <> '')
436 {
437 $sql = "DELETE FROM {$sqlTableName} WHERE ".$where;
438 $entity->getConnection()->queryExecute($sql);
439 }
440 }
441
442 public static function cleanUpAgent()
443 {
444 $date = new Main\Type\DateTime();
445 $date->add("-60D");
446
447 static::deleteByFilter(array(
448 "<STAT_DATE" => $date,
449 "=IS_SENT" => "Y",
450 ));
451
452 return "\\Bitrix\\Rest\\UsageStatTable::cleanUpAgent();";
453 }
454
455 public static function sendAgent()
456 {
457 $connection = \Bitrix\Main\Application::getConnection();
458 $helper = $connection->getSqlHelper();
459 $sqlTableName = static::getTableName();
460
461 $select = "
462 SELECT MIN(STAT_DATE) STAT_DATE_MIN
463 FROM {$sqlTableName}
464 WHERE IS_SENT = 'N'
465 AND (STAT_DATE < ".$helper->getCurrentDateFunction().")
466 ";
467 $result = $connection->query($select);
468 $date = $result->fetch();
469 if($date && $date["STAT_DATE_MIN"])
470 {
471 if (static::sendDateStat($date["STAT_DATE_MIN"]))
472 {
473 static::updateByFilter(array("=STAT_DATE" => $date["STAT_DATE_MIN"]), array("IS_SENT" => "Y"));
474 }
475 }
476 return "\\Bitrix\\Rest\\UsageStatTable::sendAgent();";
477 }
478
479 public static function sendDateStat($date)
480 {
481 $return = true;
482
483 $statList = static::getList(array(
484 "select" => array(
485 "ENTITY_ID" => "ENTITY_ID",
486 "ENTITY_TYPE" => "ENTITY.ENTITY_TYPE",
487 "ENTITY_CODE" => "ENTITY.ENTITY_CODE",
488 "SUB_ENTITY_TYPE" => "ENTITY.SUB_ENTITY_TYPE",
489 "SUB_ENTITY_NAME" => "ENTITY.SUB_ENTITY_NAME",
490 "STAT_DATE",
491 "HOUR_0",
492 "HOUR_1",
493 "HOUR_2",
494 "HOUR_3",
495 "HOUR_4",
496 "HOUR_5",
497 "HOUR_6",
498 "HOUR_7",
499 "HOUR_8",
500 "HOUR_9",
501 "HOUR_10",
502 "HOUR_11",
503 "HOUR_12",
504 "HOUR_13",
505 "HOUR_14",
506 "HOUR_15",
507 "HOUR_16",
508 "HOUR_17",
509 "HOUR_18",
510 "HOUR_19",
511 "HOUR_20",
512 "HOUR_21",
513 "HOUR_22",
514 "HOUR_23",
515 ),
516 "filter" => array(
517 "=STAT_DATE" => $date,
518 ),
519 ));
520
521 $usage = array();
522 while ($dayStat = $statList->fetch())
523 {
524 if ($dayStat["ENTITY_CODE"] && $dayStat["STAT_DATE"])
525 {
526 $dayStat["STAT_DATE"] = $dayStat["STAT_DATE"]->format("Y-m-d");
527 $dayStat['HOUR_TOTAL'] = 0;
528 for ($i = 0; $i < 24; $i++)
529 {
530 $dayStat['HOUR_TOTAL'] += (int)$dayStat['HOUR_' . $i];
531 unset($dayStat['HOUR_' . $i]);
532 }
533 $usage[] = $dayStat;
534 }
535 }
536
537 if ($usage)
538 {
539 $response = \Bitrix\Rest\OAuthService::getEngine()->getClient()->sendApplicationUsage($usage);
540 $return = is_array($response) && $response['result'] === true;
541 }
542
543 return $return;
544 }
545}
static isModuleInstalled($moduleName)
static register($entityType, $entityId, $subEntityType, $subEntityName)
static getEntityKey($entityType, $entityId, $subEntityType, $subEntityName)
static logBizProc($clientId, string $clientCode)
static logHookMethod($passwordID, $methodName)
static logLanding($clientId, $type, $count=1)
static logActivity($clientId, $clientCode)
static log(\CRestServer $server)
static deleteByFilter(array $filter)
static updateByFilter(array $filter, array $fields)
static logMessage($clientId, $messageType)
static logBISuperset(string $clientId, string $type)
static logEvent($clientId, $eventName)
static increment($entityType, $entityId, $subEntityType, $subEntityName)
static logConfiguration($clientId, $clientCode)
static logLandingKnowledge($clientId, string $type, int $count=1)
static logUserInterface($clientId, string $type, int $count=1)
static incrementByCount($entityType, $entityId, $subEntityType, $subEntityName, int $count)
static logMethod($clientId, $methodName)
static logPlacement($clientId, $placementName)
static logRobot($clientId, $clientCode)