Add cache.

This commit is contained in:
BoHung Chiu 2021-12-08 18:21:08 +08:00
parent 222477d51e
commit 2bc7281008
8 changed files with 223 additions and 28 deletions

View File

@ -0,0 +1,7 @@
class RulingWeatherCache
include Mongoid::Document
include Mongoid::Timestamps
field :cache
field :cache_time , :type=> DateTime, :default=> DateTime.now
belongs_to :ruling_weather_setting
end

View File

@ -7,6 +7,7 @@ class RulingWeatherSetting
field :location, type: String, default: ""
field :dataid, type: String, default: ""
field :observatory_name, type: String, default: ""
has_one :ruling_weather_cache
def get_now_info(custom_location=nil,custom_dataid=nil,custom_observatory_name=nil)
time_now = DateTime.now.utc.new_offset(self.time_offset)
today = time_now.strftime("%Y-%m-%d")
@ -23,18 +24,35 @@ class RulingWeatherSetting
"locationName" => custom_location}
res = Net::HTTP.get_response(URI.parse("#{url}?#{data.to_query}"))
content = JSON.parse(res.body)
weather_data = get_weather_data(content)
cahche_model = self.ruling_weather_cache
if cahche_model.nil?
cahche_model = RulingWeatherCache.create(:cache=>{},:ruling_weather_setting=>self)
end
cache = cahche_model.cache rescue {}
weather_data = get_weather_data(content) rescue {}
if (weather_data.count == 0 rescue true)
mint = cache["mint"].to_i
maxt = cache["maxt"].to_i
avgt = cache["avgt"].to_i
rain_rate = cache["rain_rate"].to_i
uvi = cache["uvi"].to_i
uvi_text = cache["uvi_text"].to_s
rh = cache["rh"].to_i
wx_text = cache["wx_text"].to_s
wx_code = cache["wx_code"].to_i
ws = cache["ws"].to_i
else
mint = get_element_value(weather_data["MinT"]).to_i #最低溫度(攝氏度)
maxt = get_element_value(weather_data["MaxT"]).to_i #最高溫度(攝氏度)
avgt = get_element_value(weather_data["T"]).to_i #平均溫度(攝氏度)
rain_rate = get_element_value(weather_data["PoP12h"]).to_i #降雨機率(百分比)
uvi = get_element_value(weather_data["UVI"],0).to_i #紫外線指數(數值)
uvi_text = get_element_value(weather_data["UVI"],1).to_s #紫外線指數(文字)
uvi_text = uvi_text.blank? ? I18n.t("ruling_weather.none") : uvi_text
rh = get_element_value(weather_data["RH"]).to_i #相對溼度
wx_text = get_element_value(weather_data["Wx"],0).to_s #天氣現象(文字)
wx_code = get_element_value(weather_data["Wx"],1).to_i #天氣現象(編碼)
ws = get_element_value(weather_data["WS"]).to_i #風速(m/s)
end
if wx_code < 10
wx_code = "0" + wx_code.to_s
else
@ -55,8 +73,12 @@ class RulingWeatherSetting
res2 = Net::HTTP.get_response(URI.parse("#{url2}?#{data2.to_query}"))
content2 = JSON.parse(res2.body)
weather_data2 = get_weather_data(content2) rescue {}
if (weather_data2.count == 0 rescue true)
rain = cache["rain"]
else
rain = weather_data2["NOW"]["elementValue"].to_f rescue 0.0
end
end
result = {"mint" => mint,
"maxt" => maxt,
"avgt" => avgt,
@ -68,6 +90,10 @@ class RulingWeatherSetting
"wx_svg" => wx_svg,
"ws" => ws,
"rain" => rain}
cahche_model.update(:cache=>result,:cache_time=>time_now)
if uvi_text.blank?
result["uvi_text"] = I18n.t("ruling_weather.none")
end
return result
end
def test

View File

