<?php
/**
 * ThunderSnarf
 *
 * @author Ricardo H.F. Sette P. <ricardohfsp@gmail.com>
 * @link http://www.freebsdbrasil.com.br/
 * @copyright Copyright &copy; 2010 - FreeBSD Brasil LTDA
 * @license licença GNU/GPL v2.0 http://www.gnu.org/licenses/gpl-2.0.html
 */
?>
<?php
$this->breadcrumbs=array(
	'Arquivos'=>array('index'),
	'Informações',
);
function formatBytes($bytes, $precision = 2) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');
  
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
  
    $bytes /= pow(1024, $pow);
  
    return round($bytes, $precision) . '' . $units[$pow];
}
function formatFile($file) {
	if(strlen($file)<25){
		return $file;
	}else{
		$totalLen = strlen($file);
		return "<a href='#' alt='".$file."' title='".$file."'>".substr($file,0,12).".....".substr($file,-12)."</a>"; 
	}
}

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$.fn.yiiGridView.update('file-grid', {
		data: $(this).serialize()
	});
	return false;
});
");
?>

<h1>Informações por Arquivo</h1>
<?php echo CHtml::link('Filtro','#',array('class'=>'search-button')); ?>
<?php echo CHtml::dropDownList(
				'pageSize',
				Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
				array(5=>'5 por página',20=>'20 por página',50=>'50 por página',100=>'100 por página'),
	            array('class'=>'change-pagesize','style'=>'margin-left:10px')
			); 
?>
<?php echo CHtml::dropDownList(
				'tipoTrafego',
				isset($_GET['tipoTrafego'])?$_GET['tipoTrafego']:'',
				array('E'=>'Somente Estático','D'=>'Somente Dinâmico'),
				array('empty'=>'Sem dados de Estático ou Dinâmico','class'=>'change-tipoTrafego')
			);
?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
	'model'=>$model,
)); ?>
</div><!-- search-form -->

<?php
if(isset($_GET['eficienciaDynamic']) AND $_GET['eficienciaDynamic']=='2')
	$valorEficienciaDynamic = '$data->hit_dynamic>0 ? \'<b>\'.number_format(((100*$data->hit_dynamic)/($data->hit_dynamic + $data->miss_dynamic)),2,",","").\'%</b>\' : \'<b>0%</b>\'';
else 
	$valorEficienciaDynamic = '$data->cache_dynamic>0 ? \'<b>\'.number_format(((100*$data->cache_dynamic)/($data->cache_dynamic + $data->traffic_dynamic)),2,",","").\'%</b>\' : \'<b>0%</b>\'';

if(isset($_GET['eficienciaStatic']) AND $_GET['eficienciaStatic']=='2')
	$valorEficienciaStatic = '$data->hit_static>0 ? \'<b>\'.number_format(((100*$data->hit_static)/($data->hit_static + $data->miss_static)),2,",","").\'%</b>\' : \'<b>0%</b>\'';
else 
	$valorEficienciaStatic = '$data->cache_static>0 ? \'<b>\'.number_format(((100*$data->cache_static)/($data->cache_static + $data->traffic_static)),2,",","").\'%</b>\' : \'<b>0%</b>\'';

