class Influxer::Client
InfluxDB API client
-
Overriding loggging (use instrumentation and Rails logger)
-
Add cache support for queries
Public Class Methods
new()
click to toggle source
Calls superclass method
# File lib/influxer/client.rb, line 8 def initialize super(**Influxer.config.to_h.symbolize_keys) end
Public Instance Methods
cache_options(sql = nil)
click to toggle source
if sql contains ‘now()’ set expires to 1 minute or :cache_now_for value of config.cache if defined
# File lib/influxer/rails/client.rb, line 34 def cache_options(sql = nil) options = Influxer.config.cache.dup options[:expires_in] = (options[:cache_now_for] || 60) if /\snow\(\)/.match?(sql) options.symbolize_keys end
log_sql(sql) { || ... }
click to toggle source
# File lib/influxer/rails/client.rb, line 17 def log_sql(sql) return yield unless logger.debug? start_ts = Time.now res = yield duration = (Time.now - start_ts) * 1000 name = "InfluxDB SQL (#{duration.round(1)}ms)" # bold black name and blue query string msg = "\e[1m\e[30m#{name}\e[0m \e[34m#{sql}\e[0m" logger.debug msg res end
logger()
click to toggle source
# File lib/influxer/rails/client.rb, line 45 def logger Rails.logger end
normalized_cache_key(sql)
click to toggle source
add prefix; remove whitespaces
# File lib/influxer/rails/client.rb, line 41 def normalized_cache_key(sql) "influxer:#{sql.gsub(/\s*/, "")}" end
query(sql, options = {})
click to toggle source
Calls superclass method
# File lib/influxer/rails/client.rb, line 7 def query(sql, options = {}) log_sql(sql) do if !(options.fetch(:cache, true) && Influxer.config.cache_enabled) super(sql, **options) else Rails.cache.fetch(normalized_cache_key(sql), **cache_options(sql)) { super(sql, **options) } end end end
time_precision()
click to toggle source
# File lib/influxer/client.rb, line 12 def time_precision @config.time_precision end