Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
weather-model
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
weather-model
Commits
f9666f96
Commit
f9666f96
authored
May 21, 2017
by
Ivan Lan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'lan/add_to_h_to_ohm' into 'master'
Lan/add to h to ohm See merge request
!12
parents
7253acba
1b5bf547
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
75 additions
and
46 deletions
+75
-46
4_create_realtime_aqi.rb
db/migrate/4_create_realtime_aqi.rb
+0
-20
aqi.rb
lib/weather-model/source_schemas/aqi.rb
+9
-9
storer.rb
lib/weather-model/storer.rb
+30
-6
weather-model_spec.rb
spec/weather-model_spec.rb
+36
-11
No files found.
db/migrate/4_create_realtime_aqi.rb
deleted
100644 → 0
View file @
7253acba
class
CreateRealtimeAqi
<
ActiveRecord
::
Migration
[
5.0
]
def
up
create_table
:realtime_aqi
do
|
t
|
t
.
datetime
:datetime
t
.
integer
:aqi
t
.
string
:level
t
.
string
:pripoll
t
.
string
:content
t
.
string
:measure
t
.
timestamps
end
add_index
:realtime_aqi
,
:datetime
end
def
down
drop_table
:realtime_aqi
end
end
lib/weather-model/source_schemas/aqi.rb
View file @
f9666f96
...
...
@@ -59,27 +59,27 @@ module WeatherModel
aqi: :integer
,
area: :string
,
co: :float
,
co_24: :float
,
co_24
h
: :float
,
no2: :float
,
no2_24: :float
,
no2_24
h
: :float
,
o3: :float
,
o3_24: :float
,
o3_24
h
: :float
,
o3_8h: :float
,
o3_8h_24: :float
,
o3_8h_24
h
: :float
,
pm10: :float
,
pm10_24: :float
,
pm10_24
h
: :float
,
pm2_5: :float
,
pm2_5_24: :float
,
pm2_5_24
h
: :float
,
position_name: :string
,
primary_pollutant: :string
,
quality: :string
,
so2: :float
,
so2_24h: :float
,
station_code: :string
,
time_point: :dateime
,
time_point: :date
t
ime
,
},
options:
{
index:
%i[area
,
time_point]
,
index:
%i[area time_point]
,
expire:
2
*
24
*
60
*
60
,
unique:
nil
,
belongs_to:
nil
,
...
...
@@ -108,7 +108,7 @@ module WeatherModel
"so2"
=>
10
,
"so2_24h"
=>
13
,
"station_code"
=>
"1885A"
,
"time_point"
=>
"2017-05-21T16:00:00Z"
,
"time_point"
=>
Time
.
parse
(
"2017-05-21 16:00:00"
)
,
},
}
end
...
...
lib/weather-model/storer.rb
View file @
f9666f96
...
...
@@ -64,8 +64,8 @@ module WeatherModel
index
:mysql_id
unique
:mysql_id
settings
[
:schema
].
keys
.
each
{
|
attr
|
attribute
attr
settings
[
:schema
].
each_pair
{
|
attr
,
type
|
attribute
attr
,
type_proc
(
type
)
}
set_options
settings
end
...
...
@@ -79,6 +79,28 @@ module WeatherModel
end
end
def
self
.
all
super
.
select
{
|
x
|
x
.
attributes
.
empty?
}.
map
(
&
:delete
)
super
end
def
self
.
type_proc
type
case
type
when
:integer
proc
{
|
val
|
val
&&
val
.
to_i
}
when
:float
proc
{
|
val
|
val
&&
val
.
to_f
}
when
:datetime
proc
{
|
val
|
val
.
is_a?
(
String
)
?
Time
.
parse
(
val
)
:
val
}
when
:json
proc
{
|
value
|
value
.
is_a?
(
String
)
?
JSON
.
parse
(
value
.
gsub
(
'=>'
,
':'
))
:
value
}
end
end
# ohm-expire 只处理了create
# 而且 只把 attributes 的值给 TTL了。。。
def
update
attributes
...
...
@@ -86,12 +108,14 @@ module WeatherModel
obj
.
update_ttl
self
.
class
.
instance_variable_get
(
:@expire
)
end
def
self
.
all
super
.
select
{
|
x
|
x
.
attributes
.
empty?
}.
map
(
&
:delete
)
super
def
to_h
keys
=
attributes
.
keys
-
[
:mysql_id
,
:updated_at
]
Hash
[
keys
.
zip
keys
.
map
{
|
column_name
|
send
(
column_name
)
}]
end
private
def
self
.
has_many
his_son
# input like :Post
# collection :posts, :Post
collection
(
his_son
.
to_s
.
underscore
.
pluralize
.
to_sym
,
his_son
)
if
his_son
&&
SourceSchema
.
const_defined?
(
his_son
)
...
...
@@ -103,7 +127,7 @@ module WeatherModel
end
def
self
.
json_column
column
attribute
column
,
->
(
value
)
{
attribute
column
,
proc
{
|
value
|
value
.
is_a?
(
String
)
?
JSON
.
parse
(
value
.
gsub
(
'=>'
,
':'
))
:
value
...
...
spec/weather-model_spec.rb
View file @
f9666f96
...
...
@@ -40,27 +40,29 @@ RSpec.describe WeatherModel do
it
'Forecast'
do
@expire
=
1
Forecast
::
CityForecast
::
Ohm
.
expire
@expire
mysql_obj
=
Forecast
::
CityForecast
.
create
({
mysql
=
Forecast
::
CityForecast
ohm
=
ohm
ohm
.
expire
@expire
mysql_obj
=
mysql
.
create
({
datetime:
'2017-05-16'
,
cityname:
'shanghai'
,
temp_high:
'100'
,
temp_low:
'0'
,
weather_text1:
'酷热'
,
weather_text
2
:
'严寒'
,
weather_pic:
'
http://xxx.com
'
,
weather_text
1
:
'严寒'
,
weather_pic:
'
酷热转严寒
'
,
win_dir:
'东南西北风'
,
win_speed:
'1级'
,
})
expect
(
Forecast
::
CityForecast
.
count
).
to
eq
(
1
)
expect
(
Forecast
::
CityForecast
::
O
hm
.
all
.
count
).
to
eq
(
1
)
expect
(
Forecast
::
CityForecast
::
O
hm
.
all
.
first
.
temp_low
).
to
eq
(
mysql_obj
.
temp_low
.
to_s
)
expect
(
Forecast
::
CityForecast
::
O
hm
.
all
.
first
.
get_ttl
).
to
eq
(
@expire
)
expect
(
mysql
.
count
).
to
eq
(
1
)
expect
(
o
hm
.
all
.
count
).
to
eq
(
1
)
expect
(
o
hm
.
all
.
first
.
temp_low
).
to
eq
(
mysql_obj
.
temp_low
.
to_s
)
expect
(
o
hm
.
all
.
first
.
get_ttl
).
to
eq
(
@expire
)
# 'ohm 对象 同步 mysql对象更新'
mysql_obj
.
update
(
temp_low:
99
)
expect
(
Forecast
::
CityForecast
::
O
hm
.
all
.
first
.
temp_low
).
to
eq
(
'99'
),
'ohm 对象 同步 mysql对象更新'
expect
(
o
hm
.
all
.
first
.
temp_low
).
to
eq
(
'99'
),
'ohm 对象 同步 mysql对象更新'
sleep
1.5
expect
(
Forecast
::
CityForecast
::
O
hm
.
all
.
count
).
to
eq
(
0
),
'TTL过期对象删除成功'
expect
(
o
hm
.
all
.
count
).
to
eq
(
0
),
'TTL过期对象删除成功'
end
it
'aqi_forecast'
do
...
...
@@ -85,13 +87,36 @@ RSpec.describe WeatherModel do
expect
(
Aqi
::
AqiForecast
::
Ohm
.
all
.
first
.
list
.
class
).
to
eq
(
Array
),
'Ohm json_column 正常'
end
describe
'test Ohm # to_h'
do
before
do
@aqi_live
=
create
(
:aqi_live
)
end
it
'test Ohm to_h'
do
ohm_obj
=
WeatherModel
::
Aqi
::
AqiLive
::
Ohm
.
all
.
first
expect
(
ohm_obj
.
time_point
.
class
).
to
eq
(
Time
)
expect
(
ohm_obj
.
aqi
.
class
).
to
eq
(
Integer
)
expect
(
ohm_obj
.
co
.
class
).
to
eq
(
Float
)
expect
(
ohm_obj
.
area
.
class
).
to
eq
(
String
)
hash
=
ohm_obj
.
to_h
expect
(
hash
[
:time_point
].
class
).
to
eq
(
Time
)
expect
(
hash
[
:aqi
].
class
).
to
eq
(
Integer
)
expect
(
hash
[
:co
].
class
).
to
eq
(
Float
)
expect
(
hash
[
:area
].
class
).
to
eq
(
String
)
end
end
it
'Test all source data'
do
[
WeatherModel
::
Forecast
::
CityForecast
,
WeatherModel
::
AutoStation
::
AutoStationTenMins
,
WeatherModel
::
Aqi
::
AqiForecast
,
WeatherModel
::
Aqi
::
AqiLive
,
].
each
{
|
source
|
source
.
all
;
source
.
const_get
(
'Ohm'
).
all
}
].
each
{
|
source
|
# create factory
source
.
all
;
source
.
const_get
(
'Ohm'
).
all
.
first
;
}
end
after
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment