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);
 }
 private function getSliceData($date_start, $date_end)
 {
     $looms = array();
     $ls = Loominfo::model()->findAll();
     for ($i = 0; $i < count($ls); $i++) {
         $l = $ls[$i];
         $looms[$l->frepeaterid][$l->flcardid] = array('name' => $l->floomname, 'fid' => $l->fid);
     }
     $c = new CDbCriteria();
     $c->select = 'frepeatid, flcardid, SUM(fwbrknum) as swb, SUM(fsbrknum) as ssb, SUM(fobrknum) as sob, SUM(frpmnum) as srp, SUM(ftbrknum) as stb';
     $c->condition = "ftimestamp>:date_start and ftimestamp<:date_end";
     $c->group = 'frepeatid, flcardid';
     $c->params = array(':date_start' => $date_start, ':date_end' => $date_end);
     $results = Hourdata::model()->findAll($c);
     // $sql = "select frepeatid, flcardid, SUM(fwbrknum) as swb, SUM(fsbrknum) as ssb, SUM(fobrknum) as sob, SUM(frpmnum) as srp, SUM(ftbrknum) as stb
     //         from {{hourdata}}
     //         group by frepeatid, flcardid";
     //where ftimestamp>:date_start and ftimestamp<:date_end
     //echo $sql;
     //$cmd = $db->createCommand($sql);
     //$cmd->bindValue(':date_start', $date_start);
     //$cmd->bindValue(':date_end', $date_end);
     // $results = array();
     // $results = $cmd->queryAll();
     $sums = array();
     $n = count($results);
     for ($i = 0; $i < $n; $i++) {
         $r = $results[$i];
         $card_id = $r['flcardid'];
         $repeat_id = $r['frepeatid'];
         if (!isset($sums[$repeat_id])) {
             $sums[$repeat_id] = array();
         }
         $sums[$repeat_id][$card_id] = array('swb' => $r['swb'], 'ssb' => $r['ssb'], 'sob' => $r['sob'], 'srp' => $r['srp'], 'stb' => $r['stb']);
     }
     //print_r($sums);
     $criteria = new CDbCriteria();
     $criteria->condition = "ftimestamp>:date_start and ftimestamp<:date_end and fstatus=1";
     $criteria->group = "ftimestamp";
     $criteria->order = "frepeatid,flcardid,ftimestamp";
     $criteria->params = array(':date_start' => $date_start, ':date_end' => $date_end);
     $results = Events::model()->findAll($criteria);
     //
     $time_slice = array();
     $card_info = array();
     $run_begin_time = 0;
     $run_end_time = 0;
     $last_event = -1;
     $begin_timestamp = 0;
     //$last_r = array();
     foreach ($results as $result) {
         $card_id = $result['flcardid'];
         $repeat_id = $result['frepeatid'];
         if (!isset($time_slice[$card_id])) {
             $name = "[{$repeat_id}][{$card_id}]";
             //$looms[$repeat_id][$card_id];
             if (isset($looms[$repeat_id][$card_id])) {
                 $name = $looms[$repeat_id][$card_id]['name'];
                 $loom_id = $looms[$repeat_id][$card_id]['fid'];
             }
             $time_slice[$card_id] = array('loomid' => $loom_id, 'name' => $name, 'max_ts' => 0, 'min_ts' => 0, 'max_fwbrknum' => 0, 'min_fwbrknum' => $result['fwbrknum'], 'max_ftbrknum' => 0, 'min_ftbrknum' => $result['ftbrknum'], 'max_fsbrknum' => 0, 'min_fsbrknum' => $result['fsbrknum'], 'max_fobrknum' => 0, 'min_fobrknum' => $result['fobrknum']);
             $card_info =& $time_slice[$card_id];
             $last_event = -1;
             $begin_timestamp = 0;
             if (isset($sums[$repeat_id][$card_id])) {
                 $card_info['wbrknum'] = $sums[$repeat_id][$card_id]['swb'];
                 //$slice['max_fwbrknum'] - $slice['min_fwbrknum'];
                 $card_info['sbrknum'] = $sums[$repeat_id][$card_id]['ssb'];
                 //$slice['max_fsbrknum'] - $slice['min_fsbrknum'];
                 $card_info['tbrknum'] = $sums[$repeat_id][$card_id]['stb'];
                 //$slice['max_ftbrknum'] - $slice['min_ftbrknum'];
                 $card_info['obrknum'] = $sums[$repeat_id][$card_id]['sob'];
                 //$slice['max_fobrknum'] - $slice['min_fobrknum'];
                 $card_info['srpnum'] = $sums[$repeat_id][$card_id]['srp'];
                 //$slice['max_fobrknum'] - $slice['min_fobrknum'];
             } else {
                 $card_info['wbrknum'] = 0;
                 $card_info['sbrknum'] = 0;
                 $card_info['tbrknum'] = 0;
                 $card_info['obrknum'] = 0;
                 $card_info['srpnum'] = 0;
             }
             // if ($last_r) {
             //     $l_card_id = $last_r['flcardid'];
             //     $time_slice[$l_card_id]['max_fwbrknum']= $last_r['fwbrknum'];
             //     $time_slice[$l_card_id]['max_ftbrknum']= $last_r['ftbrknum'];
             //     $time_slice[$l_card_id]['max_fsbrknum']= $last_r['fsbrknum'];
             //     $time_slice[$l_card_id]['max_fobrknum']= $last_r['fobrknum'];
             // }
         }
         $event_id = $result['feventid'];
         //$event_fields = array(8=>'wbrknum', 2=>'sbrknum', 1=>'tbrknum', 4=>'obrknum');
         if (!isset($card_info[$event_id])) {
             $card_info[$event_id] = array('time_len' => 0, 'num' => 0);
         }
         $card_info[$event_id]['num']++;
         //
         $card_info['max_ts'] = max($card_info['max_ts'], $result['ftimestamp']);
         if ($card_info['min_ts'] == 0) {
             $card_info['min_ts'] = $result['ftimestamp'];
         } else {
             $card_info['min_ts'] = min($card_info['min_ts'], $result['ftimestamp']);
         }
         //
         if ($last_event != $event_id) {
             if ($last_event != -1) {
                 $card_info[$last_event]['time_len'] += $result['ftimestamp'] - $begin_timestamp;
             }
             $begin_timestamp = $result['ftimestamp'];
             $last_event = $event_id;
         }
         //$last_r = $result;
     }
     return $time_slice;
 }