@ -1,49 +1,95 @@
<div>
<style type="text/css">
.weather_widget1{
background: #06dd95;
background: none;
display: inline-block;
width: 100%;
padding: 1.5em 0.8em;;
font-size: 1.3em;
font-weight: bold;
font-size:inherit;
font-weight: 500;
background: #accbe7;
}
.weather_widget1 .white_text{
color: #ffffff;
font-size: 1.5em;
color: #333;
font-size:inherit;
overflow-wrap: break-word;
}
.weatherICON{
padding: 0;
}
.weatherTXT{
font-weight: 700;
}
.UviItem{
font-weight: 700;
}
.weatherItem{
font-weight: 700;
}
.weatherBlock{
margin-bottom:1em;
}
@media(min-width:769px){
.weatherICON img{
width: 60%;
}
.weather_widget1{
text-align: center;
}
}
@media(max-width:769px){
.weatherICON img{
width: 100%;
}
.weatherICON {
padding: 0;
width: 50%;
margin: auto;
}
.one{
float: left;
}
.two{
float: right;
}
.three{
clear: both;
float: left;
}
.four{
float: right;
}
.five{
clear: both;
}
}
</style>
<div class="weather_widget1" data-list="weather" data-level="0">
<div class="col-sm-12 columns weatherBlock">
<div class="col-sm-4 columns weatherICON">
<img src="{{wx_svg}}" data-name="wx_svg" />
</div>
<div class="col-sm-4 columns">
<div class="col-sm-4 columns one">
<div class="weatherTXT" data-name="wx_text">{{wx_text}}</div>
<div class="Tempature white_text" data-name="avgt">{{avgt}}<span>℃</span></div>
</div>
<div class="col-sm-4 columns">
<div class="col-sm-4 columns two">
<div class="UviItem">{{uv-head}}</div>
<div class="UviText white_text" data-name="uvi_text">{{uvi_text}}</div>
</div>
</div>
<div class="col-sm-12 columns weatherBlock">
<div class="col-sm-4 columns">
<div class="col-sm-4 columns three">
<div class="weatherItem">{{relative_humidity-head}}</div>
<div class="weatherItemNum white_text"><span data-name="rh">{{rh}}</span>%</div>
</div>
<div class="col-sm-4 columns">
<div class="col-sm-4 columns four">
<div class="weatherItem">{{current_wind_speed-head}}</div>
<div class="weatherItemNum white_text"><span data-name="ws">{{ws}}</span>m/s</div>
</div>
<div class="col-sm-4 columns">
<div class="col-sm-4 columns five">
<div class="weatherItem">{{accumulated_rainfall-head}}</div>
<div class="weatherItemNum white_text"><span data-name="rain">{{rain}}</span>mm</div>
</div>

View File

@ -0,0 +1,52 @@
<div>
<style type="text/css">
.weather_widget1{
background: #06dd95;
display: inline-block;
width: 100%;
padding: 1.5em 0.8em;;
font-size: 1.3em;
font-weight: bold;
}
.weather_widget1 .white_text{
color: #ffffff;
font-size: 1.5em;
overflow-wrap: break-word;
}
.weatherICON{
padding: 0;
}
.weatherICON img{
width: 100%;
}
</style>
<div class="weather_widget1" data-list="weather" data-level="0">
<div class="col-sm-12 columns weatherBlock">
<div class="col-sm-4 columns weatherICON">
<img src="{{wx_svg}}" data-name="wx_svg" title="{{wx_text}}" alt="{{wx_text}}" />
</div>
<div class="col-sm-4 columns">
<div class="weatherTXT" data-name="wx_text">{{wx_text}}</div>
<div class="Tempature white_text" data-name="avgt">{{avgt}}<span>℃</span></div>
</div>
<div class="col-sm-4 columns">
<div class="UviItem">{{uv-head}}</div>
<div class="UviText white_text" data-name="uvi_text">{{uvi_text}}</div>
</div>
</div>
<div class="col-sm-12 columns weatherBlock">
<div class="col-sm-4 columns">
<div class="weatherItem">{{relative_humidity-head}}</div>
<div class="weatherItemNum white_text"><span data-name="rh">{{rh}}</span>%</div>
</div>
<div class="col-sm-4 columns">
<div class="weatherItem">{{current_wind_speed-head}}</div>
<div class="weatherItemNum white_text"><span data-name="ws">{{ws}}</span>m/s</div>
</div>
<div class="col-sm-4 columns">
<div class="weatherItem">{{accumulated_rainfall-head}}</div>
<div class="weatherItemNum white_text"><span data-name="rain">{{rain}}</span>mm</div>
</div>
</div>
</div>
</div>

