Class: Blazer::QueriesController
- Inherits:
-
BaseController
- Object
- ApplicationController
- BaseController
- Blazer::QueriesController
- Defined in:
- app/controllers/blazer/queries_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #home ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
- #refresh ⇒ Object
- #run ⇒ Object
- #show ⇒ Object
- #tables ⇒ Object
- #update ⇒ Object
Instance Method Details
#create ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'app/controllers/blazer/queries_controller.rb', line 22 def create @query = Blazer::Query.new(query_params) @query.creator = blazer_user if @query.respond_to?(:creator) if @query.save redirect_to query_path(@query, variable_params) else render :new end end |
#destroy ⇒ Object
160 161 162 163 |
# File 'app/controllers/blazer/queries_controller.rb', line 160 def destroy @query.destroy redirect_to root_url end |
#edit ⇒ Object
52 53 |
# File 'app/controllers/blazer/queries_controller.rb', line 52 def edit end |
#home ⇒ Object
5 6 7 |
# File 'app/controllers/blazer/queries_controller.rb', line 5 def home set_queries(1000) end |
#index ⇒ Object
9 10 11 12 |
# File 'app/controllers/blazer/queries_controller.rb', line 9 def index set_queries render partial: "index", layout: false end |
#new ⇒ Object
14 15 16 17 18 19 20 |
# File 'app/controllers/blazer/queries_controller.rb', line 14 def new @query = Blazer::Query.new( statement: params[:statement], data_source: params[:data_source], name: params[:name] ) end |
#refresh ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'app/controllers/blazer/queries_controller.rb', line 143 def refresh data_source = Blazer.data_sources[@query.data_source] @statement = @query.statement.dup process_vars(@statement) Blazer.transform_statement.call(data_source, @statement) if Blazer.transform_statement data_source.clear_cache(@statement) redirect_to query_path(@query, variable_params) end |
#run ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/controllers/blazer/queries_controller.rb', line 55 def run @statement = params[:statement] process_vars(@statement) @only_chart = params[:only_chart] if @success @query = Query.find_by(id: params[:query_id]) if params[:query_id] data_source = params[:data_source] data_source = @query.data_source if @query && @query.data_source @data_source = Blazer.data_sources[data_source] Blazer.transform_statement.call(@data_source, @statement) if Blazer.transform_statement # audit if Blazer.audit audit = Blazer::Audit.new(statement: @statement) audit.query = @query audit.data_source = data_source audit.user = blazer_user audit.save! end @rows, @error, @cached_at = @data_source.run_statement(@statement, user: blazer_user, query: @query, refresh_cache: params[:check]) if @query && !@error.to_s.include?("canceling statement due to statement timeout") @query.checks.each do |check| check.update_state(@rows, @error) end end @columns = {} if @rows.any? @rows.first.each do |key, value| @columns[key] = case value when Integer "int" when Float "float" else "string-ins" end end end @filename = @query.name.parameterize if @query @min_width_types = (@rows.first || {}).select { |k, v| v.is_a?(Time) || v.is_a?(String) || @data_source.smart_columns[k] }.keys @boom = {} @columns.keys.each do |key| query = @data_source.smart_columns[key] if query values = @rows.map { |r| r[key] }.compact.uniq rows, error, cached_at = @data_source.run_statement(ActiveRecord::Base.send(:sanitize_sql_array, [query.sub("{value}", "(?)"), values])) @boom[key] = Hash[rows.map(&:values)] end end @linked_columns = @data_source.linked_columns @markers = [] [["latitude", "longitude"], ["lat", "lon"]].each do |keys| if (keys - (@rows.first || {}).keys).empty? @markers = @rows.select do |r| r[keys.first] && r[keys.last] end.map do |r| { title: r.except(*keys).map{ |k, v| "<strong>#{k}:</strong> #{v}" }.join("<br />").truncate(140), latitude: r[keys.first], longitude: r[keys.last] } end end end end respond_to do |format| format.html do render layout: false end format.csv do send_data csv_data(@rows), type: "text/csv; charset=utf-8; header=present", disposition: "attachment; filename=\"#{@query ? @query.name.parameterize : 'query'}.csv\"" end end end |
#show ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/controllers/blazer/queries_controller.rb', line 33 def show @statement = @query.statement.dup process_vars(@statement) @smart_vars = {} @sql_errors = [] data_source = Blazer.data_sources[@query.data_source] @bind_vars.each do |var| query = data_source.smart_variables[var] if query rows, error, cached_at = data_source.run_statement(query) @smart_vars[var] = rows.map { |v| v.values.reverse } @sql_errors << error if error end end Blazer.transform_statement.call(data_source, @statement) if Blazer.transform_statement end |
#tables ⇒ Object
165 166 167 168 |
# File 'app/controllers/blazer/queries_controller.rb', line 165 def tables @tables = Blazer.data_sources[params[:data_source]].tables.keys render partial: "tables", layout: false end |
#update ⇒ Object
152 153 154 155 156 157 158 |
# File 'app/controllers/blazer/queries_controller.rb', line 152 def update if @query.update(query_params) redirect_to query_path(@query, variable_params) else render :edit end end |