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

class Resumo extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'thunder_data':
	 * @var integer $id_data
	 * @var integer $cache_static
	 * @var integer $cache_dynamic
	 * @var integer $traffic_static
	 * @var integer $traffic_dynamic
	 * @var integer $hit_static
	 * @var integer $hit_dynamic
	 * @var integer $miss_static
	 * @var integer $miss_dynamic
	 */
	
	public $ultimaSolicitacaoA;
	public $ultimaSolicitacaoB;
	public $id_data_week;
	public $id_data_month;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'thundersnarf_data';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_data', 'required'),
			array('id_data_week, id_data_month, id_data, cache_static, cache_dynamic, traffic_static, traffic_dynamic, hit_static, hit_dynamic, miss_static, miss_dynamic', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_data_week, id_data_month, requested, ultimaSolicitacaoA, ultimaSolicitacaoB, id_data, cache_static, cache_dynamic, traffic_static, traffic_dynamic, hit_static, hit_dynamic, miss_static, miss_dynamic', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_data' => 'Data',
			'cache_static' => 'Cache Estático',
			'cache_dynamic' => 'Cache Dinâmico',
			'traffic_static' => 'Internet Estática',
			'traffic_dynamic' => 'Internet Dinâmica',
			'hit_static' => 'Hit Estático',
			'hit_dynamic' => 'Hit Dinâmico',
			'miss_static' => 'Miss Estático',
			'miss_dynamic' => 'Miss Dinâmico',
			'requested' => 'Número de Requisições',
			'id_data_month' => 'Mês',
			'id_data_week' => 'Semana',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		if (isset($_GET['pageSize'])) {
		    Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);
		    unset($_GET['pageSize']);
		}
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		if(!isset($_GET['Resumo'])){
			foreach($this->getAttributes() as $key=>$value){
				$this->setAttribute($key,'');	
			}
		}else{
			$dataA = date_parse($_GET['Resumo']['ultimaSolicitacaoA']);
			$dataB = date_parse($_GET['Resumo']['ultimaSolicitacaoB']);
			print_r($dataA);print_r($dataB);
			if(strlen($dataA['month'])==1) $dataA['month'] = '0'.$dataA['month'];
			if(strlen($dataA['day'])==1) $dataA['day'] = '0'.$dataA['day'];
			if(strlen($dataB['month'])==1) $dataB['month'] = '0'.$dataB['month'];
			if(strlen($dataB['day'])==1) $dataB['day'] = '0'.$dataB['day'];
			if(checkdate($dataA['month'], $dataA['day'], $dataA['year']) AND checkdate($dataB['month'], $dataB['day'], $dataB['year']) ){
				$criteria->condition = 'id_data BETWEEN '.$dataA['year'].$dataA['month'].$dataA['day'].' AND '.$dataB['year'].$dataB['month'].$dataB['day'].'';
			}else if(checkdate($dataA['month'], $dataA['day'], $dataA['year'])){
				$criteria->condition = 'id_data >= '.$dataA['year'].$dataA['month'].$dataA['day'].'';				
			}else if(checkdate($dataB['month'], $dataB['day'], $dataB['year'])){
				$criteria->condition = 'id_data <= '.$dataB['year'].$dataB['month'].$dataB['day'].'';				
			}
		}

		$criteria->compare('requested',$this->requested);

		$criteria->compare('cache_static',$this->cache_static);

		$criteria->compare('cache_dynamic',$this->cache_dynamic);

		$criteria->compare('traffic_static',$this->traffic_static);

		$criteria->compare('traffic_dynamic',$this->traffic_dynamic);

		$criteria->compare('hit_static',$this->hit_static);

		$criteria->compare('hit_dynamic',$this->hit_dynamic);

		$criteria->compare('miss_static',$this->miss_static);

		$criteria->compare('miss_dynamic',$this->miss_dynamic);

		$sort = new CSort();
		if(isset($_GET["tipoTrafego"]) and $_GET["tipoTrafego"]=="E"){
			$sort->defaultOrder = 'cache_static DESC';
			if(!isset($_GET['sort'])) $_GET['sort'] = 'cache_static.desc';
		}else if(isset($_GET["tipoTrafego"]) and $_GET["tipoTrafego"]=="D"){
			$sort->defaultOrder = 'cache_dynamic DESC';
			if(!isset($_GET['sort'])) $_GET['sort'] = 'cache_dynamic.desc';
		}else{
			if(isset($_GET['periodo']) AND $_GET['periodo']=="S"){//semanal
				$sort->defaultOrder = 'id_data_week DESC';
				if(!isset($_GET['sort'])) $_GET['sort'] = 'id_data_week.desc';
			}else if(isset($_GET['periodo']) AND $_GET['periodo']=="M"){//mensal
				$sort->defaultOrder = 'id_data_month DESC';
				if(!isset($_GET['sort'])) $_GET['sort'] = 'id_data_month.desc';
			}else{
				//$sort->defaultOrder = 'id_data_month DESC';
				//if(!isset($_GET['sort'])) $_GET['sort'] = 'id_data_month.desc';
				$sort->defaultOrder = 'id_data DESC';
				if(!isset($_GET['sort'])) $_GET['sort'] = 'id_data.desc';				
			}
		}
		
		if(isset($_GET['periodo']) AND $_GET['periodo']=="S"){//semanal
			$criteria->select="(extract(week from to_date(to_char(id_data,'99999999'),'YYYYMMDD')) || '/' || extract(year from to_date(to_char(id_data,'99999999'),'YYYYMMDD'))) as id_data_week, SUM(cache_static) as cache_static, SUM(cache_dynamic) as cache_dynamic, SUM(traffic_static) as traffic_static, SUM(traffic_dynamic) as traffic_dynamic, SUM(hit_static) as hit_static, SUM(hit_dynamic) as hit_dynamic, SUM(miss_static) as miss_static, SUM(miss_dynamic) as miss_dynamic, SUM(requested) as requested";
			$criteria->group='id_data_week';
			$sort->attributes['id_data_week'] = array(
		      'asc'=>'id_data_week',
		      'desc'=>'id_data_week desc');
		}else if(isset($_GET['periodo']) AND $_GET['periodo']=="M"){//mensal
			$criteria->select="(extract(month from to_date(to_char(id_data,'99999999'),'YYYYMMDD')) || '/' || extract(year from to_date(to_char(id_data,'99999999'),'YYYYMMDD'))) as id_data_month, SUM(cache_static) as cache_static, SUM(cache_dynamic) as cache_dynamic, SUM(traffic_static) as traffic_static, SUM(traffic_dynamic) as traffic_dynamic, SUM(hit_static) as hit_static, SUM(hit_dynamic) as hit_dynamic, SUM(miss_static) as miss_static, SUM(miss_dynamic) as miss_dynamic, SUM(requested) as requested";
			$criteria->group='id_data_month';
			$sort->attributes['id_data_month'] = array(
		      'asc'=>'id_data_month',
		      'desc'=>'id_data_month desc');
		}

		return new CActiveDataProvider('Resumo', array(
			'criteria'=>$criteria,
    		'sort'=>$sort,
			'pagination'=>array(
		      'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
		    ),
		));
	}
}