Class: Blazer::QueriesController
- Inherits:
-
BaseController
- Object
- ApplicationController
- BaseController
- Blazer::QueriesController
- Defined in:
- app/controllers/blazer/queries_controller.rb
Instance Method Summary collapse
- #cancel ⇒ Object
- #columns ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #home ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
- #refresh ⇒ Object
- #run ⇒ Object
- #schema ⇒ Object
- #show ⇒ Object
- #tables ⇒ Object
- #update ⇒ Object
Instance Method Details
#cancel ⇒ Object
201 202 203 204 |
# File 'app/controllers/blazer/queries_controller.rb', line 201 def cancel @data_source.cancel(blazer_run_id) head :ok end |
#columns ⇒ Object
187 188 189 190 191 192 193 194 195 |
# File 'app/controllers/blazer/queries_controller.rb', line 187 def columns column_names = [] @data_source.schema.map do |t| if params[:tables].include?(t[:table]) column_names += t[:columns].map { |c| c[:name] } end end render json: column_names end |
#create ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/controllers/blazer/queries_controller.rb', line 49 def create return render_forbidden unless Blazer::Query.creatable?(blazer_user) @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_errors @query end end |
#destroy ⇒ Object
178 179 180 181 |
# File 'app/controllers/blazer/queries_controller.rb', line 178 def destroy @query.destroy if @query.editable?(blazer_user) redirect_to root_url end |
#edit ⇒ Object
77 78 |
# File 'app/controllers/blazer/queries_controller.rb', line 77 def edit end |
#home ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/controllers/blazer/queries_controller.rb', line 7 def home if params[:filter] == "dashboards" @queries = [] else set_queries(1000) end if params[:filter] && params[:filter] != "dashboards" @dashboards = [] # TODO show my dashboards else @dashboards = Blazer::Dashboard.order(:name) @dashboards = @dashboards.includes(:creator) if Blazer.user_class end @dashboards = @dashboards.map do |d| { id: d.id, name: d.name, creator: blazer_user && d.try(:creator) == blazer_user ? "You" : d.try(:creator).try(Blazer.user_name), to_param: d.to_param, dashboard: true } end end |
#index ⇒ Object
33 34 35 36 |
# File 'app/controllers/blazer/queries_controller.rb', line 33 def index set_queries render json: @queries end |
#new ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'app/controllers/blazer/queries_controller.rb', line 38 def new return render_forbidden unless Blazer::Query.creatable?(blazer_user) @query = Blazer::Query.new( data_source: params[:data_source], name: params[:name] ) if params[:fork_query_id] @query.statement ||= Blazer::Query.find(params[:fork_query_id]).try(:statement) end end |
#refresh ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'app/controllers/blazer/queries_controller.rb', line 154 def refresh data_source = Blazer.data_sources[@query.data_source] @statement = @query.statement.dup process_vars(@statement, @query.data_source) 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
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 142 143 144 145 146 147 148 149 150 151 152 |
# File 'app/controllers/blazer/queries_controller.rb', line 80 def run @statement = params[:statement] data_source = params[:data_source] process_vars(@statement, data_source) @only_chart = params[:only_chart] @run_id = blazer_params[:run_id] @query = Query.find_by(id: params[:query_id]) if params[:query_id] data_source = @query.data_source if @query && @query.data_source @data_source = Blazer.data_sources[data_source] # ensure viewable if !(@query || Query.new(data_source: @data_source.id)).viewable?(blazer_user) render_forbidden elsif @run_id @timestamp = blazer_params[:timestamp].to_i @result = @data_source.run_results(@run_id) @success = !@result.nil? if @success @data_source.delete_results(@run_id) @columns = @result.columns @rows = @result.rows @error = @result.error @just_cached = !@result.error && @result.cached_at.present? @cached_at = nil params[:data_source] = nil render_run elsif Time.now > Time.at(@timestamp + (@data_source.timeout || 600).to_i + 5) # query lost Rails.logger.info "[blazer lost query] #{@run_id}" @error = "We lost your query :(" @rows = [] @columns = [] render_run else continue_run end elsif @success @run_id = blazer_run_id = {user: blazer_user, query: @query, refresh_cache: params[:check], run_id: @run_id, async: Blazer.async} if Blazer.async && request.format.symbol != :csv result = [] Blazer::RunStatementJob.perform_async(result, @data_source, @statement, ) wait_start = Time.now loop do sleep(0.02) break if result.any? || Time.now - wait_start > 3 end @result = result.first else @result = Blazer::RunStatement.new.perform(@data_source, @statement, ) end if @result @data_source.delete_results(@run_id) if @run_id @columns = @result.columns @rows = @result.rows @error = @result.error @cached_at = @result.cached_at @just_cached = @result.just_cached render_run else @timestamp = Time.now.to_i continue_run end else render layout: false end end |
#schema ⇒ Object
197 198 199 |
# File 'app/controllers/blazer/queries_controller.rb', line 197 def schema @schema = @data_source.schema end |
#show ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/controllers/blazer/queries_controller.rb', line 61 def show @statement = @query.statement.dup process_vars(@statement, @query.data_source) @smart_vars = {} @sql_errors = [] data_source = Blazer.data_sources[@query.data_source] @bind_vars.each do |var| smart_var, error = parse_smart_variables(var, data_source) @smart_vars[var] = smart_var if smart_var @sql_errors << error if error end Blazer.transform_statement.call(data_source, @statement) if Blazer.transform_statement end |
#tables ⇒ Object
183 184 185 |
# File 'app/controllers/blazer/queries_controller.rb', line 183 def tables render json: @data_source.tables end |
#update ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'app/controllers/blazer/queries_controller.rb', line 163 def update if params[:commit] == "Fork" @query = Blazer::Query.new @query.creator = blazer_user if @query.respond_to?(:creator) end unless @query.editable?(blazer_user) @query.errors.add(:base, "Sorry, permission denied") end if @query.errors.empty? && @query.update(query_params) redirect_to query_path(@query, variable_params) else render_errors @query end end |