• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/volume/base.php
  • Класс: Bitrix\Crm\Volume\Base
  • Вызов: Base::prepareRangeActionList
protected function prepareRangeActionList($entityClass, $dateFieldAlias, $actionAliases)
{
	$indicatorId = static::getIndicatorId();

	$queueList = array();

	$actionCommands = array(
		'MEASURE_ENTITY',
		'MEASURE_FILE',
		'MEASURE_ACTIVITY',
		'MEASURE_EVENT',
	);

	$maxIdRange = -1;
	if (isset(static::$maxIdRangeCache[$entityClass]))
	{
		$maxIdRange = static::$maxIdRangeCache[$entityClass];
	}
	else
	{
		$cache = new \CPHPCache();
		if ($cache->startDataCache(3 * 3600, "{$entityClass}:{$indicatorId}:maxIdRange", 'crm/configs/volume'))
		{
			/**
			 * @var ORM\Data\DataManager $entityClass
			 */
			$entityQuery = $entityClass::query();

			$row0 = $entityQuery
				->registerRuntimeField(new ORM\Fields\ExpressionField('CNT', 'COUNT(*)'))
				->addSelect('CNT')
				->exec()
				->fetch();

			if ($row0)
			{
				if ((int)$row0['CNT'] > 500000)
				{
					$maxIdRange = 100000;
				}
				elseif ((int)$row0['CNT'] > 100000)
				{
					$maxIdRange = 50000;
				}
			}

			static::$maxIdRangeCache[$entityClass] = $maxIdRange;

			$cache->endDataCache($maxIdRange);
		}
		else
		{
			$maxIdRange = $cache->getVars();
			static::$maxIdRangeCache[$entityClass] = $maxIdRange;
		}
	}

	if ($maxIdRange > 0)
	{
		$cache = new \CPHPCache();
		if ($cache->startDataCache(3 * 3600, "{$entityClass}:{$indicatorId}:queueList", 'crm/configs/volume'))
		{
			/**
			 * @var ORM\Data\DataManager $entityClass
			 */
			$query = $entityClass::query();

			$month = new ORM\Fields\ExpressionField('YY', "YEAR(%s)", $dateFieldAlias);
			$query->registerRuntimeField($month)->addSelect('YY');

			$month = new ORM\Fields\ExpressionField('MM', "MONTH(%s)", $dateFieldAlias);
			$query->registerRuntimeField($month)->addSelect('MM');

			$month = new ORM\Fields\ExpressionField('DD', "DAY(%s)", $dateFieldAlias);
			$query->registerRuntimeField($month)->addSelect('DD');

			$border = new ORM\Fields\ExpressionField('BRDR', 'MAX(%s)', 'ID');
			$query->registerRuntimeField($border)->addSelect('BRDR');

			$counter = new ORM\Fields\ExpressionField('CNT', 'COUNT(*)');
			$query->registerRuntimeField($counter)->addSelect('CNT');

			$query->setGroup(array('YY', 'MM', 'DD'))->setOrder(array('BRDR' => 'ASC'));

			$res = $query->exec();
			if ($row = $res->fetch())
			{
				$count = 0;
				$appendQueueList = function($range) use ($actionCommands, $actionAliases, &$queueList, $indicatorId)
				{
					foreach ($actionCommands as $command)
					{
						if (isset($actionAliases[$command]))
						{
							$queueList[] = array(
								'indicatorId' => $indicatorId,
								'action' => $actionAliases[$command],
								'range' => $range,
							);
						}
					}
				};

				$prevId = null;
				do
				{
					if (
						$row['CNT'] >= $maxIdRange ||
						($count + (int)$row['CNT']) >= $maxIdRange * 1.3 ||
						$count >= $maxIdRange
					)
					{
						$range = '';
						if ($prevId > 0)
						{
							$range .= $prevId;
						}
						$range .= '-'.(int)$row['BRDR'];

						$appendQueueList($range);

						$count = 0;
						$prevId = (int)$row['BRDR'];
						continue;
					}

					$count += (int)$row['CNT'];
				}
				while ($row = $res->fetch());

				if ($count >= 0 && $prevId > 0)
				{
					$range = $prevId.'-';

					$appendQueueList($range);
				}
			}

			$cache->endDataCache($queueList);
		}
		else
		{
			$queueList = $cache->getVars();
		}
	}
	else
	{
		foreach ($actionCommands as $command)
		{
			if (isset($actionAliases[$command]))
			{
				$queueList[] = array(
					'indicatorId' => $indicatorId,
					'action' => $actionAliases[$command],
				);
			}
		}
	}

	return $queueList;
}