Class: Redis::TimeSeries::Multi

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

Overview

A Multi is a collection of multiple series and their samples, returned from a multi command (e.g. TS.MGET or TS.MRANGE).

See Also:

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(result_array) ⇒ Multi

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.

Multis are initialized by one of the class-level query commands. There’s no need to ever create one yourself.



13
14
15
16
17
18
19
20
21
# File 'lib/redis/time_series/multi.rb', line 13

def initialize(result_array)
  super(result_array.map do |res|
    Result.new(
      TimeSeries.new(res[0]),
      res[1],
      res[2].map { |s| Sample.new(s[0], s[1]) }
    )
  end)
end

Instance Method Details

#[](index_or_key) ⇒ Multi::Result?

Access a specific result by either array position, or hash lookup.

Parameters:

  • index_or_key (Integer, String)

    The integer position, or series key, of the specific result to return.

Returns:

  • (Multi::Result, nil)

    A single series result, or nil if there is no matching series.



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

def [](index_or_key)
  return super if index_or_key.is_a?(Integer)
  find { |result| result.series.key == index_or_key.to_s }
end

#keysArray<String>

Get all the series keys that are present in this result collection.

Returns:

  • (Array<String>)

    An array of the series keys in these results.



36
37
38
# File 'lib/redis/time_series/multi.rb', line 36

def keys
  map { |r| r.series.key }
end

#sample_countInteger

Get a count of all matching samples from all series in this result collection.

Returns:

  • (Integer)

    The total size of all samples from all series in these results.



61
62
63
# File 'lib/redis/time_series/multi.rb', line 61

def sample_count
  reduce(0) { |size, r| size += r.samples.size }
end

#seriesArray<TimeSeries>

Get all the series objects that are present in this result collection.

Returns:

  • (Array<TimeSeries>)

    An array of the series in these results.



42
43
44
# File 'lib/redis/time_series/multi.rb', line 42

def series
  map(&:series)
end

#to_hHash<Array>

Convert these results into a hash, keyed by series name.

Examples:

{"ts3"=>
  [{:timestamp=>1623945216042, :value=>0.1e1},
   {:timestamp=>1623945216055, :value=>0.3e1},
   {:timestamp=>1623945216069, :value=>0.2e1}]}

Returns:

  • (Hash<Array>)

    A hash of series names and samples.



53
54
55
56
57
# File 'lib/redis/time_series/multi.rb', line 53

def to_h
  super do |result|
    [result.series.key, result.samples.map(&:to_h)]
  end
end