Sha256: a1fa6290c0a40a53089e0341e5349df11007769bfbfe5c430fa5cd2931d1bc38

Contents?: true

Size: 1.44 KB

Versions: 13

Compression:

Stored size: 1.44 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)
          QueryService.new.build_date_query(min, max)
        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

13 entries across 13 versions & 1 rubygems

Version Path
hyrax-1.1.1 app/services/hyrax/statistics/over_time.rb
hyrax-1.1.0 app/services/hyrax/statistics/over_time.rb
hyrax-1.0.5 app/services/hyrax/statistics/over_time.rb
hyrax-2.0.0.beta4 app/services/hyrax/statistics/over_time.rb
hyrax-2.0.0.beta3 app/services/hyrax/statistics/over_time.rb
hyrax-2.0.0.beta2 app/services/hyrax/statistics/over_time.rb
hyrax-2.0.0.beta1 app/services/hyrax/statistics/over_time.rb
hyrax-1.0.4 app/services/hyrax/statistics/over_time.rb
hyrax-1.0.3 app/services/hyrax/statistics/over_time.rb
hyrax-1.0.2 app/services/hyrax/statistics/over_time.rb
hyrax-1.0.1 app/services/hyrax/statistics/over_time.rb
hyrax-1.0.0.rc2 app/services/hyrax/statistics/over_time.rb
hyrax-1.0.0.rc1 app/services/hyrax/statistics/over_time.rb