public static function getStationCalculationHandlers($station_ids) { $criteria = new CDbCriteria(); $criteria->with = array('handler'); $criteria->compare('station_id', $station_ids); $records = StationCalculation::model()->findAll($criteria); $result = array(); foreach ($records as $record) { $result[$record->station_id][] = $record; } return $result; }
public function createExport() { $return_string = ""; $sql = "SELECT\n `t1`.`measuring_timestamp` AS `TxDateTime`,\n `t4`.`station_id_code` AS `StationId`, \n `t4`.`display_name` AS `StationDisplayName`,\n `t3`.`sensor_id_code` AS `SensorId`, \n `t3`.`display_name` AS `SensorDisplayName`, \n \n `t1`.`period` AS `MeasurementPeriod`,\n `t1`.`sensor_feature_value` AS `Value`,\n `t5`.`short_name` AS `Metric`,\n `t8`.`value` AS `DewPoint`,\n `t11`.`value` AS `PressureMSL`,\n \n `t12`.`handler_id_code`,\n `t4`.`magnetic_north_offset`\n\n FROM `" . SensorData::model()->tableName() . "` t1\n LEFT JOIN `" . StationSensorFeature::model()->tableName() . "` `t2` ON `t2`.`sensor_feature_id` = `t1`.`sensor_feature_id`\n LEFT JOIN `" . StationSensor::model()->tableName() . "` `t3` ON `t3`.`station_sensor_id` = `t2`.`sensor_id`\n LEFT JOIN `" . Station::model()->tableName() . "` `t4` ON `t4`.`station_id` = `t1`.`station_id`\n LEFT JOIN `" . RefbookMetric::model()->tableName() . "` `t5` ON `t5`.`metric_id` = `t2`.`metric_id`\n\n LEFT JOIN `" . StationCalculation::model()->tableName() . "` `t6` ON (`t6`.`station_id` = `t1`.`station_id` AND `t6`.`handler_id` = 1)\n LEFT JOIN `" . StationCalculationVariable::model()->tableName() . "` `t7` ON (`t7`.`sensor_feature_id` = `t2`.`sensor_feature_id` AND `t7`.`calculation_id` = `t6`.`calculation_id`)\n LEFT JOIN `" . StationCalculationData::model()->tableName() . "` `t8` ON (`t8`.`calculation_id` = `t7`.`calculation_id` AND `t8`.`listener_log_id` = `t1`.`listener_log_id`)\n\n LEFT JOIN `" . StationCalculation::model()->tableName() . "` `t9` ON (`t9`.`station_id` = `t1`.`station_id` AND `t9`.`handler_id` = 2)\n LEFT JOIN `" . StationCalculationVariable::model()->tableName() . "` `t10` ON (`t10`.`sensor_feature_id` = `t2`.`sensor_feature_id` AND `t10`.`calculation_id` = `t9`.`calculation_id`)\n LEFT JOIN `" . StationCalculationData::model()->tableName() . "` `t11` ON (`t11`.`calculation_id` = `t10`.`calculation_id` AND `t11`.`listener_log_id` = `t1`.`listener_log_id`)\n\n LEFT JOIN `" . SensorDBHandler::model()->tableName() . "` `t12` ON `t12`.`handler_id` = `t3`.`handler_id`\n\n WHERE `t1`.`station_id` IN (" . implode(',', $this->station_id) . ")\n AND `t1`.`measuring_timestamp` >= FROM_UNIXTIME(" . $this->start_timestamp . ") \n AND `t1`.`measuring_timestamp` <= FROM_UNIXTIME(" . $this->end_timestamp . ")\n AND `t2`.`is_main` = 1\n AND `t1`.`is_m` = '0'\n ORDER BY `t1`.`measuring_timestamp` DESC, `t4`.`station_id_code`, `t3`.`sensor_id_code`"; $res = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll(); if ($res) { foreach ($res as $key => $value) { $handler_obj = SensorHandler::create($value['handler_id_code']); $res[$key]['Value'] = $handler_obj->applyOffset($res[$key]['Value'], $res[$key]['magnetic_north_offset']); $res[$key]['Value'] = $handler_obj->formatValue($res[$key]['Value'], $res[$key]['feature_code']); unset($res[$key]['magnetic_north_offset']); unset($value['handler_id_code']); unset($res[$key]['feature_code']); } $return_string .= "\"AWS Stations:\"\n" . It::prepareStringCSV($res); } $sql = "SELECT\n `t1`.`measuring_timestamp` AS `TxDateTime`,\n `t3`.`station_id_code` AS `StationId`, \n `t3`.`display_name` AS `StationDisplayName`,\n `t2`.`sensor_id_code` AS `SensorId`, \n `t2`.`display_name` AS `SensorDisplayName`, \n (`t1`.`sensor_value` * `t1`.`bucket_size`) AS `Value`,\n `t4`.`short_name` AS `Metric` \n FROM `" . SensorDataMinute::model()->tableName() . "` t1\n LEFT JOIN `" . StationSensor::model()->tableName() . "` t2 ON t2.station_sensor_id = t1.sensor_id\n LEFT JOIN `" . Station::model()->tableName() . "` t3 ON t3.station_id = t1.station_id\n LEFT JOIN `" . RefbookMetric::model()->tableName() . "` t4 ON t4.metric_id = t1.metric_id\n \n WHERE `t1`.`station_id` IN (" . implode(',', $this->station_id) . ")\n AND `t1`.`measuring_timestamp` >= FROM_UNIXTIME(" . $this->start_timestamp . ") \n AND `t1`.`measuring_timestamp` <= FROM_UNIXTIME(" . $this->end_timestamp . ")\n AND `t1`.`is_tmp` = 0\n ORDER BY t1.measuring_timestamp DESC, t3.station_id_code, t2.sensor_id_code\n "; $res = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll(); if ($res) { $return_string .= "\n\n\"RG Stations:\"\n" . It::prepareStringCSV($res); } It::downloadFile($return_string, 'export__' . date('Y-m-d_Hi') . '.csv', 'text/csv'); }
/** * AWS and AWOS stations can have calculations (dewPoint, MSL) * Run calculations basing on already parsed data * * @return type */ public function runCalculations() { $this->_logger->log(__METHOD__); if (!in_array($this->_station->station_type, array('aws', 'awos'))) { return false; } // get calculations for station ID defined from arrived message $records = StationCalculation::getStationCalculationHandlers(array($this->_station->station_id)); $handlers = array_key_exists($this->_station->station_id, $records) ? $records[$this->_station->station_id] : null; if (is_array($handlers)) { foreach ($handlers as $value) { // create object of calculation handler $handler = CalculationHandler::create($value->handler->handler_id_code); // run calculation for station basing on message's ID $handler->calculate($this->_station, $this->message_obj->log_id); } } }
protected function addCalculations($file_config) { $station_id = $file_config['station']['station_id']; $this->stations[$station_id]; foreach ($file_config['station_calculation'] as $calc) { $calc_main_handler_db = CalculationDBHandler::model()->findByAttributes(array('handler_id_code' => $calc['handler'])); $calculation_db = new StationCalculation(); $calculation_db->station_id = $station_id; $calculation_db->handler_id = $calc_main_handler_db->handler_id; $calculation_db->formula = $calc['formula'] ? $calc['formula'] : 'default'; $calculation_db->save(false); foreach ($this->sensors_for_calcs[$calc['handler']][$station_id] as $sensor) { $calc_variables = new StationCalculationVariable(); $station_sensor_features = $sensor->features(); foreach ($station_sensor_features as $s_feature) { if (is_object($s_feature)) { if ($s_feature->is_main) { $calc_variables->sensor_feature_id = $s_feature->sensor_feature_id; $calc_variables->variable_name = $s_feature->feature_code; $calc_variables->calculation_id = $calculation_db->getPrimaryKey(); $calc_variables->save(false); } } } } } }
public function generate() { $this->_logger->log(__METHOD__); if ($this->errors) { $this->_logger->log(__METHOD__, array('errors' => $this->errors)); return false; } $current_user_timezone = date_default_timezone_get(); $timezone_id = 'UTC'; if ($timezone_id != $current_user_timezone) { TimezoneWork::set($timezone_id); } $this->report_parts = array(); $this->explanations = array(); // get sensors' values for all messages received in reporting period $sql = "SELECT `t5`.`listener_log_id`,\r\n `t5`.`measuring_timestamp`,\r\n `t1`.`station_sensor_id`, `t1`.`sensor_id_code`, \r\n \r\n `t3`.`feature_code`, `t3`.`feature_constant_value`,\r\n `t4`.`code` AS `metric_code`, \r\n `t5`.`sensor_feature_value`, \r\n `t5`.`is_m`,\r\n `t5`.`period` AS `sensor_feature_period`,\r\n `t6`.`code` AS `value_metric_code`,\r\n `t7`.`handler_id_code`\r\n\r\n FROM `" . SensorData::model()->tableName() . "` `t5`\r\n LEFT JOIN `" . StationSensor::model()->tableName() . "` `t1` ON t1.station_sensor_id = t5.sensor_id\r\n LEFT JOIN `" . StationSensorFeature::model()->tableName() . "` `t3` ON (`t3`.`sensor_feature_id` = `t5`.`sensor_feature_id`)\r\n LEFT JOIN `" . RefbookMetric::model()->tableName() . "` `t4` ON `t4`.`metric_id` = `t3`.`metric_id`\r\n LEFT JOIN `" . RefbookMetric::model()->tableName() . "` `t6` ON `t6`.`metric_id` = `t5`.`metric_id`\r\n LEFT JOIN `" . SensorDBHandler::model()->tableName() . "` `t7` ON t7.handler_id = t1.handler_id\r\n WHERE `t5`.`station_id` = '" . $this->station_info->station_id . "' AND `t5`.`listener_log_id` IN (" . $this->schedule_process_info->listener_log_ids . ")\r\n ORDER BY `t5`.`measuring_timestamp` DESC, `t1`.`sensor_id_code` ASC, `t3`.`feature_code` ASC"; $sensor_data = Yii::app()->db->createCommand($sql)->queryAll(); $data = array(); if ($sensor_data) { // get calculation values for all messages received in reporting period $sql = "SELECT `t1`.`listener_log_id`,\r\n `t1`.`value`,\r\n `t3`.`handler_id_code`\r\n FROM `" . StationCalculationData::model()->tableName() . "` `t1`\r\n LEFT JOIN `" . StationCalculation::model()->tableName() . "` `t2` ON t2.calculation_id = t1.calculation_id\r\n LEFT JOIN `" . CalculationDBHandler::model()->tableName() . "` `t3` ON `t3`.`handler_id` = `t2`.`handler_id`\r\n WHERE `t2`.`station_id` = '" . $this->station_info->station_id . "' AND `t1`.`listener_log_id` IN (" . $this->schedule_process_info->listener_log_ids . ")\r\n ORDER BY `t3`.`handler_id_code`"; $res2 = Yii::app()->db->createCommand($sql)->queryAll(); if ($res2) { foreach ($res2 as $key => $value) { $calculations[$value['listener_log_id']][] = $value; } } foreach ($sensor_data as $key => $value) { $data[$value['listener_log_id']][] = $value; } // prepare $result_item array, where each line represents line in report. foreach ($data as $key => $value) { $result_item = array('StationId', $this->station_info->station_id_code, 'WMO AWS #', $this->station_info->wmo_block_number . $this->station_info->station_number, 'National AWS #', $this->station_info->national_aws_number, 'Tx DateTime', date('m/d/Y H:i', strtotime($value[0]['measuring_timestamp']))); foreach ($value as $key2 => $value2) { $handler_obj = SensorHandler::create($value2['handler_id_code']); if (in_array($value2['handler_id_code'], array('BatteryVoltage', 'Humidity', 'Pressure', 'Temperature'))) { $sensor_id_code = $value2['sensor_id_code']; } else { $sensor_id_code = $value2['sensor_id_code'] . ' (' . $handler_obj->getFeatureName($value2['feature_code']) . ')'; } $result_item[] = $sensor_id_code; if ($value2['is_m']) { $result_item[] = '-'; } else { $value2['sensor_feature_value'] = $handler_obj->applyOffset($value2['sensor_feature_value'], $this->station_info->magnetic_north_offset); $result_item[] = str_replace(',', ' ', $handler_obj->formatValue($value2['sensor_feature_value'], $value2['feature_code'])); } } if (isset($calculations[$key])) { foreach ($calculations[$key] as $key2 => $value2) { if ($value2['handler_id_code'] === 'DewPoint') { $result_item[] = 'DP'; } else { if ($value2['handler_id_code'] === 'PressureSeaLevel') { $result_item[] = 'MSL'; } else { $result_item[] = 'Unknown calculation'; } } $result_item[] = str_replace(',', ' ', number_format(round($value2['value'], 1), 1)); } } $this->report_parts[] = $result_item; } } if ($timezone_id != $current_user_timezone) { TimezoneWork::set($current_user_timezone); } $this->_logger->log(__METHOD__ . ' Export generation completed.'); return true; }
public function getUsedSensors($station_id) { $sql = "SELECT t5.sensor_id_code\n FROM `" . StationCalculationVariable::model()->tableName() . "` `t1`\n LEFT JOIN `" . StationCalculation::model()->tableName() . "` t2 ON t2.calculation_id = t1.calculation_id\n LEFT JOIN `" . CalculationDBHandler::model()->tableName() . "` t3 ON t3.handler_id = t2.handler_id\n LEFT JOIN `" . StationSensorFeature::model()->tableName() . "` t4 ON t4.sensor_feature_id = t1.sensor_feature_id\n LEFT JOIN `" . StationSensor::model()->tableName() . "` t5 ON t5.station_sensor_id = t4.sensor_id\n WHERE `t3`.handler_id_code = ? AND t2.station_id = ? \n ORDER BY t5.sensor_id_code"; $res = Yii::app()->db->createCommand($sql)->queryColumn(array($this->handler_id_code, $station_id)); return $res; }
public function _prepareCalculationsInfo($listener_log_id) { $this->_logger->log(__METHOD__); $calculations = array(); if (!is_null($this->station_info)) { $sql = "SELECT `t1`.`value`, `t3`.`handler_id_code`\r\n FROM `" . StationCalculationData::model()->tableName() . "` `t1`\r\n LEFT JOIN `" . StationCalculation::model()->tableName() . "` `t2` ON `t2`.`calculation_id` = `t1`.`calculation_id`\r\n LEFT JOIN `" . CalculationDBHandler::model()->tableName() . "` `t3` ON `t3`.`handler_id` = `t2`.`handler_id`\r\n WHERE `t1`.`listener_log_id` = '" . $listener_log_id . "'"; $calculations_info = Yii::app()->db->createCommand($sql)->queryAll(); if ($calculations_info) { foreach ($calculations_info as $value) { if (!isset($calculations[$value['handler_id_code']])) { $calculations[$value['handler_id_code']] = $value; } } } } return $calculations; }
public function actionAwsSingle() { $criteria = new CDbCriteria(); $criteria->compare('station_type', array('aws', 'awos')); $criteria->order = 'station_id_code asc'; $stations = Station::model()->findAll($criteria); if (count($stations) > 0) { $session = Yii::app()->session; $id = isset($_REQUEST['station_id']) ? intval($_REQUEST['station_id']) : (isset($session['single_aws']['station_id']) ? $session['single_aws']['station_id'] : ''); $station = null; if ($id) { foreach ($stations as $st) { if ($id == $st->station_id) { $station = $st; break; } } } $station = is_null($station) ? $stations[0] : $station; $log_id = isset($_REQUEST['log_id']) ? intval($_REQUEST['log_id']) : null; $last2Messages = ListenerLog::getAllLast2Messages(array($station->station_id), $log_id); if (array_key_exists($station->station_id, $last2Messages) && count($last2Messages[$station->station_id]) > 0) { $station->lastMessage = date('m/d/Y H:i', strtotime($last2Messages[$station->station_id][0]->measuring_timestamp)); $next_expected = strtotime($last2Messages[$station->station_id][0]->measuring_timestamp) + $station->event_message_period * 60 + 300; $station->nextMessageExpected = date('m/d/Y H:i', $next_expected); if ($next_expected < time()) { $station->nextMessageIsLates = 1; } } else { $station->lastMessage = 'Unknown'; } if (count($last2Messages[$station->station_id]) > 1) { $station->previousMessage = date('m/d/Y H:i', strtotime($last2Messages[$station->station_id][1]->measuring_timestamp)); } else { $station->previousMessage = 'Unknown'; } $list = StationSensor::getSensorsForAWSDisplay(array($station->station_id), 'aws_single'); $sensors = isset($list[$station->station_id]) ? $list[$station->station_id] : null; $handler_sensor = array(); if (!is_null($sensors)) { $sensorPairs = array(); foreach ($sensors as $sensor) { $sensorPairs[$sensor->handler->handler_id_code][] = array('station_id' => $station->station_id, 'sensor_id' => $sensor->station_sensor_id, 'last_logs' => isset($last2Messages[$station->station_id]) ? $last2Messages[$station->station_id] : array(), 'aws_single_group' => $sensor->handler->aws_single_group); } $handlersDefault = array(); SensorDBHandler::handlerWithFeature($handlersDefault, 'single'); $sensorList = SensorHandler::getFullSensorList(array($station->station_id), $handlersDefault); $sensorData = SensorData::getSensorData($last2Messages, $sensorList); foreach ($sensorPairs as $handler_id_code => $data) { $handler_obj = SensorHandler::create($handler_id_code); $res = $handler_obj->getInfoForAwsPanel($data, $sensorList, $sensorData, 'single'); if ($res) { foreach ($res as $value_sensors) { foreach ($value_sensors as $value_sensor_data) { if ($handler_id_code === 'WindDirection') { if ($session['single_aws']['chosen_wind_direction'] == 2) { $chosen_direction = '10minute_average'; } else { if ($session['single_aws']['chosen_wind_direction'] == 1) { $chosen_direction = '2minute_average'; } else { $chosen_direction = 'last'; } } $value_sensor_data['chosen_wind_direction'] = $chosen_direction; $radians = ($value_sensor_data[$chosen_direction] - 90) / 180 * M_PI; //$value_sensor_data['chosen_wind_coordinates']['x'] = round(86.5+ cos($radians)*70); //$value_sensor_data['chosen_wind_coordinates']['y'] = round(86.5+ sin($radians)*70); $value_sensor_data['chosen_wind_coordinates']['x'] = round(83 + cos($radians) * 70); $value_sensor_data['chosen_wind_coordinates']['y'] = round(83 + sin($radians) * 70); } $value_sensor_data['handler_id_code'] = $handler_id_code; $handler_sensor[$data[0]['aws_single_group']][$handler_id_code][] = $value_sensor_data; } } } } } $handlers = StationCalculation::getStationCalculationHandlers(array($station->station_id)); $calculation = array(); if (array_key_exists($station->station_id, $handlers)) { foreach ($handlers[$station->station_id] as $value) { $handler = CalculationHandler::create($value->handler->handler_id_code); $used_sensors = $handler->getUsedSensors($station->station_id); if ($used_sensors) { foreach ($handler_sensor as $group => &$v2) { foreach ($v2 as $handler_id_code => &$value_sensor_data) { if ($value->handler->handler_id_code == 'DewPoint' && in_array($handler_id_code, array('Temperature', 'TemperatureSoil', 'TemperatureWater')) || $value->handler->handler_id_code == 'PressureSeaLevel' && in_array($handler_id_code, array('Pressure'))) { foreach ($value_sensor_data as $k4 => &$v4) { if (in_array($v4['sensor_id_code'], $used_sensors)) { $calculation[$group][$value->handler->handler_id_code] = 1; $v4[$value->handler->handler_id_code] = $handler->getCalculatedValue($station->station_id, $last2Messages[$station->station_id]); } } } } } } } } $session['single_aws'] = array('station_id' => $station->station_id, 'chosen_wind_direction' => $session['single_aws']['chosen_wind_direction']); } $speciReport = null; if (count($last2Messages[$station->station_id]) > 0) { $criteria = new CDbCriteria(); $criteria->with = array('ScheduleReportToStation.schedule_report'); $criteria->compare('report_type', 'speci'); $criteria->compare('ScheduleReportToStation.station_id', $station->station_id); // $criteria->compare('is_last', 1); $criteria->compare('listener_log_id', $last2Messages[$station->station_id][0]->log_id); // $criteria->order = 't.updated desc'; $criteria->order = 't.created desc'; $criteria->limit = 1; $speciReport = ScheduleReportProcessed::model()->find($criteria); if (is_null($speciReport)) { $reportRecord = ScheduleReport::model()->findByAttributes(array('report_type' => 'speci', 'station_id' => $station->station_id)); if (!is_null($reportRecord)) { $speciReport = new ScheduleReportProcessed(); $speciReport->schedule_id = $reportRecord->schedule_id; $speciReport->listener_log_id = $last2Messages[$station->station_id][0]->log_id; $speciReport->is_processed = 0; $speciReport->check_period_start = $last2Messages[$station->station_id][0]->measuring_timestamp; $speciReport->check_period_end = $last2Messages[$station->station_id][0]->measuring_timestamp; $speciReport->save(); } } } // echo "<pre>"; // print_r($handler_sensor); // echo "</pre>";exit; $template = 'aws_single'; $render_data = array('stations' => $stations, 'station' => $station, 'last_logs' => $last2Messages[$station->station_id], 'handler_sensor' => $handler_sensor, 'calculation' => $calculation, 'speciReport' => $speciReport); if (Yii::app()->request->isAjaxRequest) { $this->renderPartial($template, array('render_data' => $render_data)); } else { $this->render('autorefresh_aws_single', array('render_data' => $render_data, 'template' => $template)); } }
public function prepareList($station_id = 0) { if ($this->hasErrors()) { return ['prepared_header' => [], 'prepared_data' => []]; } $prepared_header = array(); $prepared_data = array(); $sensor_feature_code = $this->getSensorFeatureCode(); $handler_code = array_keys($this->getSelectedGroupSensorFeatureCode()); $search_features = array(); $search_calcs = array(); foreach ($sensor_feature_code as $key) { if (in_array($key, array_keys($this->calc_handlers))) { $search_calcs[] = $this->calc_handlers[$key]; } else { $search_features[] = $key; } } if (count($this->station_id) > 1) { $sql_part = "`t2`.`station_id` IN (" . implode(',', $this->station_id) . ") "; } else { $sql_part = "`t2`.`station_id` = '" . $this->station_id[0] . "' "; } // 1.a) GET FEATURES if (count($search_features) > 0) { $sql = "SELECT `t1`.`sensor_feature_id`,\n `t1`.`feature_code`,\n `t1`.`sensor_id`,\n `t3`.`station_id_code`,\n `t2`.`sensor_id_code`,\n `t2`.`station_id`,\n `t4`.`handler_id_code`,\n `t3`.`magnetic_north_offset`\n FROM `" . StationSensorFeature::model()->tableName() . "` `t1`\n LEFT JOIN `" . StationSensor::model()->tableName() . "` `t2` ON `t1`.`sensor_id` = `t2`.`station_sensor_id`\n LEFT JOIN `" . SensorDBHandler::model()->tableName() . "` `t4` ON `t4`.`handler_id` = `t2`.`handler_id`\n LEFT JOIN `" . Station::model()->tableName() . "` `t3` ON `t3`.`station_id` = `t2`.`station_id`\n WHERE " . $sql_part . " AND `t1`.`feature_code` IN ('" . implode("','", $search_features) . "') AND `t4`.`handler_id_code` IN ('" . implode("','", $handler_code) . "')\n ORDER BY `t1`.`feature_code`, `t3`.`station_id_code`, `t2`.`sensor_id_code`"; $found_sensors = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll(); $total_found_sensors = count($found_sensors); } // 1.b) GET CALCS if (count($search_calcs) > 0) { $sql = "SELECT `t1`.`calculation_id`,\n `t1`.`handler_id`,\n `t2`.`station_id_code`,\n `t2`.`station_id`,\n IF(`t1`.`handler_id` = 1, 'DP', 'MSL') AS `sensor_id_code`,\n IF(`t1`.`handler_id` = 1, 'Dew Point', 'Pressure MSL') AS `feature_code`,\n IF(`t1`.`handler_id` = 1, 'DewPoint', 'PressureSeaLevel') AS `handler_id_code`\n FROM `" . StationCalculation::model()->tableName() . "` `t1`\n LEFT JOIN `" . Station::model()->tableName() . "` `t2` ON `t2`.`station_id` = `t1`.`station_id`\n WHERE " . $sql_part . " AND `t1`.`handler_id` IN (" . implode(',', $search_calcs) . ")\n ORDER BY `t1`.`handler_id`, `t2`.`station_id_code`"; $found_calcs = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll(); $total_found_calcs = count($found_calcs); } $start_datetime = strtotime($this->date_from . ' ' . $this->time_from); $end_datetime = strtotime($this->date_to . ' ' . $this->time_to); $features_set = array(); // 2.a) PREPARE HEADER if (is_array($found_sensors) && $total_found_sensors > 0) { $sensor_feature_ids = array(); for ($i = 0; $i < $total_found_sensors; $i++) { $key = $found_sensors[$i]['handler_id_code'] . $found_sensors[$i]['feature_code']; if (!isset($prepared_header[$key])) { $prepared_header[$key] = array('sensor_feature_code' => $found_sensors[$i]['feature_code'], 'handler_id_code' => $found_sensors[$i]['handler_id_code'], 'sensors' => array(), 'station_sensors' => array()); } $sensor_feature_ids[] = $found_sensors[$i]['sensor_feature_id']; $prepared_header[$key]['sensors'][] = array('station_id' => $found_sensors[$i]['station_id'], 'sensor_id_code' => $found_sensors[$i]['sensor_id_code']); if (isset($prepared_header[$key]['station_sensors'][$found_sensors[$i]['station_id']])) { $prepared_header[$key]['station_sensors'][$found_sensors[$i]['station_id']]++; } else { $prepared_header[$key]['station_sensors'][$found_sensors[$i]['station_id']] = 1; } $features_set[$found_sensors[$i]['sensor_feature_id']] = array('station_id' => $found_sensors[$i]['station_id'], 'station_id_code' => $found_sensors[$i]['station_id_code'], 'value' => '-', 'sensor_id' => $found_sensors[$i]['sensor_id'], 'sensor_id_code' => $found_sensors[$i]['sensor_id_code'], 'sensor_feature_code' => $found_sensors[$i]['feature_code'], 'handler_id_code' => $found_sensors[$i]['handler_id_code'], 'magnetic_north_offset' => $found_sensors[$i]['magnetic_north_offset']); } } // 2.b) PREPARE HEADER if (count($search_calcs) > 0) { $sql = "SELECT `t1`.`calculation_id`,\n `t1`.`handler_id`,\n `t2`.`station_id_code`,\n `t2`.`station_id`,\n IF(`t1`.`handler_id` = 1, 'DP', 'MSL') AS `sensor_id_code`,\n IF(`t1`.`handler_id` = 1, 'Dew Point', 'Pressure MSL') AS `feature_code`,\n IF(`t1`.`handler_id` = 1, 'DewPoint', 'PressureSeaLevel') AS `handler_id_code`\n FROM `" . StationCalculation::model()->tableName() . "` `t1`\n LEFT JOIN `" . Station::model()->tableName() . "` `t2` ON `t2`.`station_id` = `t1`.`station_id`\n WHERE " . $sql_part . " AND `t1`.`handler_id` IN (" . implode(',', $search_calcs) . ")\n ORDER BY `t1`.`handler_id`, `t2`.`station_id`"; $found_calcs = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll(); if (is_array($found_calcs) && count($found_calcs) > 0) { $calculation_ids = array(); for ($i = 0; $i < count($found_calcs); $i++) { $key = 'calc_' . $found_calcs[$i]['handler_id']; if (!isset($prepared_header[$key])) { $prepared_header[$key] = array('sensor_feature_code' => $key, 'sensors' => array(), 'station_sensors' => array()); } $calculation_ids[] = $found_calcs[$i]['calculation_id']; $prepared_header[$key]['sensors'][] = array('station_id' => $found_calcs[$i]['station_id'], 'sensor_id_code' => $found_calcs[$i]['sensor_id_code']); if (isset($prepared_header[$key]['station_sensors'][$found_calcs[$i]['station_id']])) { $prepared_header[$key]['station_sensors'][$found_calcs[$i]['station_id']]++; } else { $prepared_header[$key]['station_sensors'][$found_calcs[$i]['station_id']] = 1; } $features_set['calc_' . $found_calcs[$i]['calculation_id']] = array('station_id' => $found_calcs[$i]['station_id'], 'station_id_code' => $found_calcs[$i]['station_id_code'], 'value' => '-', 'calculation_id' => $found_calcs[$i]['calculation_id'], 'sensor_id_code' => $found_calcs[$i]['sensor_id_code'], 'sensor_feature_code' => $found_calcs[$i]['feature_code'], 'handler_id_code' => $found_calcs[$i]['handler_id_code']); } } } // 3.a) PREPARE DATA if (is_array($found_sensors) && $total_found_sensors) { $qb = new CDbCriteria(); $qb->select = ['sensor_data_id', 'station_id', 'sensor_id', 'sensor_feature_id', 'sensor_feature_normalized_value', 'is_m', 'measuring_timestamp']; $qb->addInCondition('sensor_feature_id', $sensor_feature_ids); $qb->addBetweenCondition('measuring_timestamp', date('Y-m-d H:i:s', $start_datetime), date('Y-m-d H:i:s', $end_datetime)); $qb->order = 'measuring_timestamp DESC'; $found_values = SensorData::model()->long()->findAll($qb); $total_found_values = count($found_values); if (is_array($found_values) && $total_found_values > 0) { if ($this->accumulation_period == 0) { for ($j = 0; $j < $total_found_values; $j++) { $f_id = $found_values[$j]['sensor_feature_id']; $f_time = $found_values[$j]['measuring_timestamp']; $f_code = $features_set[$f_id]['sensor_feature_code']; $magnetic_north_offset = $features_set[$f_id]['magnetic_north_offset']; $st_id = $found_values[$j]['station_id']; if (!isset($prepared_data[$f_time])) { $prepared_data[$f_time] = array(); $prepared_data[$f_time]['stations'] = array(); } if (!isset($prepared_data[$f_time]['data'])) { $prepared_data[$f_time]['data'] = $features_set; } $handler_obj = SensorHandler::create($features_set[$f_id]['handler_id_code']); if ($found_values[$j]['is_m'] == 1) { $prepared_data[$f_time]['data'][$f_id]['value'] = '-'; } else { $found_values[$j]['sensor_feature_normalized_value'] = $handler_obj->applyOffset($found_values[$j]['sensor_feature_normalized_value'], $magnetic_north_offset); $prepared_data[$f_time]['data'][$f_id]['value'] = $handler_obj->formatValue($found_values[$j]['sensor_feature_normalized_value'], $f_code); } if (!in_array($st_id, $prepared_data[$f_time]['stations'])) { $prepared_data[$f_time]['stations'][] = $st_id; } } } else { for ($j = 0; $j < $total_found_values; $j++) { $f_id = $found_values[$j]['sensor_feature_id']; $f_time = $found_values[$j]['measuring_timestamp']; $f_code = $features_set[$f_id]['sensor_feature_code']; $magnetic_north_offset = $features_set[$f_id]['magnetic_north_offset']; $st_id = $found_values[$j]['station_id']; $period = $start_datetime + (intval((strtotime($f_time) - $start_datetime) / ($this->accumulation_period * 60)) + 1) * $this->accumulation_period * 60; $period = $period > $end_datetime ? $end_datetime : $period; $period = date('Y-m-d H:i:s', $period); if (!isset($prepared_data[$period])) { $prepared_data[$period] = array(); $prepared_data[$period]['stations'] = array(); } if (!isset($prepared_data[$period]['data'])) { $prepared_data[$period]['data'] = $features_set; } $handler_obj = SensorHandler::create($features_set[$f_id]['handler_id_code']); if ($found_values[$j]['is_m'] == 1) { $prepared_data[$period]['data'][$f_id]['value'] = '-'; } else { $found_values[$j]['sensor_feature_normalized_value'] = $handler_obj->applyOffset($found_values[$j]['sensor_feature_normalized_value'], $magnetic_north_offset); $prepared_data[$period]['data'][$f_id]['value'] = ($prepared_data[$period]['data'][$f_id]['value'] ? $prepared_data[$period]['data'][$f_id]['value'] : 0) + $handler_obj->formatValue($found_values[$j]['sensor_feature_normalized_value'], $f_code); } if (!in_array($st_id, $prepared_data[$period]['stations'])) { $prepared_data[$period]['stations'][] = $st_id; } } } } } // 3.b) PREPARE DATA if (is_array($found_calcs) && $total_found_calcs > 0) { $sql = "SELECT `t2`.`station_id`,\n `t1`.`calculation_id`,\n `t1`.`value`,\n `t2`.`measuring_timestamp`\n FROM `" . StationCalculationData::model()->tableName() . "` `t1`\n LEFT JOIN `" . ListenerLog::model()->tableName() . "` `t2` ON `t2`.`log_id` = `t1`.`listener_log_id`\n WHERE `t1`.`calculation_id` IN (" . implode(',', $calculation_ids) . ")\n AND `t2`.`measuring_timestamp` >= '" . date('Y-m-d H:i:s', $start_datetime) . "'\n AND `t2`.`measuring_timestamp` <= '" . date('Y-m-d H:i:s', $end_datetime) . "'\n ORDER BY `t2`.`measuring_timestamp` DESC"; $found_values = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll(); $total_found_values = count($found_values); if (is_array($found_values) && $total_found_values) { for ($j = 0; $j < $total_found_values; $j++) { $f_id = 'calc_' . $found_values[$j]['calculation_id']; $f_time = $found_values[$j]['measuring_timestamp']; $st_id = $found_values[$j]['station_id']; if (!$prepared_data[$f_time]) { $prepared_data[$f_time] = array(); $prepared_data[$f_time]['stations'] = array(); } if (!$prepared_data[$f_time]['data']) { $prepared_data[$f_time]['data'] = $features_set; } $prepared_data[$f_time]['data'][$f_id]['value'] = CalculationHandler::formatValue($found_values[$j]['value']); $prepared_data[$f_time]['data'][$f_id]['station_id'] = $st_id; if (!in_array($st_id, $prepared_data[$f_time]['stations'])) { $prepared_data[$f_time]['stations'][] = $st_id; } } } } //need sort krsort($prepared_data); //print_r(array( // 'prepared_header' => $prepared_header, // 'prepared_data' => $prepared_data, // ));exit; return array('prepared_header' => $prepared_header, 'prepared_data' => $prepared_data); }
/** * Prepare calculation list * * @param $handler_id int * @return array */ private function prepareCalculationList($handler_id) { $qb = new CDbCriteria(); $qb->with = ['Station' => ['select' => array('Station.station_id_code', 'Station.color'), 'condition' => 'Station.station_id IN (' . implode(',', $this->station_id) . ')']]; $qb->condition = "t.handler_id = {$handler_id}"; $calculations = StationCalculation::model()->long()->findAll($qb); if ($calculations) { $i = 0; foreach ($calculations as $calculation) { $colorWorker = new Color($calculation->Station->color); $series_names[$i]['name'] = $calculation->Station->station_id_code . ', ' . $calculation->handler->display_name; $series_names[$i]['params']['color'] = '#' . $colorWorker->getHex(); $i++; } $start_datetime = strtotime($this->date_from . ' ' . $this->time_from); $end_datetime = strtotime($this->date_to . ' ' . $this->time_to); $qb = new CDbCriteria(); $qb->with = ['ListenerLog' => ['select' => 'ListenerLog.measuring_timestamp', 'condition' => "ListenerLog.measuring_timestamp BETWEEN '" . date('Y-m-d H:i:s', $start_datetime) . "' AND '" . date('Y-m-d H:i:s', $end_datetime) . "'"]]; $qb->select = 't.calculation_id, t.value'; $qb->order = 'ListenerLog.measuring_timestamp ASC'; $qb->condition = 't.calculation_id = ?'; foreach ($calculations as $calculation) { $qb->params = [$calculation->calculation_id]; $found_data = StationCalculationData::model()->long()->findAll($qb); $tmp = array(); foreach ($found_data as $data) { $tmp[] = ['x' => strtotime($data->ListenerLog->measuring_timestamp) * 1000, 'y' => floatval($data->value)]; } $series_data[] = $tmp; } } return ['series_names' => isset($series_names) ? $series_names : [], 'series_data' => isset($series_data) ? $series_data : []]; }
public function actionCalculationDelete() { $calculation = StationCalculation::model()->findByPk(isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null); if (!is_null($calculation)) { $url = $this->createUrl('admin/Sensors', array('station_id' => $calculation->station_id)); $calculation->delete(); It::memStatus('admin_station_calculation_deleted'); $this->redirect($url); } $this->redirect($this->createUrl('admin/Stations')); }
public function m_0_4_15() { @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 fields types!"); $sql = "ALTER TABLE `station_calculation_data` CHANGE `value` `value` DECIMAL( 15, 4 ) NOT NULL"; Yii::app()->db->createCommand($sql)->query(); Yii::app()->db->createCommand("COMMIT")->query(); $this->flushNotification("<br/><br/>...Done!"); $this->flushNotification("<br/><br/>...Update fields types!"); $sql = "ALTER TABLE `sensor_data` CHANGE `sensor_feature_value` `sensor_feature_value` DECIMAL( 15, 4 ) NOT NULL"; Yii::app()->db->createCommand($sql)->query(); Yii::app()->db->createCommand("COMMIT")->query(); $this->flushNotification("<br/><br/>...Done!"); $this->flushNotification("<br/><br/>...Update fields types!"); $sql = "ALTER TABLE `sensor_data` CHANGE `sensor_feature_normalized_value` `sensor_feature_normalized_value` DECIMAL( 15, 4 ) NOT NULL"; Yii::app()->db->createCommand($sql)->query(); Yii::app()->db->createCommand("COMMIT")->query(); $this->flushNotification("<br/><br/>...Done!"); $sql = "SELECT t2.sensor_data_id, `t1`.`calculation_data_id`, `t1`.`listener_log_id`, `t1`.`value`\n FROM `" . StationCalculationData::model()->tableName() . "` `t1`\n LEFT JOIN `" . SensorData::model()->tableName() . "` `t2` ON `t2`.`listener_log_id` = `t1`.`listener_log_id`\n where t2.sensor_data_id is null \n "; $res = Yii::app()->db->createCommand($sql)->queryAll(); if ($res) { $total = count($res); for ($i = 0; $i < $total; $i++) { if (!$res[$i]['sensor_data_id']) { StationCalculationData::model()->deleteByPk($res[$i]['calculation_data_id']); } } } $sql = "SELECT `t1`.`station_id`, t1.handler_id\n FROM `" . StationCalculation::model()->tableName() . "` `t1`\n LEFT JOIN `" . Station::model()->tableName() . "` `t2` ON t2.station_id = t1.station_id"; $res_stations = Yii::app()->db->createCommand($sql)->queryAll(); $count = count($res_stations); $stations = array(); if (is_array($res_stations) && $count) { $station_ids = array(); for ($i = 0; $i < $count; $i++) { if (!$stations[$res_stations[$i]['station_id']]) { $stations[$res_stations[$i]['station_id']]['station_object'] = Station::model()->findByPk($res_stations[$i]['station_id']); $stations[$res_stations[$i]['station_id']]['last_logs'] = ListenerLog::getLastMessages($res_stations[$i]['station_id']); } $handler_id = $res_stations[$i]['handler_id'] == 1 ? 'DewPoint' : 'PressureSeaLevel'; $handler = CalculationHandler::create($handler_id); if ($stations[$res_stations[$i]['station_id']]['last_logs'][0]['log_id']) { $handler->calculate($stations[$res_stations[$i]['station_id']]['station_object'], $stations[$res_stations[$i]['station_id']]['last_logs'][0]['log_id']); } if ($stations[$res_stations[$i]['station_id']]['last_logs'][1]['log_id']) { $handler->calculate($stations[$res_stations[$i]['station_id']]['station_object'], $stations[$res_stations[$i]['station_id']]['last_logs'][1]['log_id']); } } } It::memStatus('update__success'); $this->flushNotification('<script type="text/javascript"> setTimeout(function(){document.location.href="' . Yii::app()->controller->createUrl('update/index') . '"}, 5000)</script>'); }
/** * Update general tables in backup database. * Actual Data in these tables are required to support integrity of database and store sensors values * @return string */ private function updateMainStationInformation() { $tables = array(Station::model()->tableName(), Settings::model()->tableName(), StationSensor::model()->tableName(), StationSensorFeature::model()->tableName(), StationCalculation::model()->tableName(), StationCalculationVariable::model()->tableName()); $result_sql = array(); foreach ($tables as $table) { $sql = "SELECT * FROM `" . $table . "`"; $res = Yii::app()->db->createCommand($sql)->queryAll(); $total = count($res); It::debug("updateMainStationInformation: Table = " . $table . ", TOTAL = " . $total, 'backup_database'); if ($res) { $fields = array(); foreach ($res[0] as $key2 => $value2) { $fields[] = $key2; } $sql_header = "INSERT IGNORE INTO `" . $table . "` (`" . implode('`,`', $fields) . "`) VALUES "; $res_sql = $sql_header; foreach ($res as $key => $value) { $res_sql .= "('" . implode("','", $value) . "')"; if ($key + 1 < $total) { $res_sql .= ", "; } } $result_sql[] = $res_sql; } } It::debug("updateMainStationInformation: DONE", 'backup_database'); return $result_sql; }