Sha256: 51090a3d46b6a4cda96fbbc9da8c6f071bd1259ba9be1cafa0b1ee2d3349fcdb

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

module RailsPerformance
  class DataSource
    attr_reader :q

    def initialize(q: {})
      q[:on] ||= Date.today
      @q       = q

      #puts "  [DataSource Q]  -->  #{@q.inspect}\n\n"
    end

    def db
      result = RP::Collection.new
      RP::Utils.days.times do |e|
        RP::DataSource.new(q: self.q.merge({ on: e.days.ago.to_date })).add_to(result)
      end
      result
    end

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

    def add_to(storage = RP::Collection.new)
      store do |record|
        storage.add(record)
      end
      storage
    end

    def store
#      puts "\n\n   [REDIS QUERY]   -->   #{query}\n\n"

      keys   = RP.redis.keys(query)
      return [] if keys.blank?
      values = RP.redis.mget(keys)

      keys.each_with_index do |key, index|
        yield RP::Record.new(key, values[index])
      end
    end

    private

    # key = performance|
    # controller|HomeController|
    # action|index|
    # format|html|
    # status|200|
    # datetime|20200124T0523|
    # datetimei|1579861423|
    # method|GET|
    # path|/|
    # END

    def compile_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

    def query
      "performance|*#{compile_query}*|END"
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_performance-0.0.1.4 lib/rails_performance/data_source.rb
rails_performance-0.0.1.3 lib/rails_performance/data_source.rb