Class: Influxer::Client
- Inherits:
-
InfluxDB::Client
- Object
- InfluxDB::Client
- Influxer::Client
- Defined in:
- lib/influxer/client.rb,
lib/influxer/rails/client.rb
Overview
-
Overriding loggging (use instrumentation and Rails logger)
-
Add cache support for queries
Instance Method Summary collapse
-
#cache_options(sql = nil) ⇒ Object
if sql contains ‘now()’ set expires to 1 minute or :cache_now_for value of config.cache if defined.
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #log_sql(sql) ⇒ Object
- #logger ⇒ Object
-
#normalized_cache_key(sql) ⇒ Object
add prefix; remove whitespaces.
- #query(sql, options = {}) ⇒ Object
- #time_precision ⇒ Object
Constructor Details
Instance Method Details
#cache_options(sql = nil) ⇒ Object
if sql contains ‘now()’ set expires to 1 minute or :cache_now_for value of config.cache if defined
34 35 36 37 38 |
# File 'lib/influxer/rails/client.rb', line 34 def (sql = nil) = Influxer.config.cache.dup [:expires_in] = ([:cache_now_for] || 60) if sql =~ /\snow\(\)/ end |
#log_sql(sql) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 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 ⇒ Object
45 46 47 |
# File 'lib/influxer/rails/client.rb', line 45 def logger Rails.logger end |
#normalized_cache_key(sql) ⇒ Object
add prefix; remove whitespaces
41 42 43 |
# File 'lib/influxer/rails/client.rb', line 41 def normalized_cache_key(sql) "influxer:#{sql.gsub(/\s*/, '')}" end |
#query(sql, options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/influxer/rails/client.rb', line 7 def query(sql, = {}) log_sql(sql) do if !.fetch(:cache, true) || Influxer.config.cache == false super(sql, ) else Rails.cache.fetch(normalized_cache_key(sql), (sql)) { super(sql, ) } end end end |
#time_precision ⇒ Object
12 13 14 |
# File 'lib/influxer/client.rb', line 12 def time_precision @config.time_precision end |