Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'id' param to all Ariadne 'get' methods #6

Merged
merged 1 commit into from
Jun 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added 'id' param to all Ariadne 'get' methods
- 'id' param will help improving the overall performance of application
as the user will be able to fetch the required data directly by passing the
'id' i.e. previously used to store the data.
- new TCs written for modified methods
  • Loading branch information
Swapnil committed Jun 24, 2016
commit d7652a2dae344274f97c4b1492e70e72eab0c4fc
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
ariadne (0.0.1)
fakeredis
hiredis
redis

GEM
Expand All @@ -15,6 +16,7 @@ GEM
docile (1.1.5)
fakeredis (0.5.0)
redis (~> 3.0)
hiredis (0.6.1)
json (1.8.3)
method_source (0.8.2)
minitest (5.8.4)
Expand Down Expand Up @@ -44,6 +46,7 @@ DEPENDENCIES
minitest (~> 5.0)
pry-byebug
rake
simplecov
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this gem is automatically added after bundle install


BUNDLED WITH
1.11.2
1 change: 1 addition & 0 deletions ariadne.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
spec.add_runtime_dependency 'redis'
spec.add_runtime_dependency 'hiredis'
spec.add_runtime_dependency 'fakeredis'
end
30 changes: 16 additions & 14 deletions lib/ariadne/ariadne_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@
require 'json'
require 'time'
require 'redis'
require 'redis/connection/hiredis'

module Ariadne
DataUtil.init_redis_cli(Redis.new(url: ENV['REDIS_URL']))
def self.get_app_name(app_name: nil)
app_name ||= ( ENV['APP_NAME'] || '' )
rescue StandardError => e
puts "Can't connect to the Redis databse! Object Undefined!"
end

DataUtil.init_redis_cli(redis_obj: Redis.new(url: ENV['REDIS_URL']))

def self.insert_data(options = {})
raise 'Please specify options to be passed for Ariadne.insert_data method!' if options.size <= 0
raise 'Please specify id and application name to be passed for Ariadne.insert_data method!' if options[:id].nil? || options[:id].size <= 0
DataUtil.insert_data_in_redis(options.merge!(app_name: get_app_name(options[:app_name])))
def self.insert_data(options: {}, app_name: nil)
raise 'Please specify data to be inserted for Ariadne.insert_data method!' if options.empty?
raise 'Please specify id to be passed for Ariadne.insert_data method!' if options['id'].nil? || options['id'].size == 0
DataUtil.insert_data_in_redis(options: options, app_name: get_app_name(app_name: app_name))
rescue StandardError => e
puts e
e
end

def self.get_data(app_name = '')
DataUtil.get_data_from_redis(get_app_name(app_name))
def self.get_data(id: nil, app_name: nil)
DataUtil.get_data_from_redis(id: id, app_name: get_app_name(app_name: app_name))
rescue StandardError => e
puts e
e
end

def self.get_data_with_time_difference(app_name = '')
redis_data = get_data(get_app_name(app_name))
def self.get_data_with_time_difference(id: nil, app_name: nil)
redis_data = get_data(id: id, app_name: app_name)
default_time_diff_threshold = 30
output_data = []
unless redis_data.nil?
Expand All @@ -40,9 +47,4 @@ def self.get_data_with_time_difference(app_name = '')
end
output_data.to_json
end

def self.get_app_name(app_name = '')
app_name = ENV['APP_NAME'] if app_name.nil? || app_name.size <= 0
app_name || ''
end
end
18 changes: 11 additions & 7 deletions lib/ariadne/data_util.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@

module DataUtil
def self.init_redis_cli(obj = nil)
@redis_cli = obj
def self.init_redis_cli(redis_obj: nil)
raise "Redis object not found!" if redis_obj.nil?
@redis_cli = redis_obj
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is begin not required for the begin-resque-end ? Sure?

rescue StandardError => e
puts e
end

def self.get_data_from_redis(app_name)
keys = @redis_cli.keys "#{app_name}*"
def self.get_data_from_redis(id:, app_name:)
key = id.nil? ? "#{app_name}*" : "#{app_name}:#{id}"
keys = @redis_cli.keys key
raise "Data not available for #{app_name}!" if keys.empty?
keys.compact!
redis_data = (@redis_cli.mget keys)
Expand All @@ -14,13 +18,13 @@ def self.get_data_from_redis(app_name)
puts e
end

