nquery 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +36 -0
- data/README.md +54 -0
- data/app/assets/builds/nquery/application.css +110 -6
- data/app/assets/builds/nquery/application.js +401 -37
- data/app/controllers/concerns/nquery/breadcrumbs.rb +2 -13
- data/app/controllers/concerns/nquery/chart_actions.rb +5 -5
- data/app/controllers/concerns/nquery/chart_results.rb +70 -9
- data/app/controllers/concerns/nquery/sharing_page.rb +38 -0
- data/app/controllers/nquery/application_controller.rb +2 -0
- data/app/controllers/nquery/dashboard/cards_controller.rb +28 -0
- data/app/controllers/nquery/dashboards_controller.rb +56 -11
- data/app/controllers/nquery/embed/charts_controller.rb +15 -13
- data/app/controllers/nquery/embed/dashboards_controller.rb +34 -8
- data/app/controllers/nquery/public/charts_controller.rb +19 -0
- data/app/controllers/nquery/public/dashboards_controller.rb +33 -0
- data/app/controllers/nquery/public_resources_controller.rb +79 -0
- data/app/controllers/nquery/queries_controller.rb +25 -48
- data/app/controllers/nquery/sharing/base_controller.rb +89 -0
- data/app/controllers/nquery/sharing/embed_tokens_controller.rb +41 -0
- data/app/controllers/nquery/sharing/embeddings_controller.rb +17 -0
- data/app/controllers/nquery/sharing/public_links_controller.rb +21 -0
- data/app/helpers/nquery/charts_helper.rb +13 -0
- data/app/helpers/nquery/sharing_helper.rb +67 -0
- data/app/javascript/nquery/controllers/chart_preview_controller.js +19 -1
- data/app/javascript/nquery/controllers/clipboard_controller.js +23 -0
- data/app/models/concerns/nquery/shareable.rb +43 -0
- data/app/models/nquery/chart.rb +18 -2
- data/app/models/nquery/dashboard.rb +131 -0
- data/app/models/nquery/data_source.rb +1 -1
- data/app/models/nquery/query.rb +72 -0
- data/app/services/nquery/application_service.rb +9 -0
- data/app/services/nquery/dashboards/card_parameters.rb +37 -0
- data/app/services/nquery/dashboards/filters.rb +75 -0
- data/app/services/nquery/dashboards/parameter_charts.rb +31 -0
- data/app/services/nquery/dashboards/parameter_rows.rb +60 -0
- data/app/views/layouts/nquery/application.html.erb +8 -0
- data/app/views/layouts/nquery/embed.html.erb +3 -1
- data/app/views/nquery/charts/_builder_form.html.erb +13 -6
- data/app/views/nquery/charts/_variable_row.html.erb +10 -0
- data/app/views/nquery/charts/_variables.html.erb +28 -0
- data/app/views/nquery/charts/embed.html.erb +5 -11
- data/app/views/nquery/charts/new.html.erb +2 -1
- data/app/views/nquery/charts/update.turbo_stream.erb +3 -0
- data/app/views/nquery/dashboard/charts/new.html.erb +2 -1
- data/app/views/nquery/dashboards/_chart_card.html.erb +27 -1
- data/app/views/nquery/dashboards/_parameter_chart_ids.html.erb +6 -0
- data/app/views/nquery/dashboards/_parameters.html.erb +159 -0
- data/app/views/nquery/dashboards/edit.html.erb +10 -0
- data/app/views/nquery/dashboards/embed.html.erb +9 -0
- data/app/views/nquery/dashboards/show.html.erb +41 -1
- data/app/views/nquery/embed/charts/show.html.erb +7 -8
- data/app/views/nquery/embed/dashboards/show.html.erb +12 -6
- data/app/views/nquery/embed/unavailable.html.erb +1 -0
- data/app/views/nquery/shared/_icon.html.erb +10 -0
- data/app/views/nquery/shared/_shared_chart_card.html.erb +20 -0
- data/app/views/nquery/shared/_sharing_panel.html.erb +94 -0
- data/app/views/nquery/shared/_sidebar.html.erb +1 -1
- data/app/views/nquery/shared/_topbar.html.erb +3 -0
- data/config/routes.rb +24 -1
- data/db/migrate/20260907120000_add_sharing_to_nquery_charts_and_dashboards.rb +19 -0
- data/db/migrate/20260907130000_remove_embedding_params_from_nquery_charts_and_dashboards.rb +8 -0
- data/db/migrate/20261005145000_add_parameters_to_nquery_dashboards.rb +7 -0
- data/db/migrate/20261005190000_add_parameters_to_nquery_queries.rb +7 -0
- data/lib/nquery/boolean.rb +9 -0
- data/lib/nquery/configuration.rb +6 -0
- data/lib/nquery/dashboard_parameters.rb +56 -0
- data/lib/nquery/data_sources/adapter.rb +10 -1
- data/lib/nquery/data_sources/mysql_adapter.rb +2 -2
- data/lib/nquery/data_sources/postgresql_adapter.rb +2 -2
- data/lib/nquery/data_sources/rails_adapter.rb +2 -7
- data/lib/nquery/data_sources/row_limit.rb +211 -0
- data/lib/nquery/embed_token_service.rb +10 -5
- data/lib/nquery/encryption.rb +1 -1
- data/lib/nquery/parameter_value.rb +97 -0
- data/lib/nquery/query_runner.rb +37 -4
- data/lib/nquery/seeder.rb +29 -7
- data/lib/nquery/statement_binds.rb +112 -0
- data/lib/nquery/version.rb +1 -1
- data/lib/nquery.rb +5 -0
- data/nquery.gemspec +2 -2
- metadata +40 -9
- data/app/javascript/nquery/controllers/query_editor_controller.js +0 -41
- data/app/views/nquery/queries/edit.html.erb +0 -40
- data/app/views/nquery/queries/new.html.erb +0 -37
|
@@ -6,30 +6,55 @@ module Nquery
|
|
|
6
6
|
|
|
7
7
|
private
|
|
8
8
|
|
|
9
|
-
def chart_result(chart)
|
|
10
|
-
return demo_result unless chart.
|
|
9
|
+
def chart_result(chart, parameters: nil)
|
|
10
|
+
return demo_result unless chart.statement?
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
resolved = parameters || query_default_parameters(chart)
|
|
13
|
+
chart_query_result(chart, parameters: resolved)
|
|
14
|
+
rescue QueryRunner::ParameterError => error
|
|
15
|
+
{ error: parameter_load_error(chart.statement, resolved, error) }
|
|
16
|
+
rescue QueryRunner::Error => error
|
|
17
|
+
Rails.logger.error("[nquery] chart #{chart.id} failed: #{error.class}: #{error.message}")
|
|
18
|
+
{ error: shared_chart_load_error }
|
|
13
19
|
rescue StandardError
|
|
14
20
|
demo_result
|
|
15
21
|
end
|
|
16
22
|
|
|
17
23
|
def chart_builder_result(chart)
|
|
18
|
-
return nil unless chart.
|
|
24
|
+
return nil unless chart.statement?
|
|
19
25
|
|
|
20
|
-
chart_query_result(chart, audit: false)
|
|
26
|
+
chart_query_result(chart, audit: false, parameters: query_default_parameters(chart))
|
|
21
27
|
rescue QueryRunner::PermissionError, QueryRunner::Error => e
|
|
22
28
|
{ error: e.message }
|
|
23
29
|
rescue StandardError => e
|
|
24
30
|
{ error: e.message }
|
|
25
31
|
end
|
|
26
32
|
|
|
27
|
-
def
|
|
33
|
+
def shared_chart_result(chart, parameters: nil)
|
|
34
|
+
return { error: "This chart has no query." } unless chart.statement?
|
|
35
|
+
return { error: shared_chart_load_error } unless chart.data_source
|
|
36
|
+
|
|
37
|
+
chart_query_result(chart, audit: false, parameters: parameters)
|
|
38
|
+
rescue QueryRunner::ParameterError => error
|
|
39
|
+
{ error: parameter_load_error(chart.statement, parameters, error) }
|
|
40
|
+
rescue StandardError => e
|
|
41
|
+
Rails.logger.error("[nquery] shared chart #{chart.id} failed: #{e.class}: #{e.message}")
|
|
42
|
+
{ error: shared_chart_load_error }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def query_default_parameters(chart)
|
|
46
|
+
return unless chart.query
|
|
47
|
+
|
|
48
|
+
DashboardParameters.call(dashboard: chart.query, token_params: {}, request_params: {})
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def chart_query_result(chart, audit: true, parameters: nil)
|
|
28
52
|
QueryRunner.new(
|
|
29
|
-
data_source: chart.
|
|
30
|
-
statement: chart.
|
|
53
|
+
data_source: chart.data_source || DataSource.first,
|
|
54
|
+
statement: chart.statement,
|
|
31
55
|
user: current_nquery_user,
|
|
32
|
-
query: chart.query
|
|
56
|
+
query: chart.query,
|
|
57
|
+
parameters: parameters
|
|
33
58
|
).run(audit: audit)
|
|
34
59
|
end
|
|
35
60
|
|
|
@@ -40,5 +65,41 @@ module Nquery
|
|
|
40
65
|
row_count: 4
|
|
41
66
|
}
|
|
42
67
|
end
|
|
68
|
+
|
|
69
|
+
def shared_chart_load_error
|
|
70
|
+
"This chart could not be loaded."
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def parameter_load_error(statement, parameters, error)
|
|
74
|
+
return shared_chart_load_error unless error.message.match?(/\A(?:Missing value for|Unknown parameter) /)
|
|
75
|
+
|
|
76
|
+
names = StatementBinds.missing_names(
|
|
77
|
+
statement: statement,
|
|
78
|
+
parameters: parameter_value_hash(parameters),
|
|
79
|
+
invalid_names: parameter_invalid_names(parameters)
|
|
80
|
+
)
|
|
81
|
+
return shared_chart_load_error if names.empty?
|
|
82
|
+
|
|
83
|
+
labels = names.map { |name| name.tr("_", " ").capitalize }
|
|
84
|
+
if labels.one?
|
|
85
|
+
"Enter a value for #{labels.first}."
|
|
86
|
+
else
|
|
87
|
+
"Enter values for #{labels.to_sentence}."
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def parameter_value_hash(parameters)
|
|
92
|
+
case parameters
|
|
93
|
+
when DashboardParameters::Result then parameters.values
|
|
94
|
+
when Hash then parameters
|
|
95
|
+
else {}
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def parameter_invalid_names(parameters)
|
|
100
|
+
return parameters.invalid_names if parameters.is_a?(DashboardParameters::Result)
|
|
101
|
+
|
|
102
|
+
[]
|
|
103
|
+
end
|
|
43
104
|
end
|
|
44
105
|
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Nquery
|
|
4
|
+
module SharingPage
|
|
5
|
+
extend ActiveSupport::Concern
|
|
6
|
+
|
|
7
|
+
private
|
|
8
|
+
|
|
9
|
+
def assign_sharing_page(resource)
|
|
10
|
+
@shareable = resource
|
|
11
|
+
@embed_token = resource.active_embed_token
|
|
12
|
+
@embed_url = signed_embed_url_for(resource)
|
|
13
|
+
@public_url = public_share_url_for(resource)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def signed_embed_url_for(resource)
|
|
17
|
+
token = resource.active_embed_token
|
|
18
|
+
return unless token
|
|
19
|
+
|
|
20
|
+
signed = EmbedTokenService.signed_token_for(token)
|
|
21
|
+
if resource.is_a?(Dashboard)
|
|
22
|
+
embed_public_dashboard_url(token: signed)
|
|
23
|
+
else
|
|
24
|
+
embed_public_chart_url(token: signed)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def public_share_url_for(resource)
|
|
29
|
+
return unless resource.publicly_shared?
|
|
30
|
+
|
|
31
|
+
if resource.is_a?(Dashboard)
|
|
32
|
+
public_dashboard_url(resource.public_uuid)
|
|
33
|
+
else
|
|
34
|
+
public_chart_url(resource.public_uuid)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Nquery
|
|
4
|
+
class Dashboard::CardsController < ApplicationController
|
|
5
|
+
before_action :set_dashboard
|
|
6
|
+
before_action :authorize_dashboard_curate!
|
|
7
|
+
|
|
8
|
+
def parameters
|
|
9
|
+
card = @dashboard.dashboard_cards.find(params[:id])
|
|
10
|
+
Dashboards::CardParameters.call(
|
|
11
|
+
dashboard: @dashboard,
|
|
12
|
+
chart: card.chart,
|
|
13
|
+
linked_names: Array(params[:parameter_names])
|
|
14
|
+
)
|
|
15
|
+
redirect_to dashboard_path(@dashboard), notice: "Chart filters updated."
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def set_dashboard
|
|
21
|
+
@dashboard = Dashboard.find(params[:dashboard_id])
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def authorize_dashboard_curate!
|
|
25
|
+
authorize_collection_access!(@dashboard.collection, required: :curate)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -4,10 +4,11 @@ module Nquery
|
|
|
4
4
|
class DashboardsController < ApplicationController
|
|
5
5
|
include ChartResults
|
|
6
6
|
include Browsable
|
|
7
|
+
include SharingPage
|
|
7
8
|
|
|
8
|
-
before_action :set_dashboard, only: %i[show edit update destroy archive unarchive update_layout]
|
|
9
|
-
before_action :authorize_dashboard_view!, only: %i[show]
|
|
10
|
-
before_action :authorize_dashboard_curate!, only: %i[edit update destroy archive unarchive update_layout]
|
|
9
|
+
before_action :set_dashboard, only: %i[show edit update destroy archive unarchive update_layout embed connect_parameter]
|
|
10
|
+
before_action :authorize_dashboard_view!, only: %i[show embed]
|
|
11
|
+
before_action :authorize_dashboard_curate!, only: %i[edit update destroy archive unarchive update_layout connect_parameter]
|
|
11
12
|
|
|
12
13
|
def index
|
|
13
14
|
@dashboards = filter_viewable_dashboards(Dashboard.active.includes(:collection).order(:name))
|
|
@@ -37,11 +38,11 @@ module Nquery
|
|
|
37
38
|
end
|
|
38
39
|
|
|
39
40
|
def show
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
@
|
|
41
|
+
assign_dashboard_filters
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def embed
|
|
45
|
+
assign_sharing_page(@dashboard)
|
|
45
46
|
end
|
|
46
47
|
|
|
47
48
|
def edit
|
|
@@ -52,8 +53,19 @@ module Nquery
|
|
|
52
53
|
@collections = assignable_collections
|
|
53
54
|
authorize_collection_access!(Collection.find(dashboard_params[:collection_id]), required: :curate) if dashboard_params[:collection_id].present?
|
|
54
55
|
|
|
56
|
+
previous_names = @dashboard.parameter_names
|
|
55
57
|
if @dashboard.update(dashboard_params)
|
|
56
|
-
|
|
58
|
+
added = @dashboard.parameter_names - previous_names
|
|
59
|
+
if parameter_submission? && added.one?
|
|
60
|
+
redirect_to dashboard_path(@dashboard, edit: "parameters", wire: added.first), notice: "Filter added."
|
|
61
|
+
elsif parameter_submission? && params[:edit] == "parameters"
|
|
62
|
+
redirect_to dashboard_path(@dashboard, **wire_query), notice: "Dashboard updated."
|
|
63
|
+
else
|
|
64
|
+
redirect_to dashboard_path(@dashboard), notice: "Dashboard updated."
|
|
65
|
+
end
|
|
66
|
+
elsif parameter_submission?
|
|
67
|
+
assign_dashboard_filters
|
|
68
|
+
render :show, status: :unprocessable_content
|
|
57
69
|
else
|
|
58
70
|
render :edit, status: :unprocessable_content
|
|
59
71
|
end
|
|
@@ -74,6 +86,15 @@ module Nquery
|
|
|
74
86
|
redirect_to dashboard_path(@dashboard), notice: "Dashboard unarchived."
|
|
75
87
|
end
|
|
76
88
|
|
|
89
|
+
def connect_parameter
|
|
90
|
+
Dashboards::ParameterCharts.call(
|
|
91
|
+
dashboard: @dashboard,
|
|
92
|
+
name: params[:name],
|
|
93
|
+
chart_ids: params.permit(chart_ids: []).fetch(:chart_ids, [])
|
|
94
|
+
)
|
|
95
|
+
redirect_to dashboard_path(@dashboard, **wire_query), notice: "Filter connected."
|
|
96
|
+
end
|
|
97
|
+
|
|
77
98
|
def update_layout
|
|
78
99
|
card_layouts.each do |card_id, layout|
|
|
79
100
|
card = @dashboard.dashboard_cards.find(card_id)
|
|
@@ -85,7 +106,7 @@ module Nquery
|
|
|
85
106
|
private
|
|
86
107
|
|
|
87
108
|
def set_dashboard
|
|
88
|
-
@dashboard = if action_name.in?(%w[show edit])
|
|
109
|
+
@dashboard = if action_name.in?(%w[show edit embed])
|
|
89
110
|
Dashboard.includes(:creator, dashboard_cards: :chart).find(params[:id])
|
|
90
111
|
else
|
|
91
112
|
Dashboard.find(params[:id])
|
|
@@ -106,8 +127,32 @@ module Nquery
|
|
|
106
127
|
end
|
|
107
128
|
end
|
|
108
129
|
|
|
130
|
+
def assign_dashboard_filters
|
|
131
|
+
filters = ::Nquery::Dashboards::Filters.call(dashboard: @dashboard, request_params: params)
|
|
132
|
+
@filter_dashboard = filters.filter_dashboard
|
|
133
|
+
@invalid_parameter_names = filters.parameters.invalid_names
|
|
134
|
+
@dashboard_cards = @dashboard.dashboard_cards
|
|
135
|
+
.joins(:chart)
|
|
136
|
+
.merge(Chart.active)
|
|
137
|
+
.includes(chart: { query: :data_source })
|
|
138
|
+
@card_results = @dashboard_cards.index_with do |card|
|
|
139
|
+
chart_result(card.chart, parameters: @dashboard.parameters_for_chart(card.chart, filters.parameters))
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def parameter_submission?
|
|
144
|
+
params[:dashboard]&.key?(:parameters)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def wire_query
|
|
148
|
+
{ edit: params[:edit].presence, wire: params[:wire].presence }.compact
|
|
149
|
+
end
|
|
150
|
+
|
|
109
151
|
def dashboard_params
|
|
110
|
-
params.require(:dashboard).permit(
|
|
152
|
+
permitted = params.require(:dashboard).permit(
|
|
153
|
+
:name, :description, :collection_id, settings: {}, parameters: [:name, :type, :default, { chart_ids: [] }]
|
|
154
|
+
)
|
|
155
|
+
Dashboards::ParameterRows.call(dashboard: @dashboard, attributes: permitted)
|
|
111
156
|
end
|
|
112
157
|
|
|
113
158
|
def default_dashboard_collection
|
|
@@ -2,23 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
module Nquery
|
|
4
4
|
module Embed
|
|
5
|
-
class ChartsController <
|
|
6
|
-
include Nquery::Engine.routes.url_helpers
|
|
7
|
-
protect_from_forgery with: :exception
|
|
8
|
-
layout "nquery/embed"
|
|
9
|
-
|
|
5
|
+
class ChartsController < PublicResourcesController
|
|
10
6
|
def show
|
|
7
|
+
unless Nquery.configuration.static_embedding_enabled
|
|
8
|
+
return render_forbidden("Embedding is disabled")
|
|
9
|
+
end
|
|
10
|
+
|
|
11
11
|
payload = EmbedTokenService.verify(params[:token])
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
render plain: "Invalid or expired embed token", status: :forbidden
|
|
16
|
-
end
|
|
12
|
+
unless payload[:resource_type] == "Nquery::Chart"
|
|
13
|
+
return render_forbidden
|
|
14
|
+
end
|
|
17
15
|
|
|
18
|
-
|
|
16
|
+
@chart = Chart.active.find(payload[:resource_id])
|
|
17
|
+
unless @chart.enable_embedding?
|
|
18
|
+
return render_forbidden("Embedding is disabled")
|
|
19
|
+
end
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
@result = shared_chart_result(@chart)
|
|
22
|
+
rescue EmbedTokenService::Error, ActiveRecord::RecordNotFound
|
|
23
|
+
render_forbidden
|
|
22
24
|
end
|
|
23
25
|
end
|
|
24
26
|
end
|
|
@@ -2,16 +2,42 @@
|
|
|
2
2
|
|
|
3
3
|
module Nquery
|
|
4
4
|
module Embed
|
|
5
|
-
class DashboardsController <
|
|
6
|
-
include Nquery::Engine.routes.url_helpers
|
|
7
|
-
protect_from_forgery with: :exception
|
|
8
|
-
layout "nquery/embed"
|
|
9
|
-
|
|
5
|
+
class DashboardsController < PublicResourcesController
|
|
10
6
|
def show
|
|
7
|
+
unless Nquery.configuration.static_embedding_enabled
|
|
8
|
+
return render_forbidden("Embedding is disabled")
|
|
9
|
+
end
|
|
10
|
+
|
|
11
11
|
payload = EmbedTokenService.verify(params[:token])
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
unless payload[:resource_type] == "Nquery::Dashboard"
|
|
13
|
+
return render_forbidden
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
@dashboard = Dashboard.active.find(payload[:resource_id])
|
|
17
|
+
unless @dashboard.enable_embedding?
|
|
18
|
+
return render_forbidden("Embedding is disabled")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
resolved = DashboardParameters.call(
|
|
22
|
+
dashboard: @dashboard.with_agreed_chart_variables,
|
|
23
|
+
token_params: embed_parameter_params(payload[:params]),
|
|
24
|
+
request_params: embed_parameter_params(params)
|
|
25
|
+
)
|
|
26
|
+
@dashboard_cards = load_dashboard_cards(@dashboard)
|
|
27
|
+
@card_results = @dashboard_cards.index_with do |card|
|
|
28
|
+
shared_chart_result(card.chart, parameters: @dashboard.parameters_for_chart(card.chart, resolved))
|
|
29
|
+
end
|
|
30
|
+
rescue EmbedTokenService::Error, ActiveRecord::RecordNotFound
|
|
31
|
+
render_forbidden
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def embed_parameter_params(source)
|
|
37
|
+
return {} if source.blank?
|
|
38
|
+
|
|
39
|
+
hash = source.respond_to?(:to_unsafe_h) ? source.to_unsafe_h : source.to_h
|
|
40
|
+
hash.stringify_keys.except("token", "titled", "bordered", "theme").slice(*@dashboard.parameter_names)
|
|
15
41
|
end
|
|
16
42
|
end
|
|
17
43
|
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Nquery
|
|
4
|
+
module Public
|
|
5
|
+
class ChartsController < PublicResourcesController
|
|
6
|
+
def show
|
|
7
|
+
unless Nquery.configuration.public_sharing_enabled
|
|
8
|
+
return render_unavailable(status: :not_found)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
@chart = Chart.active.find_by!(public_uuid: params[:uuid])
|
|
12
|
+
@result = shared_chart_result(@chart)
|
|
13
|
+
render template: "nquery/embed/charts/show"
|
|
14
|
+
rescue ActiveRecord::RecordNotFound
|
|
15
|
+
render_unavailable(status: :not_found)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Nquery
|
|
4
|
+
module Public
|
|
5
|
+
class DashboardsController < PublicResourcesController
|
|
6
|
+
def show
|
|
7
|
+
unless Nquery.configuration.public_sharing_enabled
|
|
8
|
+
return render_unavailable(status: :not_found)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
@dashboard = Dashboard.active.find_by!(public_uuid: params[:uuid])
|
|
12
|
+
@dashboard_cards = load_dashboard_cards(@dashboard)
|
|
13
|
+
resolved = declared_dashboard_parameters
|
|
14
|
+
@card_results = @dashboard_cards.index_with do |card|
|
|
15
|
+
shared_chart_result(card.chart, parameters: @dashboard.parameters_for_chart(card.chart, resolved))
|
|
16
|
+
end
|
|
17
|
+
render template: "nquery/embed/dashboards/show"
|
|
18
|
+
rescue ActiveRecord::RecordNotFound
|
|
19
|
+
render_unavailable(status: :not_found)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def declared_dashboard_parameters
|
|
25
|
+
DashboardParameters.call(
|
|
26
|
+
dashboard: @dashboard.with_agreed_chart_variables,
|
|
27
|
+
token_params: {},
|
|
28
|
+
request_params: {}
|
|
29
|
+
)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Nquery
|
|
4
|
+
class PublicResourcesController < ActionController::Base
|
|
5
|
+
include Nquery::Engine.routes.url_helpers
|
|
6
|
+
include ChartResults
|
|
7
|
+
|
|
8
|
+
protect_from_forgery with: :exception
|
|
9
|
+
layout "nquery/embed"
|
|
10
|
+
|
|
11
|
+
helper Nquery::Engine.helpers
|
|
12
|
+
helper_method :current_nquery_user, :embed_titled?, :embed_bordered?, :embed_theme, :embed_appearance_classes
|
|
13
|
+
|
|
14
|
+
after_action :set_embed_headers
|
|
15
|
+
|
|
16
|
+
def self.apply_embed_rate_limit(to: self)
|
|
17
|
+
if to.respond_to?(:rate_limit)
|
|
18
|
+
to.rate_limit to: 60, within: 1.minute, by: -> { request.remote_ip }
|
|
19
|
+
else
|
|
20
|
+
Rails.logger.warn("[nquery] public/embed endpoints are not rate limited; Rails 7.2+ rate_limit is unavailable")
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
apply_embed_rate_limit
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def current_nquery_user
|
|
28
|
+
nil
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def embed_titled?
|
|
32
|
+
params[:titled] != "false"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def embed_bordered?
|
|
36
|
+
params[:bordered] != "false"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def embed_theme
|
|
40
|
+
theme = params[:theme].to_s
|
|
41
|
+
theme.presence_in(%w[night transparent])
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def embed_appearance_classes
|
|
45
|
+
classes = ["nq-embed-body"]
|
|
46
|
+
classes << "nq-embed-untitled" unless embed_titled?
|
|
47
|
+
classes << "nq-embed-borderless" unless embed_bordered?
|
|
48
|
+
classes << "nq-embed-theme-#{embed_theme}" if embed_theme
|
|
49
|
+
classes
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def set_embed_headers
|
|
53
|
+
response.headers.delete("X-Frame-Options")
|
|
54
|
+
response.headers["Content-Security-Policy"] = "frame-ancestors #{frame_ancestors_value}"
|
|
55
|
+
response.headers["X-Robots-Tag"] = "noindex"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def frame_ancestors_value
|
|
59
|
+
ancestors = Nquery.configuration.embed_frame_ancestors
|
|
60
|
+
ancestors.present? ? Array(ancestors).join(" ") : "*"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def render_unavailable(status:, message: "This visualization is not available.")
|
|
64
|
+
@unavailable_message = message
|
|
65
|
+
render template: "nquery/embed/unavailable", status: status
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def render_forbidden(message = "Invalid or expired embed token")
|
|
69
|
+
render_unavailable(status: :forbidden, message: message)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def load_dashboard_cards(dashboard)
|
|
73
|
+
dashboard.dashboard_cards
|
|
74
|
+
.joins(:chart)
|
|
75
|
+
.merge(Chart.active)
|
|
76
|
+
.includes(chart: { query: :data_source })
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -2,60 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
module Nquery
|
|
4
4
|
class QueriesController < ApplicationController
|
|
5
|
-
before_action :set_query, only:
|
|
6
|
-
before_action :authorize_query_collection!, only:
|
|
5
|
+
before_action :set_query, only: :update
|
|
6
|
+
before_action :authorize_query_collection!, only: :update
|
|
7
7
|
before_action :set_data_source_for_run, only: %i[run schema]
|
|
8
8
|
before_action :authorize_run_data_source!, only: %i[run schema]
|
|
9
9
|
|
|
10
|
-
def new
|
|
11
|
-
@query = Query.new(statement: "SELECT 1 AS example")
|
|
12
|
-
@data_sources = DataSource.active.order(:name)
|
|
13
|
-
@schema_tables = schema_tables
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def create
|
|
17
|
-
@query = Query.new(query_params.merge(creator: current_nquery_user))
|
|
18
|
-
if @query.save
|
|
19
|
-
redirect_to edit_query_path(@query), notice: "Query created."
|
|
20
|
-
else
|
|
21
|
-
@data_sources = DataSource.active.order(:name)
|
|
22
|
-
@schema_tables = schema_tables
|
|
23
|
-
render :new, status: :unprocessable_content
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def edit
|
|
28
|
-
@data_sources = DataSource.active.order(:name)
|
|
29
|
-
@schema_tables = schema_tables
|
|
30
|
-
end
|
|
31
|
-
|
|
32
10
|
def update
|
|
33
11
|
if @query.update(query_params)
|
|
34
|
-
|
|
35
|
-
format.html { redirect_to edit_query_path(@query), notice: "Query saved." }
|
|
36
|
-
format.json { render json: { ok: true, notice: "Query saved." } }
|
|
37
|
-
end
|
|
12
|
+
render json: { ok: true, notice: "Query saved." }
|
|
38
13
|
else
|
|
39
|
-
@
|
|
40
|
-
|
|
41
|
-
respond_to do |format|
|
|
42
|
-
format.html { render :edit, status: :unprocessable_content }
|
|
43
|
-
format.json {
|
|
44
|
-
render json: { error: @query.errors.full_messages.to_sentence.presence || "Query could not be saved." },
|
|
45
|
-
status: :unprocessable_content
|
|
46
|
-
}
|
|
47
|
-
end
|
|
14
|
+
render json: { error: @query.errors.full_messages.to_sentence.presence || "Query could not be saved." },
|
|
15
|
+
status: :unprocessable_content
|
|
48
16
|
end
|
|
49
17
|
end
|
|
50
18
|
|
|
51
|
-
def show
|
|
52
|
-
end
|
|
53
|
-
|
|
54
19
|
def run
|
|
55
20
|
result = QueryRunner.new(
|
|
56
21
|
data_source: @data_source,
|
|
57
22
|
statement: params[:statement],
|
|
58
|
-
user: current_nquery_user
|
|
23
|
+
user: current_nquery_user,
|
|
24
|
+
parameters: run_parameters
|
|
59
25
|
).run
|
|
60
26
|
|
|
61
27
|
render json: result
|
|
@@ -89,16 +55,27 @@ module Nquery
|
|
|
89
55
|
end
|
|
90
56
|
|
|
91
57
|
def query_params
|
|
92
|
-
params.require(:query).permit(:name, :statement, :data_source_id, :collection_id)
|
|
58
|
+
params.require(:query).permit(:name, :statement, :data_source_id, :collection_id, parameters: [:name, :type, :default])
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def run_parameters
|
|
62
|
+
rows = run_parameter_rows
|
|
63
|
+
return if rows.empty?
|
|
64
|
+
|
|
65
|
+
explicit = {}
|
|
66
|
+
definitions = rows.map do |row|
|
|
67
|
+
explicit[row["name"]] = row["value"] if row.key?("value")
|
|
68
|
+
row.except("value")
|
|
69
|
+
end
|
|
70
|
+
source = Query.new(statement: params[:statement], parameters: definitions)
|
|
71
|
+
DashboardParameters.call(dashboard: source, token_params: {}, request_params: explicit)
|
|
93
72
|
end
|
|
94
73
|
|
|
95
|
-
def
|
|
96
|
-
|
|
97
|
-
return [] unless data_source
|
|
74
|
+
def run_parameter_rows
|
|
75
|
+
return [] if params[:parameters].blank?
|
|
98
76
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
[]
|
|
77
|
+
permitted = params.permit(parameters: [:name, :type, :default, :value])
|
|
78
|
+
Array(permitted[:parameters]).map { |row| row.to_h.stringify_keys }
|
|
102
79
|
end
|
|
103
80
|
end
|
|
104
81
|
end
|