queries_controller.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. module Blazer
  2. class QueriesController < ApplicationController
  3. # skip all filters
  4. skip_filter *_process_action_callbacks.map(&:filter)
  5. protect_from_forgery with: :exception
  6. if ENV["BLAZER_PASSWORD"]
  7. http_basic_authenticate_with name: ENV["BLAZER_USERNAME"], password: ENV["BLAZER_PASSWORD"]
  8. end
  9. layout "blazer/application"
  10. before_action :ensure_database_url
  11. before_action :set_query, only: [:show, :edit, :update, :destroy]
  12. def index
  13. @queries = Blazer::Query.order(:name).includes(:creator)
  14. @trending_queries = Blazer::Audit.group(:query_id).where("created_at > ?", 2.days.ago).having("COUNT(DISTINCT user_id) >= 3").uniq.count(:user_id)
  15. end
  16. def new
  17. @query = Blazer::Query.new(statement: params[:statement])
  18. end
  19. def create
  20. @query = Blazer::Query.new(query_params)
  21. @query.creator = current_user if respond_to?(:current_user)
  22. if @query.save
  23. redirect_to @query
  24. else
  25. render :new
  26. end
  27. end
  28. def show
  29. @statement = @query.statement
  30. process_vars(@statement)
  31. @smart_vars = {}
  32. @sql_errors = []
  33. @bind_vars.each do |var|
  34. query = smart_variables[var]
  35. if query
  36. rows, error = run_statement(query)
  37. @smart_vars[var] = rows.map{|v| v.values.reverse }
  38. @sql_errors << error if error
  39. end
  40. end
  41. end
  42. def edit
  43. end
  44. def run
  45. @statement = params[:statement]
  46. process_vars(@statement)
  47. if @success
  48. @query = Query.find_by(id: params[:query_id]) if params[:query_id]
  49. # audit
  50. if Blazer.audit
  51. audit = Blazer::Audit.new(statement: @statement)
  52. audit.query = @query
  53. audit.user = current_user if respond_to?(:current_user)
  54. audit.save!
  55. end
  56. @rows, @error = run_statement(@statement)
  57. @columns = {}
  58. if @rows.any?
  59. @rows.first.each do |key, value|
  60. @columns[key] =
  61. case value
  62. when Integer
  63. "int"
  64. when Float
  65. "float"
  66. else
  67. "string-ins"
  68. end
  69. end
  70. end
  71. @filename = @query.name.parameterize if @query
  72. @min_width_types = (@rows.first || {}).select{|k, v| v.is_a?(Time) or v.is_a?(String) or smart_columns[k] }.keys
  73. @boom = {}
  74. @columns.keys.each do |key|
  75. query = smart_columns[key]
  76. if query
  77. values = @rows.map{|r| r[key] }.compact.uniq
  78. rows, error = run_statement(ActiveRecord::Base.send(:sanitize_sql_array, [query.sub("{value}", "(?)"), values]))
  79. @boom[key] = Hash[ rows.map(&:values) ]
  80. end
  81. end
  82. @linked_columns = linked_columns
  83. end
  84. respond_to do |format|
  85. format.html do
  86. render layout: false
  87. end
  88. format.csv do
  89. send_data csv_data(@rows), type: 'text/csv; charset=utf-8; header=present', disposition: "attachment; filename=\"#{@query ? @query.name.parameterize : "query"}.csv\""
  90. end
  91. end
  92. end
  93. def update
  94. if @query.update(query_params)
  95. redirect_to query_path(@query)
  96. else
  97. render :edit
  98. end
  99. end
  100. def destroy
  101. @query.destroy
  102. redirect_to root_url
  103. end
  104. private
  105. def ensure_database_url
  106. render text: "BLAZER_DATABASE_URL required" if !ENV["BLAZER_DATABASE_URL"] && !Rails.env.development?
  107. end
  108. def set_query
  109. @query = Blazer::Query.find(params[:id].to_s.split("-").first)
  110. end
  111. def query_params
  112. params.require(:query).permit(:name, :description, :statement)
  113. end
  114. def csv_data(rows)
  115. CSV.generate do |csv|
  116. if rows.any?
  117. csv << rows.first.keys
  118. end
  119. rows.each do |row|
  120. csv << row.values
  121. end
  122. end
  123. end
  124. def run_statement(statement)
  125. rows = []
  126. error = nil
  127. begin
  128. Blazer::Connection.transaction do
  129. Blazer::Connection.connection.execute("SET statement_timeout = #{Blazer.timeout * 1000}") if Blazer.timeout && postgresql?
  130. result = Blazer::Connection.connection.select_all(statement)
  131. result.each do |untyped_row|
  132. row = {}
  133. untyped_row.each do |k, v|
  134. row[k] = result.column_types.empty? ? v : result.column_types[k].send(:type_cast, v)
  135. end
  136. rows << row
  137. end
  138. raise ActiveRecord::Rollback
  139. end
  140. rescue ActiveRecord::StatementInvalid => e
  141. error = e.message.sub(/.+ERROR: /, "")
  142. end
  143. [rows, error]
  144. end
  145. def extract_vars(statement)
  146. statement.scan(/\{.*?\}/).map{|v| v[1...-1] }.uniq
  147. end
  148. def process_vars(statement)
  149. @bind_vars = extract_vars(statement)
  150. @success = @bind_vars.all?{|v| params[v] }
  151. if @success
  152. @bind_vars.each do |var|
  153. value = params[var].presence
  154. value = value.to_i if value.to_i.to_s == value
  155. if var.end_with?("_at")
  156. value = Blazer.time_zone.parse(value) rescue nil
  157. end
  158. statement.gsub!("{#{var}}", ActiveRecord::Base.connection.quote(value))
  159. end
  160. end
  161. end
  162. def settings
  163. YAML.load(File.read(Rails.root.join("config", "blazer.yml")))
  164. end
  165. def linked_columns
  166. settings["linked_columns"] || {}
  167. end
  168. def smart_columns
  169. settings["smart_columns"] || {}
  170. end
  171. def smart_variables
  172. settings["smart_variables"] || {}
  173. end
  174. def tables
  175. default_schema = postgresql? ? "public" : Blazer::Connection.connection_config[:database]
  176. schema = Blazer::Connection.connection_config[:schema] || default_schema
  177. rows, error = run_statement(Blazer::Connection.send(:sanitize_sql_array, ["SELECT table_name, column_name, ordinal_position, data_type FROM information_schema.columns WHERE table_schema = ?", schema]))
  178. Hash[ rows.group_by{|r| r["table_name"] }.map{|t, f| [t, f.sort_by{|f| f["ordinal_position"] }.map{|f| f.slice("column_name", "data_type") }] }.sort_by{|t, f| t } ]
  179. end
  180. helper_method :tables
  181. def postgresql?
  182. Blazer::Connection.connection.adapter_name == "PostgreSQL"
  183. end
  184. end
  185. end