Sha256: 910f4eaffaca1ca11a864b22fae01b10ab19c9cfaa0d77587e980f11b4df8c01

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Trifle
  module Stats
    module Operations
      module Timeseries
        class Classify
          attr_reader :key, :values

          def initialize(**keywords)
            @key = keywords.fetch(:key)
            @at = keywords.fetch(:at)
            @values = keywords.fetch(:values)
            @config = keywords[:config]
          end

          def config
            @config || Trifle::Stats.default
          end

          def deep_classify(hash)
            hash.transform_values do |value|
              next deep_classify(value) if value.is_a?(Hash)

              { classify(value) => 1 }
            end
          end

          def classify(value)
            config.designator.designate(value: value).to_s.gsub('.', '_')
          end

          def key_for(range:)
            at = Nocturnal.new(@at, config: config).send(range)
            [key, range, at.to_i]
          end

          def perform
            config.driver.inc(
              keys: config.ranges.map { |range| key_for(range: range) },
              **deep_classify(values)
            )
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
trifle-stats-1.3.1 lib/trifle/stats/operations/timeseries/classify.rb
trifle-stats-1.3.0 lib/trifle/stats/operations/timeseries/classify.rb
trifle-stats-1.2.0 lib/trifle/stats/operations/timeseries/classify.rb
trifle-stats-1.1.2 lib/trifle/stats/operations/timeseries/classify.rb
trifle-stats-1.1.1 lib/trifle/stats/operations/timeseries/classify.rb