Sha256: 695fa9d002a566049d13f760d1813d2c2b4e6bfdf57493e2680b7aa8a8b9e9d0

Contents?: true

Size: 1.52 KB

Versions: 20

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true
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

20 entries across 20 versions & 1 rubygems

Version Path
hyrax-3.6.0 app/services/hyrax/statistics/over_time.rb
hyrax-4.0.0 app/services/hyrax/statistics/over_time.rb
hyrax-4.0.0.rc3 app/services/hyrax/statistics/over_time.rb
hyrax-4.0.0.rc2 app/services/hyrax/statistics/over_time.rb
hyrax-4.0.0.rc1 app/services/hyrax/statistics/over_time.rb
hyrax-3.5.0 app/services/hyrax/statistics/over_time.rb
hyrax-4.0.0.beta2 app/services/hyrax/statistics/over_time.rb
hyrax-3.4.2 app/services/hyrax/statistics/over_time.rb
hyrax-4.0.0.beta1 app/services/hyrax/statistics/over_time.rb
hyrax-3.4.1 app/services/hyrax/statistics/over_time.rb
hyrax-3.4.0 app/services/hyrax/statistics/over_time.rb
hyrax-3.3.0 app/services/hyrax/statistics/over_time.rb
hyrax-3.2.0 app/services/hyrax/statistics/over_time.rb
hyrax-3.1.0 app/services/hyrax/statistics/over_time.rb
hyrax-3.0.2 app/services/hyrax/statistics/over_time.rb
hyrax-3.0.1 app/services/hyrax/statistics/over_time.rb
hyrax-3.0.0 app/services/hyrax/statistics/over_time.rb
hyrax-3.0.0.pre.rc4 app/services/hyrax/statistics/over_time.rb
hyrax-3.0.0.pre.rc3 app/services/hyrax/statistics/over_time.rb
hyrax-3.0.0.pre.rc2 app/services/hyrax/statistics/over_time.rb