public function beforeDelete() { $features = StationSensorFeature::model()->selectDb($this->getUseLong())->findAll('sensor_id = :sensor_id', array(':sensor_id' => $this->station_sensor_id)); foreach ($features as $feature) { SensorData::model()->selectDb($this->getUseLong())->deleteAll('sensor_feature_id = :feature_id', array(':feature_id' => $feature->sensor_feature_id)); } return parent::beforeDelete(); }
public function beforeDelete() { $cr_sd = new CDbCriteria(); $cr_sd->addCondition('station_id = ' . $this->station_id); $cr_cd = new CDbCriteria(); $cr_cd->with = ['calculation' => ['select' => false, 'condition' => 'calculation.station_id = ' . $this->station_id]]; $cr_slt = new CDbCriteria(); $cr_slt->with = ['sensor' => ['select' => false, 'condition' => 'sensor.station_id = ' . $this->station_id]]; if ($this->getUseLong()) { SensorData::model()->long()->deleteAll($cr_sd); StationCalculationData::model()->long()->deleteAll($cr_cd); SeaLevelTrend::model()->long()->deleteAll($cr_slt); } else { SensorData::model()->deleteAll($cr_sd); StationCalculationData::model()->deleteAll($cr_cd); SeaLevelTrend::model()->long()->deleteAll($cr_slt); } return parent::beforeDelete(); }
public static function addSensorsData(&$sensorData, $logsId, $handlers) { /* structure sensorData array: $sensorData = ['handlers'] [handler_id] ['code'] [sensor_id_code] ['stations'] [station_id] ['view'] ['features'] [feature_code] ['info'] = $feature - AR ['data'] [sensor_data_id] = $data - AR */ $featureAllow = array(); foreach ($handlers as $handler) { foreach ($handler->features as $feature) { $featureAllow[] = $handler->handler_id . $feature->feature_code; } } $criteria = new CDbCriteria(); $criteria->with = array('feature.sensor', 'feature.metric'); $criteria->compare('t.listener_log_id', $logsId); $criteria->compare('CONCAT(sensor.handler_id,feature.feature_code)', $featureAllow); $criteria->order = "sensor.sensor_id_code asc, t.measuring_timestamp desc"; $result = SensorData::model()->findAll($criteria); foreach ($result as $data) { $arr =& $sensorData['handlers'][$data->feature->sensor->handler_id]['code']; $count = $handlers[$data->feature->sensor->handler_id]->aws_panel_show - count($arr); if ($count > 0 or array_key_exists($data->feature->sensor->sensor_id_code, $arr)) { $arr[$data->feature->sensor->sensor_id_code]['stations'][$data->feature->sensor->station_id]['features'][$data->feature->feature_code]['info'] = $data->feature; $arr[$data->feature->sensor->sensor_id_code]['stations'][$data->feature->sensor->station_id]['features'][$data->feature->feature_code]['data'][$data->sensor_data_id] = $data; } } }
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'); }
public function repairTrend($params) { $measuring_timestamp1 = date('Y-m-d H:i:s', $params['incoming_measuring_timestamp']); $measuring_timestamp2 = date('Y-m-d H:i:s', $params['incoming_measuring_timestamp'] - $params['feature_treshold_period'] * 60); $measuring_timestamp3 = date('Y-m-d H:i:s', $params['incoming_measuring_timestamp'] - $params['feature_treshold_period'] * 2 * 60); $measuring_timestamp4 = date('Y-m-d H:i:s', $params['incoming_measuring_timestamp'] - 18000); $criteria = new CDbCriteria(); $criteria->compare('log_id', $params['log_id']); $criteria->compare('sensor_id', $params['sensor_id']); $sea_trend = SeaLevelTrend::model()->find($criteria); if (!$sea_trend) { $sea_trend = new SeaLevelTrend(); $sea_trend->log_id = $params['log_id']; $sea_trend->sensor_id = $params['sensor_id']; } $sea_trend->measuring_timestamp = $measuring_timestamp1; $minutes = array('00'); for ($i = 1; $i <= 60; $i++) { if ($i % $params['feature_treshold_period'] == 0) { $minutes[] = $i == 60 ? '00' : str_pad($i, 2, '0', STR_PAD_LEFT); $minutes[] = $i == 60 ? '01' : str_pad($i + 1, 2, '0', STR_PAD_LEFT); } } $criteria = new CDbCriteria(); $criteria->compare('sensor_id', $params['sensor_id']); $criteria->compare('measuring_timestamp', '<' . $measuring_timestamp1); $criteria->order = 'measuring_timestamp desc'; $criteria->limit = '1'; $last_trend = SeaLevelTrend::model()->find($criteria); if (!in_array(date('i', $params['incoming_measuring_timestamp']), $minutes)) { if (is_null($last_trend)) { $sea_trend->trend = 'unknown'; $sea_trend->is_significant = 0; $sea_trend->last_high = 0; $sea_trend->last_low = 0; $sea_trend->last_high_timestamp = '0000-00-00 00:00:00'; $sea_trend->last_low_timestamp = '0000-00-00 00:00:00'; } else { $sea_trend->trend = $last_trend->trend; $sea_trend->is_significant = $last_trend->is_significant; $sea_trend->last_high = $last_trend->last_high; $sea_trend->last_low = $last_trend->last_low; $sea_trend->last_high_timestamp = $last_trend->last_high_timestamp; $sea_trend->last_low_timestamp = $last_trend->last_low_timestamp; } $sea_trend->save(); return true; } $sql = "SELECT SUM(CAST(`t1`.`sensor_feature_value` AS DECIMAL(15,4))) AS `sum`, COUNT(`t1`.`sensor_feature_id`) AS `cnt`\n FROM `" . SensorData::model()->tableName() . "` `t1`\n WHERE `t1`.`sensor_feature_id` = ?\n AND `t1`.`measuring_timestamp` > ? \n AND `t1`.`measuring_timestamp` <= ?\n AND `t1`.`is_m` = '0'\n GROUP BY `t1`.`sensor_feature_id`"; $p = array($params['sensor_feature_id'], $measuring_timestamp2, $measuring_timestamp1); $res_last = Yii::app()->db->createCommand($sql)->queryRow(true, $p); $p = array($params['sensor_feature_id'], $measuring_timestamp3, $measuring_timestamp2); $res_prev = Yii::app()->db->createCommand($sql)->queryRow(true, $p); if (!$res_prev) { if (is_null($last_trend)) { $sea_trend->is_significant = 0; $sea_trend->last_high = 0; $sea_trend->last_low = 0; $sea_trend->last_high_timestamp = '0000-00-00 00:00:00'; $sea_trend->last_low_timestamp = '0000-00-00 00:00:00'; } else { $sea_trend->trend = $last_trend->trend; $sea_trend->is_significant = $last_trend->is_significant; $sea_trend->last_high = $last_trend->last_high; $sea_trend->last_low = $last_trend->last_low; $sea_trend->last_high_timestamp = $last_trend->last_high_timestamp; $sea_trend->last_low_timestamp = $last_trend->last_low_timestamp; } } else { $t0 = $res_last['sum'] / $res_last['cnt']; $t1 = $res_prev['sum'] / $res_prev['cnt']; $difference = $t0 - $t1; if ($difference > 0) { $sea_trend->trend = 'up'; } else { if ($difference < 0) { $sea_trend->trend = 'down'; } else { $sea_trend->trend = $last_trend->trend; } } $sea_trend->is_significant = abs($difference) >= $params['feature_treshold'] ? 1 : 0; $sea_trend->last_high = $last_trend->last_high; $sea_trend->last_low = $last_trend->last_low; $sea_trend->last_high_timestamp = $last_trend->last_high_timestamp; $sea_trend->last_low_timestamp = $last_trend->last_low_timestamp; if ($sea_trend->is_significant && ($last_trend->trend != $sea_trend->trend || !$last_trend->is_significant)) { if ($sea_trend->trend === 'down') { $sql_groupped_table = "SELECT `sensor_feature_id`, MAX(CAST(`sensor_feature_value` AS DECIMAL(15,4))) AS `MaxValue` \n FROM `" . SensorData::model()->tableName() . "` \n WHERE `sensor_feature_id` = ?\n AND `measuring_timestamp` > ? \n AND `measuring_timestamp` <= ?\n AND `is_m` = '0'"; $sql = "SELECT CAST(`tt`.`sensor_feature_value` AS DECIMAL(15,4)) as `sensor_feature_value`, `tt`.measuring_timestamp\n FROM `" . SensorData::model()->tableName() . "` `tt`\n INNER JOIN ( {$sql_groupped_table} ) `gr` ON `tt`.`sensor_feature_id` = `gr`.`sensor_feature_id` AND `tt`.`sensor_feature_value` = `gr`.`MaxValue`\n WHERE `tt`.`measuring_timestamp` > ? AND `tt`.`measuring_timestamp` <= ?"; $p = array($params['sensor_feature_id'], $measuring_timestamp4, $measuring_timestamp1, $measuring_timestamp4, $measuring_timestamp1); $extr = Yii::app()->db->createCommand($sql)->queryRow(true, $p); if ($extr) { $sea_trend->last_high = $extr['sensor_feature_value']; $sea_trend->last_high_timestamp = $extr['measuring_timestamp']; } } else { $sql_groupped_table = "SELECT `sensor_feature_id`, MIN(CAST(`sensor_feature_value` AS DECIMAL(15,4))) AS `MinValue` \n FROM `" . SensorData::model()->tableName() . "` \n WHERE `sensor_feature_id` = ?\n AND `measuring_timestamp` > ? \n AND `measuring_timestamp` <= ?\n AND `is_m` = '0'"; $sql = "SELECT CAST(`tt`.`sensor_feature_value` AS DECIMAL(15,4)) as `sensor_feature_value`, `tt`.measuring_timestamp\n FROM `" . SensorData::model()->tableName() . "` `tt`\n INNER JOIN ( {$sql_groupped_table} ) `gr` ON `tt`.`sensor_feature_id` = `gr`.`sensor_feature_id` AND `tt`.`sensor_feature_value` = `gr`.`MinValue`\n WHERE `tt`.`measuring_timestamp` > ? AND `tt`.`measuring_timestamp` <= ?"; $p = array($params['sensor_feature_id'], $measuring_timestamp4, $measuring_timestamp1, $measuring_timestamp4, $measuring_timestamp1); $extr = Yii::app()->db->createCommand($sql)->queryRow(true, $p); if ($extr) { $sea_trend->last_low = $extr['sensor_feature_value']; $sea_trend->last_low_timestamp = $extr['measuring_timestamp']; } } } } $sea_trend->save(); return true; }
public function prepareFormulaParams() { $sql = "SELECT `t4`.`sensor_data_id`, `t1`.`sensor_feature_id`, `t4`.`metric_id`, `t4`.`sensor_feature_value`, `t5`.`code` AS `metric_code`, `t4`.`is_m`\n FROM `" . StationCalculationVariable::model()->tableName() . "` `t1`\n JOIN `" . StationSensorFeature::model()->tableName() . "` `t3` ON `t3`.`sensor_feature_id` = `t1`.`sensor_feature_id`\n JOIN `" . SensorData::model()->tableName() . "` `t4` ON (`t4`.`sensor_feature_id` = `t1`.`sensor_feature_id` AND `t4`.`listener_log_id` = ? AND `t4`.`is_m` = '0')\n JOIN `" . RefbookMetric::model()->tableName() . "` `t5` ON `t5`.`metric_id` = `t4`.`metric_id`\n WHERE `t1`.`calculation_id` = ? AND `t1`.`variable_name` = ?"; $measurements = $this->getMeasurements(); if ($measurements) { foreach ($measurements as $key => $value) { $res = Yii::app()->db->createCommand($sql)->queryRow(true, array($this->log_id, $this->calculation_details['calculation_id'], $value['variable_name'])); if ($res) { $this->formula_params[$value['variable_name']] = It::convertMetric($res['sensor_feature_value'], $res['metric_code'], $value['metric']); } } } return true; }
public function checkPeriod($measuring_timestamp, $sensor_feature_id, $station_id, $key) { // todo $station = new Station(); $stationData = $station->findbypk($station_id); if ($stationData['timezone_id']) { TimezoneWork::set($stationData['timezone_id']); } $last = $this->prepared_pairs[$this->checkPeriodArray[$key]]; $criteria = new CDbCriteria(); $criteria->select = 'measuring_timestamp, sensor_feature_value, sensor_feature_normalized_value, period, measuring_timestamp, sensor_feature_id, station_id'; $criteria->addCondition('measuring_timestamp < "' . $measuring_timestamp . '"'); $criteria->addCondition('sensor_feature_id = "' . $sensor_feature_id . '"'); $criteria->addCondition('station_id = "' . $station_id . '"'); $criteria->order = 'measuring_timestamp DESC'; $criteria->limit = 1; $data = SensorData::model()->find($criteria); if (!is_null($data) and $data != false) { $value_norm = $last['normilized_value'] - $data['sensor_feature_normalized_value']; $getDate = getdate(strtotime($measuring_timestamp)); $getDatePrev = getdate(strtotime($data['measuring_timestamp'])); if ($getDate['yday'] != $getDatePrev['yday'] && ($getDate['hours'] != 0 || $getDate['minutes'] != 0 || $getDate['seconds'] != 0) || $getDate['yday'] == $getDatePrev['yday'] && $getDatePrev['hours'] == 0 && $getDatePrev['minutes'] == 0 && $getDatePrev['seconds'] == 0) { $this->prepared_pairs[$key]['period'] = $getDate['hours'] * 60 + $getDate['minutes']; $this->prepared_pairs[$key]['value'] = $last['value']; $this->prepared_pairs[$key]['normilized_value'] = $last['normilized_value']; } else { $this->prepared_pairs[$key]['period'] = round((strtotime($measuring_timestamp) - strtotime($data['measuring_timestamp'])) / 60); $this->prepared_pairs[$key]['value'] = $last['value'] - $data['sensor_feature_value']; $this->prepared_pairs[$key]['normilized_value'] = $value_norm; if ($this->prepared_pairs[$key]['value'] < 0) { $this->prepared_pairs[$key]['period'] = $getDate['hours'] * 60 + $getDate['minutes']; $this->prepared_pairs[$key]['value'] = $last['value']; $this->prepared_pairs[$key]['normilized_value'] = $last['normilized_value']; } if ($last['value'] == 0 && $this->prepared_pairs[$key]['value'] < 0) { $this->prepared_pairs[$key]['value'] = 0; $this->prepared_pairs[$key]['normilized_value'] = 0.0; } } $this->_logger->log(__METHOD__ . ' getDate: ' . $measuring_timestamp); $this->_logger->log(__METHOD__ . ' sensor_feature_id: ' . $sensor_feature_id); $this->_logger->log(__METHOD__ . ' station_id: ' . $station_id); $this->_logger->log(__METHOD__ . ' getDatePrev: ' . $data['measuring_timestamp']); } return $this->prepared_pairs[$key]; }
protected function getTotalHour($sensor_feature_id, $measuring_timestamp, $timezone_id) { $hour_start = mktime(date('H', $measuring_timestamp), 0, 0, date('m', $measuring_timestamp), date('d', $measuring_timestamp), date('Y', $measuring_timestamp)); $sql = "SELECT SUM(CAST(`sensor_feature_value` AS DECIMAL(15,4))) AS `total_today`\n FROM `" . SensorData::model()->tableName() . "`\n WHERE `sensor_feature_id` = '" . $sensor_feature_id . "' \n AND `measuring_timestamp` <= '" . date('Y-m-d H:i:s', $measuring_timestamp) . "' \n AND `measuring_timestamp` > '" . date('Y-m-d H:i:s', $hour_start) . "'\n AND `is_m` = '0'"; $res = Yii::app()->db->createCommand($sql)->queryScalar(); return $res; }
public function prepareSensorsInfo($listener_log_id) { $this->_logger->log(__METHOD__); $this->_logger->log(__METHOD__ . " listener_log_id " . $listener_log_id); $this->_logger->log(__METHOD__ . " this->schedule_process_info->ScheduleReportToStation->realStation->station_id " . $this->schedule_process_info->ScheduleReportToStation->realStation->station_id); $sensors = array(); if (!is_null($this->schedule_process_info->ScheduleReportToStation->realStation->station_id)) { // get sensors $sql = "SELECT `t1`.`station_sensor_id`, `t1`.`sensor_id_code`, \r\n `t2`.`handler_id_code`, \r\n `t3`.`feature_code`, `t3`.`feature_constant_value`,\r\n `t3`.`sensor_feature_id`,\r\n `t4`.`code` AS `metric_code`, \r\n `t5`.`sensor_feature_value`, \r\n `t5`.`period` AS `sensor_feature_period`,\r\n `t6`.`code` AS `value_metric_code`,\r\n `t5`.`is_m`\r\n FROM `" . StationSensor::model()->tableName() . "` `t1`\r\n LEFT JOIN `" . SensorDBHandler::model()->tableName() . "` `t2` ON `t2`.`handler_id` = `t1`.`handler_id`\r\n LEFT JOIN `" . StationSensorFeature::model()->tableName() . "` `t3` ON (`t3`.`sensor_id` = `t1`.`station_sensor_id`)\r\n LEFT JOIN `" . RefbookMetric::model()->tableName() . "` `t4` ON `t4`.`metric_id` = `t3`.`metric_id`\r\n LEFT JOIN `" . SensorData::model()->tableName() . "` `t5` ON (`t5`.`sensor_feature_id` = `t3`.`sensor_feature_id` AND `t5`.`listener_log_id` = '" . $listener_log_id . "')\r\n LEFT JOIN `" . RefbookMetric::model()->tableName() . "` `t6` ON `t6`.`metric_id` = `t5`.`metric_id`\r\n WHERE `t1`.`station_id` = '" . $this->schedule_process_info->ScheduleReportToStation->realStation->station_id . "'\r\n ORDER BY `t1`.`sensor_id_code` ASC"; $sensors_info = Yii::app()->db->createCommand($sql)->queryAll(); if (!$sensors_info) { $this->errors[] = 'Station has no sensors'; return; } $sensors = array(); foreach ($sensors_info as $value) { if ($value['feature_code'] === 'height' && $value['handler_id_code'] === 'Pressure') { $sensors['pressure_height']['height'] = $value['feature_constant_value']; $sensors['pressure_height']['height_metric_code'] = $value['metric_code']; continue; } $value['metric_code'] = isset($value['value_metric_code']) ? $value['value_metric_code'] : $value['metric_code']; unset($value['value_metric_code']); if (!isset($sensors[$value['feature_code']])) { $sensors[$value['feature_code']] = $value; } } } return $sensors; }
private function prepareListenerLogDependantInserts($listener_log_ids) { $tables = array(ListenerLogProcessError::model()->tableName() => 'log_id', SensorData::model()->tableName() => 'listener_log_id', SensorDataMinute::model()->tableName() => 'listener_log_id', SeaLevelTrend::model()->tableName() => 'log_id', StationCalculationData::model()->tableName() => 'listener_log_id'); $result_sql = array(); foreach ($tables as $table => $log_id_field) { $sql = "SELECT * \n FROM `" . $table . "`\n WHERE `" . $log_id_field . "` IN (" . implode(',', $listener_log_ids) . ")"; $res = Yii::app()->db->createCommand($sql)->queryAll(); $total = count($res); 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; $cnt = 0; foreach ($res as $key => $value) { $res_sql .= "('" . implode("','", $value) . "')"; if ($key + 1 < $total) { $res_sql .= ", "; } $cnt++; } $result_sql[] = $res_sql; $this->addBackupLog("Prepared " . $cnt . " inserts for " . $table); } } return $result_sql; }
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 sensor list * * @param $sensor_feature_code * @param $handler_id_code * * @return array */ private function prepareSensorList($sensor_feature_code, $handler_id_code) { date_default_timezone_set('UTC'); $start_datetime = strtotime($this->date_from . ' ' . $this->time_from); $end_datetime = strtotime($this->date_to . ' ' . $this->time_to); $qb = new CDbCriteria(); $qb->with = ['sensor.station' => ['select' => array('station.station_id_code', 'station.color'), 'condition' => 'station.station_id IN (' . implode(',', $this->station_id) . ')'], 'sensor.handler' => ['select' => false, 'condition' => "handler.handler_id_code LIKE '" . $handler_id_code . "'"]]; $qb->select = ['t.feature_code', 't.feature_display_name']; $qb->addCondition("t.feature_code LIKE '{$sensor_feature_code}'"); $features = StationSensorFeature::model()->long()->findAll($qb); if ($features) { $i = 0; foreach ($features as $feature) { $colorWorker = new Color($feature->sensor->station->color); $series_names[$i] = array(); $series_names[$i]['name'] = $feature->sensor->station->station_id_code . ', ' . $feature->sensor->sensor_id_code . ', ' . $this->getGroupSensorsFeaturesList()[$handler_id_code]['sensor_features'][$feature->feature_code]; $series_names[$i]['params']['color'] = '#' . $colorWorker->getHex(); $i++; $colorWorker = new Color($feature->sensor->station->color); } $qb = new CDbCriteria(); $qb->select = 't.sensor_feature_normalized_value, t.sensor_feature_value, t.measuring_timestamp'; $qb->addCondition('t.sensor_feature_id = :sensor_feature_id'); $qb->addBetweenCondition('t.measuring_timestamp', date('Y-m-d H:i:s', $start_datetime), date('Y-m-d H:i:s', $end_datetime)); $qb->order = 't.measuring_timestamp ASC'; foreach ($features as $feature) { $qb->params[':sensor_feature_id'] = $feature->sensor_feature_id; $found_data = SensorData::model()->long()->findAll($qb); $tmp = array(); if ($this->accumulation_period != 0) { foreach ($found_data as $data) { $period = $start_datetime + (intval((strtotime($data->measuring_timestamp) - $start_datetime) / ($this->accumulation_period * 60)) + 1) * $this->accumulation_period * 60; $period = $period > $end_datetime ? $end_datetime : $period; $period *= 1000; $tmp[$period] = ['x' => $period, 'y' => $tmp[$period] ? $tmp[$period]['y'] : 0 + floatval($data->sensor_feature_value)]; } } else { foreach ($found_data as $data) { $period = strtotime($data->measuring_timestamp) * 1000; $tmp[$period] = ['x' => $period, 'y' => floatval($data->sensor_feature_value)]; } } $series_data[] = array_values($tmp); } } return ['series_names' => isset($series_names) ? $series_names : [], 'series_data' => isset($series_data) ? $series_data : []]; }
public function actionAwsFiltered() { $delete = isset($_REQUEST['delete']) ? intval($_REQUEST['delete']) : null; if ($delete) { $obj = SensorData::model()->findByPk($delete); if ($obj) { $obj->delete(); It::memStatus('admin_suspicious_value_was_deleted'); } } $session = new CHttpSession(); $session->open(); $sess_name = 'awsfiltered_filter1'; $fparams = $session[$sess_name]; $fparams['showdata'] = false; if ($fparams['redirect'] == true or isset($_GET['page'])) { $fparams['showdata'] = true; } $stations = Station::getList("'aws','awos'", false); $time_pattern = "/^(\\d{1,2}):(\\d{1,2})\$/"; if (!$fparams || isset($_POST['clear']) || isset($_POST['filter'])) { $cur_time = time(); $some_time_ago = mktime(0, 0, 0, date('m', $cur_time), date('d', $cur_time), date('Y', $cur_time)); $fparams = array('station_id' => $stations[0]['station_id'], 'date_from' => date('m/d/Y', $some_time_ago), 'date_to' => date('m/d/Y', $cur_time), 'time_from' => '00:00', 'time_to' => '23:59', 'order_field' => 'date', 'order_direction' => 'DESC'); } if (isset($_POST['filter'])) { $fparams['station_id'] = intval($_POST['search']['station_id']); $fparams['date_from'] = $_POST['search']['date_from']; $fparams['date_to'] = $_POST['search']['date_to']; if (preg_match($time_pattern, $_POST['search']['time_from'])) { $fparams['time_from'] = $_POST['search']['time_from']; } if (preg_match($time_pattern, $_POST['search']['time_to'])) { $fparams['time_to'] = $_POST['search']['time_to']; } } if (isset($_REQUEST['of']) && in_array($_REQUEST['of'], array('stationid', 'date', 'sensorid', 'value'))) { if ($_REQUEST['of'] == $fparams['order_field']) { $fparams['order_direction'] = $fparams['order_direction'] == 'ASC' ? 'DESC' : 'ASC'; } else { $fparams['order_direction'] = 'ASC'; } $fparams['order_field'] = $_REQUEST['of']; } $session[$sess_name] = $fparams; if ($_POST || $_REQUEST['of']) { $fparams['showdata'] = true; $fparams['redirect'] = true; $session[$sess_name] = $fparams; $this->redirect($this->createUrl('admin/awsfiltered') . (isset($_GET['page']) ? 'page/' . $_GET['page'] : '')); } else { $fparams['redirect'] = false; $session[$sess_name] = $fparams; } /*----------- filter prepare -------------*/ $sql_where = array(); $sql_where[] = "`t1`.`is_m` = '0'"; if ($fparams['date_from']) { $sql_where[] = "`t1`.`measuring_timestamp` >= '" . date('Y-m-d H:i:s', strtotime($fparams['date_from'] . ' ' . $fparams['time_from'])) . "'"; } if ($fparams['date_to']) { $sql_where[] = "`t1`.`measuring_timestamp` <= '" . date('Y-m-d H:i:s', strtotime($fparams['date_to'] . ' ' . $fparams['time_to'])) . "'"; } if ($fparams['station_id']) { $sql_where[] = "t1.station_id = '" . $fparams['station_id'] . "'"; } $sql_where_str = count($sql_where) ? " AND " . implode(' AND ', $sql_where) . " " : ""; if ($fparams['order_field'] == 'date') { $sql_order = "`t1`.`measuring_timestamp` " . $fparams['order_direction']; } elseif ($fparams['order_field'] == 'stationid') { $sql_order = "`t1`.`station_id` " . $fparams['order_direction']; } elseif ($fparams['order_field'] == 'sensorid') { $sql_order = "`t1`.`sensor_id` " . $fparams['order_direction']; } elseif ($fparams['order_field'] == 'value') { $sql_order = "CAST(`t1`.`sensor_feature_value` AS DECIMAL(15,4)) " . $fparams['order_direction']; } else { $sql_order = "`t1`.`measuring_timestamp` "; } /*----------- /end filter prepare --------*/ if ($fparams['showdata']) { $sql_groupped = "SELECT `sensor_data_id`, `sensor_feature_id`, `measuring_timestamp`, CAST(`sensor_feature_value` AS DECIMAL(15,4)) as `sensor_feature_value`, `listener_log_id`\n FROM `" . SensorData::model()->tableName() . "`\n ORDER BY `measuring_timestamp` DESC\n LIMIT 1000"; $sql = "SELECT `t1`.`sensor_data_id`\n FROM " . SensorData::model()->tableName() . " `t1`\n LEFT JOIN ({$sql_groupped}) `gt` ON `gt`.`sensor_feature_id` = `t1`.`sensor_feature_id` AND `gt`.`measuring_timestamp` < `t1`.`measuring_timestamp`\n LEFT JOIN `" . StationSensorFeature::model()->tableName() . "` t2 ON t2.sensor_feature_id = t1.sensor_feature_id\n LEFT JOIN `" . SensorDBHandlerDefaultFeature::model()->tableName() . "` t3 ON t3.feature_code LIKE t2.feature_code\n WHERE (\n (t2.has_filter_max AND CAST(`t1`.`sensor_feature_value` AS DECIMAL(15,4)) > (t3.filter_max * IF(t2.is_cumulative, t1.period/60, 1) ) )\n OR (t2.has_filter_min AND CAST(`t1`.`sensor_feature_value` AS DECIMAL(15,4)) < (t3.filter_min * IF(t2.is_cumulative, t1.period/60, 1) ))\n OR (t2.has_filter_diff AND `gt`.`sensor_data_id` > 0 AND ABS(CAST(`gt`.`sensor_feature_value` AS DECIMAL(15,4)) - CAST(`t1`.`sensor_feature_value` AS DECIMAL(15,4))) > (t3.filter_diff * IF(t2.is_cumulative, t1.period/60, 1)))\n )\n {$sql_where_str}\n GROUP BY `t1`.`sensor_data_id`\n LIMIT 1000"; $total = count(Yii::app()->db->createCommand($sql)->queryColumn()); $pages = new CPagination($total); $pages->pageSize = 20; //$pages->applyLimit($criteria); $sql = "SELECT `t1`.`sensor_data_id`, `t1`.`sensor_feature_id`, CAST(`t1`.`sensor_feature_value` AS DECIMAL(15,4)) as `sensor_feature_value`, `t1`.`measuring_timestamp`, t1.period,\n `gt`.`measuring_timestamp` AS `prev_measuring_timestamp`, `gt`.`listener_log_id` AS `prev_listener_log_id`, CAST(`gt`.`sensor_feature_value` AS DECIMAL(15,4)) as `prev_sensor_feature_value`, `gt`.`sensor_data_id` as `prev_sensor_data_id`,\n `t5`.`filter_max`, `t5`.`filter_min`, `t5`.`filter_diff`, t2.has_filter_max, t2.has_filter_min, t2.has_filter_diff, t2.is_cumulative,\n `t3`.`sensor_id_code`,\n `t4`.`station_id_code`\n FROM " . SensorData::model()->tableName() . " `t1`\n LEFT JOIN ({$sql_groupped}) `gt` ON `gt`.`sensor_feature_id` = `t1`.`sensor_feature_id` AND `gt`.`measuring_timestamp` < `t1`.`measuring_timestamp`\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 = t1.sensor_id\n LEFT JOIN `" . Station::model()->tableName() . "` t4 ON t4.station_id = t1.station_id\n LEFT JOIN `" . SensorDBHandlerDefaultFeature::model()->tableName() . "` t5 ON t5.feature_code LIKE t2.feature_code\n WHERE (\n (t2.has_filter_max AND CAST(`t1`.`sensor_feature_value` AS DECIMAL(15,4)) > (t5.filter_max * IF(t2.is_cumulative, t1.period/60, 1) ) )\n OR (t2.has_filter_min AND CAST(`t1`.`sensor_feature_value` AS DECIMAL(15,4)) < (t5.filter_min * IF(t2.is_cumulative, t1.period/60, 1) ))\n OR (t2.has_filter_diff AND `gt`.`sensor_data_id` > 0 AND ABS(CAST(`gt`.`sensor_feature_value` AS DECIMAL(15,4)) - CAST(`t1`.`sensor_feature_value` AS DECIMAL(15,4))) > (t5.filter_diff * IF(t2.is_cumulative, t1.period/60, 1) ))\n )\n {$sql_where_str}\n GROUP BY `t1`.`sensor_data_id`\n HAVING (\n (t2.has_filter_max AND CAST(`sensor_feature_value` AS DECIMAL(15,4)) > (t5.filter_max * IF(t2.is_cumulative, t1.period/60, 1) ) )\n OR (t2.has_filter_min AND CAST(`sensor_feature_value` AS DECIMAL(15,4)) < (t5.filter_min * IF(t2.is_cumulative, t1.period/60, 1) ))\n OR (t2.has_filter_diff AND prev_sensor_data_id > 0 AND ABS(CAST(`prev_sensor_feature_value` AS DECIMAL(15,4)) - CAST(`sensor_feature_value` AS DECIMAL(15,4))) > (t5.filter_diff * IF(t2.is_cumulative, t1.period/60, 1) ))\n\t\t\t )\n ORDER BY {$sql_order}\n LIMIT " . $pages->currentPage * $pages->pageSize . ", " . $pages->pageSize; $list = Yii::app()->db->createCommand($sql)->queryAll(); if ($list) { foreach ($list as $key => &$value) { $multiplyer_str = ''; $multiplyer = 1; if ($value['is_cumulative']) { $multiplyer_str = $value['period'] != 60 ? ' * ' . $value['period'] . 'min/60' : ''; $multiplyer = $value['period'] / 60; } if (isset($value['has_filter_max']) && $value['sensor_feature_value'] > $value['filter_max'] * $multiplyer) { $value['filter_reason'][] = array('main' => 'T1 > ' . $value['filter_max'] . $multiplyer_str); } if (isset($value['has_filter_min']) && $value['sensor_feature_value'] < $value['filter_min'] * $multiplyer) { $value['filter_reason'][] = array('main' => 'T1 < ' . $value['filter_min'] . $multiplyer_str); } if (isset($value['prev_sensor_feature_value']) && isset($value['has_filter_diff']) && abs($value['sensor_feature_value'] - $value['prev_sensor_feature_value']) > $value['filter_diff'] * $multiplyer) { $value['filter_reason'][] = array('main' => '|T1 - T0| > ' . $value['filter_diff'] . $multiplyer_str, 'extra' => '(Previous value: ' . $value['prev_sensor_feature_value'] . ' on ' . $value['prev_measuring_timestamp'] . ')'); } } } $this->render('awsfiltered', array('list' => $list, 'clean_page' => false, 'pages' => $pages, 'stations' => $stations, 'fparams' => $fparams)); } else { $this->render('awsfiltered', array('list' => $list, 'clean_page' => true, 'stations' => $stations, 'fparams' => $fparams)); } }
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>'); }
public function actionMetrics() { $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('superadmin/metrics')); } $this->render('metrics', array('meas_types' => $meas_types)); }
public function actionMsgHistory() { $form = new MessagesLogFilterForm(); $ids_to_delete = array(); if (Yii::app()->request->isPostRequest && isset($_POST['delete_checked'])) { if (isset($_POST['log_id'])) { $ids_to_delete = $_POST['log_id']; } } if (isset($_REQUEST['delete'])) { $id = intval($_REQUEST['delete']); if ($id) { $ids_to_delete[] = $id; } } if ($ids_to_delete) { if (!Yii::app()->user->isSuperAdmin()) { It::memStatus('only_admin_can_delete_message_history_log_messages'); } else { $cnt = 0; foreach ($ids_to_delete as $id) { $sensorData = SensorData::model()->findall('listener_log_id=:log_id', array(':log_id' => $id)); if ($sensorData) { foreach ($sensorData as $item) { $item->delete(); } } $seaLevelTrend = SeaLevelTrend::model()->findall('log_id=:log_id', array(':log_id' => $id)); if ($seaLevelTrend) { foreach ($seaLevelTrend as $item) { $item->delete(); } } $calcData = StationCalculationData::model()->findall('listener_log_id=:log_id', array(':log_id' => $id)); if ($calcData) { foreach ($calcData as $item) { $item->delete(); } } $log = ListenerLog::model()->find('log_id=:log_id', array(':log_id' => $id)); if ($log) { $log->delete(); $cnt++; } $sensorData = SensorData::model()->selectDb(true)->findall('listener_log_id=:log_id', array(':log_id' => $id)); if ($sensorData) { foreach ($sensorData as $item) { $item->selectDb(true)->delete(); } } $seaLevelTrend = SeaLevelTrend::model()->selectDb(true)->findall('log_id=:log_id', array(':log_id' => $id)); if ($seaLevelTrend) { foreach ($seaLevelTrend as $item) { $item->selectDb(true)->delete(); } } $calcData = StationCalculationData::model()->selectDb(true)->findall('listener_log_id=:log_id', array(':log_id' => $id)); if ($calcData) { foreach ($calcData as $item) { $item->selectDb(true)->delete(); } } $log = new ListenerLog(); $log = $log->selectDb(true)->find('log_id=:log_id', array(':log_id' => $id)); if ($log) { $log->selectDb(true)->delete(); $cnt++; } } It::memStatus($cnt ? 'message_history_log_was_deleted' : 'message_history_log_not_found'); } $this->redirect($this->createUrl('site/msghistory')); } if (Yii::app()->request->isPostRequest && isset($_POST['clear']) or isset($_GET['clear'])) { $form->clearMemory(); $this->redirect($this->createUrl('site/msghistory')); } if (Yii::app()->request->isPostRequest && isset($_POST['filter'])) { $form->attributes = $_POST['MessagesLogFilterForm']; if ($form->validate()) { $this->redirect($this->createUrl('site/msghistory')); } } if (isset($_REQUEST['of'])) { $form->setOrders($_REQUEST['of']); $this->redirect($this->createUrl('site/msghistory')); } if (Yii::app()->request->isPostRequest && isset($_POST['export'])) { $form->attributes = $_POST['MessagesLogFilterForm']; if ($form->validate()) { $res = $form->makeExport(); } } $res = $form->prepareList(); $this->render('messages_history', array('list' => $res['list'], 'pages' => $res['pages'], 'form' => $form)); }
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; }
/** * create statistics now * @param null $report */ private function ini($report = null) { date_default_timezone_set("UTC"); if (is_null($report)) { $report = new HeartbeatReport(); $report->created = date("Y-m-d H:i:s"); $report->period = date("Y-m-d H:i:s", time() - 24 * 60 * 60); } $minute_in_period = (strtotime($report->created) - strtotime($report->period)) / 60; $databases = array('db' => false, 'db_long' => true); $config_file = array('db' => 'db_params', 'db_long' => 'db_long_params'); $sql = "SELECT TABLE_NAME, `DATA_LENGTH`+`INDEX_LENGTH` as 'TABLE_SIZE', TABLE_ROWS\r\n FROM information_schema.TABLES\r\n WHERE `table_schema` LIKE :db;" . ""; foreach ($databases as $database => $use_long) { /* * $db_stat * $db_long_stat */ $res = CStubActiveRecord::getDbConnect($use_long)->createCommand("SHOW STATUS;")->queryAll(); foreach ($res as $val) { $this->{$database . '_stat'}[$val['Variable_name']] = $val['Value']; } /* * $db_tables_size * $db_long_tables_size * $db_tables_rows * $db_long_tables_rows */ // $db_conf = new ConfigManager($config_file[$database]); // $db_name = $db_conf::getConfigSection('database', 'dbname'); $db_name = Yii::app()->params[$config_file[$database]]['dbname']; $res = CStubActiveRecord::getDbConnect($use_long)->createCommand($sql)->bindValue(':db', $db_name)->queryAll(); $allSize = $allCount = 0; foreach ($res as $table) { $allSize += $this->{$database . '_tables_size'}[$table['TABLE_NAME']] = number_format($table['TABLE_SIZE'] / 1024 / 1024, 2); $allCount += $this->{$database . '_tables_rows'}[$table['TABLE_NAME']] = $table['TABLE_ROWS']; } $this->{$database . '_tables_size'}['All'] = $allSize; $this->{$database . '_tables_rows'}['All'] = $allCount; if ($use_long) { /* * $stations * $stations_logger * $stations_communication_type * $stations_message_interval * $stations_message_count * $stations_message_expected * $stations_message_error * $stations_message_is_processing * $stations_message_last * $stations_schedule_synop * $stations_schedule_bufr * $stations_schedule_speci * $stations_schedule_metar * $stations_schedule_data_export */ $criteria = new CDbCriteria(); $criteria->index = 'station_id'; $criteria->with = array('messages' => array('together' => false, 'select' => 'messages.measuring_timestamp, messages.failed, is_processing', 'condition' => 'messages.measuring_timestamp > \'' . $report->period . '\'', 'order' => 'messages.measuring_timestamp DESC'), 'schedule' => array('select' => 'schedule.report_type, schedule.period', 'together' => false, 'with' => array('processed' => array('select' => 'processed.updated', 'together' => false, 'condition' => 'processed.updated > \'' . $report->period . '\'')))); $stations = Station::model()->long()->findAll($criteria); foreach ($stations as $station_id => $station) { $this->stations[$station_id] = $station['display_name']; $this->stations_logger[$station_id] = $station['logger_type']; $this->stations_communication_type[$station_id] = $station['communication_type']; $this->stations_message_interval[$station_id] = $station['event_message_period']; $this->stations_message_count[$station_id] = count($station->messages); $this->stations_message_expected[$station_id] = $minute_in_period / $station['event_message_period']; foreach ($station->messages as $message) { $this->stations_message_error[$station_id] += $message['failed']; $this->stations_message_is_processing[$station_id] += $message['is_processing']; } $this->stations_message_last[$station_id] = $station->messages[0]['measuring_timestamp']; $logIds[] = $station->messages[0]['log_id']; foreach ($station->schedule as $schedule) { $schedule_count = count($schedule->processed); if ($schedule_count) { if ($schedule->report_type == 'speci') { $this->stations_schedule_speci[$station_id]['gen'] += $schedule_count; } else { $this->{'stations_schedule_' . $schedule->report_type}[$station_id]['gen'] += $schedule_count; $this->{'stations_schedule_' . $schedule->report_type}[$station_id]['sch'] += number_format($minute_in_period / $schedule->period); } } } } foreach ($this as $key => $arr) { if (stripos($key, 'stations_schedule_') !== false && !is_null($arr)) { foreach ($arr as $station_id => $val) { if (!is_null($val['gen']) && !is_null($val['sch'])) { $this->{$key}[$station_id] = $val['sch'] . ' / ' . $val['gen'] . ' = ' . number_format($val['gen'] / $val['sch'] * 100) . '%'; } elseif (stripos($key, 'speci') === false) { $this->{$key}[$station_id] = $val['sch'] . ' / ' . $val['gen']; } else { $this->{$key}[$station_id] = $val['gen']; } } } } /* * $stations_bv_last */ $criteria = new CDbCriteria(); $criteria->with = array('sensor_feature' => array('select' => false, 'joinType' => 'INNER JOIN', 'condition' => 'sensor_feature.feature_code = \'battery_voltage\'')); $criteria->index = 'station_id'; $criteria->compare('listener_log_id', $logIds); $bv_data = SensorData::model()->long()->findAll($criteria); foreach ($stations as $station_id => $station) { $this->stations_sensor_bv[$station_id] = $bv_data[$station_id]['sensor_feature_value']; } } } /* * $system */ $this->system['disk_free_space'] = number_format(disk_free_space('/') / pow(10, 9), 2) . 'Gb'; $this->system['disk_total_space'] = number_format(disk_total_space('/') / pow(10, 9), 2) . 'Gb'; }