Class: RailsPerformance::DataSource

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_performance/data_source.rb

Constant Summary collapse

KLASSES =
{
  requests: RailsPerformance::Models::RequestRecord,
  sidekiq: RailsPerformance::Models::SidekiqRecord,
  delayed_job: RailsPerformance::Models::DelayedJobRecord,
  grape: RailsPerformance::Models::GrapeRecord,
  rake: RailsPerformance::Models::RakeRecord,
  custom: RailsPerformance::Models::CustomRecord
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, q: {}) ⇒ DataSource

Returns a new instance of DataSource.



14
15
16
17
18
19
# File 'lib/rails_performance/data_source.rb', line 14

def initialize(type:, q: {})
  @type = type
  @klass = KLASSES[type]
  q[:on] ||= Date.today
  @q = q
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



12
13
14
# File 'lib/rails_performance/data_source.rb', line 12

def klass
  @klass
end

#qObject (readonly)

Returns the value of attribute q.



12
13
14
# File 'lib/rails_performance/data_source.rb', line 12

def q
  @q
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/rails_performance/data_source.rb', line 12

def type
  @type
end

Instance Method Details

#add_to(storage = RailsPerformance::Models::Collection.new) ⇒ Object



33
34
35
36
37
38
# 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

#dbObject



21
22
23
24
25
26
27
# File 'lib/rails_performance/data_source.rb', line 21

def db
  result = RailsPerformance::Models::Collection.new
  (0..(RailsPerformance::Utils.days + 1)).to_a.reverse_each do |e|
    RailsPerformance::DataSource.new(q: q.merge({on: (Time.current - e.days).to_date}), type: type).add_to(result)
  end
  result
end

#default?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/rails_performance/data_source.rb', line 29

def default?
  @q.keys == [:on]
end

#storeObject



40
41
42
43
44
45
46
47
48
# 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