Skip to content

Commit

Permalink
feature: позволит передавать набор аргументов для дампа
Browse files Browse the repository at this point in the history
  • Loading branch information
vadshalamov committed Aug 31, 2016
1 parent 9fec297 commit 3b0235c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
21 changes: 9 additions & 12 deletions lib/redis_counters/dumpers/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module Dumpers
# end
# end
#
# dumper.process!(counter, Date.yesterday)
# dumper.process!(counter, date: Date.yesterday)
#
# В результате все данные счетчика за вчера, будут
# смерджены в целевые таблицы, по ключевым полям: company_id и date,
Expand Down Expand Up @@ -100,7 +100,7 @@ class Engine
attr_accessor :common_params

attr_reader :counter
attr_reader :date
attr_reader :args

# callbacks

Expand All @@ -127,12 +127,13 @@ class Engine
# Public: Производит перенос данных счетчика.
#
# counter - экземпляр счетчика.
# date - Date - дата, за которую производится перенос данных.
# args - Hash - набор аргументов(кластер и/или партиции) для переноса данных.
#
# Returns Fixnum - кол-во обработанных строк.
#
def process!(counter, date)
@counter, @date = counter, date
def process!(counter, args = {})
@counter = counter
@args = args

db_transaction do
merge_data
Expand Down Expand Up @@ -185,7 +186,7 @@ def merge_data
end

def fill_temp_table
@rows_processed = counter.data(:date => formatted_date) do |batch|
@rows_processed = counter.data(args) do |batch|
@current_batch = batch
prepare_batch
insert_batch
Expand Down Expand Up @@ -225,8 +226,8 @@ def batch_data

def delete_from_redis
redis_session.pipelined do |redis|
counter.partitions(:date => formatted_date).each do |partition|
counter.delete_partition_direct!(partition, redis)
counter.partitions(args).each do |partition|
counter.delete_partition_direct!(args.merge(partition), redis)
end
end

Expand Down Expand Up @@ -283,10 +284,6 @@ def columns_definition
end.join(',')
end

def formatted_date
date.strftime(DATE_FORMAT)
end

def db_connection
destinations.first.connection
end
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/redis_counters/dumpers/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
end

on_before_merge do |dumper, _connection|
dumper.common_params = {date: dumper.date.strftime('%Y-%m-%d')}
dumper.common_params = {date: dumper.args[:date].strftime('%Y-%m-%d')}
end
end
end
Expand Down Expand Up @@ -68,14 +68,14 @@
counter.increment(date: prev_date_s, record_id: 1, column_id: 200, subject: '')
counter.increment(date: prev_date_s, record_id: 2, column_id: 100, subject: nil)

dumper.process!(counter, prev_date)
dumper.process!(counter, date: prev_date)

counter.increment(date: date_s, record_id: 1, column_id: 100, subject: '')
counter.increment(date: date_s, record_id: 1, column_id: 200, subject: '')
counter.increment(date: date_s, record_id: 1, column_id: 200, subject: '')
counter.increment(date: date_s, record_id: 2, column_id: 100, subject: nil)

dumper.process!(counter, date)
dumper.process!(counter, date: date)
end

Then { expect(StatsByDay.count).to eq 6 }
Expand Down Expand Up @@ -134,7 +134,7 @@
end

on_before_merge do |dumper, _connection|
dumper.common_params = {date: dumper.date.strftime('%Y-%m-%d')}
dumper.common_params = {date: dumper.args[:date].strftime('%Y-%m-%d')}
end
end
end
Expand Down

0 comments on commit 3b0235c

Please sign in to comment.