<?php
class report_proc extends c_report
{
 var $title='Отчет нагрузки на систему' ;
 var $filter=array() ;
 public $use_fusioncharts=1 ;
 //var $PDF_master='TCPDF' ;  // короткие отчеты выгружвать через обычный  PDF

 function _setting()
   {
     $data_info=execSQL_van('select min(c_data) as min_data,max(c_data) as max_data from obj_site_mo_session') ;
      // damp_array($data_info) ;
     $t=$data_info['min_data'] ;
     while($t<$data_info['max_data'])
     {   $ts=getdate($t) ;
         $cur_month=mktime(0,0,0,$ts['mon'],1,$ts['year']) ;
         $arr_from[$cur_month]=get_month_year($cur_month,'month year') ;
         $t=mktime(0,0,0,$ts['mon']+1,1,$ts['year']) ;
         $arr_to[$t-1]=get_month_year($t-1,'month year') ;;
     }




      $recs=execSQL_row('select pkey,c_data from obj_site_mo_session where c_data_m is null limit 100000',1) ;
      if (_sizeof($recs)) foreach($recs as $id=>$time)
      {   $now=getdate($time) ;
          $data['c_data_m']=mktime($now['hours'],$now['minutes'],0,$now['mon'],$now['mday'],$now['year']) ;
          $data['c_data_h']=mktime($now['hours'],0,0,$now['mon'],$now['mday'],$now['year']) ;
          $data['c_data_d']=mktime(0,0,0,$now['mon'],$now['mday'],$now['year']) ;
          update_rec_in_table('obj_site_mo_session',$data,'pkey='.$id) ;
      }

      $recs=execSQL_row('select pkey,c_data from obj_site_mon_res where c_data_m is null limit 10000',1) ;
      if (_sizeof($recs)) foreach($recs as $id=>$time)
      {   $now=getdate($time) ;
          $data['c_data_m']=mktime($now['hours'],$now['minutes'],0,$now['mon'],$now['mday'],$now['year']) ;
          $data['c_data_h']=mktime($now['hours'],0,0,$now['mon'],$now['mday'],$now['year']) ;
          $data['c_data_d']=mktime(0,0,0,$now['mon'],$now['mday'],$now['year']) ;
          update_rec_in_table('obj_site_mon_res',$data,'pkey='.$id) ;
      }


     ?><div id="panel_filter">
                <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                     <fieldset>
                         <div class="row">
                             <section class="col col-2">
                                 <label class="label">С, месяц:</label>
                                 <label class="select">
                                     <select name="filter[month_from]">
                                         <?foreach($arr_from as $t=>$month){?><option value="<?echo $t?>" <?if ($this->filter['month_from']==$t) echo 'selected'?>><?echo $month?></option><?}?>
                                     </select>
                                 </label>
                             </section>
                             <section class="col col-2">
                                 <label class="label">По, месяц</label>
                                 <label class="select">
                                     <select name="filter[month_to]">
                                         <?foreach($arr_to as $t=>$month){?><option value="<?echo $t?>" <?if ($this->filter['month_to']==$t) echo 'selected'?>><?echo $month?></option><?}?>
                                     </select>
                                 </label>
                             </section>
                             <section class="col col-2">
                                 <label class="label">Группировка по времени</label>
                                 <label class="select">
                                     <select name="filter[tg]">
                                         <option value="2" <?if ($this->filter['tg']==2) echo 'selected'?>>Поминутно</option>
                                         <option value="1" <?if ($this->filter['tg']==1) echo 'selected'?>>Почасово</option>
                                         <option value="0" <?if ($this->filter['tg']==0) echo 'selected'?>>Ежедневно</option>
                                     </select>
                                 </label>
                             </section>
                         </div>
                     </fieldset>
                     <?$this->panel_buttons();?>
                 </form>
         </div>
     <?
   }

