Commit 7253acba by liyijie

Merge branch 'liyijie/aqi_live' into 'master'

Liyijie/aqi live See merge request !11
parents 7b0b87c6 408f95c9
class CreateAqiLives < ActiveRecord::Migration[5.0]
def up
create_table :aqi_lives do |t|
t.integer :aqi
t.string :area
t.float :co
t.float :co_24h
t.float :no2
t.float :no2_24h
t.float :o3
t.float :o3_24h
t.float :o3_8h
t.float :o3_8h_24h
t.float :pm10
t.float :pm10_24h
t.float :pm2_5
t.float :pm2_5_24h
t.string :position_name
t.string :primary_pollutant
t.string :quality
t.float :so2
t.float :so2_24h
t.string :station_code
t.datetime :time_point
t.timestamps
end
end
def down
drop_table :aqi_lives
end
end
module WeatherModel module WeatherModel
module SourceSchema module SourceSchema
AqiForecast =
{ AqiForecast = {
sort: 'Aqi', sort: 'Aqi',
table_name: :aqi_forecasts, table_name: :aqi_forecasts,
schema: { schema: {
...@@ -52,37 +52,64 @@ module WeatherModel ...@@ -52,37 +52,64 @@ module WeatherModel
} }
} }
RealtimeAqi = AqiLive = {
{
sort: 'Aqi', sort: 'Aqi',
table_name: :realtime_aqi, table_name: :aqi_lives,
schema: { schema: {
datetime: :datetime, aqi: :integer,
aqi: :string, area: :string,
level: :string, co: :float,
pripoll: :string, co_24: :float,
content: :string, no2: :float,
measure: :string, no2_24: :float,
o3: :float,
o3_24: :float,
o3_8h: :float,
o3_8h_24: :float,
pm10: :float,
pm10_24: :float,
pm2_5: :float,
pm2_5_24: :float,
position_name: :string,
primary_pollutant: :string,
quality: :string,
so2: :float,
so2_24h: :float,
station_code: :string,
time_point: :dateime,
}, },
options: { options: {
index: %i[datetime], index: %i[area, time_point],
expire: 3 * 24 * 60 * 60, expire: 2 * 24 * 60 * 60,
unique: nil, unique: nil,
belongs_to: nil, belongs_to: nil,
has_many: nil, has_many: nil,
json_column: :msg json_column: :nil,
}, },
factory_name: :realtime_aqi, factory_name: :aqi_live,
factory: { factory: {
datetime: Time.parse('2011-11-11 11:11'), "aqi" => 46,
aqi: '54', "area" => "宜宾",
level: '良', "co" => 1.1,
pripoll: 'PM10', "co_24h" => 1.2,
msg: { "no2" => 20,
content: '空气质量可接受,但某些污染物可能对极少数异常敏感人群健康有较弱影响。', "no2_24h" => 22,
measure: '建议措施极少数异常敏感人群应减少户外活动。' "o3" => 73,
} "o3_24h" => 129,
} "o3_8h" => 45,
"o3_8h_24h" => 53,
"pm10" => 45,
"pm10_24h" => 60,
"pm2_5" => 32,
"pm2_5_24h" => 42,
"position_name" => "市政府",
"primary_pollutant" => nil,
"quality" => "优",
"so2" => 10,
"so2_24h" => 13,
"station_code" => "1885A",
"time_point" => "2017-05-21T16:00:00Z",
},
} }
end end
end end
require "spec_helper" require "spec_helper"
require 'active_record' require 'active_record'
require "ohm" require "ohm"
Dir[File.dirname(__FILE__) + '../lib/weather-model.rb'].each { |file| require file }
include WeatherModel # include WeatherModel
RSpec.describe WeatherModel do RSpec.describe WeatherModel do
...@@ -12,7 +13,7 @@ RSpec.describe WeatherModel do ...@@ -12,7 +13,7 @@ RSpec.describe WeatherModel do
# run migration # run migration
Dir[File.dirname(__FILE__) + '/../db/migrate/*.rb'].each { |file| require file } Dir[File.dirname(__FILE__) + '/../db/migrate/*.rb'].each { |file| require file }
# drop table # drop table
FileUtils.rm 'weather-model-test.sqlite3' FileUtils.rm_f 'weather-model-test.sqlite3'
ActiveRecord::Base.establish_connection( ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3', :adapter => 'sqlite3',
:database => 'weather-model-test.sqlite3' :database => 'weather-model-test.sqlite3'
...@@ -46,7 +47,7 @@ RSpec.describe WeatherModel do ...@@ -46,7 +47,7 @@ RSpec.describe WeatherModel do
temp_high: '100', temp_high: '100',
temp_low: '0', temp_low: '0',
weather_text1: '酷热', weather_text1: '酷热',
weather_text1: '严寒', weather_text2: '严寒',
weather_pic: 'http://xxx.com', weather_pic: 'http://xxx.com',
win_dir: '东南西北风', win_dir: '东南西北风',
win_speed: '1级', win_speed: '1级',
...@@ -86,10 +87,10 @@ RSpec.describe WeatherModel do ...@@ -86,10 +87,10 @@ RSpec.describe WeatherModel do
it 'Test all source data' do it 'Test all source data' do
[ [
Forecast::CityForecast, WeatherModel::Forecast::CityForecast,
AutoStation::AutoStationTenMins, WeatherModel::AutoStation::AutoStationTenMins,
Aqi::AqiForecast, WeatherModel::Aqi::AqiForecast,
Aqi::RealtimeAqi WeatherModel::Aqi::AqiLive,
].each { |source| source.all; source.const_get('Ohm').all } ].each { |source| source.all; source.const_get('Ohm').all }
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment