Sha256: 93e7f78c8bf113b5cec8499ab0828aa19f332d2512297e683f58119f4088f565

Contents?: true

Size: 1.52 KB

Versions: 44

Compression:

Stored size: 1.52 KB

Contents

module Hyrax
  module Statistics
    # An abstract class for generating cumulative graphs
    # you must provide a `relation` method in the concrete class
    class OverTime
      # @param [Fixnum] delta_x change in x (in days)
      # @param [Time] x_min minimum date
      # @param [Time] x_max max date
      # @param [Lambda] x_output a lambda for converting x to the desired output value.
      #                 defaults to milliseconds since the epoch
      def initialize(delta_x: 7,
                     x_min: 1.month.ago.beginning_of_day,
                     x_max: Time.zone.now.end_of_day,
                     x_output: ->(x) { x.to_i * 1000 })
        @delta_x = delta_x
        @x_min = x_min
        @x_max = x_max
        @x_output = x_output
      end

      def points
        Enumerator.new(size) do |y|
          x = @x_min
          while x <= @x_max
            x += @delta_x.days
            y.yield [@x_output.call(x), point(@x_min, x)]
          end
        end
      end

      private

        def point(min, max)
          relation.where(query(min, max)).count
        end

        def query(min, max)
          query_service.build_date_query(min, max)
        end

        def query_service
          Hyrax::Statistics::QueryService.new
        end

        # How many points are in this data set
        def size
          ((@x_max - @x_min) / @delta_x.days.to_i).ceil
        end

        def relation
          raise NotImplementedError, "Implement the relation method in a concrete class"
        end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
hyrax-2.9.6 app/services/hyrax/statistics/over_time.rb
hyrax-2.9.5 app/services/hyrax/statistics/over_time.rb
hyrax-2.9.4 app/services/hyrax/statistics/over_time.rb
hyrax-2.9.3 app/services/hyrax/statistics/over_time.rb
hyrax-2.9.2 app/services/hyrax/statistics/over_time.rb
hyrax-2.9.1 app/services/hyrax/statistics/over_time.rb
hyrax-2.9.0 app/services/hyrax/statistics/over_time.rb
hyrax-2.8.0 app/services/hyrax/statistics/over_time.rb
hyrax-2.7.2 app/services/hyrax/statistics/over_time.rb
hyrax-2.7.1 app/services/hyrax/statistics/over_time.rb
hyrax-2.7.0 app/services/hyrax/statistics/over_time.rb
hyrax-2.6.0 app/services/hyrax/statistics/over_time.rb
hyrax-3.0.0.pre.rc1 app/services/hyrax/statistics/over_time.rb
hyrax-3.0.0.pre.beta3 app/services/hyrax/statistics/over_time.rb
hyrax-2.5.1 app/services/hyrax/statistics/over_time.rb
hyrax-2.5.0 app/services/hyrax/statistics/over_time.rb
hyrax-3.0.0.pre.beta2 app/services/hyrax/statistics/over_time.rb
hyrax-2.4.1 app/services/hyrax/statistics/over_time.rb
hyrax-3.0.0.pre.beta1 app/services/hyrax/statistics/over_time.rb
hyrax-2.4.0 app/services/hyrax/statistics/over_time.rb