    function HTML_report_body($options=array())
    {   $data_info=execSQL_van('select min(c_data) as min_data,max(c_data) as max_data from obj_site_mo_session') ;
        if (!$this->filter['month_from'])
        { $ts=getdate($data_info['min_data']) ;
          $this->filter['month_from']=mktime(0,0,0,$ts['mon'],1,$ts['year']) ;;
        }

        if (!$this->filter['month_to'])
        { $ts=getdate($data_info['max_data']) ;
          $this->filter['month_to']=mktime(0,0,0,$ts['mon'],1,$ts['year']) ;;
        }


        switch($this->filter['tg'])
          { case 0: $date_format='d.m.Y' ; $fname='c_data_d' ; break ; // по дням
            case 1: $date_format='d.m.Y H:i' ; $fname='c_data_h' ; break ; // по часам
            case 2: $date_format='d.m.Y H:i' ; $fname='c_data_m' ; break ; // по минутам

          }

        $recs=execSQL('select '.$fname.',status,dopusk,count(pkey) as cnt
                       from obj_site_mo_session
                       where c_data>='.$this->filter['month_from'].' and c_data<='.$this->filter['month_to'].' and status>0
                       group by '.$fname.',status,dopusk
                       order by '.$fname.',status,dopusk',0,1) ;
        if (_sizeof($recs)) foreach($recs as $rec) $arr[$rec[$fname]][$rec['status'].'_'.$rec['dopusk']]=$rec['cnt'] ;

        $recs_proc=execSQL_row('select '.$fname.',avg(cnt_a_proc) as avg_cnt_a_prog
                       from obj_site_mon_res
                       where c_data>='.$this->filter['month_from'].' and c_data<='.$this->filter['month_to'].'
                       group by '.$fname.'
                       order by '.$fname.'') ;

        //damp_array($arr) ;

        ?><div id="reports_itog2">
          <div id="chart-container">FusionCharts will render here</div>
           </div>


 <?
    $caption='Показатели нагрузки на систему' ;
    $yAxisName='Время' ;
        $all_cnt=array() ; $arr_data=array() ;  $arr_colors=array() ;
     //$arr_category_str[]='{"label": "'.$category_title.'" }' ;
     if (_sizeof($arr)) foreach($arr as $date=>$rec)
      {   $arr_data['1_0'][]='{"value": "'.$rec['1_0'].'" }' ;    $all_cnt['1_0']+=$rec['1_0'] ;
          $arr_data['2_0'][]='{"value": "'.$rec['2_0'].'" }' ;    $all_cnt['2_0']+=$rec['2_0'] ;
          $arr_data['3_0'][]='{"value": "'.$rec['3_0'].'" }' ;    $all_cnt['3_0']+=$rec['3_0'] ;
          $arr_data['4_0'][]='{"value": "'.$rec['4_0'].'" }' ;    $all_cnt['4_0']+=$rec['4_0'] ;
          $arr_data['1_2'][]='{"value": "'.$rec['1_2'].'" }' ;    $all_cnt['1_2']+=$rec['1_2'] ;
          $arr_data['2_2'][]='{"value": "'.$rec['2_2'].'" }' ;    $all_cnt['2_2']+=$rec['2_2'] ;
          $arr_data['3_2'][]='{"value": "'.$rec['3_2'].'" }' ;    $all_cnt['3_2']+=$rec['3_2'] ;
          $arr_data['4_2'][]='{"value": "'.$rec['4_2'].'" }' ;    $all_cnt['4_2']+=$rec['4_2'] ;
          $arr_data['1_1'][]='{"value": "'.$rec['1_1'].'" }' ;    $all_cnt['1_1']+=$rec['1_1'] ;
          $arr_data['2_1'][]='{"value": "'.$rec['2_1'].'" }' ;    $all_cnt['2_1']+=$rec['2_1'] ;
          $arr_data['3_1'][]='{"value": "'.$rec['3_1'].'" }' ;    $all_cnt['3_1']+=$rec['3_1'] ;
          $arr_data['4_1'][]='{"value": "'.$rec['4_1'].'" }' ;    $all_cnt['4_1']+=$rec['4_1'] ;

          $cnt_to_data=0 ;
          if (_sizeof($rec)) foreach($rec as $cnt)  $cnt_to_data+=$cnt ;
          $arr_data['all'][]='{"value": "'.$cnt_to_data.'" }' ;
          $all_cnt['all']+=$cnt_to_data ;


          $arr_info['proc'][]='{"value": "'.round($recs_proc[$date]).'" }' ;

      }

     //damp_array($arr_info) ;

      if (_sizeof($all_cnt)) foreach($all_cnt as $name=>$cnt) if (!$cnt) unset($arr_data[$name]) ;

        $_SESSION['ARR_status_sess']=array(0=>'',
                                           1=>'Осмотр начат',
                                           2=>'Осмотр окончен, полож.',
                                           3=>'Осмотр окончен, отриц.',
                                           4=>'Осмотр прерван'
                                           ) ;

      //#f2c500,#0075c2,#1aaf5d
      if (_sizeof($arr_data)) foreach($arr_data as $name=>$rec) switch($name)
      { case '1_0': $arr_colors[]='#f2c500' ; break ;   // Осмотр начат                                  +
        case '2_0': $arr_colors[]='#59F147' ; break ;   // Осмотр окончен, полож.                        +
        case '3_0': $arr_colors[]='#F97309' ; break ;   // Осмотр окончен, отриц.                        +
        case '4_0': $arr_colors[]='#4793F1' ; break ;   // Осмотр прерван                                +

        case '1_1': $arr_colors[]='#000000' ; break ;   // Осмотр начат              Допуск Разрешен
        case '2_1': $arr_colors[]='#1aaf5d' ; break ;   // Осмотр окончен, полож.    Допуск Разрешен     +
        case '3_1': $arr_colors[]='#000000' ; break ;   // Осмотр окончен, отриц.    Допуск Разрешен
        case '4_1': $arr_colors[]='#000000' ; break ;   // Осмотр прерван            Допуск Разрешен

        case '1_2': $arr_colors[]='#000000' ; break ;  // Осмотр начат              Допуск Запрещен
        case '2_2': $arr_colors[]='#000000' ; break ;  // Осмотр окончен, полож.    Допуск Запрещен
        case '3_2': $arr_colors[]='#ff0000' ; break ;  // Осмотр окончен, отриц.    Допуск Запрещен      +
        case '4_2': $arr_colors[]='#000000' ; break ;  // Осмотр прерван            Допуск Запрещен

        case 'all': $arr_colors[]='#1aaf5d' ; break ;  // Осмотр прерван            Допуск Запрещен
      }


      //damp_array($arr_data) ;


 ?>
     <script type="text/javascript">
      $j(document).ready(function(){
       FusionCharts.ready(function(){


    var revenueChart2 = new FusionCharts({
        type: 'mscombidy2d',
        renderAt: 'chart-container',
        width: '1000',
        height: '400',
        dataFormat: 'json',
        dataSource: {
            "chart": {
                "caption": "<?echo $caption?>",
                //"subCaption": "от <? echo date('d.m.Y',$t1).' до '.date('d.m.Y',$t2)?>",
                "xAxisname": "<?echo $yAxisName?>",
                "pYAxisName": "Кол-во сеансов МО",
                "sYAxisName": "Число процессов apache",
                "numberPrefix": "",
                "sNumberSuffix" : "",
                //"sYAxisMaxValue" : "50",

                //Cosmetics
                "paletteColors" : "<?echo implode(',',$arr_colors)?>",
                "baseFontColor" : "#333333",
                "baseFont" : "Helvetica Neue,Arial",
                "captionFontSize" : "14",
                "subcaptionFontSize" : "14",
                "subcaptionFontBold" : "0",
                "showBorder" : "0",
                "bgColor" : "#ffffff",
                "showShadow" : "0",
                "canvasBgColor" : "#ffffff",
                "canvasBorderAlpha" : "0",
                "divlineAlpha" : "100",
                "divlineColor" : "#999999",
                "divlineThickness" : "1",
                "divLineIsDashed" : "1",
                "divLineDashLen" : "1",
                "divLineGapLen" : "1",
                "usePlotGradientColor" : "0",
                "showplotborder" : "0",
                "showXAxisLine" : "1",
                "xAxisLineThickness" : "1",
                "xAxisLineColor" : "#999999",
                "showAlternateHGridColor" : "0",
                "showAlternateVGridColor" : "0",
                "legendBgAlpha" : "0",
                "legendBorderAlpha" : "0",
                "legendShadow" : "0",
                "legendItemFontSize" : "10",
                "legendItemFontColor" : "#666666"
            },
            "categories":
            [{
                "category": [ <? foreach($arr as $t=>$rec) echo '{ "label": "'.date($date_format,$t).'" },' ; ?>]
            }],
            "dataset":
            [   <?if (_sizeof($arr_data)) foreach($arr_data as $status_name=>$recs_status) if ($status_name=='all')
                { list($status,$dopusk)=explode('_',$status_name) ;
                  $status_txt=$_SESSION['ARR_status_sess'][$status] ;
                  $dopusk_txt=($dopusk)? ', допуск '.$_SESSION['ARR_status_dopusk'][$dopusk]:'' ;
                  ?>{   "seriesName": "<?echo 'Число МО'/*$status_txt.$dopusk_txt*/?>",
                        "data": [ <?echo implode(',',$recs_status)?>]
                    },
                  <?
                }
                ?>
                {
                                    "seriesName": "Число процессов",
                                    "parentYAxis": "S",
                                    "renderAs": "line",
                                    "showValues": "1",
                                    "divlineThickness" : "1",
                                    "data": [ <?echo implode(',',$arr_info['proc'])?>]
                 }
            ]
        }
    });

    revenueChart2.render();
});
      }) ;
     </script>
     <style type="text/css">#chart-container{margin:0 auto;width:1000px;}</style>
   <?







    }
}

?>
