Class: Trifle::Stats::Operations::Timeseries::Classify

Inherits:
Object
  • Object
show all
Defined in:
lib/trifle/stats/operations/timeseries/classify.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**keywords) ⇒ Classify

Returns a new instance of Classify.



10
11
12
13
14
15
# File 'lib/trifle/stats/operations/timeseries/classify.rb', line 10

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/trifle/stats/operations/timeseries/classify.rb', line 8

def key
  @key
end

#valuesObject (readonly)

Returns the value of attribute values.



8
9
10
# File 'lib/trifle/stats/operations/timeseries/classify.rb', line 8

def values
  @values
end

Instance Method Details

#classify(value) ⇒ Object



29
30
31
# File 'lib/trifle/stats/operations/timeseries/classify.rb', line 29

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

#configObject



17
18
19
# File 'lib/trifle/stats/operations/timeseries/classify.rb', line 17

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

#deep_classify(hash) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/trifle/stats/operations/timeseries/classify.rb', line 21

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

    { classify(value) => 1 }
  end
end

#key_for(range:) ⇒ Object



33
34
35
36
# File 'lib/trifle/stats/operations/timeseries/classify.rb', line 33

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

#performObject



38
39
40
41
42
43
# File 'lib/trifle/stats/operations/timeseries/classify.rb', line 38

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