Module: Blazer
- Defined in:
- lib/blazer.rb,
lib/blazer/engine.rb,
lib/blazer/result.rb,
lib/blazer/version.rb,
lib/blazer/statement.rb,
lib/blazer/data_source.rb,
app/models/blazer/audit.rb,
app/models/blazer/check.rb,
app/models/blazer/query.rb,
lib/blazer/check_mailer.rb,
lib/blazer/result_cache.rb,
app/models/blazer/record.rb,
app/models/blazer/upload.rb,
lib/blazer/run_statement.rb,
lib/blazer/email_notifier.rb,
lib/blazer/slack_notifier.rb,
app/models/blazer/dashboard.rb,
app/models/blazer/connection.rb,
lib/blazer/run_statement_job.rb,
app/helpers/blazer/base_helper.rb,
lib/blazer/adapters/sql_adapter.rb,
lib/blazer/adapters/base_adapter.rb,
lib/blazer/adapters/hive_adapter.rb,
lib/blazer/adapters/soda_adapter.rb,
app/models/blazer/dashboard_query.rb,
lib/blazer/adapters/drill_adapter.rb,
lib/blazer/adapters/druid_adapter.rb,
lib/blazer/adapters/neo4j_adapter.rb,
lib/blazer/adapters/spark_adapter.rb,
lib/blazer/adapters/athena_adapter.rb,
lib/blazer/adapters/ignite_adapter.rb,
lib/blazer/adapters/presto_adapter.rb,
app/models/blazer/uploads_connection.rb,
lib/blazer/adapters/bigquery_adapter.rb,
lib/blazer/adapters/influxdb_adapter.rb,
lib/blazer/adapters/cassandra_adapter.rb,
lib/blazer/adapters/snowflake_adapter.rb,
app/controllers/blazer/base_controller.rb,
lib/blazer/adapters/clickhouse_adapter.rb,
lib/blazer/adapters/opensearch_adapter.rb,
lib/blazer/adapters/salesforce_adapter.rb,
lib/blazer/adapters/snowflake2_adapter.rb,
lib/generators/blazer/install_generator.rb,
lib/generators/blazer/uploads_generator.rb,
app/controllers/blazer/checks_controller.rb,
app/controllers/blazer/queries_controller.rb,
app/controllers/blazer/uploads_controller.rb,
lib/blazer/adapters/elasticsearch_adapter.rb,
app/controllers/blazer/dashboards_controller.rb
Defined Under Namespace
Modules: Adapters, BaseHelper, Generators
Classes: Audit, BaseController, Check, CheckMailer, ChecksController, Connection, Dashboard, DashboardQuery, DashboardsController, DataSource, EmailNotifier, Engine, Error, QueriesController, Query, Record, Result, ResultCache, RunStatement, RunStatementJob, SlackNotifier, Statement, TimeoutNotSupported, Upload, UploadError, UploadsConnection, UploadsController
Constant Summary
collapse
- VARIABLE_MESSAGE =
"Variable cannot be used in this position"
- TIMEOUT_MESSAGE =
"Query timed out :("
- TIMEOUT_ERRORS =
[
"canceling statement due to statement timeout", "canceling statement due to conflict with recovery", "cancelled on user's request", "canceled on user's request", "system requested abort", "maximum statement execution time exceeded" ]
- VERSION =
"3.5.0"
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.anomaly_checks ⇒ Object
Returns the value of attribute anomaly_checks.
69
70
71
|
# File 'lib/blazer.rb', line 69
def anomaly_checks
@anomaly_checks
end
|
.async ⇒ Object
Returns the value of attribute async.
71
72
73
|
# File 'lib/blazer.rb', line 71
def async
@async
end
|
.audit ⇒ Object
Returns the value of attribute audit.
58
59
60
|
# File 'lib/blazer.rb', line 58
def audit
@audit
end
|
.before_action ⇒ Object
Returns the value of attribute before_action.
63
64
65
|
# File 'lib/blazer.rb', line 63
def before_action
@before_action
end
|
.cache ⇒ Object
Returns the value of attribute cache.
65
66
67
|
# File 'lib/blazer.rb', line 65
def cache
@cache
end
|
.check_schedules ⇒ Object
Returns the value of attribute check_schedules.
68
69
70
|
# File 'lib/blazer.rb', line 68
def check_schedules
@check_schedules
end
|
.forecasting ⇒ Object
Returns the value of attribute forecasting.
70
71
72
|
# File 'lib/blazer.rb', line 70
def forecasting
@forecasting
end
|
.from_email ⇒ Object
Returns the value of attribute from_email.
64
65
66
|
# File 'lib/blazer.rb', line 64
def from_email
@from_email
end
|
.images ⇒ Object
Returns the value of attribute images.
72
73
74
|
# File 'lib/blazer.rb', line 72
def images
@images
end
|
.mapbox_access_token ⇒ Object
Returns the value of attribute mapbox_access_token.
76
77
78
|
# File 'lib/blazer.rb', line 76
def mapbox_access_token
@mapbox_access_token
end
|
.override_csp ⇒ Object
Returns the value of attribute override_csp.
73
74
75
|
# File 'lib/blazer.rb', line 73
def override_csp
@override_csp
end
|
.slack_oauth_token ⇒ Object
Returns the value of attribute slack_oauth_token.
74
75
76
|
# File 'lib/blazer.rb', line 74
def slack_oauth_token
@slack_oauth_token
end
|
.slack_webhook_url ⇒ Object
Returns the value of attribute slack_webhook_url.
75
76
77
|
# File 'lib/blazer.rb', line 75
def slack_webhook_url
@slack_webhook_url
end
|
.time_zone ⇒ Object
Returns the value of attribute time_zone.
59
60
61
|
# File 'lib/blazer.rb', line 59
def time_zone
@time_zone
end
|
Returns the value of attribute transform_statement.
66
67
68
|
# File 'lib/blazer.rb', line 66
def transform_statement
@transform_statement
end
|
Returns the value of attribute transform_variable.
67
68
69
|
# File 'lib/blazer.rb', line 67
def transform_variable
@transform_variable
end
|
.user_class ⇒ Object
104
105
106
107
108
109
|
# File 'lib/blazer.rb', line 104
def self.user_class
if !defined?(@user_class)
@user_class = settings.key?("user_class") ? settings["user_class"] : (User.name rescue nil)
end
@user_class
end
|
.user_method ⇒ Object
111
112
113
114
115
116
117
118
119
|
# File 'lib/blazer.rb', line 111
def self.user_method
if !defined?(@user_method)
@user_method = settings["user_method"]
if user_class
@user_method ||= "current_#{user_class.to_s.downcase.singularize}"
end
end
@user_method
end
|
.user_name ⇒ Object
Returns the value of attribute user_name.
60
61
62
|
# File 'lib/blazer.rb', line 60
def user_name
@user_name
end
|
Class Method Details
.adapters ⇒ Object
228
229
230
|
# File 'lib/blazer.rb', line 228
def self.adapters
@adapters ||= {}
end
|
.anomaly_detectors ⇒ Object
236
237
238
|
# File 'lib/blazer.rb', line 236
def self.anomaly_detectors
@anomaly_detectors ||= {}
end
|
.archive_queries ⇒ Object
268
269
270
271
272
273
274
|
# File 'lib/blazer.rb', line 268
def self.archive_queries
raise "Audits must be enabled to archive" unless Blazer.audit
raise "Missing status column - see https://github.com/ankane/blazer#23" unless Blazer::Query.column_names.include?("status")
viewed_query_ids = Blazer::Audit.where("created_at > ?", 90.days.ago).group(:query_id).count.keys.compact
Blazer::Query.active.where.not(id: viewed_query_ids).update_all(status: "archived")
end
|
.check_types ⇒ Object
260
261
262
|
# File 'lib/blazer.rb', line 260
def self.check_types
@check_types ||= {}
end
|
.data_sources ⇒ Object
132
133
134
135
136
137
138
139
140
|
# File 'lib/blazer.rb', line 132
def self.data_sources
@data_sources ||= begin
ds = Hash.new { |hash, key| raise Blazer::Error, "Unknown data source: #{key}" }
settings["data_sources"].each do |id, s|
ds[id] = Blazer::DataSource.new(id, s)
end
ds
end
end
|
.forecasters ⇒ Object
244
245
246
|
# File 'lib/blazer.rb', line 244
def self.forecasters
@forecasters ||= {}
end
|
.maps? ⇒ Boolean
TODO show warning on invalid access token
207
208
209
|
# File 'lib/blazer.rb', line 207
def self.maps?
mapbox_access_token.present? && mapbox_access_token.start_with?("pk.")
end
|
.monotonic_time ⇒ Object
277
278
279
|
# File 'lib/blazer.rb', line 277
def self.monotonic_time
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
|
.notifiers ⇒ Object
252
253
254
|
# File 'lib/blazer.rb', line 252
def self.notifiers
@notifiers ||= []
end
|
.register_adapter(name, adapter) ⇒ Object
232
233
234
|
# File 'lib/blazer.rb', line 232
def self.register_adapter(name, adapter)
adapters[name] = adapter
end
|
.register_anomaly_detector(name, &anomaly_detector) ⇒ Object
240
241
242
|
# File 'lib/blazer.rb', line 240
def self.register_anomaly_detector(name, &anomaly_detector)
anomaly_detectors[name] = anomaly_detector
end
|
.register_check_type(name, label, &block) ⇒ Object
264
265
266
|
# File 'lib/blazer.rb', line 264
def self.register_check_type(name, label, &block)
check_types[name] = {label: label, block: block}
end
|
.register_forecaster(name, &forecaster) ⇒ Object
248
249
250
|
# File 'lib/blazer.rb', line 248
def self.register_forecaster(name, &forecaster)
forecasters[name] = forecaster
end
|
.register_notifier(notifier) ⇒ Object
256
257
258
|
# File 'lib/blazer.rb', line 256
def self.register_notifier(notifier)
self.notifiers << notifier
end
|
.run_check(check) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/blazer.rb', line 151
def self.run_check(check)
tries = 1
ActiveSupport::Notifications.instrument("run_check.blazer", check_id: check.id, query_id: check.query.id, state_was: check.state) do |instrument|
statement = check.query.statement_object
data_source = statement.data_source
while tries <= 3
result = data_source.run_statement(statement, refresh_cache: true, check: check, query: check.query)
if result.timed_out?
Rails.logger.info "[blazer timeout] query=#{check.query.name}"
tries += 1
sleep(10)
elsif result.error.to_s.start_with?("PG::ConnectionBad")
data_source.reconnect
Rails.logger.info "[blazer reconnect] query=#{check.query.name}"
tries += 1
sleep(10)
else
break
end
end
begin
check.reload check.update_state(result)
rescue ActiveRecord::RecordNotFound
end
Rails.logger.info "[blazer check] query=#{check.query.name} state=#{check.state} rows=#{result.rows.try(:size)} error=#{result.error}"
instrument[:statement] = statement.bind_statement
instrument[:data_source] = data_source
instrument[:state] = check.state
instrument[:rows] = result.rows.try(:size)
instrument[:error] = result.error
instrument[:tries] = tries
end
end
|
.run_checks(schedule: nil) ⇒ Object
142
143
144
145
146
147
148
149
|
# File 'lib/blazer.rb', line 142
def self.run_checks(schedule: nil)
checks = Blazer::Check.includes(:query)
checks = checks.where(schedule: schedule) if schedule
checks.find_each do |check|
next if check.state == "disabled"
Safely.safely { run_check(check) }
end
end
|
.send_failing_checks ⇒ Object
195
196
197
198
199
200
|
# File 'lib/blazer.rb', line 195
def self.send_failing_checks
checks = Blazer::Check.includes(:query).where(state: ["failing", "error", "timed out", "disabled"]).to_a
Blazer.notifiers.each do |notifier|
notifier.failing_checks(checks)
end
end
|
.settings ⇒ Object
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/blazer.rb', line 121
def self.settings
@settings ||= begin
path = Rails.root.join("config", "blazer.yml").to_s
if File.exist?(path)
YAML.safe_load(ERB.new(File.read(path)).result, aliases: true)
else
{}
end
end
end
|
.slack? ⇒ Boolean
202
203
204
|
# File 'lib/blazer.rb', line 202
def self.slack?
slack_oauth_token.present? || slack_webhook_url.present?
end
|
.uploads? ⇒ Boolean
211
212
213
|
# File 'lib/blazer.rb', line 211
def self.uploads?
settings.key?("uploads")
end
|
.uploads_connection ⇒ Object
215
216
217
218
|
# File 'lib/blazer.rb', line 215
def self.uploads_connection
raise "Empty url for uploads" unless settings.dig("uploads", "url")
Blazer::UploadsConnection.connection
end
|
.uploads_schema ⇒ Object
220
221
222
|
# File 'lib/blazer.rb', line 220
def self.uploads_schema
settings.dig("uploads", "schema") || "uploads"
end
|
.uploads_table_name(name) ⇒ Object
224
225
226
|
# File 'lib/blazer.rb', line 224
def self.uploads_table_name(name)
uploads_connection.quote_table_name("#{uploads_schema}.#{name}")
end
|