public function actionSummary()
 {
     $a = array();
     $rows = Status::model()->findAll();
     $run_num = 0;
     $fault_num = 0;
     $stop_num = 0;
     $comp_num = 0;
     $total_num = count($rows);
     for ($i = 0; $i < $total_num; $i++) {
         switch ($rows[$i]['frstatus']) {
             case 1:
             case 2:
             case 4:
             case 8:
                 $fault_num++;
                 break;
             case 32:
                 $run_num++;
                 break;
             case 201:
                 $comp_num++;
                 break;
             default:
                 $stop_num++;
         }
     }
     $a['run_num'] = $run_num;
     $a['fault_num'] = $fault_num;
     $a['stop_num'] = $stop_num;
     $a['comp_num'] = $comp_num;
     $total_num = Loominfo::model()->count();
     $a['total_num'] = $total_num;
     //effect
     $day_start = strtotime(date('Y-m-d'));
     $week_start = strtotime("-7 days");
     $week_start = strtotime(date("Y-m-d", $week_start));
     $month_start = strtotime("-1 month");
     $month_start = strtotime(date("Y-m-d", $month_start));
     $c = array('select' => 'AVG(fpowersec) AS fpowersec, AVG(frunsec) AS frunsec, AVG(frpmnum) AS frpmnum, SUM(frpmnum) AS frlength', 'condition' => "fhourid>:start");
     $c['params'] = array(':start' => $day_start);
     $day = Hourdata::model()->find($c);
     $c['params'] = array(':start' => $week_start);
     $week = Hourdata::model()->find($c);
     $c['params'] = array(':start' => $month_start);
     $month = Hourdata::model()->find($c);
     $a['effect_day'] = $day['fpowersec'] > 0 ? round($day['frunsec'] / $day['fpowersec'] * 100, 2) . '%' : '-';
     $a['effect_week'] = $week['fpowersec'] > 0 ? round($week['frunsec'] / $week['fpowersec'] * 100, 2) . '%' : '-';
     $a['effect_month'] = $month['fpowersec'] > 0 ? round($month['frunsec'] / $month['fpowersec'] * 100, 2) . '%' : '-';
     $a['rpm_day'] = $day['frpmnum'] == null ? '-' : $day['frpmnum'];
     $a['rpm_week'] = $week['frpmnum'] == null ? '-' : $week['frpmnum'];
     $a['rpm_month'] = $month['frpmnum'] == null ? '-' : $month['frpmnum'];
     $rollinfo = Rollinfo::model()->find();
     $fdensity = $rollinfo->fweft->fdensity;
     $a['output_day'] = $day['frlength'] > 0 ? round($day['frlength'] / $fdensity, 2) : '-';
     $a['output_week'] = $week['frlength'] > 0 ? round($week['frlength'] / $fdensity, 2) : '-';
     $a['output_month'] = $month['frlength'] > 0 ? round($month['frlength'] / $fdensity, 2) : '-';
     echo CJSON::encode($a);
 }
 public function actionIndex()
 {
     $viewName = 'index';
     $infoRows = array();
     /*$num = 1;
       for($i = 1; $i < 10; $i++) {
           $row = array();
           for($j = 1; $j < 20; $j++) {
               $row[] = array(
                   'sid'   => sprintf('A%03d', $num++),
                   'st'    =>  $j == 3 ? 'stop' : ($j == 7 ? 'low' : 'run')
               );
           }
           $infoRows[] = $row;
       }*/
     $rows = Loominfo::model()->findAll();
     for ($i = 0; $i < count($rows); $i++) {
         $row = $rows[$i];
         $fid = $row['fid'];
         $c = array('condition' => "frefloomid='ALL' or frefloomid LIKE '%,{$fid},%'", 'order' => 't.fid');
         $roll = Rollinfo::model()->find($c);
         $r = $roll->getAttributes();
         $r['fproduct'] = $roll->fproduct->getAttributes();
         $r['fchaine'] = $roll->fchaine->getAttributes();
         $r['fweft'] = $roll->fweft->getAttributes();
         $a = $row->getAttributes();
         $a['rollinfo'] = $r;
         //effect
         $day_start = strtotime(date('Y-m-d'));
         $week_start = strtotime("-7 days");
         $week_start = strtotime(date("Y-m-d", $week_start));
         $month_start = strtotime("-1 month");
         $month_start = strtotime(date("Y-m-d", $month_start));
         $c = array('select' => 'AVG(fpowersec) AS fpowersec, AVG(frunsec) AS frunsec', 'condition' => "frepeatid=:frepeatid AND flcardid=:flcardid AND fhourid>:start ");
         $c['params'] = array(':start' => $week_start, ':frepeatid' => $row['frepeaterid'], ':flcardid' => $row['flcardid']);
         $week = Hourdata::model()->find($c);
         $c['params'] = array(':start' => $month_start, ':frepeatid' => $row['frepeaterid'], ':flcardid' => $row['flcardid']);
         $month = Hourdata::model()->find($c);
         $a['effect_week'] = $week['fpowersec'] > 0 ? round($week['frunsec'] / $week['fpowersec'] * 100, 2) . '%' : '-';
         $a['effect_month'] = $month['fpowersec'] > 0 ? round($month['frunsec'] / $month['fpowersec'] * 100, 2) . '%' : '-';
         $infoRows[] = $a;
     }
     $this->view->media = array('scripts' => array('loomstatus/index.js'), 'styles' => array('loomstatus/index.css'));
     $this->view->looms = array('info' => $infoRows, 'json' => CJSON::encode($infoRows));
     $this->render($viewName);
 }
 public function ActionProduct()
 {
     if (isset($_REQUEST['start_time']) && $_REQUEST['start_time']) {
         $date_start = strtotime($_REQUEST['start_time']);
     } else {
         $prev = time() - 86400;
         $date_start = mktime(0, 0, 0, date("n", $prev), date("j", $prev), date("Y", $prev));
         $_REQUEST['start_time'] = date('Y-m-d H:i', $date_start);
     }
     if (isset($_REQUEST['end_time']) && $_REQUEST['end_time']) {
         $date_end = strtotime($_REQUEST['end_time']);
     } else {
         $date_end = time();
         $_REQUEST['end_time'] = date('Y-m-d H:i', $date_end);
     }
     $time_slice = $this->getSliceData($date_start, $date_end);
     $event_fields = array(8 => 'wbrknum', 2 => 'sbrknum', 1 => 'tbrknum');
     $skip_keys = array('name', 'max_ts', 'min_ts');
     $stop_events = array(1, 4, 8);
     $other_events = array(2);
     $n = count($time_slice);
     $removes = array();
     foreach ($time_slice as $k => $slice) {
         //$slice = $time_slice[$i];
         $fid = $slice['loomid'];
         $rollinfo = Rollinfo::model()->findByAttributes(array(), "(frefloomid = 'ALL' OR frefloomid LIKE '%,{$fid},%')");
         if (!$rollinfo) {
             $removes[] = $k;
             continue;
         }
         $fdensity = $rollinfo->fweft->fdensity;
         $time_slice[$k]['output'] = $slice['srpnum'] / $fdensity;
         $stop_time = 0;
         $other_time = 0;
         $all_time = 0;
         foreach ($slice as $key => $value) {
             if (is_numeric($key)) {
                 //!in_array($key, $skip_keys)
                 if (in_array($key, $stop_events)) {
                     $stop_time += $value['time_len'];
                 }
                 if (in_array($key, $other_events)) {
                     $other_time += $value['time_len'];
                 }
                 $all_time += $value['time_len'];
                 if (isset($event_fields[$key])) {
                 }
             }
         }
         //print_r($slice);
         $time_slice[$k]['stop_times'] = $time_slice[$k]['wbrknum'] + $time_slice[$k]['sbrknum'] + $time_slice[$k]['tbrknum'] + $time_slice[$k]['obrknum'];
         $time_slice[$k]['all_time0'] = $slice['max_ts'] - $slice['min_ts'];
         $time_slice[$k]['all_time'] = $all_time;
         $time_slice[$k]['stop_time'] = $stop_time;
         $time_slice[$k]['other_time'] = $other_time;
         if ($all_time > 0) {
             $time_slice[$k]['efficiency'] = ($all_time - $stop_time) / $all_time;
         }
     }
     $n = count($removes);
     for ($i = 0; $i < $n; $i++) {
         unset($time_slice[$removes[$i]]);
     }
     //print_r($time_slice);
     //TODO
     $viewName = 'product';
     $this->render($viewName, array('time_slice' => $time_slice));
 }
 function getLastRollinfo()
 {
     $criteria = new CDbCriteria();
     $criteria->condition = 'frefloomid LIKE %,' . $this->fid . ',%';
     $criteria->order = 'fid DESC';
     $criteria->limit = 1;
     $ri = Rollinfo::model()->find($criteria);
     return $ri;
 }
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  */
 public function loadModel()
 {
     if ($this->_model === null) {
         if (isset($_GET['id'])) {
             $this->_model = Rollinfo::model()->findbyPk($_GET['id']);
         }
         if ($this->_model === null) {
             throw new CHttpException(404, 'The requested page does not exist.');
         }
     }
     return $this->_model;
 }