public static function getHistory($schedule_id, $page_size = 15)
 {
     if ($page_size) {
         $sql = "SELECT COUNT(t1.schedule_processed_id)\n                    FROM `" . ScheduleReportProcessed::model()->tableName() . "` t1\n                    WHERE `t1`.`schedule_id` = '" . $schedule_id . "'";
         $total = Yii::app()->db->createCommand($sql)->queryScalar();
     }
     if ($total || !$page_size) {
         if ($page_size) {
             $pages = new CPagination($total);
             $pages->pageSize = $page_size;
         }
         $sql = "SELECT \n\t\t\t\t\t\tt1.*, t2.message, t2.measuring_timestamp, t3.report_type \n                    FROM \n\t\t\t\t\t\t`" . ScheduleReportProcessed::model()->tableName() . "` t1\n                    LEFT JOIN \n\t\t\t\t\t\t`" . ListenerLog::model()->tableName() . "` t2 ON t2.log_id = t1.listener_log_id\n                    LEFT JOIN \n\t\t\t\t\t\t`" . ScheduleReport::model()->tableName() . "` t3 ON t3.schedule_id = t1.schedule_id\n                    WHERE \n\t\t\t\t\t\t`t1`.`schedule_id` = '" . $schedule_id . "'\n                    ORDER BY \n\t\t\t\t\t\t`t1`.`created` DESC";
         if ($page_size) {
             $sql .= " LIMIT " . $pages->currentPage * $pages->pageSize . ", " . $pages->pageSize;
         }
         $res = Yii::app()->db->createCommand($sql)->queryAll();
         if ($res) {
             $files_path = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "files" . DIRECTORY_SEPARATOR . "schedule_reports";
             foreach ($res as $key => $value) {
                 if ($value['serialized_report_errors']) {
                     $res[$key]['report_errors'] = unserialize($value['serialized_report_errors']);
                 }
                 if ($value['serialized_report_explanations']) {
                     $res[$key]['report_explanations'] = unserialize($value['serialized_report_explanations']);
                 }
                 if (in_array($value['report_type'], array('synop', 'metar', 'speci')) && file_exists($files_path . DIRECTORY_SEPARATOR . $value['schedule_processed_id'])) {
                     $res[$key]['report_string_initial'] = file_get_contents($files_path . DIRECTORY_SEPARATOR . $value['schedule_processed_id']);
                 }
                 if ($value['listener_log_ids']) {
                     $sql = "SELECT t2.log_id, t2.message, t2.measuring_timestamp \n                                FROM  `" . ListenerLog::model()->tableName() . "` t2\n                                WHERE t2.log_id IN (" . $value['listener_log_ids'] . ")\n                                ORDER BY `t2`.`measuring_timestamp` DESC";
                     $res[$key]['logs'] = Yii::app()->db->createCommand($sql)->queryAll();
                 }
             }
         }
     }
     return array('list' => $res, 'pages' => $pages);
 }
    public function run($args)
    {
        if (!Yii::app()->mutex->lock('ScheduleCommand', 3600)) {
            Yii::app()->end();
        }
        $synchronization = new Synchronization();
        if (!$synchronization->isMaster() and $synchronization->isProcessed()) {
            return;
        }
        $generationTime = time();
        $proper_periods = ScheduleCommand::getProperPeriods($generationTime);
        if (count($proper_periods) === 0) {
            self::$_logger->log(__METHOD__ . ' Exiting. No proper periods found.' . "\n\n");
            //            Yii::app()->mutex->unlock();
            //			Yii::app()->end();
        }
        $criteria = new CDbCriteria();
        $criteria->select = array('schedule_id', 'report_type', 'station_id', 'report_format', 'period', 'last_scheduled_run_planned', 'last_scheduled_run_fact', '(`last_scheduled_run_planned` + INTERVAL `period` MINUTE) AS nextScheduleTime', 'UNIX_TIMESTAMP(`last_scheduled_run_planned` + INTERVAL `period` MINUTE) AS nextScheduleUnixTime');
        $criteria->with = array('station');
        $criteria->compare('period', '>0');
        $criteria->compare('period', $proper_periods);
        $criteria->addCondition('(UNIX_TIMESTAMP(`last_scheduled_run_planned` + INTERVAL `period` MINUTE) <= UNIX_TIMESTAMP()
										OR
									`last_scheduled_run_planned` = "0000-00-00 00:00:00")');
        /** @var array|ScheduleReport[] $scheduledReports */
        $scheduledReports = ScheduleReport::model()->findAll($criteria);
        if (count($scheduledReports) === 0) {
            self::$_logger->log(__METHOD__ . ' Exiting. No proper reports found.' . "\n\n");
            Yii::app()->mutex->unlock();
            Yii::app()->end();
        }
        self::$_logger->log(__METHOD__ . ' New scheduled reports', array('report count' => count($scheduledReports)));
        $reportProcesses = array();
        foreach ($scheduledReports as $scheduledReport) {
            self::$_logger->log("\n");
            self::$_logger->log(__METHOD__ . ' Check scheduled report', array('schedule_id' => $scheduledReport->schedule_id));
            $check_period = ScheduleCommand::getCheckPeriod($generationTime, $scheduledReport->period);
            if ($scheduledReport->report_type === 'data_export') {
                self::$_logger->log(__METHOD__ . ' scheduledReport->report_type  = data_export');
                // add record about schedule running to process afterwards
                for ($i = 0; $i < count($scheduledReport->station); $i++) {
                    $schedule_report_process = new ScheduleReportProcessed();
                    $schedule_report_process->sr_to_s_id = $scheduledReport->station[$i]->id;
                    $schedule_report_process->check_period_start = $check_period[3];
                    $schedule_report_process->check_period_end = $check_period[4];
                    $schedule_report_process->save();
                    $scheduledReport->last_scheduled_run_fact = $check_period[1];
                    $scheduledReport->last_scheduled_run_planned = $check_period[2];
                    if ($scheduledReport->validate()) {
                        $scheduledReport->save(false);
                    } else {
                        self::$_logger->log(__METHOD__ . ' Schedule report not saved ', array('schedule_error' => $scheduledReport->getErrors()));
                    }
                    $reportProcesses[$scheduledReport->station[$i]->id] = array('schedule_id' => $scheduledReport->schedule_id, 'schedule_processed_id' => $schedule_report_process->schedule_processed_id, 'schedule_info' => $scheduledReport, 'check_period_start' => $check_period[3], 'check_period_end' => $check_period[4]);
                }
            } else {
                $logRecords = array();
                foreach ($scheduledReport->station as $station) {
                    $criteria = new CDbCriteria();
                    $criteria->compare('station_id', $station->station_id);
                    $criteria->compare('failed', '0');
                    $criteria->compare('measuring_timestamp', '>' . $check_period[0]);
                    $criteria->compare('measuring_timestamp', '<=' . $check_period[1]);
                    $criteria->order = 'measuring_timestamp desc, log_id desc';
                    $criteria->limit = '1';
                    $logRecord = ListenerLog::model()->find($criteria);
                    if (!is_null($logRecord)) {
                        $logRecords[] = $logRecord;
                    }
                }
                // add record about schedule running to process afterwards (only in case system received base message)
                if (count($logRecords) > 0) {
                    for ($i = 0; $i < count($scheduledReport->station); $i++) {
                        $schedule_report_process = new ScheduleReportProcessed();
                        $continue = false;
                        foreach ($logRecords as $logRecord) {
                            if ($logRecord->station_id == $scheduledReport->station[$i]->station_id) {
                                $schedule_report_process->listener_log_id = $logRecord->log_id;
                                $listener_log_id = $logRecord->log_id;
                                $continue = false;
                                break;
                            } else {
                                $continue = true;
                            }
                        }
                        if ($continue) {
                            continue;
                        }
                        $schedule_report_process->sr_to_s_id = $scheduledReport->station[$i]->id;
                        $schedule_report_process->check_period_start = $check_period[3];
                        $schedule_report_process->check_period_end = $check_period[4];
                        $schedule_report_process->save();
                        $scheduledReport->last_scheduled_run_fact = $check_period[1];
                        $scheduledReport->last_scheduled_run_planned = $check_period[2];
                        if ($scheduledReport->validate()) {
                            $scheduledReport->save(false);
                        } else {
                            self::$_logger->log(__METHOD__ . ' Schedule report not saved ', array('schedule_error' => $scheduledReport->getErrors()));
                        }
                        $reportProcesses[$scheduledReport->station[$i]->id] = array('log_id' => $listener_log_id, 'schedule_id' => $scheduledReport->schedule_id, 'schedule_processed_id' => $schedule_report_process->schedule_processed_id, 'schedule_info' => $scheduledReport, 'check_period_start' => $check_period[3], 'check_period_end' => $check_period[4]);
                    }
                }
            }
        }
        if (count($reportProcesses) > 0) {
            $total = count($reportProcesses);
            $i = 1;
            foreach ($reportProcesses as $reportProcess) {
                $weatherReport = null;
                switch (strtolower($reportProcess['schedule_info']->report_type)) {
                    case 'synop':
                        $weatherReport = WeatherReport::create('Synop', self::$_logger);
                        break;
                    case 'bufr':
                        $weatherReport = WeatherReport::create('Bufr', self::$_logger);
                        break;
                    case 'metar':
                        $weatherReport = WeatherReport::create('Metar', self::$_logger);
                        break;
                    case 'odss':
                        $weatherReport = WeatherReport::create('ODSS', self::$_logger);
                        break;
                    default:
                        $weatherReport = WeatherReport::create('Export', self::$_logger);
                        break;
                }
                try {
                    $weatherReport->load($reportProcess['schedule_processed_id']);
                    $weatherReport->generate();
                    $weatherReport->saveProcess();
                    $weatherReport->deliverReport();
                } catch (Exteption $e) {
                    self::$_logger->log(__METHOD__ . ' Error ', array('err' => $e->getMessage()));
                }
                self::$_logger->log(__METHOD__ . ' Completed', array('num' => $i++, 'total' => $total));
            }
        }
        //send report from sttions together
        $scheduleProcessedIdArray_schedule_id = array();
        foreach ($reportProcesses as $reportProcess) {
            $scheduleProcessedIdArray_schedule_id[$reportProcess['schedule_id']][] = $reportProcess['schedule_processed_id'];
        }
        foreach ($scheduleProcessedIdArray_schedule_id as $scheduleProcessedIdArray) {
            new WeatherReportMailSender($scheduleProcessedIdArray);
        }
        self::$_logger->log(__METHOD__ . ' Schedule report completed' . "\n\n\n\n\n\n\n\n\n");
        Yii::app()->mutex->unlock();
    }
