redis-time-series 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23852f9c6baca09369bbcce49642070c13d1212ba2ca9e7eec93dd691702eb72
4
- data.tar.gz: 9edac086b6fc8119382bac0777160d96279df597cdf7cb4f84d27a36532118d2
3
+ metadata.gz: 51234a8f3542da255912f95982ebab2bf66dd657da9e38e55afdedd97029685d
4
+ data.tar.gz: 42a49fdafb136d8ebe4b094f935b0427430672ab810691a35ca2f78328d92135
5
5
  SHA512:
6
- metadata.gz: c8efbc6e6426d316715fc134f71d5d2d7bc334ac2d088340eba39d050185fdbd5cbf2a8d7c6cd80274e9bb3b53766905c30124d551b0d5b863180cb12c89eec0
7
- data.tar.gz: 5a1d988f13fbace8a269b7156fea1858072a15d030439fbad38e2dfcf720503c71182267d424c5557f8bacf61305975c75c35ad2050c8e307c3b46fb8dc8b5f8
6
+ metadata.gz: acc08d1745cf24bfc87c22d3809570daa0b2feae4e5d5b97d4a43a0626217b44ea3ffdb12c3f6799c068bb6e62f0cc7a1b65274e5d8eac5cb13017bfea73dff4
7
+ data.tar.gz: 1ff7aa6fb44dd2729612ac24e2be45496d45bd5a0fc86ed692bd18c38bcb160971c0efed05e598ffabb6471cf4d6903d303a1d0b74e55672c479829b98b26012
@@ -5,6 +5,8 @@ on:
5
5
  branches: [ master ]
6
6
  pull_request:
7
7
  branches: [ master ]
8
+ schedule:
9
+ - cron: '0 0 * * *'
8
10
 
9
11
  jobs:
10
12
  spec:
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## Unreleased
4
+
5
+ ## 0.2.0
6
+ * Converted `#info` to a struct instead of a hash.
7
+ * Added methods on time series for getting info attributes.
8
+
3
9
  ## 0.1.1
4
10
  Fix setting labels on `TS.CREATE` and `TS.ALTER`
5
11
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- redis-time-series (0.1.0)
4
+ redis-time-series (0.1.1)
5
5
  redis (~> 4.0)
6
6
 
7
7
  GEM
@@ -24,7 +24,7 @@ GEM
24
24
  coderay (~> 1.1)
25
25
  method_source (~> 1.0)
26
26
  rake (13.0.1)
27
- redis (4.1.4)
27
+ redis (4.2.1)
28
28
  rspec (3.9.0)
29
29
  rspec-core (~> 3.9.0)
30
30
  rspec-expectations (~> 3.9.0)
data/README.md CHANGED
@@ -133,16 +133,31 @@ ts.range from: 10.minutes.ago, to: Time.current # Time range as keyword args
133
133
  Get info about the series
134
134
  ```ruby
135
135
  ts.info
136
- => {"total_samples"=>3,
137
- "memory_usage"=>4184,
138
- "first_timestamp"=>1593155422582,
139
- "last_timestamp"=>1593155709333,
140
- "retention_time"=>0,
141
- "chunk_count"=>1,
142
- "max_samples_per_chunk"=>256,
143
- "labels"=>[],
144
- "source_key"=>nil,
145
- "rules"=>[]}
136
+ => #<struct Redis::TimeSeries::Info
137
+ total_samples=3,
138
+ memory_usage=4184,
139
+ first_timestamp=1594060993011,
140
+ last_timestamp=1594060993060,
141
+ retention_time=0,
142
+ chunk_count=1,
143
+ max_samples_per_chunk=256,
144
+ labels={"foo"=>"bar"},
145
+ source_key=nil,
146
+ rules=[]>
147
+ # Each info property is also a method on the time series object
148
+ ts.memory_usage
149
+ => 4208
150
+ ts.labels
151
+ => {"foo"=>"bar"}
152
+ ts.total_samples
153
+ => 3
154
+ # Total samples also available as #count, #length, and #size
155
+ ts.count
156
+ => 3
157
+ ts.length
158
+ => 3
159
+ ts.size
160
+ => 3
146
161
  ```
147
162
 
148
163
  ### TODO
@@ -14,7 +14,7 @@ Redis.current.flushall
14
14
 
15
15
  @series = [@ts1, @ts2, @ts3]
