Class: Blazer::DataSource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/blazer/data_source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
31
32
# 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 for data source: #{id}"
  end

  @adapter_instance =
    case adapter
    when "elasticsearch"
      Blazer::Adapters::ElasticsearchAdapter.new(self)
    when "mongodb"
      Blazer::Adapters::MongodbAdapter.new(self)
    when "presto"
      Blazer::Adapters::PrestoAdapter.new(self)
    when "sql"
      Blazer::Adapters::SqlAdapter.new(self)
    else
      raise Blazer::Error, "Unknown adapter"
    end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/blazer/data_source.rb', line 7

def adapter
  @adapter
end

#adapter_instanceObject (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

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/blazer/data_source.rb', line 7

def id
  @id
end

#settingsObject (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

#cacheObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/blazer/data_source.rb', line 62

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_inObject



83
84
85
# File 'lib/blazer/data_source.rb', line 83

def cache_expires_in
  (cache["expires_in"] || 60).to_f
end

#cache_key(key) ⇒ Object



150
151
152
# File 'lib/blazer/data_source.rb', line 150

def cache_key(key)
  (["blazer", "v4"] + key).join("/")
end

#cache_modeObject



79
80
81
# File 'lib/blazer/data_source.rb', line 79

def cache_mode
  cache["mode"]
end

#cache_slow_thresholdObject



87
88
89
# File 'lib/blazer/data_source.rb', line 87

def cache_slow_threshold
  (cache["slow_threshold"] || 15).to_f
end

#clear_cache(statement) ⇒ Object



146
147
148
# File 'lib/blazer/data_source.rb', line 146

def clear_cache(statement)
  Blazer.cache.delete(statement_cache_key(statement))
end

#delete_results(run_id) ⇒ Object



106
107
108
# File 'lib/blazer/data_source.rb', line 106

def delete_results(run_id)
  Blazer.cache.delete(run_cache_key(run_id))
end

#linked_columnsObject



42
43
44
# File 'lib/blazer/data_source.rb', line 42

def linked_columns
  settings["linked_columns"] || {}
end

#local_time_suffixObject



91
92
93
# File 'lib/blazer/data_source.rb', line 91

def local_time_suffix
  @local_time_suffix ||= Array(settings["local_time_suffix"])
end

#nameObject



38
39
40
# File 'lib/blazer/data_source.rb', line 38

def name
  settings["name"] || @id
end

#read_cache(cache_key) ⇒ Object



95
96
97
98
99
100
# File 'lib/blazer/data_source.rb', line 95

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



158
159
160
# File 'lib/blazer/data_source.rb', line 158

def run_cache_key(run_id)
  cache_key(["run", run_id])
end

#run_results(run_id) ⇒ Object



102
103
104
# File 'lib/blazer/data_source.rb', line 102

def run_results(run_id)
  read_cache(run_cache_key(run_id))
end

#run_statement(statement, options = {}) ⇒ Object



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
135
136
137
138
139
140
141
142
143
144
# File 'lib/blazer/data_source.rb', line 110

def run_statement(statement, options = {})
  run_id = options[:run_id]
  async = options[:async]
  result = nil
  if cache_mode != "off"
    if options[:refresh_cache]
      clear_cache(statement) # for checks
    else
      result = read_cache(statement_cache_key(statement))
    end
  end

  unless result
    comment = "blazer"
    if options[:user].respond_to?(:id)
      comment << ",user_id:#{options[:user].id}"
    end
    if options[: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 options[:query].respond_to?(:id)
      comment << ",query_id:#{options[:query].id}"
    end
    if options[:check]
      comment << ",check_id:#{options[:check].id},check_emails:#{options[:check].emails}"
    end
    if options[:run_id]
      comment << ",run_id:#{options[:run_id]}"
    end
    result = run_statement_helper(statement, comment, async ? options[:run_id] : nil)
  end

  result
end

#smart_columnsObject



46
47
48
# File 'lib/blazer/data_source.rb', line 46

def smart_columns
  settings["smart_columns"] || {}
end

#smart_variablesObject



50
51
52
# File 'lib/blazer/data_source.rb', line 50

def smart_variables
  settings["smart_variables"] || {}
end

#statement_cache_key(statement) ⇒ Object



154
155
156
# File 'lib/blazer/data_source.rb', line 154

def statement_cache_key(statement)
  cache_key(["statement", id, Digest::MD5.hexdigest(statement.to_s.gsub("\r\n", "\n"))])
end

#timeoutObject



58
59
60
# File 'lib/blazer/data_source.rb', line 58

def timeout
  settings["timeout"]
end

#variable_defaultsObject



54
55
56
# File 'lib/blazer/data_source.rb', line 54

def variable_defaults
  settings["variable_defaults"] || {}
end