Example #3
0
 public static function getScheduleList()
 {
     //        $sql = "SELECT `t1`.`schedule_id`,
     //                       `t1`.`report_type`,
     //                       `t1`.`station_id`,
     //                       `t1`.`period`,
     //                       `t1`.`report_format`,
     //                       `t1`.`last_scheduled_run_fact`,
     //                       `t1`.`last_scheduled_run_planned`,
     //
     //                       `t2`.`timezone_id`, `t2`.`timezone_offset`,  `t2`.`station_id_code`,
     //
     //                       `t3`.`schedule_processed_id`,  `t3`.`check_period_end`,
     //                       `t3`.`listener_log_ids` AS `log_ids`, `t3`.`check_period_start`, `t3`.`check_period_end`,
     //
     //                       `t4`.`message`, `t4`.`measuring_timestamp`, `t4`.`log_id`
     //                FROM `".ScheduleReport::tableName()."` `t1`
     //                LEFT JOIN `".Station::tableName()."`                 `t2` ON `t2`.`station_id` = `t1`.`station_id`
     //                LEFT JOIN `".ScheduleReportProcessed::tableName()."` `t3` ON (`t3`.`schedule_id` = `t1`.`schedule_id` AND `is_last` = 1 AND `is_processed` = 1)
     //                LEFT JOIN `".ListenerLog::tableName()."`             `t4` ON `t4`.`log_id` = `t3`.`listener_log_id`";
     //        $sql = "SELECT `t1`.`schedule_id`,
     //                       `t1`.`report_type`,
     //                     /* `t1`.`station_id`,*/
     //                       `t1`.`period`,
     //                       `t1`.`report_format`,
     //                       `t1`.`last_scheduled_run_fact`,
     //                       `t1`.`last_scheduled_run_planned`,
     //
     //                     /*  `t2`.`timezone_id`, `t2`.`timezone_offset`,  `t2`.`station_id_code`,*/
     //
     //                       `t3`.`schedule_processed_id`,  `t3`.`check_period_end`,
     //                       `t3`.`listener_log_ids` AS `log_ids`, `t3`.`check_period_start`, `t3`.`check_period_end`,
     //
     //                       `t4`.`message`, `t4`.`measuring_timestamp`, `t4`.`log_id`
     //                FROM `".ScheduleReport::tableName()."` `t1`
     //               /* LEFT JOIN `".Station::tableName()."`                 `t2` ON `t2`.`station_id` = `t1`.`station_id`*/
     //                LEFT JOIN `".ScheduleReportProcessed::tableName()."` `t3` ON `t3`.`schedule_processed_id` = (
     //                    SELECT `t5`.`schedule_processed_id`
     //                    FROM `".ScheduleReportProcessed::tableName()."` AS `t5`
     //                    WHERE `t5`.`schedule_id` = `t1`.`schedule_id` AND `is_processed` = 1
     //                    ORDER BY `t5`.`created` DESC
     //                    LIMIT 1
     //                )
     //                LEFT JOIN `".ListenerLog::tableName()."`             `t4` ON `t4`.`log_id` = `t3`.`listener_log_id`";
     $files_path = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'schedule_reports';
     $schedule_list = ScheduleReport::model()->with('station.processed.listenerLog')->with('station.realStation')->with('destinations')->findAll();
     foreach ($schedule_list as $key => $value) {
         $new_schedule_list[$key] = $value->attributes;
         foreach ($value->station as $stationsKey => $stationsValue) {
             $new_schedule_list[$key]['station'][$stationsKey] = $stationsValue->attributes;
             foreach ($schedule_list[$key]['station'][$stationsKey]->processed as $processedKey => $processedValue) {
                 $new_schedule_list[$key]['station'][$stationsKey]['processed'][$processedKey] = $processedValue->attributes;
                 $new_schedule_list[$key]['station'][$stationsKey]['processed'][$processedKey]['listenerLog'] = $schedule_list[$key]['station'][$stationsKey]['processed'][$processedKey]['listenerLog'];
             }
             $new_schedule_list[$key]['station'][$stationsKey]['realStation'] = $schedule_list[$key]['station'][$stationsKey]->realStation->attributes;
             $file = $files_path . DIRECTORY_SEPARATOR . $new_schedule_list[$key]['station'][$stationsKey]['processed'][0]['schedule_processed_id'];
             if (in_array($new_schedule_list[$key]['report_type'], array('synop', 'metar', 'speci')) && isset($new_schedule_list[$key]['station'][$stationsKey]['processed'][0]['schedule_processed_id']) && file_exists($file)) {
                 $new_schedule_list[$key]['report_string_initial'] = file_get_contents($file);
             }
             if (file_exists($file)) {
                 $new_schedule_list[$key]['station'][$stationsKey]['realStation']['report_string_initial'] = file_get_contents($file);
                 $new_schedule_list[$key]['station'][$stationsKey]['realStation']['file_name'] = $new_schedule_list[$key]['station'][$stationsKey]['realStation']['station_id_code'] . '_' . strtoupper($new_schedule_list[$key]['report_type']) . '_' . gmdate('Y-m-d_Hi', strtotime($new_schedule_list[$key]['station'][$stationsKey]['processed'][0]['check_period_end'])) . '.' . $new_schedule_list[$key]['report_format'];
             }
         }
         foreach ($value->destinations as $destinationsKey => $destinationsValue) {
             $new_schedule_list[$key]['destinations'][$destinationsKey] = $destinationsValue->attributes;
         }
     }
     return $new_schedule_list;
 }
