Пример #1
0
 public function afterDelete()
 {
     if (!$this->getUseLong()) {
         ScheduleReportProcessed::model()->deleteAllByAttributes(array('listener_log_id' => $this->log_id));
     }
     parent::afterDelete();
 }
 public function __construct($scheduleProcessedIdArray = array(), $logger = null)
 {
     if (!is_null($logger)) {
         $this->_logger = $logger;
     } else {
         $this->_logger = LoggerFactory::getFileLogger('email_report');
     }
     $this->_logger->log(__METHOD__ . ' schedule_processed_ids: ' . print_r($scheduleProcessedIdArray, 1));
     if (count($scheduleProcessedIdArray) == 0) {
         return false;
     }
     $this->scheduleProcessedReports = ScheduleReportProcessed::model()->with('listenerLog', 'ScheduleReportToStation.realStation')->findAllByPk($scheduleProcessedIdArray);
     $this->schedule_report = $this->scheduleProcessedReports[0]->ScheduleReportToStation->schedule_report;
     if (!$this->schedule_report->send_email_together) {
         return false;
     }
     $this->destinations = $this->scheduleProcessedReports[0]->ScheduleReportToStation->schedule_report->destinations;
     if ($this->schedule_report->send_like_attach) {
         $this->_sendLikeAttach();
     } else {
         $this->_sendInMessageBody();
     }
 }
Пример #3
0
    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();
    }
Пример #4
0
 public function saveProcess()
 {
     $this->_logger->log(__METHOD__);
     $string = $this->getReportComplete();
     $obj = ScheduleReportProcessed::model()->findByPk($this->_schedule_processed_id);
     $obj->is_processed = 1;
     $obj->serialized_report_errors = serialize($this->errors);
     $obj->serialized_report_explanations = serialize($this->explanations);
     $obj->save();
     $file_path = $this->tmpReportFile();
 }
Пример #5
0
 public function actionScheduleDownload()
 {
     $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
     $process = ScheduleReportProcessed::model()->with('ScheduleReportToStation.realStation')->with('ScheduleReportToStation.schedule_report')->findByPk($id);
     if (!$process) {
         $this->redirect($_SERVER['HTTP_REFERER']);
     }
     $station = $process->ScheduleReportToStation->realStation;
     $schedule = $process->ScheduleReportToStation->schedule_report;
     $file_name = $station->station_id_code . '_' . strtoupper($schedule->report_type) . '_' . gmdate('Y-m-d_Hi', strtotime($process->check_period_end ? $process->check_period_end : $process->created)) . '.' . $schedule->report_format;
     $file_path = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "files" . DIRECTORY_SEPARATOR . "schedule_reports" . DIRECTORY_SEPARATOR . $id;
     $report_string = file_exists($file_path) ? file_get_contents($file_path) : ($report_string = '');
     It::downloadFile($report_string, $file_name);
 }
 public static function getInfoForRegenerate($id)
 {
     return ScheduleReportProcessed::model()->with('ScheduleReportToStation.schedule_report')->findByPk($id);
 }
Пример #7
0
 public function actionSetup()
 {
     $criteria = new CDbCriteria();
     $criteria->condition = "ord > 0";
     $criteria->order = "ord ASC";
     $meas_types = RefbookMeasurementType::model()->findAll($criteria);
     if ($meas_types) {
         foreach ($meas_types as $key => $value) {
             $sql = "SELECT `t1`.`metric_id`, CONCAT(`t2`.`html_code`, ' (', `t2`.`full_name`, ')') AS `name`, `t1`.`is_main`, `t1`.`measurement_type_metric_id`\n                        FROM `" . RefbookMeasurementTypeMetric::model()->tableName() . "` `t1`\n                        LEFT JOIN `" . RefbookMetric::model()->tableName() . "` `t2` ON `t2`.`metric_id` = `t1`.`metric_id`\n                        WHERE `t1`.`measurement_type_id` = '" . $value->measurement_type_id . "'";
             $meas_types[$key]->metrics_list = Yii::app()->db->createCommand($sql)->queryAll();
         }
     }
     if (Yii::app()->request->isPostRequest && isset($_POST['main_metric'])) {
         foreach ($_POST['main_metric'] as $key => $value) {
             if ($meas_types[$key]->metrics_list) {
                 foreach ($meas_types[$key]->metrics_list as $v1) {
                     $update = array('is_main' => $v1['metric_id'] == $value ? 1 : 0);
                     RefbookMeasurementTypeMetric::model()->updateByPk($v1['measurement_type_metric_id'], $update);
                 }
             }
         }
         StationSensorFeature::updateMetric();
         $DB = array('db' => CStubActiveRecord::getDbConnect(), 'db_long' => CStubActiveRecord::getDbConnect(true));
         foreach ($DB as $db) {
             $db->createCommand("DELETE FROM `" . ScheduleReportProcessed::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . ForwardedMessage::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . StationCalculationData::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . SeaLevelTrend::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . SensorDataMinute::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . SensorData::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . ListenerLog::model()->tableName() . "`")->query();
         }
         It::memStatus('admin_metrics_saved');
         $this->redirect($this->createUrl('admin/setup'));
     }
     $this->render('setup', array('meas_types' => $meas_types));
 }