def self.insert_data_in_redis(options = {})
key = "#{options[:app_name]}:#{options[:id]}"
def self.insert_data_in_redis(options:, app_name:)
key = "#{app_name}:#{options['id']}"
options[:time] = Time.now
redis_data = nil
@redis_cli.pipelined do
redis_data = @redis_cli.set key, options.to_json
@redis_cli.expireat(key, Time.now + (24 * 60 * 60)) # expire a key after 1 day
@redis_cli.expire(key, (24 * 60 * 60)) # expire a key after 1 day
end
redis_data
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ariadne/testing.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative 'data_util'
require 'fakeredis'

DataUtil.init_redis_cli(Redis.new)
DataUtil.init_redis_cli(redis_obj: Redis.new)
85 changes: 59 additions & 26 deletions test/test_ariadne.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,90 @@
require_relative 'test_helper'

class AriadneTest < MiniTest::Test
def test_insert_data
app_name = 'test'
data = {
id: 123,
state: 'active',
app_name: app_name
'id' => 123,
'state' => 'active'
}

out = Ariadne.insert_data(data)
out = Ariadne.insert_data(options: data, app_name: app_name)
assert_equal Redis::Future, out.class
end

def test_get_data
app_name = 'test'
data = {
id: 123,
state: 'active',
app_name: app_name
'id' => 123,
'state' => 'active'
}

Ariadne.insert_data(data)
out = Ariadne.get_data(data[:app_name])
assert_equal JSON.parse(out[0])['id'], data[:id]
Ariadne.insert_data(options: data, app_name: app_name)
out = Ariadne.get_data(app_name: app_name)
assert_equal JSON.parse(out[0])['id'], data['id']
end

def test_get_data_with_time_diff_without_delay_should_be_empty
app_name = 'test'
data = {
id: 123,
state: 'active',
app_name: app_name
'id' => 123,
'state' => 'active'
}

Ariadne.insert_data(data)
out = Ariadne.get_data_with_time_difference(app_name)
Ariadne.insert_data(options: data, app_name: app_name)
out = Ariadne.get_data_with_time_difference(app_name: app_name)
assert_empty JSON.parse(out)
end

def test_get_data_with_time_diff_with_delay_should_not_be_empty
app_name = 'test'
app_name = 'test'
delay_interval = 2
data = {
'id' => 123,
'state' => 'active',
'delay_interval' => delay_interval
}

Ariadne.insert_data(options: data, app_name: app_name)
sleep delay_interval
out = Ariadne.get_data_with_time_difference(app_name: app_name)
assert_equal JSON.parse(out)[0]['id'], data['id']
end

def test_get_data_with_id
app_name = 'test'
data = {
'id' => 123,
'state' => 'active'
}

Ariadne.insert_data(options: data, app_name: app_name)
out = Ariadne.get_data(id: data['id'], app_name: app_name)
assert_equal JSON.parse(out[0])['id'], data['id']
end

def test_get_data_with_time_diff_with_id_without_delay_should_be_empty
app_name = 'test'
data = {
'id' => 123,
'state' => 'active'
}

Ariadne.insert_data(options: data, app_name: app_name)
out = Ariadne.get_data_with_time_difference(id: data['id'], app_name: app_name)
assert_empty JSON.parse(out)
end

def test_get_data_with_time_diff_with_id_with_delay_should_not_be_empty
app_name = 'test'
delay_interval = 2
data = {
id: 123,
state: 'active',
app_name: app_name,
delay_interval: delay_interval
'id' => 123,
'state' => 'active',
'delay_interval' => delay_interval
}

Ariadne.insert_data(data)
Ariadne.insert_data(options: data, app_name: app_name)
sleep delay_interval
out = Ariadne.get_data_with_time_difference(app_name)
assert_equal JSON.parse(out)[0]['id'], data[:id]
out = Ariadne.get_data_with_time_difference(id: data['id'], app_name: app_name)
assert_equal JSON.parse(out)[0]['id'], data['id']
end
end
end