Example #4
0
 public function actionScheduleHistory()
 {
     $schedule_id = isset($_REQUEST['schedule_id']) ? intval($_REQUEST['schedule_id']) : null;
     if (!$schedule_id) {
         $this->redirect($this->createUrl('site/schedule'));
     }
     // this would a very cool, but need some pagination
     //        $schedule = ScheduleReport::model()->with('station.realStation','station.processed.listenerLog','destinations')->findByPk($schedule_id);
     $schedule = ScheduleReport::model()->with('station.realStation', 'destinations')->findByPk($schedule_id);
     if (!$schedule) {
         $this->redirect($this->createUrl('site/schedule'));
     }
     $scheduleFormated = array();
     $scheduleFormated['report'] = $schedule->attributes;
     foreach ($schedule->destinations as $destinationItem) {
         $scheduleFormated['report']['destinations'][] = $destinationItem->attributes;
     }
     foreach ($schedule->station as $key => $station) {
         $scheduleFormated['report']['stations'][$key] = $station->attributes;
         $scheduleFormated['report']['realStation'][$key] = $station->realStation->attributes;
     }
     $this->render('schedule_report_stations', array('schedule' => $scheduleFormated));
 }
                </td>

            </tr>
        </table>

        <table class="formtable station_table">
            <?php 
