Format files. (fix indent)
This commit is contained in:
parent
d2b3464359
commit
be5b892aee
File diff suppressed because it is too large
Load Diff
|
@ -1,221 +1,227 @@
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
require 'rubyXL'
|
require "rubyXL"
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
class CancerpredictsController < ApplicationController
|
class CancerpredictsController < ApplicationController
|
||||||
def initialize
|
def initialize
|
||||||
super
|
super
|
||||||
@app_title = "cancerpredict"
|
@app_title = "cancerpredict"
|
||||||
end
|
end
|
||||||
|
|
||||||
def calculate
|
def calculate
|
||||||
create_first_field
|
create_first_field
|
||||||
if params['header'].to_i == 1
|
if params["header"].to_i == 1
|
||||||
locale = params['locale'].to_s rescue 'zh_tw'
|
locale = params["locale"].to_s rescue "zh_tw"
|
||||||
locale = 'zh_tw' if locale == 'zh_cn'
|
locale = "zh_tw" if locale == "zh_cn"
|
||||||
result = {}
|
result = {}
|
||||||
@head_images = {}
|
@head_images = {}
|
||||||
@form_to_show.head_images_id.each do |image_id|
|
@form_to_show.head_images_id.each do |image_id|
|
||||||
next if image_id.to_s == ""
|
next if image_id.to_s == ""
|
||||||
@image = Headimages.find_by(:id=>image_id.to_s)
|
@image = Headimages.find_by(:id => image_id.to_s)
|
||||||
@url = @image.temp_file.to_s
|
@url = @image.temp_file.to_s
|
||||||
@head_images[@image.sort_number.to_i] = ('<img class="head_logo" alt ="' + Pathname.new(@image.temp_file.file.file).basename.to_s+'" src="'+@url+'"/>')
|
@head_images[@image.sort_number.to_i] = ('<img class="head_logo" alt ="' + Pathname.new(@image.temp_file.file.file).basename.to_s + '" src="' + @url + '"/>')
|
||||||
end
|
end
|
||||||
result['head_images'] = Hash[@head_images.sort].values.join('')
|
result["head_images"] = Hash[@head_images.sort].values.join("")
|
||||||
@head_images = {}
|
@head_images = {}
|
||||||
@form_to_show.title_images_id.each do |image_id|
|
@form_to_show.title_images_id.each do |image_id|
|
||||||
next if image_id.to_s == ""
|
next if image_id.to_s == ""
|
||||||
@image = Headimages.find_by(:id=>image_id.to_s)
|
@image = Headimages.find_by(:id => image_id.to_s)
|
||||||
@url = @image.temp_file.to_s
|
@url = @image.temp_file.to_s
|
||||||
@head_images[@image.sort_number.to_i] = ('<img class="head_logo" alt ="' + Pathname.new(@image.temp_file.file.file).basename.to_s+'" src="'+@url+'"/>')
|
@head_images[@image.sort_number.to_i] = ('<img class="head_logo" alt ="' + Pathname.new(@image.temp_file.file.file).basename.to_s + '" src="' + @url + '"/>')
|
||||||
end
|
end
|
||||||
result['danger_texts'] = (@form_to_show.danger_texts[locale] rescue '')
|
result["danger_texts"] = (@form_to_show.danger_texts[locale] rescue "")
|
||||||
result['title'] = Hash[@head_images.sort].values.join('')
|
result["title"] = Hash[@head_images.sort].values.join("")
|
||||||
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'] = JSON.parse(@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
|
||||||
@choice_keys = []
|
@choice_keys = []
|
||||||
@choice_values = []
|
@choice_values = []
|
||||||
@choice_names = []
|
@choice_names = []
|
||||||
@form_to_show.form_show.values.each{|choice| @choice_keys.push choice[:variable]}
|
@form_to_show.form_show.values.each { |choice| @choice_keys.push choice[:variable] }
|
||||||
@form_to_show.form_show.values.each{|choice| @choice_values.push ((choice[:is_num] == 1) ? [] : choice[:choice_fields])}
|
@form_to_show.form_show.values.each { |choice| @choice_values.push ((choice[:is_num] == 1) ? [] : choice[:choice_fields]) }
|
||||||
@form_to_show.form_show.values.each{|choice| @choice_names.push choice[:name]}
|
@form_to_show.form_show.values.each { |choice| @choice_names.push choice[:name] }
|
||||||
@choice_keys.each_with_index{|key,i| @record.names[key] = @choice_names[i]}
|
@choice_keys.each_with_index { |key, i| @record.names[key] = @choice_names[i] }
|
||||||
@choice_keys.each_with_index{|key,i| @record.values[key] = @choice_values[i]}
|
@choice_keys.each_with_index { |key, i| @record.values[key] = @choice_values[i] }
|
||||||
params['data'].each do |rec_key,rec_value|
|
params["data"].each do |rec_key, rec_value|
|
||||||
@record.result[rec_key] = rec_value
|
@record.result[rec_key] = rec_value
|
||||||
end
|
end
|
||||||
@record.submit_time = Time.now.to_s
|
@record.submit_time = Time.now.to_s
|
||||||
@record.save
|
@record.save
|
||||||
locale = params['data']['locale'].to_s rescue 'zh_tw'
|
locale = params["data"]["locale"].to_s rescue "zh_tw"
|
||||||
locale = 'zh_tw' if locale == 'zh_cn'
|
locale = "zh_tw" if locale == "zh_cn"
|
||||||
result = {}
|
result = {}
|
||||||
mapping_data_from_csv = JSON.parse(@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.all_variables.each do |v|
|
@form_to_show.all_variables.each do |v|
|
||||||
result[v] = 0
|
result[v] = 0
|
||||||
end
|
end
|
||||||
@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?
|
||||||
if property[:is_num] == 1
|
if property[:is_num] == 1
|
||||||
if property[:is_float] == 1
|
if property[:is_float] == 1
|
||||||
result[@variable] = params['data'][@variable].to_f rescue 0.0
|
result[@variable] = params["data"][@variable].to_f rescue 0.0
|
||||||
else
|
else
|
||||||
result[@variable] = params['data'][@variable].to_i rescue 0
|
result[@variable] = params["data"][@variable].to_i rescue 0
|
||||||
end
|
end
|
||||||
elsif property[:choice_fields].present?
|
elsif property[:choice_fields].present?
|
||||||
if !(@form_to_show.advance_mode)
|
if !(@form_to_show.advance_mode)
|
||||||
result[@variable] = params['data'][@variable].to_i rescue 0
|
result[@variable] = params["data"][@variable].to_i rescue 0
|
||||||
else
|
else
|
||||||
if property[:need_map_values] == 1
|
if property[:need_map_values] == 1
|
||||||
result[@variable] = property[:map_values][params['data'][@variable].to_i - 1]
|
result[@variable] = property[:map_values][params["data"][@variable].to_i - 1]
|
||||||
else
|
else
|
||||||
if property[:revert_value] != 1
|
if property[:revert_value] != 1
|
||||||
result[@variable] = params['data'][@variable].to_i - 1
|
result[@variable] = params["data"][@variable].to_i - 1
|
||||||
else
|
else
|
||||||
result[@variable] = ((property[:choice_fields].length - params['data'][@variable].to_i) rescue params['data'][@variable].to_i)
|
result[@variable] = ((property[:choice_fields].length - params["data"][@variable].to_i) rescue params["data"][@variable].to_i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if @form_to_show.advance_mode && property[:cancer_predict_mapping_file].present?
|
if @form_to_show.advance_mode && property[:cancer_predict_mapping_file].present?
|
||||||
if (mapping_data_from_csv != {})
|
if (mapping_data_from_csv != {})
|
||||||
mapping_hash = mapping_data_from_csv[@variable]
|
mapping_hash = mapping_data_from_csv[@variable]
|
||||||
temp_index = 0
|
temp_index = 0
|
||||||
temp_value = result[@variable]
|
temp_value = result[@variable]
|
||||||
mapping_hash.each_with_index do |(k,v),i|
|
mapping_hash.each_with_index do |(k, v), i|
|
||||||
if i == 0
|
if i == 0
|
||||||
index_val = v.index(temp_value) rescue nil
|
index_val = v.index(temp_value) rescue nil
|
||||||
if !index_val.nil?
|
if !index_val.nil?
|
||||||
temp_index = index_val
|
temp_index = index_val
|
||||||
else
|
else
|
||||||
closest_value = v.min_by{|x| (temp_value-x).abs}
|
closest_value = v.min_by { |x| (temp_value - x).abs }
|
||||||
temp_index = v.index(closest_value)
|
temp_index = v.index(closest_value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
result[k] = v[temp_index]
|
result[k] = v[temp_index]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
formula_variables = @form_to_show.tmp_lpv_variables
|
formula_variables = @form_to_show.tmp_lpv_variables
|
||||||
begin
|
begin
|
||||||
eval_hidden_variables(result)
|
eval_hidden_variables(result)
|
||||||
rescue => e
|
rescue => e
|
||||||
@form_to_show.generate_eval_formula
|
@form_to_show.generate_eval_formula
|
||||||
eval_hidden_variables(result)
|
eval_hidden_variables(result)
|
||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
eval_formula(result)
|
eval_formula(result)
|
||||||
rescue => e
|
rescue => e
|
||||||
@form_to_show.generate_eval_formula
|
@form_to_show.generate_eval_formula
|
||||||
eval_formula(result)
|
eval_formula(result)
|
||||||
end
|
end
|
||||||
result['lpv'] = result[formula_variables.last]
|
result["lpv"] = result[formula_variables.last]
|
||||||
result['lpv_variable'] = {}
|
result["lpv_variable"] = {}
|
||||||
formula_variables.each do |variable_name|
|
formula_variables.each do |variable_name|
|
||||||
result['lpv_variable']["#{variable_name}"] = result[variable_name]
|
result["lpv_variable"]["#{variable_name}"] = result[variable_name]
|
||||||
end
|
end
|
||||||
@years = @form_to_show.years
|
@years = @form_to_show.years
|
||||||
result['years'] = @years
|
result["years"] = @years
|
||||||
@therapy_choices = [I18n.t('cancerpredict.table.Surgeryonly')]
|
@therapy_choices = [I18n.t("cancerpredict.table.Surgeryonly")]
|
||||||
@form_to_show.form_show_in_result.values.each{|choice| @therapy_choices.push choice["name"][locale]}
|
@form_to_show.form_show_in_result.values.each { |choice| @therapy_choices.push choice["name"][locale] }
|
||||||
@therapy_names = @form_to_show.treatment_method
|
@therapy_names = @form_to_show.treatment_method
|
||||||
result['treatment_method'] = @therapy_names
|
result["treatment_method"] = @therapy_names
|
||||||
result['treatment_method_active_indices'] = @form_to_show.treatment_method_active_indices
|
result["treatment_method_active_indices"] = @form_to_show.treatment_method_active_indices
|
||||||
result['table'] = @form_to_show.result_table_translations[locale]
|
result["table"] = @form_to_show.result_table_translations[locale]
|
||||||
year = params['data']['year'] rescue nil
|
year = params["data"]["year"] rescue nil
|
||||||
if year.nil?
|
if year.nil?
|
||||||
year = @years.last.to_f
|
year = @years.last.to_f
|
||||||
else
|
else
|
||||||
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_for_ruby[year_index])
|
@servive_ratio = eval(@form_to_show.tmp_years_settings_for_ruby[year_index])
|
||||||
@servive_ratio = (@servive_ratio * 100).round(2)
|
@servive_ratio = (@servive_ratio * 100).round(2)
|
||||||
result['texts'] = @form_to_show.result_text_translations[locale]
|
result["texts"] = @form_to_show.result_text_translations[locale]
|
||||||
result['extra_therapy_texts'] = @form_to_show.extra_therapy_texts[locale] rescue @form_to_show.extra_therapy_texts['zh_tw']
|
result["extra_therapy_texts"] = @form_to_show.extra_therapy_texts[locale] rescue @form_to_show.extra_therapy_texts["zh_tw"]
|
||||||
result['servive_ratio'] = @servive_ratio
|
result["servive_ratio"] = @servive_ratio
|
||||||
end
|
end
|
||||||
result = result.merge(params)
|
result = result.merge(params)
|
||||||
render :json=> result
|
render :json => result
|
||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
uid = OrbitHelper.params[:uid] rescue ""
|
uid = OrbitHelper.params[:uid] rescue ""
|
||||||
tags = OrbitHelper.widget_tags
|
tags = OrbitHelper.widget_tags
|
||||||
categories = OrbitHelper.widget_categories || []
|
categories = OrbitHelper.widget_categories || []
|
||||||
@table_str = File.read("#{Cancerpredictfields::ToolTablePrefix}#{I18n.locale}.txt")
|
@table_str = File.read("#{Cancerpredictfields::ToolTablePrefix}#{I18n.locale}.txt")
|
||||||
preidct_js_url = '/assets/cancer_predict.js'
|
preidct_js_url = "/assets/cancer_predict.js"
|
||||||
if File.exist?(Cancerpredictfields::JSFileName)
|
if File.exist?(Cancerpredictfields::JSFileName)
|
||||||
js_filename = File.read(Cancerpredictfields::JSFileName)
|
js_filename = File.read(Cancerpredictfields::JSFileName)
|
||||||
if js_filename.include?('cancer_predict.js')
|
if js_filename.include?("cancer_predict.js")
|
||||||
asset = Rails.application.assets[js_filename]
|
asset = Rails.application.assets[js_filename]
|
||||||
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}"
|
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}"
|
||||||
else
|
else
|
||||||
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{File.basename(js_filename)}"
|
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{File.basename(js_filename)}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
{
|
{
|
||||||
"cancerpredict" => [],
|
"cancerpredict" => [],
|
||||||
"extras"=>{"table"=> @table_str,'preidct_js_url'=>preidct_js_url}
|
"extras" => { "table" => @table_str, "preidct_js_url" => preidct_js_url },
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def widget
|
def widget
|
||||||
uid = OrbitHelper.params[:uid] rescue ""
|
uid = OrbitHelper.params[:uid] rescue ""
|
||||||
tags = OrbitHelper.widget_tags
|
tags = OrbitHelper.widget_tags
|
||||||
categories = OrbitHelper.widget_categories || []
|
categories = OrbitHelper.widget_categories || []
|
||||||
@table_str = File.read("#{Cancerpredictfields::ToolTablePrefix}#{I18n.locale}.txt")
|
@table_str = File.read("#{Cancerpredictfields::ToolTablePrefix}#{I18n.locale}.txt")
|
||||||
preidct_js_url = '/assets/cancer_predict.js'
|
preidct_js_url = "/assets/cancer_predict.js"
|
||||||
if File.exist?(Cancerpredictfields::JSFileName)
|
if File.exist?(Cancerpredictfields::JSFileName)
|
||||||
js_filename = File.read(Cancerpredictfields::JSFileName)
|
js_filename = File.read(Cancerpredictfields::JSFileName)
|
||||||
if js_filename.include?('cancer_predict.js')
|
if js_filename.include?("cancer_predict.js")
|
||||||
asset = Rails.application.assets[js_filename]
|
asset = Rails.application.assets[js_filename]
|
||||||
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}"
|
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}"
|
||||||
else
|
else
|
||||||
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{File.basename(js_filename)}"
|
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{File.basename(js_filename)}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
{
|
{
|
||||||
"cancerpredict" => [],
|
"cancerpredict" => [],
|
||||||
"extras"=>{"table"=> @table_str,'preidct_js_url'=>preidct_js_url}
|
"extras" => { "table" => @table_str, "preidct_js_url" => preidct_js_url },
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_first_field
|
def create_first_field
|
||||||
if Cancerpredictfields.where("title"=>(@app_title + "_back")).count == 0
|
if Cancerpredictfields.where("title" => (@app_title + "_back")).count == 0
|
||||||
@form_to_show = Cancerpredictfields.new()
|
@form_to_show = Cancerpredictfields.new()
|
||||||
@form_to_show.title = @app_title +"_back"
|
@form_to_show.title = @app_title + "_back"
|
||||||
@form_to_show.save
|
@form_to_show.save
|
||||||
end
|
end
|
||||||
@form_to_show
|
@form_to_show
|
||||||
if Cancerpredictfields.where("title"=>@app_title).count == 0
|
if Cancerpredictfields.where("title" => @app_title).count == 0
|
||||||
@form_to_show = Cancerpredictfields.new()
|
@form_to_show = Cancerpredictfields.new()
|
||||||
@form_to_show.title=@app_title
|
@form_to_show.title = @app_title
|
||||||
@form_to_show.save
|
@form_to_show.save
|
||||||
@form_to_show = Cancerpredictfields.where("title"=>@app_title).first
|
@form_to_show = Cancerpredictfields.where("title" => @app_title).first
|
||||||
else
|
else
|
||||||
@form_to_show = Cancerpredictfields.where("title"=>@app_title).first
|
@form_to_show = Cancerpredictfields.where("title" => @app_title).first
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_mapping_file(mapping_file)
|
def read_mapping_file(mapping_file)
|
||||||
if mapping_file.class != CancerPredictMappingFile
|
if mapping_file.class != CancerPredictMappingFile
|
||||||
mapping_file = CancerPredictMappingFile.find(mapping_file_id) rescue nil
|
mapping_file = CancerPredictMappingFile.find(mapping_file_id) rescue nil
|
||||||
end
|
end
|
||||||
if !mapping_file.nil?
|
if !mapping_file.nil?
|
||||||
csv_rows = CSV.read(mapping_file.temp_file.file.path)
|
csv_rows = CSV.read(mapping_file.temp_file.file.path)
|
||||||
titles = csv_rows[0]
|
titles = csv_rows[0]
|
||||||
infos = {}
|
infos = {}
|
||||||
titles.each_with_index do |title,i|
|
titles.each_with_index do |title, i|
|
||||||
infos[title] = []
|
infos[title] = []
|
||||||
csv_rows[1..-1].each do |row|
|
csv_rows[1..-1].each do |row|
|
||||||
infos[title] << row[i].to_f
|
infos[title] << row[i].to_f
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return infos
|
return infos
|
||||||
else
|
else
|
||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in New Issue