class RailsPerformance::DataSource

Constants

KLASSES

Attributes

klass[R]
q[R]
type[R]

Public Class Methods

new(q: {}, type:) click to toggle source
# File lib/rails_performance/data_source.rb, line 14
def initialize(q: {}, type:)
  @type    = type
  @klass   = KLASSES[type]
  q[:on] ||= Date.today
  @q       = q
end

Public Instance Methods

add_to(storage = RailsPerformance::Models::Collection.new) click to toggle source
# File lib/rails_performance/data_source.rb, line 33
def add_to(storage = RailsPerformance::Models::Collection.new)
  store do |record|
    storage.add(record)
  end
  storage
end
db() click to toggle source
# File lib/rails_performance/data_source.rb, line 21
def db
  result = RailsPerformance::Models::Collection.new
  (RailsPerformance::Utils.days + 1).times do |e|
    RailsPerformance::DataSource.new(q: self.q.merge({ on: e.days.ago.to_date }), type: type).add_to(result)
  end
  result
end
default?() click to toggle source
# File lib/rails_performance/data_source.rb, line 29
def default?
  @q.keys == [:on]
end
store() { |from_db| ... } click to toggle source
# File lib/rails_performance/data_source.rb, line 40
def store
  keys, values = Utils.fetch_from_redis(query)

  return [] if keys.blank?

  keys.each_with_index do |key, index|
    yield klass.from_db(key, values[index])
  end
end

Private Instance Methods

compile_custom_query() click to toggle source
# File lib/rails_performance/data_source.rb, line 106
def compile_custom_query
  str = []
  str << "datetime|#{q[:on].strftime('%Y%m%d')}*|" if q[:on].present?
  str << "status|#{q[:status]}|" if q[:status].present?
  str.join("*")
end
compile_delayed_job_query() click to toggle source
# File lib/rails_performance/data_source.rb, line 92
def compile_delayed_job_query
  str = []
  str << "datetime|#{q[:on].strftime('%Y%m%d')}*|" if q[:on].present?
  str << "status|#{q[:status]}|" if q[:status].present?
  str.join("*")
end
compile_grape_query() click to toggle source
# File lib/rails_performance/data_source.rb, line 113
def compile_grape_query
  str = []
  str << "datetime|#{q[:on].strftime('%Y%m%d')}*|" if q[:on].present?
  str << "status|#{q[:status]}|" if q[:status].present?
  str.join("*")
end
compile_rake_query() click to toggle source
# File lib/rails_performance/data_source.rb, line 99
def compile_rake_query
  str = []
  str << "datetime|#{q[:on].strftime('%Y%m%d')}*|" if q[:on].present?
  str << "status|#{q[:status]}|" if q[:status].present?
  str.join("*")
end
compile_requests_query() click to toggle source
# File lib/rails_performance/data_source.rb, line 71
def compile_requests_query
  str = []
  str << "controller|#{q[:controller]}|" if q[:controller].present?
  str << "action|#{q[:action]}|" if q[:action].present?
  str << "format|#{q[:format]}|" if q[:format].present?
  str << "status|#{q[:status]}|" if q[:status].present?
  str << "datetime|#{q[:on].strftime('%Y%m%d')}*|" if q[:on].present?
  str << "method|#{q[:method]}|" if q[:method].present?
  str << "path|#{q[:path]}|" if q[:path].present?
  str.join("*")
end
compile_sidekiq_query() click to toggle source
# File lib/rails_performance/data_source.rb, line 83
def compile_sidekiq_query
  str = []
  str << "queue|#{q[:queue]}|" if q[:queue].present?
  str << "worker|#{q[:worker]}|" if q[:worker].present?
  str << "datetime|#{q[:on].strftime('%Y%m%d')}*|" if q[:on].present?
  str << "status|#{q[:status]}|" if q[:status].present?
  str.join("*")
end
query() click to toggle source
# File lib/rails_performance/data_source.rb, line 52
def query
  case type
  when :requests
    "performance|*#{compile_requests_query}*|END|#{RailsPerformance::SCHEMA}"
  when :sidekiq
    "sidekiq|*#{compile_sidekiq_query}*|END|#{RailsPerformance::SCHEMA}"
  when :delayed_job
    "delayed_job|*#{compile_delayed_job_query}*|END|#{RailsPerformance::SCHEMA}"
  when :grape
    "grape|*#{compile_grape_query}*|END|#{RailsPerformance::SCHEMA}"
  when :rake
    "rake|*#{compile_rake_query}*|END|#{RailsPerformance::SCHEMA}"
  when :custom
    "custom|*#{compile_custom_query}*|END|#{RailsPerformance::SCHEMA}"
  else
    raise "wrong type for datasource query builder"
  end
end