Location: PHPKode > projects > Stuffed Tracker - Web Traffic Monitoring & Analysis > stuffed-tracker-web-traffic-monitoring-analysis/actions/admin.cron_log.php
<?
if (!defined("NS_TRACKER_INDEX")) exit;
if (!$nsUser->Logged()) $nsProduct->Redir("login", "", "admin");
if ($nsProduct->LICENSE==3&&!$nsUser->ADMIN) $nsProduct->Redir("default", "", "admin");
if ($nsProduct->LICENSE!=3&&!$nsUser->SUPER_USER) $nsProduct->Redir("default", "", "admin");

require_once SELF."/lib/reports/cache_path.func.php";
require_once SYS."/system/lib/validate.func.php";
require_once SELF."/class/pagenums2.class.php";
$nsLang->TplInc("constructor/periods");
$nsLang->TplInc("inc/reports");

$CompanyId=ValidVar($_REQUEST['CompanyId']);
$RunId=ValidVar($_REQUEST['RunId']);
$ViewDate=ValidVar($_REQUEST['ViewDate']);
$ShowSleep=ValidVar($_REQUEST['ShowSleep']);
$ViewId=ValidVar($_REQUEST['ViewId']);
if (!$ViewDate || !ValidDate($ViewDate)) $ViewDate=UserDate();

$PageTitle=$Lang['Title'];
$nsLang->TplInc("inc/top_panel.inc");
$ProgPath[0]['Name']=$Lang['Administr'];
$ProgPath[0]['Url']=getURL("admin", "", "admin");
$ProgPath[1]['Name']=$Lang['Title'];
$ProgPath[1]['Url']=getURL("cron_log", "", "admin");
$MenuSection="admin";

//$Settings=GetSettings();
$CpuAvail=GetCpuLoad(true);
if ($CpuAvail==-1) $CpuAvail=false;

$Query = "SELECT ID,NAME FROM ?_client CL";
$Sql = new Query($Query);
$ClientArr=array();
while ($Row=$Sql->Row()) {
    if (!$CompanyId) $CompanyId=$Row->ID;
    $ClientArr[$Row->ID]=$Row;
}


