<?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
 */

$array_discos = array();
$xml_thunder = Yii::app()->user->getState('xml_thunder',array("vazio"));
if((int)$xml_thunder["THUNDER_CACHEDIRS"]["@attributes"]["quantidade"]>0){
	if((int)$xml_thunder["THUNDER_CACHEDIRS"]["@attributes"]["quantidade"]==1){
		$array_disco = array();
		exec('df '.$xml_thunder["THUNDER_CACHEDIRS"]["CACHEDIR"]["DIR"], $array_disco);
		if(isset($array_disco[1])){
			$array_discos[] = $array_disco[1];
		}
	}else{
		foreach($xml_thunder["THUNDER_CACHEDIRS"]["CACHEDIR"] as $key=>$value){
			$array_disco = array();
			exec('df '.$value["DIR"], $array_disco);
			if(isset($array_disco[1])){
				$array_discos[] = $array_disco[1];
			}
		}
	}
}else{
	echo "Não foi possível mapear unidade de disco, reveja a configuração do Painel de controle no Site do ThunderCache";
}
if(count($array_discos)==0){
	echo "Não foi possível mapear unidade de disco do Thunder, reveja a configuração do Painel de controle no Site do ThunderCache";
	$array_discos = array('xxx xxx xxx xxx xxx /usr');
}
foreach($array_discos as $key=>$value){
	$value = preg_replace('/\s\s+/', ' ', $value);
	$array_cache_dir = explode(" ", $value);
	//if($cache_dir == $array_cache_dir[5] OR substr($array_cache_dir[5]."/",0,strlen($cache_dir."/")) == $cache_dir."/"){
		?>
 	<table>
		<tr>
			<td style="text-align:center;">
		<?php
		$totalDiskSpace = round(disk_total_space($array_cache_dir[5]),2);
		$totalDiskFree = round(disk_free_space($array_cache_dir[5]),2);
		if($totalDiskFree<0) $totalDiskFree=0;
                $retornoTipoFileSystem = "";
                exec('mount | grep "'.$array_cache_dir[5].' ("', $retornoTipoFileSystem);
                $tipoFileSystem = explode(" ", $retornoTipoFileSystem[0]);
                if(isset($tipoFileSystem[3]))
                    $tipoFileSystem = substr($tipoFileSystem[3],1,-1);
                else
                    $tipoFileSystem = "ufs";
		if($tipoFileSystem=="ufs")
                    $totalDiskUsed = ($totalDiskSpace)*0.92-$totalDiskFree;
                else
                    $totalDiskUsed = $totalDiskSpace-$totalDiskFree;
$format_func = <<<EOD
js:function(label, series){
    return '<div style="font-size:12px;text-align:center;padding:2px;color:white;">'+label+'<br/>'+Math.round(series.percent)+'%</div>';}
EOD;

$format_label = <<<EOD
js:function(label, series){
    return '<div style="background-color:#000;opacity:0.5;font-size:8pt;text-align:center;padding:2px;color:white;">'+label+'<br/>'+Math.round(series.percent)+'%</div>';}
EOD;
		$this->widget('application.extensions.EFlot.EFlotGraphWidget',
		    array(
		    	'idAjax'=>"DiscoCache",
		        'data'=>array(
		            array('label'=>'Usado', 'data'=>$totalDiskUsed, ),
		            array('label'=>'Livre', 'data'=>$totalDiskFree, 'color'=>'#AFD8F8'),
		        ),
		        'options'=>array(
		            'series'=> array('pie'=>array(
		                'show'=>true,
		                'radius'=> 3/4,
		                'tilt'=> 0.7,
		                'stroke'=>array('width'=>'5'),
		                'formatter'=> $format_func,
			            'label'=>array(
			                'show'=>true,
			                'radius'=> 3/4,
			                'formatter'=>$format_label,
			            	),
		                ),
		            ),
		            'combine'=>array('color'=>'#999', 'threshold'=>0.9),
		            'legend'=> array('show'=>false),
		        ),
		        'htmlOptions'=>array('style'=>'width:250px;height:250px;')
		    )
		);

                echo $array_cache_dir[5];
                echo '<br><span style="font-size: 8pt">FileSystem '.strtoupper($tipoFileSystem).'</span>';
		?>
			</td>
			<td style='width:70%;vertical-align:top'>
				<table style="border:1px solid #c1c1c1">
					<tr>
						<td style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1' >TAMANHO DISCO</td>
						<td style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1' >USADO</td>
						<td style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1' >LIVRE</td>
					</tr>
						<td style="border:1px solid #c1c1c1;text-align:center;" ><?php echo $this->sizeFormat($totalDiskSpace);?></td>
						<td style="border:1px solid #c1c1c1;text-align:center;" ><?php echo $this->sizeFormat($totalDiskUsed);?></td>
						<td style="border:1px solid #c1c1c1;text-align:center;" ><?php echo $this->sizeFormat($totalDiskFree);?></td>
					</tr>
				</table>
				<br><br>
				<span style="padding: 2px; background-color: rgb(0, 0, 0); opacity: 0.5; font-size: 8pt; text-align: center; color: white;">&nbsp;&nbsp;<?php echo $array_cache_dir[5];?>&nbsp;&nbsp;</span>
			</td>
		</tr>
	</table>
		<?php
	//}
}