CBPAllStateService::getRunningCounters: Bitrix method

      
<?php 
//  CBPAllStateService :: getRunningCounters()
//  /dist/bitrix/modules/bizproc/classes/general/stateservice.php:672

    
public static function getRunningCounters($userId)
    {
        global 
$DB;

        
$counters = array('*' => 0);
        
$cache = \Bitrix\Main\Application::getInstance()->getManagedCache();
        
$cacheTag self::COUNTERS_CACHE_TAG_PREFIX.$userId;
        if (
$cache->read(3600*24*7$cacheTag))
        {
            
$counters = (array) $cache->get($cacheTag);
        }
        else
        {
            
$query =
                
"SELECT WI.MODULE_ID AS MODULE_ID, WI.ENTITY AS ENTITY, COUNT('x') AS CNT ".
                
'FROM b_bp_workflow_instance WI '.
                
'WHERE WI.STARTED_BY = '.(int)$userId.' '.
                
'GROUP BY MODULE_ID, ENTITY';

            
$iterator $DB->Query($querytrue);
            if (
$iterator)
            {
                while (
$row $iterator->fetch())
                {
                    
$cnt = (int)$row['CNT'];
                    
$counters[$row['MODULE_ID']][$row['ENTITY']] = $cnt;
                    if (!isset(
$counters[$row['MODULE_ID']]['*']))
                        
$counters[$row['MODULE_ID']]['*'] = 0;
                    
$counters[$row['MODULE_ID']]['*'] += $cnt;
                    
$counters['*'] += $cnt;
                }
                
$cache->set($cacheTag$counters);
            }
        }
        return 
$counters;
    }