Skip to content

Commit 3b0235c

Browse files
committed
feature: позволит передавать набор аргументов для дампа
1 parent 9fec297 commit 3b0235c

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

lib/redis_counters/dumpers/engine.rb

+9-12
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module Dumpers
5959
# end
6060
# end
6161
#
62-
# dumper.process!(counter, Date.yesterday)
62+
# dumper.process!(counter, date: Date.yesterday)
6363
#
6464
# В результате все данные счетчика за вчера, будут
6565
# смерджены в целевые таблицы, по ключевым полям: company_id и date,
@@ -100,7 +100,7 @@ class Engine
100100
attr_accessor :common_params
101101

102102
attr_reader :counter
103-
attr_reader :date
103+
attr_reader :args
104104

105105
# callbacks
106106

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

137138
db_transaction do
138139
merge_data
@@ -185,7 +186,7 @@ def merge_data
185186
end
186187

187188
def fill_temp_table
188-
@rows_processed = counter.data(:date => formatted_date) do |batch|
189+
@rows_processed = counter.data(args) do |batch|
189190
@current_batch = batch
190191
prepare_batch
191192
insert_batch
@@ -225,8 +226,8 @@ def batch_data
225226

226227
def delete_from_redis
227228
redis_session.pipelined do |redis|
228-
counter.partitions(:date => formatted_date).each do |partition|
229-
counter.delete_partition_direct!(partition, redis)
229+
counter.partitions(args).each do |partition|
230+
counter.delete_partition_direct!(args.merge(partition), redis)
230231
end
231232
end
232233

@@ -283,10 +284,6 @@ def columns_definition
283284
end.join(',')
284285
end
285286

286-
def formatted_date
287-
date.strftime(DATE_FORMAT)
288-
end
289-
290287
def db_connection
291288
destinations.first.connection
292289
end

spec/lib/redis_counters/dumpers/engine_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
end
3838

3939
on_before_merge do |dumper, _connection|
40-
dumper.common_params = {date: dumper.date.strftime('%Y-%m-%d')}
40+
dumper.common_params = {date: dumper.args[:date].strftime('%Y-%m-%d')}
4141
end
4242
end
4343
end
@@ -68,14 +68,14 @@
6868
counter.increment(date: prev_date_s, record_id: 1, column_id: 200, subject: '')
6969
counter.increment(date: prev_date_s, record_id: 2, column_id: 100, subject: nil)
7070

71-
dumper.process!(counter, prev_date)
71+
dumper.process!(counter, date: prev_date)
7272

7373
counter.increment(date: date_s, record_id: 1, column_id: 100, subject: '')
7474
counter.increment(date: date_s, record_id: 1, column_id: 200, subject: '')
7575
counter.increment(date: date_s, record_id: 1, column_id: 200, subject: '')
7676
counter.increment(date: date_s, record_id: 2, column_id: 100, subject: nil)
7777

78-
dumper.process!(counter, date)
78+
dumper.process!(counter, date: date)
7979
end
8080

8181
Then { expect(StatsByDay.count).to eq 6 }
@@ -134,7 +134,7 @@
134134
end
135135

136136
on_before_merge do |dumper, _connection|
137-
dumper.common_params = {date: dumper.date.strftime('%Y-%m-%d')}
137+
dumper.common_params = {date: dumper.args[:date].strftime('%Y-%m-%d')}
138138
end
139139
end
140140
end

0 commit comments

Comments
 (0)