16
16
  @series.each do |ts|
17
- 3.times { ts.increment }
17
+ 3.times { ts.increment; sleep 0.01 }
18
18
  end
19
19
 
20
20
  Pry.start
@@ -1,8 +1,10 @@
1
1
  require 'bigdecimal'
2
+ require 'forwardable'
2
3
  require 'time/msec'
4
+ require 'redis/time_series/info'
3
5
  require 'redis/time_series'
4
6
  require 'redis/time_series/sample'
5
7
 
6
8
  class RedisTimeSeries
7
- VERSION = '0.1.1'
9
+ VERSION = '0.2.0'
8
10
  end
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
  class Redis
3
3
  class TimeSeries
4
+ extend Forwardable
5
+
4
6
  class << self
5
7
  def create(key, **options)
6
- new(key, **options).create
8
+ new(key, **options).create(labels: options[:labels])
7
9
  end
8
10
 
9
11
  def madd(data)
@@ -40,12 +42,10 @@ class Redis
40
42
  end
41
43
  end
42
44
 
43
- attr_reader :key, :labels, :redis, :retention, :uncompressed
45
+ attr_reader :key, :redis, :retention, :uncompressed
44
46
 
45
47
  def initialize(key, options = {})
46
48
  @key = key
47
- # TODO: read labels from redis if not loaded in memory
48
- @labels = options[:labels] || []
49
49
  @redis = options[:redis] || self.class.redis
50
50
  @retention = options[:retention]
51
51
  @uncompressed = options[:uncompressed] || false
@@ -57,11 +57,11 @@ class Redis
57
57
  Sample.new(ts, value)
58
58
  end
59
59
 
60
- def create
60
+ def create(labels: nil)
61
61
  args = [key]
62
62
  args << ['RETENTION', retention] if retention
63
63
  args << 'UNCOMPRESSED' if uncompressed
64
- args << ['LABELS', labels.to_a] if labels.any?
64
+ args << ['LABELS', labels.to_a] if labels&.any?
65
65
  cmd 'TS.CREATE', args.flatten
66
66
  self
67
67
  end
@@ -91,17 +91,14 @@ class Redis
91
91
  end
92
92
  alias increment incrby
93
93
 
94
- # TODO: extract Info module, with methods for each property
95
94
  def info
96
- cmd('TS.INFO', key).each_slice(2).reduce({}) do |h, (key, value)|
97
- h[key.gsub(/(.)([A-Z])/,'\1_\2').downcase] = value
98
- h
99
- end
95
+ cmd('TS.INFO', key).then(&Info.method(:parse))
100
96
  end
97
+ def_delegators :info, *Info.members
98
+ %i[count length size].each { |m| def_delegator :info, :total_samples, m }
101
99
 
102
100
  def labels=(val)
103
- @labels = val
104
- cmd 'TS.ALTER', key, 'LABELS', labels.to_a.flatten
101
+ cmd 'TS.ALTER', key, 'LABELS', val.to_a.flatten
105
102
  end
106
103
 
107
104
  def madd(*values)
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ class Redis
3
+ class TimeSeries
4
+ Info = Struct.new(
5
+ :total_samples,
6
+ :memory_usage,
7
+ :first_timestamp,
8
+ :last_timestamp,
9
+ :retention_time,
10
+ :chunk_count,
11
+ :max_samples_per_chunk,
12
+ :labels,
13
+ :source_key,
14
+ :rules,
15
+ keyword_init: true
16
+ ) do
17
+ def self.parse(raw_array)
18
+ raw_array.each_slice(2).reduce({}) do |h, (key, value)|
19
+ # Convert camelCase info keys to snake_case
20
+ h[key.gsub(/(.)([A-Z])/,'\1_\2').downcase] = value
21
+ h
22
+ end.then do |parsed_hash|
23
+ parsed_hash['labels'] = parsed_hash['labels'].to_h
24
+ new(parsed_hash)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-time-series
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Duszynski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-30 00:00:00.000000000 Z
11
+ date: 2020-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -114,6 +114,7 @@ files:
114
114
  - bin/setup
115
115
  - lib/redis-time-series.rb
116
116
  - lib/redis/time_series.rb
117
+ - lib/redis/time_series/info.rb
117
118
  - lib/redis/time_series/sample.rb
118
119
  - lib/time/msec.rb
119
120
  - redis-time-series.gemspec