Skip to content

Commit

Permalink
Fix setting labels on TS.CREATE and TS.ALTER (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzunk authored Jun 30, 2020
1 parent a3020d1 commit 579c1ff
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.1.1
Fix setting labels on `TS.CREATE` and `TS.ALTER`

## 0.1.0

Basic functionality. Includes commands:
Expand Down
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'bundler/setup'
require 'active_support/core_ext/numeric/time'
require 'pry'
require 'redis'
require 'redis-time-series'

Redis.current.flushall
Expand Down
2 changes: 1 addition & 1 deletion lib/redis-time-series.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
require 'redis/time_series/sample'

class RedisTimeSeries
VERSION = '0.1.0'
VERSION = '0.1.1'
end
14 changes: 5 additions & 9 deletions lib/redis/time_series.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def add(value, timestamp = '*')

def create
args = [key]
args << "RETENTION #{retention}" if retention
args << "UNCOMPRESSED" if uncompressed
args << "LABELS #{label_string}" if labels.any?
cmd 'TS.CREATE', args
args << ['RETENTION', retention] if retention
args << 'UNCOMPRESSED' if uncompressed
args << ['LABELS', labels.to_a] if labels.any?
cmd 'TS.CREATE', args.flatten
self
end

Expand Down Expand Up @@ -101,7 +101,7 @@ def info

def labels=(val)
@labels = val
cmd 'TS.ALTER', key, 'LABELS', label_string
cmd 'TS.ALTER', key, 'LABELS', labels.to_a.flatten
end

def madd(*values)
Expand Down Expand Up @@ -153,9 +153,5 @@ def cmd(name, *args)
puts "DEBUG: #{name} #{args.join(' ')}" if ENV['DEBUG']
redis.call name, *args
end

def label_string
labels.map { |label, value| "#{label} #{value}" }.join(' ')
end
end
end
11 changes: 10 additions & 1 deletion spec/redis/time_series_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def msec(ts)
end

describe 'TS.CREATE' do
subject(:create) { described_class.create(key, **options) }
subject(:ts) { described_class.new(key) }

let(:create) { described_class.create(key, **options) }
let(:options) { {} }

context 'with no arguments' do
Expand Down Expand Up @@ -50,6 +52,12 @@ def msec(ts)

specify do
expect { create }.to issue_command "TS.CREATE #{key} LABELS foo bar baz 1 plugh true"
expect(ts.info['labels']).to eq [
# TODO: cast values
['foo', 'bar'],
['baz', '1'],
['plugh', 'true']
]
end
end

Expand Down Expand Up @@ -80,6 +88,7 @@ def msec(ts)
specify do
expect { ts.labels = { foo: 'bar' } }.to issue_command \
"TS.ALTER #{key} LABELS foo bar"
expect(ts.info['labels']).to eq [['foo', 'bar']]
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
require 'redis-time-series'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'

# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
config.example_status_persistence_file_path = '.rspec_status'
config.filter_run_when_matching :focus

config.expect_with :rspec do |c|
c.syntax = :expect
Expand Down

0 comments on commit 579c1ff

Please sign in to comment.