dashboards_controller.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. module Blazer
  2. class DashboardsController < BaseController
  3. before_action :set_dashboard, only: [:show, :edit, :update, :destroy, :refresh]
  4. def index
  5. @dashboards = Blazer::Dashboard.order(:name)
  6. end
  7. def new
  8. @dashboard = Blazer::Dashboard.new
  9. end
  10. def create
  11. @dashboard = Blazer::Dashboard.new
  12. # use creator_id instead of creator
  13. # since we setup association without checking if column exists
  14. @dashboard.creator = blazer_user if @dashboard.respond_to?(:creator_id=) && blazer_user
  15. if update_dashboard(@dashboard)
  16. redirect_to dashboard_path(@dashboard)
  17. else
  18. render :new
  19. end
  20. end
  21. def show
  22. @queries = @dashboard.dashboard_queries.order(:position).preload(:query).map(&:query)
  23. @queries.each do |query|
  24. process_vars(query.statement, query.data_source)
  25. end
  26. @bind_vars ||= []
  27. @smart_vars = {}
  28. @sql_errors = []
  29. @data_sources = @queries.map { |q| Blazer.data_sources[q.data_source] }.uniq
  30. @bind_vars.each do |var|
  31. @data_sources.each do |data_source|
  32. query = data_source.smart_variables[var]
  33. if query
  34. columns, rows, error, cached_at = data_source.run_statement(query)
  35. ((@smart_vars[var] ||= []).concat(rows.map { |v| v.reverse })).uniq!
  36. @sql_errors << error if error
  37. end
  38. end
  39. end
  40. end
  41. def edit
  42. end
  43. def update
  44. if update_dashboard(@dashboard)
  45. redirect_to dashboard_path(@dashboard, variable_params)
  46. else
  47. render :edit
  48. end
  49. end
  50. def destroy
  51. @dashboard.destroy
  52. redirect_to dashboards_path
  53. end
  54. def refresh
  55. @dashboard.queries.each do |query|
  56. data_source = Blazer.data_sources[query.data_source]
  57. statement = query.statement.dup
  58. process_vars(statement, query.data_source)
  59. Blazer.transform_statement.call(data_source, statement) if Blazer.transform_statement
  60. data_source.clear_cache(statement)
  61. end
  62. redirect_to dashboard_path(@dashboard, variable_params)
  63. end
  64. protected
  65. def dashboard_params
  66. params.require(:dashboard).permit(:name)
  67. end
  68. def set_dashboard
  69. @dashboard = Blazer::Dashboard.find(params[:id])
  70. end
  71. def update_dashboard(dashboard)
  72. dashboard.assign_attributes(dashboard_params)
  73. Blazer::Dashboard.transaction do
  74. if params[:query_ids].is_a?(Array)
  75. query_ids = params[:query_ids].map(&:to_i)
  76. @queries = Blazer::Query.find(query_ids).sort_by { |q| query_ids.index(q.id) }
  77. end
  78. if dashboard.save
  79. if @queries
  80. @queries.each_with_index do |query, i|
  81. dashboard_query = dashboard.dashboard_queries.where(query_id: query.id).first_or_initialize
  82. dashboard_query.position = i
  83. dashboard_query.save!
  84. end
  85. if dashboard.persisted?
  86. dashboard.dashboard_queries.where.not(query_id: query_ids).destroy_all
  87. end
  88. end
  89. true
  90. end
  91. end
  92. end
  93. end
  94. end