foreach ($forms_s as $key => $value) {
    ?>
                <tr>
                    <td>
                        <?php 
    echo CHtml::activeHiddenField($forms_s[$key], "[{$key}]id");
    ?>
                        <?php 
    echo CHtml::activeDropDownList($forms_s[$key], "[{$key}]station_id", ScheduleReport::getStationsList());
    ?>
                    </td>
                    <td>
                        &nbsp;&nbsp;<a class="remove_button">[remove]</a>
                    </td>
                </tr>
            <?php 
}
?>
        </table>
        <br>
        <a href="javascript:void(0)" data-item-count="<?php 
echo count($forms_s);
?>
" class="add_button">[Add new]</a>
 public function m_0_4_16()
 {
     @apache_setenv('no-gzip', 1);
     @ini_set('zlib.output_compression', 0);
     @ini_set('implicit_flush', 1);
     ini_set('memory_limit', '-1');
     $this->flushNotification("\n...Please wait... Updating is going on... DO NOT LEAVE THIS PAGE!");
     $this->flushNotification("<br/><br/>...Update settings table....");
     Yii::app()->db->createCommand("ALTER TABLE `settings` ADD `local_timezone_id` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'UTC'")->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Update settings table....");
     Yii::app()->db->createCommand("ALTER TABLE `settings` ADD `local_timezone_offset` VARCHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '+00:00'")->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Create new table....");
     $sql = "CREATE TABLE `schedule_report_destination` (\n                  `schedule_destination_id` int(11) NOT NULL AUTO_INCREMENT,\n                  `schedule_id` int(11) NOT NULL,\n                  `method` varchar(20) NOT NULL DEFAULT 'mail',\n                  `destination_email` varchar(255) NOT NULL,\n                  `destination_local_folder` varchar(255) NOT NULL,\n                  `destination_ip` varchar(15) NOT NULL,\n                  `destination_ip_port` smallint(5) NOT NULL DEFAULT '21',\n                  `destination_ip_folder` varchar(255) NOT NULL DEFAULT '/',\n                  `destination_ip_user` varchar(255) NOT NULL,\n                  `destination_ip_password` varchar(255) NOT NULL,\n                  PRIMARY KEY (`schedule_destination_id`),\n                  KEY `schedule_id` (`schedule_id`),\n                  CONSTRAINT `schedule_report_destination_fk` FOREIGN KEY (`schedule_id`) REFERENCES `schedule_report` (`schedule_id`) ON DELETE CASCADE ON UPDATE NO ACTION\n                ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
     Yii::app()->db->createCommand($sql)->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Done!");
     $this->flushNotification("<br/><br/>...Group existed schedules...");
     $sql = "SELECT * from `" . ScheduleReport::model()->tableName() . "`\n                ORDER BY station_id, report_type, report_format, period";
     $res = Yii::app()->db->createCommand($sql)->queryAll();
     if ($res) {
         $groupped = array();
         $delete = array();
         foreach ($res as $key => $value) {
             $groupped_code = $value['station_id'] . '_' . $value['report_type'] . '_' . $value['period'] . '_' . $value['report_format'];
             if (!$groupped[$groupped_code]) {
                 $groupped[$groupped_code] = array('schedule_id' => $value['schedule_id'], 'destinations' => array());
             } else {
                 $delete[] = $value['schedule_id'];
             }
             $groupped[$groupped_code]['destinations'][] = array('schedule_id' => $value['schedule_id'], 'method' => $value['method'], 'destination_email' => $value['destination_email'], 'destination_local_folder' => $value['destination_local_folder'], 'destination_ip' => $value['destination_ip'], 'destination_ip_port' => $value['destination_ip_port'], 'destination_ip_folder' => $value['destination_ip_folder'], 'destination_ip_user' => $value['destination_ip_user'], 'destination_ip_password' => $value['destination_ip_password']);
         }
         foreach ($groupped as $groupped_code => $groupped_data) {
             $schedule_id = $groupped_data['schedule_id'];
             foreach ($groupped_data['destinations'] as $key => $value) {
                 $dest = new ScheduleReportDestination();
                 $dest->schedule_id = $schedule_id;
                 $dest->method = $value['method'];
                 $dest->destination_email = $value['destination_email'];
                 $dest->destination_local_folder = $value['destination_local_folder'];
                 $dest->destination_ip = $value['destination_ip'];
                 $dest->destination_ip_port = $value['destination_ip_port'];
                 $dest->destination_ip_folder = $value['destination_ip_folder'];
                 $dest->destination_ip_user = $value['destination_ip_user'];
                 $dest->destination_ip_password = $value['destination_ip_password'];
                 $dest->save();
             }
         }
     }
     It::memStatus('update__success');
     $this->flushNotification('<script type="text/javascript"> setTimeout(function(){document.location.href="' . Yii::app()->controller->createUrl('update/index') . '"}, 5000)</script>');
 }