$colunasCGridView = array(	
	array(
		'name'=>'id',
		'value'=>'CHtml::link(CHtml::encode($data->id), array("view", "id"=>$data->id))',
		'type'=>'html'
	),
	array(
		'name'=>'domínio',
		'value'=>'$data->domain->domain',
	),
	array(
		'name'=>'file',
		'value'=>'formatFile($data->file)',
		'type'=>'html'
	),
	array(
        'name'=>'size',
        'value'=>'formatBytes($data->size)',
    ),
	array(
    	'header'=>CHtml::label("Nro Reqs",false,array("title"=>"Número de Requisições")),
    	'name'=>'requested',
    	'value'=>'$data->requested',
    ),
    array(
        'name'=>'last_requested',
        'value'=>'date(\'d-m-Y H:i:s\',$data->last_requested)',
    ),
	array(
        'name'=>'cache_static',
        'value'=>'formatBytes($data->cache_static)',
    ),
	array(
        'name'=>'traffic_static',
        'value'=>'formatBytes($data->traffic_static)',
    ),
	array(
        'name'=>'Total',
        'value'=>'formatBytes($data->cache_static + $data->traffic_static)',
    ),
	'hit_static',
	'miss_static',
	array(
        'name'=>'Total',
        'value'=>'$data->hit_static + $data->miss_static',
    ),	
	array(
        'header'=>CHtml::label('Eficiência E',false,array("title"=>"Eficiencia Estática")).CHtml::dropDownList(
						"eficienciaStatic",
						isset($_GET['eficienciaStatic']) ? $_GET['eficienciaStatic'] : '1',
						array("1"=>"Cache","2"=>"Hit"),
			            array("class"=>"change-eficienciaStatic")
					),
        'value'=>$valorEficienciaStatic,
		'type'=>'html'
    ),
	array(
        'name'=>'cache_dynamic',
        'value'=>'formatBytes($data->cache_dynamic)',
    ),
	array(
        'name'=>'traffic_dynamic',
        'value'=>'formatBytes($data->traffic_dynamic)',
    ),
	array(
        'name'=>'Total',
        'value'=>'formatBytes($data->cache_dynamic + $data->traffic_dynamic)',
    ),
	'hit_dynamic',
	'miss_dynamic',		
	array(
        'name'=>'Total',
        'value'=>'$data->hit_dynamic + $data->miss_dynamic',
    ),	
	array(
        'header'=>CHtml::label('Eficiência D',false,array("title"=>"Eficiencia Dinâmica")).CHtml::dropDownList(
						"eficienciaDynamic",
						isset($_GET['eficienciaDynamic']) ? $_GET['eficienciaDynamic'] : '1',
						array("1"=>"Cache","2"=>"Hit"),
			            array("class"=>"change-eficienciaDynamic")
					),
        'value'=>$valorEficienciaDynamic,
		'type'=>'html'
    ),
);
if(isset($_GET["tipoTrafego"]) and $_GET["tipoTrafego"]=="E"){
	unset($colunasCGridView[5]);
	unset($colunasCGridView[13]);
	unset($colunasCGridView[14]);
	unset($colunasCGridView[15]);
	unset($colunasCGridView[16]);
	unset($colunasCGridView[17]);
	unset($colunasCGridView[18]);
	unset($colunasCGridView[19]);
}else if(isset($_GET["tipoTrafego"]) and $_GET["tipoTrafego"]=="D"){
	unset($colunasCGridView[5]);
	unset($colunasCGridView[6]);
	unset($colunasCGridView[7]);
	unset($colunasCGridView[8]);
	unset($colunasCGridView[9]);
	unset($colunasCGridView[10]);
	unset($colunasCGridView[11]);
	unset($colunasCGridView[12]);
}else{
	unset($colunasCGridView[6]);
	unset($colunasCGridView[7]);
	unset($colunasCGridView[8]);
	unset($colunasCGridView[9]);
	unset($colunasCGridView[10]);
	unset($colunasCGridView[11]);
	unset($colunasCGridView[12]);
	unset($colunasCGridView[13]);	
	unset($colunasCGridView[14]);	
	unset($colunasCGridView[15]);	
	unset($colunasCGridView[16]);	
	unset($colunasCGridView[17]);	
	unset($colunasCGridView[18]);	
	unset($colunasCGridView[19]);	
}
$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'file-grid',
	'dataProvider'=>$model->search(),
	//'filter'=>$model,
	'summaryText'=>'Exibindo {start} até {end} de {count} resultado(s).',
	'columns'=>$colunasCGridView,
)); ?>
<?php Yii::app()->clientScript->registerScript('initPageSize',<<<EOD
    $('.change-pagesize').bind('change', function() {
        $.fn.yiiGridView.update('file-grid',{ data:{ pageSize: $(this).val() }});
    });
EOD
,CClientScript::POS_READY); ?>
<?php Yii::app()->clientScript->registerScript('tipoTrafego',<<<EOD
    $('.change-tipoTrafego').bind('change', function() {
        $.fn.yiiGridView.update('file-grid',{ data:{ tipoTrafego: $(this).val() }});
    });
EOD
,CClientScript::POS_READY); ?>
<?php Yii::app()->clientScript->registerScript('eficienciaDynamic',<<<EOD
    $('.change-eficienciaDynamic').live('change', function() {
        $.fn.yiiGridView.update('file-grid',{ data:{ eficienciaDynamic: $(this).val() }});
    });
EOD
,CClientScript::POS_READY); ?>
<?php Yii::app()->clientScript->registerScript('eficienciaStatic',<<<EOD
    $('.change-eficienciaStatic').live('change', function() {
        $.fn.yiiGridView.update('file-grid',{ data:{ eficienciaStatic: $(this).val() }});
    });
EOD
,CClientScript::POS_READY); ?>

