public function test_PrepareStringCSV() { $s = array('1' => array('1.1' => '213213', '1.2' => '21213', '1.3' => '2133'), '2' => array('2.1' => '2113', '2.2' => '2132', '2.3' => '3213')); $result = It::prepareStringCSV($s); $expected = '"1.1","1.2","1.3"' . "\n" . '"213213","21213","2133"' . "\n" . '"2113","2132","3213"' . "\n"; $this->assertEquals($expected, $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'); }
public function makeExport() { $return_string = "\"Messages History\"\n\nChoosed filters:\n\n"; if ($this->types) { $subtypes = array(); if (in_array('failed', $this->types)) { $subtypes[] = "Failed"; } if (in_array('warning', $this->types)) { $subtypes[] = "Warning"; } if (in_array('successfull', $this->types)) { $subtypes[] = "Successfull"; } if ($this->types && in_array('not_processed_yet', $this->types)) { $subtypes[] = "Not procecessed yet"; } $return_string .= "* Types " . implode(' OR ', $subtypes); } if ($this->date_from || $this->date_to) { $return_string .= "\n* Message was added into DB in period "; if ($this->date_from) { $return_string .= " from: " . date('Y-m-d H:i:s', strtotime($this->date_from . ' ' . $this->time_from)); } if ($this->date_to) { if ($this->date_from) { $return_string .= ' and'; } $return_string .= ' Before ' . date('Y-m-d H:i:s', strtotime($this->date_to . ' ' . $this->time_to)); } } $res = $this->prepareList(0); if (!$res['list']) { $return_string .= "\n\nList is empty"; } else { $prepared_res = array(); foreach ($res['list'] as $key => $value) { $prepared_res[] = array('Message' => $value['message'], 'Added' => $value['created'], 'St Id' => $value['station_id_code'] ? $value['station_id_code'] : 'Unknown', 'St Name' => $value['station_id_code'] ? $value['display_name'] : 'Unknown', 'St Type' => $value['station_id_code'] ? $value['station_type'] : 'No', 'Has Errors?' => $value['errors'] ? 'Fatal errors. Was not processed.' : 'No', 'Has Warnings?' => $value['warnings'] ? 'Data from some sensors were not processed.' : 'No', 'Errors Description' => addslashes($value['errors'] ? implode(' ; ', $value['errors']) : 'No'), 'Warnings Description' => addslashes($value['warnings'] ? implode(' ; ', $value['warnings']) : 'No')); } $return_string .= "\n\n" . It::prepareStringCSV($prepared_res); } It::downloadFile($return_string, 'export_history__' . date('Y-m-d_Hi') . '.csv', 'text/csv'); }