Class: Redis::TimeSeries::Aggregation

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/time_series/aggregation.rb

Overview

An aggregation is a combination of a mathematical function, and a time window over which to apply that function. In RedisTimeSeries, aggregations are used to downsample data from a source series to a destination series, using compaction rules.

Constant Summary collapse

TYPES =
%w[
  avg
  count
  first
  last
  max
  min
  range
  std.p
  std.s
  sum
  var.p
  var.s
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, duration) ⇒ Aggregation

Create a new Aggregation given a type and duration.

Parameters:

  • type (String, Symbol)

    one of the valid aggregation TYPES

  • duration (Integer, ActiveSupport::Duration)

    A time window to apply this aggregation over. If you’re using ActiveSupport, duration objects (e.g. 10.minutes) will be automatically coerced.

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/redis/time_series/aggregation.rb', line 55

def initialize(type, duration)
  type = type.to_s.downcase
  unless TYPES.include? type
    raise AggregationError, "#{type} is not a valid aggregation type!"
  end
  @type = type
  if defined?(ActiveSupport::Duration) && duration.is_a?(ActiveSupport::Duration)
    @duration = duration.in_milliseconds
  else
    @duration = duration.to_i
  end
end

Instance Attribute Details

#durationInteger (readonly) Also known as: time_bucket

Returns the time window to apply the aggregation over, in milliseconds.

Returns:

  • (Integer)

    the time window to apply the aggregation over, in milliseconds



33
34
35
# File 'lib/redis/time_series/aggregation.rb', line 33

def duration
  @duration
end

#typeString (readonly) Also known as: aggregation_type

Returns the type of aggregation to apply.

Returns:

  • (String)

    the type of aggregation to apply

See Also:



29
30
31
# File 'lib/redis/time_series/aggregation.rb', line 29

def type
  @type
end

Class Method Details

.parse(agg) ⇒ Aggregation

Parse a method argument into an aggregation.

Parameters:

  • agg (Array, Aggregation)

    an aggregation object, or an array of type and duration [:avg, 60000]

Returns:

  • (Aggregation)

    the parsed aggregation, or the original argument if already an aggregation

Raises:



41
42
43
44
45
46
# File 'lib/redis/time_series/aggregation.rb', line 41

def self.parse(agg)
  return unless agg
  return agg if agg.is_a?(self)
  return new(agg.first, agg.last) if agg.is_a?(Array) && agg.size == 2
  raise AggregationError, "Couldn't parse #{agg} into an aggregation rule!"
end

Instance Method Details

#==(other) ⇒ Boolean

Compares aggregations based on type and duration.

Returns:

  • (Boolean)

    whether the given aggregations are equivalent



82
83
84
85
# File 'lib/redis/time_series/aggregation.rb', line 82

def ==(other)
  parsed = self.class.parse(other)
  type == parsed.type && duration == parsed.duration
end

#to_aArray

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Array)


70
71
72
# File 'lib/redis/time_series/aggregation.rb', line 70

def to_a
  ['AGGREGATION', type, duration]
end

#to_sString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


76
77
78
# File 'lib/redis/time_series/aggregation.rb', line 76

def to_s
  to_a.join(' ')
end