Fix bug.(Accelerate most time)

This commit is contained in:
BoHung Chiu 2022-01-23 14:00:57 +08:00
parent 360a86fecd
commit a56f6ba5e3
3 changed files with 37 additions and 13 deletions

View File

@ -413,7 +413,7 @@ class Admin::CancerpredictsController < OrbitAdminController
@field_property["comment_text"] = property["comment_text"][locale.to_s] @field_property["comment_text"] = property["comment_text"][locale.to_s]
break break
end end
if @field_property["comment_text"] != "" if @field_property["comment_text"].present?
@table_str_right += '<button tabindex="0" class="cancer_help_btn" data-target="#show_help_modal" style="float:left;cursor: pointer;padding: 0em 0.475em;font-size: 1.25em;border-radius: 15px;background-color: rgb(210, 106, 2);border-color: rgb(210, 106, 2);color: white;"><i aria-hidden="true" class="fa fa-question"></i></button>' @table_str_right += '<button tabindex="0" class="cancer_help_btn" data-target="#show_help_modal" style="float:left;cursor: pointer;padding: 0em 0.475em;font-size: 1.25em;border-radius: 15px;background-color: rgb(210, 106, 2);border-color: rgb(210, 106, 2);color: white;"><i aria-hidden="true" class="fa fa-question"></i></button>'
@table_str_right += '<input class="help_texts" type="hidden" value="'+@field_property["comment_text"]+'" name='+@field_property["variable"]+'/>' @table_str_right += '<input class="help_texts" type="hidden" value="'+@field_property["comment_text"]+'" name='+@field_property["variable"]+'/>'
else else

View File

@ -1,5 +1,6 @@
# encoding: utf-8 # encoding: utf-8
require 'rubyXL' require 'rubyXL'
require "json"
class CancerpredictsController < ApplicationController class CancerpredictsController < ApplicationController
def initialize def initialize
super super
@ -32,7 +33,7 @@ class CancerpredictsController < ApplicationController
result['page_title'] = @form_to_show.title_texts[params[:locale]] result['page_title'] = @form_to_show.title_texts[params[:locale]]
elsif params['get_mapping_data_from_csv'].to_i == 1 elsif params['get_mapping_data_from_csv'].to_i == 1
result = {} result = {}
result['mapping_data_from_csv'] = YAML.load(@form_to_show.mapping_data_from_csv) rescue {} result['mapping_data_from_csv'] = JSON.parse(@form_to_show.mapping_data_from_csv) rescue {}
else else
@record = Cancerpredictrecord.new @record = Cancerpredictrecord.new
@record.title = @app_title @record.title = @app_title
@ -59,7 +60,7 @@ class CancerpredictsController < ApplicationController
# else # else
# result['cal_value'] = 1 # result['cal_value'] = 1
# end # end
mapping_data_from_csv = YAML.load(@form_to_show.mapping_data_from_csv) rescue {} mapping_data_from_csv = JSON.parse(@form_to_show.mapping_data_from_csv) rescue {}
@form_to_show.form_show.each do |num,property| @form_to_show.form_show.each do |num,property|
@variable = property[:variable] @variable = property[:variable]
if @variable.present? if @variable.present?
@ -102,13 +103,17 @@ class CancerpredictsController < ApplicationController
end end
end end
#result['lpv'] = -0.51427548* (result['sex_value']- 0.508312) + 0.05764604* (result['Age_value'] - 61.894501) + 0.49138819*(result['cal_value'] - 0.334399) rescue 'error' #result['lpv'] = -0.51427548* (result['sex_value']- 0.508312) + 0.05764604* (result['Age_value'] - 61.894501) + 0.49138819*(result['cal_value'] - 0.334399) rescue 'error'
formula = @form_to_show.tmp_lpv_ruby_code formula_variables = @form_to_show.tmp_lpv_variables
formula_variables = @form_to_show.tmp_lpv_variables_ruby_code begin
eval(formula) eval_formula(result)
result['lpv'] = eval(formula_variables.last) rescue => e
@form_to_show.generate_eval_formula
eval_formula(result)
end
result['lpv'] = instance_variable_get(formula_variables.last)
result['lpv_variable'] = {} result['lpv_variable'] = {}
formula_variables.each do |variable_name| formula_variables.each do |variable_name|
eval( "result['lpv_variable']['#{variable_name}'] = #{variable_name}" ) result['lpv_variable']["#{variable_name[1..-1]}"] = instance_variable_get(variable_name)
end end
@years = @form_to_show.years @years = @form_to_show.years
result['table'] = '<input id="current_year" type="hidden" value="'+@years[0].to_s+'" index="0"/><p id="cancer_table_texts">'+@form_to_show.table_above_texts[locale].to_s+'</p>' result['table'] = '<input id="current_year" type="hidden" value="'+@years[0].to_s+'" index="0"/><p id="cancer_table_texts">'+@form_to_show.table_above_texts[locale].to_s+'</p>'
@ -141,7 +146,7 @@ class CancerpredictsController < ApplicationController
year = year.to_f year = year.to_f
end end
year_index = @years.index(year) year_index = @years.index(year)
@servive_ratio = eval(@form_to_show.tmp_years_settings[year_index]) @servive_ratio = eval(@form_to_show.tmp_years_settings_for_ruby[year_index])
@servive_ratio = ((1 - @servive_ratio) * 100).round(2) @servive_ratio = ((1 - @servive_ratio) * 100).round(2)
@texts = @form_to_show.text_above_texts[locale].to_s.gsub('<br/>','</span><br/><span>').gsub('{{Surgery_only}}','<span class="'+@therapy_names[0]+' Overall_Survival">'+@servive_ratio.round(2).to_s+'</span>') @texts = @form_to_show.text_above_texts[locale].to_s.gsub('<br/>','</span><br/><span>').gsub('{{Surgery_only}}','<span class="'+@therapy_names[0]+' Overall_Survival">'+@servive_ratio.round(2).to_s+'</span>')
@texts = @texts.split('{{years}}') @texts = @texts.split('{{years}}')