View File

@ -9,6 +9,14 @@
"en" : "1. Weather layout 1"
},
"thumbnail" : "weather1_thumbs.png"
},
{
"filename" : "ruling_weather_widget2",
"name" : {
"zh_tw" : "2. 天氣呈現樣式2",
"en" : "2. Weather layout 2"
},
"thumbnail" : "weather2_thumbs.png"
}
]
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -8,11 +8,67 @@ if bundle_update_flag
app_path = File.expand_path(__dir__)
template_path = env_pwd + '/app/templates'
all_template = Dir.glob(template_path+'/*/')
default_weather_widget_info = JSON.parse(File.read("#{app_path}/modules/ruling_weather/info.json"))["widgets"].sort_by{|h| h["filename"].to_i} rescue []
all_template.each do |folder|
if !folder.include?('mobile')
moudle_path = "#{folder}modules/ruling_weather/"
if !Dir.exist?(moudle_path) && Dir.exist?(File.dirname(moudle_path))
if Dir.exist?(File.dirname(moudle_path))
if !Dir.exist?(moudle_path)
Bundler.with_clean_env{system ('cp -r '+ app_path + '/modules/* ' + folder + '/modules/.')}
else
info_json_file = "#{moudle_path}info.json"
if File.exist?(info_json_file)
begin
file_text = File.read(info_json_file) rescue ""
encode_file_text = file_text.encode("UTF-8", "UTF-8", invalid: :replace, replace: "???")
next if (encode_file_text.include?("???") rescue true)
info = JSON.parse(encode_file_text) rescue {}
flag = (info.count != 0 rescue false)
if flag
puts "Checking RulingWeather widgets"
widget_info = info["widgets"].sort_by{|h| h["filename"].to_i} rescue []
update_flag = false
last_index = widget_info[-1]["filename"].match(/\d+/)[0].to_i rescue nil
if !last_index.nil?
idx_regex = /^(\d+[\. \t]*)|[ \t]+$/
default_weather_widget_info.each do |h|
name_without_index = h["name"]["zh_tw"].gsub(idx_regex,'')
widget_info_index = (widget_info.index{|hh| hh["name"]["zh_tw"].gsub(idx_regex,'') == name_without_index}||-1 rescue -1)
if (widget_info_index == -1 || widget_info_index.nil?)
update_flag = true
copy_h = h.dup
h.delete("force_cover")
last_index = last_index + 1
copy_h["filename"] = copy_h["filename"].sub(/\d+/){|ff| last_index.to_s}
copy_h["name"].keys.each do |locale|
copy_h["name"][locale] = copy_h["name"][locale].sub(/\d+/){|ff| last_index.to_s}
end
widget_info << copy_h
Bundler.with_clean_env{%x[cp -f #{app_path}/modules/ruling_weather/_#{h["filename"]}.html.erb #{folder}modules/ruling_weather/_#{copy_h["filename"]}.html.erb]}
elsif h["force_cover"] == "true"
Bundler.with_clean_env{%x[cp -f #{app_path}/modules/ruling_weather/_#{h["filename"]}.html.erb #{folder}modules/ruling_weather/_#{widget_info[widget_info_index]["filename"]}.html.erb]}
end
end
if update_flag
info["widgets"] = widget_info
puts "Writing json #{info["widgets"].count} in #{info_json_file}"
begin
info_json = JSON.pretty_generate(info).gsub(":[",":[\n").gsub(":{",":{\n")
rescue
info_json = info.to_s.gsub("=>",": \n")
end
File.open(info_json_file,"w+"){|f| f.write(info_json)}
end
Bundler.with_clean_env{%x[cp -rn #{app_path}/modules/ruling_weather/thumbs/* #{folder}modules/ruling_weather/thumbs/.]}
Bundler.with_clean_env{%x[cp -f #{app_path}/modules/ruling_weather/thumbs/weather1_thumbs.png #{folder}modules/ruling_weather/thumbs/.]}
end
end
rescue => e
puts e
puts "There has some error when checking RulingWeather widgets"
end
end
end
end
end
end