Class: Blazer::DataSource
- Inherits:
-
Object
- Object
- Blazer::DataSource
- Extended by:
- Forwardable
- Defined in:
- lib/blazer/data_source.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#adapter_instance ⇒ Object
readonly
Returns the value of attribute adapter_instance.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Instance Method Summary collapse
- #cache ⇒ Object
- #cache_expires_in ⇒ Object
- #cache_key(key) ⇒ Object
- #cache_mode ⇒ Object
- #cache_slow_threshold ⇒ Object
- #clear_cache(statement) ⇒ Object
- #delete_results(run_id) ⇒ Object
-
#initialize(id, settings) ⇒ DataSource
constructor
A new instance of DataSource.
- #linked_columns ⇒ Object
- #local_time_suffix ⇒ Object
- #name ⇒ Object
- #read_cache(cache_key) ⇒ Object
- #run_cache_key(run_id) ⇒ Object
- #run_results(run_id) ⇒ Object
- #run_statement(statement, options = {}) ⇒ Object
- #smart_columns ⇒ Object
- #smart_variables ⇒ Object
- #statement_cache_key(statement) ⇒ Object
- #timeout ⇒ Object
- #variable_defaults ⇒ Object
Constructor Details
#initialize(id, settings) ⇒ DataSource
Returns a new instance of DataSource.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/blazer/data_source.rb', line 11 def initialize(id, settings) @id = id @settings = settings unless settings["url"] || Rails.env.development? raise Blazer::Error, "Empty url" end @adapter_instance = case adapter when "sql" Blazer::Adapters::SqlAdapter.new(self) when "elasticsearch" Blazer::Adapters::ElasticsearchAdapter.new(self) when "mongodb" Blazer::Adapters::MongodbAdapter.new(self) else raise Blazer::Error, "Unknown adapter" end end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
7 8 9 |
# File 'lib/blazer/data_source.rb', line 7 def adapter @adapter end |
#adapter_instance ⇒ Object (readonly)
Returns the value of attribute adapter_instance.
7 8 9 |
# File 'lib/blazer/data_source.rb', line 7 def adapter_instance @adapter_instance end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/blazer/data_source.rb', line 7 def id @id end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
7 8 9 |
# File 'lib/blazer/data_source.rb', line 7 def settings @settings end |
Instance Method Details
#cache ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/blazer/data_source.rb', line 60 def cache @cache ||= begin if settings["cache"].is_a?(Hash) settings["cache"] elsif settings["cache"] { "mode" => "all", "expires_in" => settings["cache"] } else { "mode" => "off" } end end end |
#cache_expires_in ⇒ Object
81 82 83 |
# File 'lib/blazer/data_source.rb', line 81 def cache_expires_in (cache["expires_in"] || 60).to_f end |
#cache_key(key) ⇒ Object
140 141 142 |
# File 'lib/blazer/data_source.rb', line 140 def cache_key(key) (["blazer", "v4"] + key).join("/") end |
#cache_mode ⇒ Object
77 78 79 |
# File 'lib/blazer/data_source.rb', line 77 def cache_mode cache["mode"] end |
#cache_slow_threshold ⇒ Object
85 86 87 |
# File 'lib/blazer/data_source.rb', line 85 def cache_slow_threshold (cache["slow_threshold"] || 15).to_f end |
#clear_cache(statement) ⇒ Object
136 137 138 |
# File 'lib/blazer/data_source.rb', line 136 def clear_cache(statement) Blazer.cache.delete(statement_cache_key(statement)) end |
#delete_results(run_id) ⇒ Object
104 105 106 |
# File 'lib/blazer/data_source.rb', line 104 def delete_results(run_id) Blazer.cache.delete(run_cache_key(run_id)) end |
#linked_columns ⇒ Object
40 41 42 |
# File 'lib/blazer/data_source.rb', line 40 def linked_columns settings["linked_columns"] || {} end |
#local_time_suffix ⇒ Object
89 90 91 |
# File 'lib/blazer/data_source.rb', line 89 def local_time_suffix @local_time_suffix ||= Array(settings["local_time_suffix"]) end |
#name ⇒ Object
36 37 38 |
# File 'lib/blazer/data_source.rb', line 36 def name settings["name"] || @id end |
#read_cache(cache_key) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/blazer/data_source.rb', line 93 def read_cache(cache_key) value = Blazer.cache.read(cache_key) if value Blazer::Result.new(self, *Marshal.load(value), nil) end end |
#run_cache_key(run_id) ⇒ Object
148 149 150 |
# File 'lib/blazer/data_source.rb', line 148 def run_cache_key(run_id) cache_key(["run", run_id]) end |
#run_results(run_id) ⇒ Object
100 101 102 |
# File 'lib/blazer/data_source.rb', line 100 def run_results(run_id) read_cache(run_cache_key(run_id)) end |
#run_statement(statement, options = {}) ⇒ Object
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 |
# File 'lib/blazer/data_source.rb', line 108 def run_statement(statement, = {}) run_id = [:run_id] result = nil if cache_mode != "off" && ![:refresh_cache] result = read_cache(statement_cache_key(statement)) end unless result comment = "blazer" if [:user].respond_to?(:id) comment << ",user_id:#{options[:user].id}" end if [:user].respond_to?(Blazer.user_name) # only include letters, numbers, and spaces to prevent injection comment << ",user_name:#{options[:user].send(Blazer.user_name).to_s.gsub(/[^a-zA-Z0-9 ]/, "")}" end if [:query].respond_to?(:id) comment << ",query_id:#{options[:query].id}" end if [:check] comment << ",check_id:#{options[:check].id},check_emails:#{options[:check].emails}" end result = run_statement_helper(statement, comment, [:run_id]) end result end |
#smart_columns ⇒ Object
44 45 46 |
# File 'lib/blazer/data_source.rb', line 44 def smart_columns settings["smart_columns"] || {} end |
#smart_variables ⇒ Object
48 49 50 |
# File 'lib/blazer/data_source.rb', line 48 def smart_variables settings["smart_variables"] || {} end |
#statement_cache_key(statement) ⇒ Object
144 145 146 |
# File 'lib/blazer/data_source.rb', line 144 def statement_cache_key(statement) cache_key(["statement", id, Digest::MD5.hexdigest(statement)]) end |
#timeout ⇒ Object
56 57 58 |
# File 'lib/blazer/data_source.rb', line 56 def timeout settings["timeout"] end |
#variable_defaults ⇒ Object
52 53 54 |
# File 'lib/blazer/data_source.rb', line 52 def variable_defaults settings["variable_defaults"] || {} end |