Пример #8
0
 public function m_0_4_10()
 {
     @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!");
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Drop field `description` from `calculation_handler`!");
     Yii::app()->db->createCommand("ALTER TABLE `calculation_handler` DROP `description`")->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     Yii::app()->db->createCommand("ALTER TABLE `schedule_report_processed` DROP `serialized_report_problems`")->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Move schduled outputs from database into files !");
     $sql = "SELECT `schedule_processed_id`, `report_string_initial`\n                FROM `" . ScheduleReportProcessed::model()->tableName() . "`";
     $res = Yii::app()->db->createCommand($sql)->queryAll();
     if ($res) {
         $files_path = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "files" . DIRECTORY_SEPARATOR . "schedule_reports";
         if (!is_dir($files_path)) {
             mkdir($files_path, 0777, TRUE);
         }
         foreach ($res as $key => $value) {
             $file_path = $files_path . DIRECTORY_SEPARATOR . $value['schedule_processed_id'];
             if ($h = fopen($file_path, "w+")) {
                 fwrite($h, $value['report_string_initial']);
                 fclose($h);
             }
         }
     }
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Delete useless columns !");
     Yii::app()->db->createCommand("ALTER TABLE `schedule_report_processed` DROP `report_string_initial`")->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     Yii::app()->db->createCommand("ALTER TABLE `schedule_report_processed` DROP `report_string_changed`")->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     It::memStatus('update__success');
     $this->flushNotification('<script type="text/javascript"> setTimeout(function(){document.location.href="' . Yii::app()->controller->createUrl('update/index') . '"}, 5000)</script>');
 }
Пример #9
0
 /**
  * Remove all old sensors data from actual database
  * @param type $timestamp 
  */
 private function removeDataBeforeDate($timestamp)
 {
     return;
     //TODO: because of LONG DB
     $this->addBackupLog("DELETE all records before " . $timestamp);
     It::debug("DELETE all records before " . $timestamp, 'backup_database');
     // delete from `listener`
     $sql = "SELECT listener_id FROM `" . Listener::model()->tableName() . "` WHERE `created` <= '" . $timestamp . "'";
     $res = Yii::app()->db->createCommand($sql)->queryColumn();
     It::debug("DELETE " . count($res) . " from " . Listener::model()->tableName(), 'backup_database');
     $this->addBackupLog("DELETE " . count($res) . " from " . Listener::model()->tableName());
     if ($res) {
         $sql = "DELETE FROM `" . Listener::model()->tableName() . "` WHERE `created` <= '" . $timestamp . "'";
         $res = Yii::app()->db->createCommand($sql)->query();
     }
     // delete from `listener_log`
     $sql = "SELECT log_id FROM `" . ListenerLog::model()->tableName() . "` WHERE `created` <= '" . $timestamp . "' ";
     $res = Yii::app()->db->createCommand($sql)->queryColumn();
     It::debug("DELETE " . count($res) . " from " . ListenerLog::model()->tableName(), 'backup_database');
     $this->addBackupLog("DELETE " . count($res) . " from " . ListenerLog::model()->tableName());
     if ($res) {
         $sql = "DELETE FROM `" . ListenerLog::model()->tableName() . "` WHERE `created` <= '" . $timestamp . "'";
         $res = Yii::app()->db->createCommand($sql)->query();
     }
     // delete from `schedule_processed`
     $sql = "SELECT `schedule_processed_id` FROM `" . ScheduleReportProcessed::model()->tableName() . "` WHERE `created` <= '" . $timestamp . "'";
     $res = Yii::app()->db->createCommand($sql)->queryColumn();
     It::debug("DELETE " . count($res) . " from " . ScheduleReportProcessed::model()->tableName(), 'backup_database');
     $this->addBackupLog("DELETE " . count($res) . " from " . ScheduleReportProcessed::model()->tableName());
     if ($res) {
         $sql = "DELETE FROM `" . ScheduleReportProcessed::model()->tableName() . "` WHERE `created` <= '" . $timestamp . "'";
         $res = Yii::app()->db->createCommand($sql)->query();
     }
     // delete from `xml_log`
     $sql = "SELECT `xml_log_id` FROM `" . XmlLog::model()->tableName() . "` WHERE `created` <= '" . $timestamp . "'";
     $res = Yii::app()->db->createCommand($sql)->queryColumn();
     It::debug("DELETE " . count($res) . " from " . XmlLog::model()->tableName(), 'backup_database');
     $this->addBackupLog("DELETE " . count($res) . " from " . XmlLog::model()->tableName());
     if ($res) {
         $sql = "DELETE FROM `" . XmlLog::model()->tableName() . "` WHERE `created` <= '" . $timestamp . "'";
         $res = Yii::app()->db->createCommand($sql)->query();
     }
 }
Пример #10
0
 public function actionScheduleRegenerateReport()
 {
     // All reports are generated basing on data in UTC time.
     TimezoneWork::set('UTC');
     $return = array('ok' => 0);
     $schedule_processed_id = isset($_REQUEST['schedule_processed_id']) ? intval($_REQUEST['schedule_processed_id']) : null;
     if ($schedule_processed_id) {
         $data = ScheduleReportProcessed::getInfoForRegenerate($schedule_processed_id);
         if (!is_null($data)) {
             $reportType = null;
             switch ($data->ScheduleReportToStation->schedule_report->report_type) {
                 case 'bufr':
                     $reportType = 'Bufr';
                     break;
                 case 'synop':
                     $reportType = 'Synop';
                     break;
                 case 'metar':
                     $reportType = 'Metar';
                     break;
                 case 'speci':
                     $reportType = 'Speci';
                     break;
                 default:
                     $reportType = 'Export';
                     break;
             }
             $weather_report = WeatherReport::create($reportType);
             $weather_report->load($schedule_processed_id);
             if (!$weather_report->errors) {
                 $weather_report->generate();
                 $weather_report->prepareReportComplete();
                 $weather_report->saveProcess();
                 $return['ok'] = 1;
                 $return['report_string_initial'] = $weather_report->getReportComplete();
             } else {
                 $return['errors'] = $weather_report->errors;
             }
         }
     }
     print json_encode($return);
     CApplication::end();
 }