Class: Trifle::Stats::Operations::Timeseries::Values

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**keywords) ⇒ Values

Returns a new instance of Values.



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

def initialize(**keywords)
  @key = keywords.fetch(:key)
  @from = keywords.fetch(:from)
  @to = keywords.fetch(:to)
  @granularity = keywords.fetch(:granularity)
  @config = keywords[:config]
  @skip_blanks = keywords[:skip_blanks]
end

Instance Attribute Details

#granularityObject (readonly)

Returns the value of attribute granularity.



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

def granularity
  @granularity
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Instance Method Details

#clean_valuesObject



44
45
46
47
48
49
50
51
# File 'lib/trifle/stats/operations/timeseries/values.rb', line 44

def clean_values
  timeline.each_with_object({ at: [], values: [] }).with_index do |(_at, res), idx|
    next if data[idx].empty?

    res[:at] << timeline[idx]
    res[:values] << data[idx]
  end
end

#configObject



19
20
21
# File 'lib/trifle/stats/operations/timeseries/values.rb', line 19

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

#dataObject



36
37
38
39
40
41
42
# File 'lib/trifle/stats/operations/timeseries/values.rb', line 36

def data
  @data ||= config.driver.get(
    keys: timeline.map do |at|
      Nocturnal::Key.new(key: key, granularity: granularity, at: at)
    end
  )
end

#performObject



60
61
62
# File 'lib/trifle/stats/operations/timeseries/values.rb', line 60

def perform
  @skip_blanks ? clean_values : values
end

#timelineObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/trifle/stats/operations/timeseries/values.rb', line 23

def timeline
  @timeline ||= begin
    pgrn = Nocturnal::Parser.new(granularity)
    Nocturnal.timeline(
      from: localized_time(@from),
      to: localized_time(@to),
      offset: pgrn.offset,
      unit: pgrn.unit,
      config: config
    )
  end
end

#valuesObject



53
54
55
56
57
58
# File 'lib/trifle/stats/operations/timeseries/values.rb', line 53

def values
  {
    at: timeline,
    values: data
  }
end