if (!$RunId) {

$LogArr=array();
foreach ($ClientArr as $CRow) {
    $CompanyId=$CRow->ID;
    
    $Query = "
        SELECT 
        CL.ID, CL.RUN_ID,
        UNIX_TIMESTAMP(MIN(CL.STAMP)) AS MIN_STAMP,
        DATE_FORMAT(DATE_ADD(MIN(CL.STAMP),INTERVAL '$nsUser->SQL_TZ' HOUR_MINUTE),'%Y-%m-%d %H:%i') AS RUN_STAMP,
        SUM(CL.RUN_TIME) AS SUM_TIME,
        MAX(CL.CPU_LOAD) AS MAX_CPU,
        COUNT(CL.ID) AS REPORTS_SUM
        FROM ?_{$CompanyId}_cache_cron_log CL
        INNER JOIN ?_{$CompanyId}_cache C ON C.ID=CL.CACHE_ID
        WHERE CL.STAMP BETWEEN DATE_ADD('$ViewDate 00:00:00', INTERVAL '$nsUser->_SQL_TZ' HOUR_MINUTE) AND DATE_ADD('$ViewDate 23:59:59', INTERVAL '$nsUser->_SQL_TZ' HOUR_MINUTE)
                    AND CL.CACHE_ID>0
        GROUP BY CL.RUN_ID
    ";
    $Sql = new Query($Query);
    while ($Row=$Sql->Row()) {
        if (isset($LogArr[$Row->RUN_ID])) {
            $LogArr[$Row->RUN_ID]->SUM_TIME=$Row->SUM_TIME=$Row->SUM_TIME+$LogArr[$Row->RUN_ID]->SUM_TIME;
            $LogArr[$Row->RUN_ID]->MAX_CPU=$Row->MAX_CPU=max($Row->MAX_CPU,$LogArr[$Row->RUN_ID]->MAX_CPU);
            $LogArr[$Row->RUN_ID]->REPORTS_SUM=$Row->REPORTS_SUM=$Row->REPORTS_SUM+$LogArr[$Row->RUN_ID]->REPORTS_SUM;
        }
        if (!isset($LogArr[$Row->RUN_ID]) || $LogArr[$Row->RUN_ID]->MIN_STAMP>$Row->MIN_STAMP) {
            $LogArr[$Row->RUN_ID]=$Row;
        }
    }
    
    if ($ShowSleep) {
        $Query = "
            SELECT CL.ID, CL.RUN_ID,
            UNIX_TIMESTAMP(MIN(CL.STAMP)) AS MIN_STAMP,
            DATE_FORMAT(DATE_ADD(MIN(CL.STAMP),INTERVAL '$nsUser->SQL_TZ' HOUR_MINUTE),'%Y-%m-%d %H:%i') AS RUN_STAMP,
            '0' AS SUM_TIME, '0' AS MAX_CPU, '0' AS REPORTS_SUM
            FROM ?_{$CompanyId}_cache_cron_log CL
            WHERE CL.STAMP BETWEEN DATE_ADD('$ViewDate 00:00:00', INTERVAL '$nsUser->_SQL_TZ' HOUR_MINUTE) AND DATE_ADD('$ViewDate 23:59:59', INTERVAL '$nsUser->_SQL_TZ' HOUR_MINUTE)
                        AND CL.CACHE_ID=0
            GROUP BY CL.RUN_ID
        ";
        $Sql = new Query($Query);
        while ($Row=$Sql->Row()) {
            $Row->TIME_ARR=array();
            if (!isset($LogArr[$Row->RUN_ID])) $LogArr[$Row->RUN_ID]=$Row;
        }
    }

}



function SortByStamp($a, $b) {
    if ($a->MIN_STAMP < $b->MIN_STAMP) return 1;
    else if ($a->MIN_STAMP > $b->MIN_STAMP) return -1;
    else return $a->ID>$b->ID?-1:1;
}

usort($LogArr,"SortByStamp");
$Pages = new PageNums(count($LogArr),20);
$Pages->Args="&ViewDate=$ViewDate&CpId=$CpId&ShowSleep=$ShowSleep";
$Pages->Calculate();
if ($Pages->Pages>1) $LogArr=array_slice($LogArr,$Pages->PageStart,$Pages->Limit);


foreach ($LogArr as $i=>$Row) {
    $Row->SUM_TIME=number_format($Row->SUM_TIME,3,'.','');
    $Row->TIME_ARR=TimeArr($Row->SUM_TIME);
    $LogArr[$i]=$Row;    
}


include_once $nsTemplate->Inc();

} // endif !RunId

///////////////////////////////////////////////////////////////

