Class: Redis::TimeSeries::Sample

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

Overview

A sample is an immutable value object that represents a single data point within a time series.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamp, value) ⇒ Sample

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.

Samples are returned by time series query methods, there’s no need to create one yourself.



17
18
19
20
# File 'lib/redis/time_series/sample.rb', line 17

def initialize(timestamp, value)
  @time = Time.from_msec(timestamp)
  @value = BigDecimal(value)
end

Instance Attribute Details

#timeTime (readonly)

Returns the sample’s timestamp.

Returns:

  • (Time)

    the sample’s timestamp



9
10
11
# File 'lib/redis/time_series/sample.rb', line 9

def time
  @time
end

#valueBigDecimal (readonly)

Returns the decimal value of the sample.

Returns:

  • (BigDecimal)

    the decimal value of the sample



11
12
13
# File 'lib/redis/time_series/sample.rb', line 11

def value
  @value
end

Instance Method Details

#to_hHash

Returns a hash representation of the sample.

Examples:

{:timestamp=>1595199272401, :value=>0.2e1}

Returns:

  • (Hash)

    a hash representation of the sample



34
35
36
37
38
39
# File 'lib/redis/time_series/sample.rb', line 34

def to_h
  {
    timestamp: to_msec,
    value: value
  }
end

#to_msecInteger

Note:

We’re wrapping the method provided by the TimeMsec refinement for convenience, otherwise it wouldn’t be callable on #time and devs would have to litter using TimeMsec or * 1000.0 wherever they wanted the value.

Returns the millisecond value of the sample’s timestamp.

Returns:

  • (Integer)

    the millisecond value of the sample’s timestamp



27
28
29
# File 'lib/redis/time_series/sample.rb', line 27

def to_msec
  time.ts_msec
end