View File

@ -95,9 +95,10 @@ class Cancerpredictfields
+0.2002546*(O9-0.06176471)" +0.2002546*(O9-0.06176471)"
field :years_settings , type: Array , default: ["0.8095037^( exp(A) )","0.729158^( exp(A) )","0.6717211^( exp(A) )","0.6056773^( exp(A) )"] field :years_settings , type: Array , default: ["0.8095037^( exp(A) )","0.729158^( exp(A) )","0.6717211^( exp(A) )","0.6056773^( exp(A) )"]
field :tmp_years_settings , type: Array , default: [] field :tmp_years_settings , type: Array , default: []
field :tmp_years_settings_for_ruby , type: Array , default: []
field :lpv_calc, type: Hash, default: {} #for js code field :lpv_calc, type: Hash, default: {} #for js code
field :tmp_lpv_ruby_code, type: String, default: "" field :tmp_lpv_ruby_code, type: String, default: ""
field :tmp_lpv_variables_ruby_code, type: Array, default: [] field :tmp_lpv_variables, type: Array, default: []
field :mapping_data_from_csv , type: String ,default: "" field :mapping_data_from_csv , type: String ,default: ""
scope :can_display, ->{where(:is_hidden=>false,:is_preview => false).any_of({:postdate.lt=>Time.now, :deadline.gt=>Time.now},{:postdate.lt=>Time.now, :deadline=>nil}).order_by([:is_top, :desc],[:postdate, :desc])} scope :can_display, ->{where(:is_hidden=>false,:is_preview => false).any_of({:postdate.lt=>Time.now, :deadline.gt=>Time.now},{:postdate.lt=>Time.now, :deadline=>nil}).order_by([:is_top, :desc],[:postdate, :desc])}
scope :is_approved, ->{where(:approved => true)} scope :is_approved, ->{where(:approved => true)}
@ -121,15 +122,33 @@ class Cancerpredictfields
end end
formula = self.prediction_formula.gsub("\r\n"," ").gsub("^","**") formula = self.prediction_formula.gsub("\r\n"," ").gsub("^","**")
result_keys.each do |k| result_keys.each do |k|
formula = formula.gsub(/#{k}?(-|\+|\*|\s|\=)/){ "result[\"#{k}\"]#{$1}" } formula = formula.gsub(/#{k}?(-|\+|\*|\/|\s|\=)/){ "result[\"#{k}\"]#{$1}" }
end end
formula = formula.split(/^([^=]+)=([^=])/).select{|s| s.present?}.each_slice(2).map do |a,b|
if b
("@"+ a + "=" + b)
else
"@"+ a
end
end.join("\n")
self.tmp_lpv_ruby_code = formula self.tmp_lpv_ruby_code = formula
formula_variables = formula.enum_for(:scan,/([^\=]*)?=/).map { ::Regexp.last_match[1] }.map{|s| s.strip.split(/(\s*|;\r\n)/).last} formula_variables = formula.enum_for(:scan,/([^\=\(\)]+)?=[^=]/).map {|x| x[-1] }.compact.map{|s| s.strip}
self.tmp_lpv_variables_ruby_code = formula_variables self.tmp_lpv_variables = formula_variables
self.tmp_years_settings = self.years_settings.map do |s| self.tmp_years_settings = self.years_settings.map do |s|
s.gsub('^','**').gsub('exp','Math.exp') s.gsub('^','**').gsub('exp','Math.exp')
end end
self.tmp_years_settings_for_ruby = self.tmp_years_settings.clone
formula_variables.each do |variable_name|
self.tmp_years_settings_for_ruby = self.tmp_years_settings_for_ruby.map do |y|
y.gsub(variable_name[1..-1],variable_name)
end
end
self.lpv_calc = get_years_settings_dict self.lpv_calc = get_years_settings_dict
self.generate_eval_formula
end
def generate_eval_formula
eval_formula = "def eval_formula(result); #{self.tmp_lpv_ruby_code}; end"
CancerpredictsController.module_eval(eval_formula)
end end
def update_user def update_user
User.find(update_user_id) rescue nil User.find(update_user_id) rescue nil