if ($RunId) {

$FullLogArr=array();

$SiteHostArr=array("-1"=>"");
$CampNameArr=array();
$CampGrpArr=array();
$SplitNameArr=array();
$PeriodsName=array("-1"=>"");

$SiteIds=array();
$CampIds=array();
$GrpIds=array();
$SplitIds=array();
$PeriodIds=array();

$And="";
if (!$ShowSleep) $And="AND CL.CACHE_ID!=0";
else $And= " AND (CL.CACHE_ID=0 OR (CL.CACHE_ID > 0 AND C.ID IS NOT NULL))";
foreach ($ClientArr as $CRow) {
    $CompanyId=$CRow->ID;

$Query = "
    SELECT CL.*, 
        UNIX_TIMESTAMP(CL.STAMP) AS USTAMP,
        DATE_FORMAT(CL.STAMP, '%Y-%m-%d %H:%i') AS STAMP,
        C.CACHE_TYPE,
        QS.PARAMS_SET,
        C.MODE,
        SR.NAME AS RNAME, RG.NAME AS GNAME
        FROM ?_{$CompanyId}_cache_cron_log CL
        LEFT JOIN ?_{$CompanyId}_cache C
            ON C.ID=CL.CACHE_ID
        LEFT JOIN ?_{$CompanyId}_cache_query_set QS
            ON QS.ID=C.QUERY_SET_ID

        LEFT JOIN ?_static_report SR
            ON SR.ID=CL.REPORT_ID
        LEFT JOIN ?_static_report_group RG
            ON RG.ID=SR.GROUP_ID

        WHERE RUN_ID=? $And
        ORDER BY CL.STAMP DESC
    ";
$Sql = new Query($Query,false,$RunId);
while ($Row=$Sql->Row()) {
    $Row->COMPANY_ID=$CompanyId;
    if ($Row->RUN_MODE==5) {
        if (! trim($Row->MESSAGE)) continue;
        if (isset($Lang[$Row->MESSAGE])) $Row->MESSAGE=$Lang[$Row->MESSAGE];
        $FullLogArr[]=$Row;
        continue;
    }
    
    $Row->NAME="";
    $Row->CNAME=$ClientArr[$CompanyId]->NAME;
    $Row->P=false;
    $Row->URL=false;
    $Row->RUN_TIME=number_format($Row->RUN_TIME,3,'.','');
    $Row->TIME_ARR=TimeArr($Row->RUN_TIME);
    if ($Row->CACHE_TYPE==1 && !$Row->REPORT_ID) $Row->CACHE_TYPE=0;
    if ($Row->RUN_MODE==4) $Row->CACHE_TYPE=4;
    if ($Row->RUN_MODE==1) $Row->CACHE_TYPE=3;
    
        $Arr=unserialize($Row->PARAMS_SET);
        
        if ($Row->REPORT_ID) {
            if (isset($Lang[$Row->RNAME])) $Row->RNAME=$Lang[$Row->RNAME];
            if (isset($Lang[$Row->GNAME])) $Row->GNAME=$Lang[$Row->GNAME];        
            $Row->NAME=$Row->GNAME." : ".$Row->RNAME;                
        }
        
        if( isset($Arr['WhereArr']) && !$Row->REPORT_ID) {
                $PathArr=CachePath($Arr['WhereArr'], $CompanyId);
                foreach ($Arr as $k=>$v) {
                    if ($k=="WhereArr") continue;
                    $PathArr['Path'].="&$k=$v";
                }
                $Row->NAME.=$PathArr['Name'];
                if ($Row->MODE=="NATURAL") {
                    $Row->URL=getURL("natural_constructor",$PathArr['Path']."&CpId=".$CompanyId,"report");
                }
                if ($Row->MODE=="PAID") {
                    $Row->URL=getURL("paid_constructor",$PathArr['Path']."&CpId=".$CompanyId,"report");
                }
        }

        if (ValidVar($Arr['SiteId'])) {$Row->SITE_ID=$Arr['SiteId']; $SiteIds[]=$Arr['SiteId'];}
        if (ValidVar($Arr['CampId'])) {$Row->CAMP_ID=$Arr['CampId']; $CampIds[]=$Arr['CampId'];}
        if (ValidVar($Arr['GrpId'])) {$Row->GRP_ID=$Arr['GrpId']; $GrpIds[]=$Arr['GrpId'];}
        if (ValidVar($Arr['SplitId'])) {$Row->SPLIT_ID=$Arr['SplitId']; $SplitIds[]=$Arr['SplitId'];}
        if (ValidVar($Arr['DynamicPeriod'])) {$Row->PERIOD_TEMPL=$Arr['DynamicPeriod']; $PeriodIds[]=$Arr['DynamicPeriod'];}

        if (ValidVar($Arr['ViewDate'])) $Row->P=$Arr['ViewDate'];
        if (ValidVar($Arr['StartDate'])&&ValidVar($Arr['EndDate'])) $Row->P=$Arr['StartDate']." &mdash; ".$Arr['EndDate'];

    $FullLogArr[]=$Row;
}
}


if(!empty($SiteIds)) {
    $SiteIds=array_unique($SiteIds);
    $Query = "SELECT ID, HOST FROM ?_site WHERE ID IN (?)";
    $Sql = new Query($Query,false,$SiteIds);
    while ($Row=$Sql->Row()) $SiteHostArr[$Row->ID]=$Row->HOST;
}
if(!empty($CampIds)) {
    $CampIds=array_unique($CampIds);
    $Query = "SELECT ID, NAME FROM ?_camp_piece WHERE ID IN (?)";
    $Sql = new Query($Query,false,$CampIds);
    while ($Row=$Sql->Row()) $CampNameArr[$Row->ID]=$Row->NAME;
}
if(!empty($GrpIds)) {
    $GrpIds=array_unique($GrpIds);
    $Query = "SELECT ID, NAME FROM ?_campaign WHERE ID IN (?)";
    $Sql = new Query($Query,false,$GrpIds);
    while ($Row=$Sql->Row()) $CampGrpArr[$Row->ID]=$Row->NAME;
}
if(!empty($SplitIds)) {
    $SplitIds=array_unique($SplitIds);
    $Query = "SELECT ID, NAME FROM ?_camp_piece WHERE ID IN (?)";
    $Sql = new Query($Query,false,$SplitIds);
    while ($Row=$Sql->Row()) $SplitNameArr[$Row->ID]=$Row->NAME;
}
if(!empty($PeriodIds)) {
    $PeriodIds=array_unique($PeriodIds);
    $Query = "SELECT NAME, TEMPLATE FROM ?_periods WHERE TEMPLATE IN (?)";
    $Sql = new Query($Query,false,$PeriodIds);
    while ($Row=$Sql->Row()) {
        if (isset($Lang[$Row->TEMPLATE])) $Row->NAME=$Lang[$Row->TEMPLATE];
        $PeriodsName[$Row->TEMPLATE]=$Row->NAME;
    }
}

function SortByStamp2($a, $b) {
    if ($a->USTAMP < $b->USTAMP) return 1;
    else if ($a->USTAMP > $b->USTAMP) return -1;
    else return $a->ID>$b->ID?-1:1;
}
usort($FullLogArr,"SortByStamp2");

$AddColSpan=0;
$AddColSpan+=($CpuAvail)?1:0;
$AddColSpan+=($nsProduct->LICENSE!=2)?1:0;

foreach ($FullLogArr as $i=>$Row) {
    if ($Row->RUN_MODE==5) continue;
    if (V($Row->SITE_ID)&&V($SiteHostArr[$Row->SITE_ID])) $Row->NAME.=($Row->NAME?"<br>":"").$SiteHostArr[$Row->SITE_ID];
    if (V($Row->CAMP_ID)&&V($CampNameArr[$Row->CAMP_ID])) $Row->NAME.=($Row->NAME?"<br>":"").$CampNameArr[$Row->CAMP_ID];
    if (V($Row->GRP_ID)&&V($CampGrpArr[$Row->GRP_ID])) $Row->NAME.=($Row->NAME?"<br>":"").$CampGrpArr[$Row->GRP_ID];
    if (V($Row->SPLIT_ID)&&V($SplitNameArr[$Row->SPLIT_ID])) $Row->NAME.=($Row->NAME?"<br>":"").$SplitNameArr[$Row->SPLIT_ID];
    if (V($Row->PERIOD_TEMPL)&&V($PeriodsName[$Row->PERIOD_TEMPL])) $Row->P=$PeriodsName[$Row->PERIOD_TEMPL];
    if (!$Row->NAME) $Row->NAME=$Row->CNAME;
    $FullLogArr[$i]=$Row;
}
include_once $nsTemplate->Inc('admin.cron_log_list');

}




?>
Return current item: Stuffed Tracker - Web Traffic Monitoring & Analysis