Skip to content

Commit b5c9bd9

Browse files
committed
feature: добавит hstore в список возможных полей
1 parent 3b0235c commit b5c9bd9

File tree

7 files changed

+43
-23
lines changed

7 files changed

+43
-23
lines changed

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ source 'https://rubygems.org'
44

55
group :development, :test do
66
gem 'combustion', github: 'pat/combustion', ref: '7d0d24c3f36ce0eb336177fc493be0721bc26665'
7+
gem 'activerecord-postgres-hstore', require: false
8+
gem 'simple_hstore_accessor', '~> 0.2', require: false
79
end
810

911
gem 'rack', '< 2' if RUBY_VERSION < '2.2.0'

lib/redis_counters/dumpers/engine.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,8 @@ def columns_definition
266266
'character varying(4000)'
267267
when :integer, :serial, :number
268268
'integer'
269-
when :date
270-
'date'
271-
when :timestamp
272-
'timestamp'
273-
when :boolean
274-
'boolean'
269+
when :date, :timestamp, :boolean, :hstore
270+
type.to_s
275271
else
276272
if type.is_a?(Array) && type.first == :enum
277273
type.last.fetch(:name)
+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class StatsByDay < ActiveRecord::Base
2+
store_accessor :params
23
end
+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class StatsTotal < ActiveRecord::Base
2+
store_accessor :params
23
end

spec/internal/db/schema.rb

+10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
CREATE TYPE subject_types AS ENUM ('');
44
SQL
55

6+
if ::ActiveRecord::VERSION::MAJOR < 4
7+
execute <<-SQL
8+
CREATE EXTENSION IF NOT EXISTS hstore;
9+
SQL
10+
else
11+
enable_extension :hstore
12+
end
13+
614
create_table :stats_by_days do |t|
715
t.integer :record_id, null: false
816
t.integer :column_id, null: false
917
t.date :date, null: false
1018
t.integer :hits, null: false, default: 0
1119
t.column :subject, :subject_types
20+
t.hstore :params
1221
end
1322

1423
add_index :stats_by_days, [:record_id, :column_id, :date], unique: true
@@ -18,6 +27,7 @@
1827
t.integer :column_id, null: false
1928
t.integer :hits, null: false, default: 0
2029
t.column :subject, :subject_types
30+
t.hstore :params
2131
end
2232

2333
add_index :stats_totals, [:record_id, :column_id], unique: true

spec/lib/redis_counters/dumpers/engine_spec.rb

+22-17
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@
88
column_id: :integer,
99
value: :integer,
1010
date: :date,
11-
subject: [:enum, {name: :subject_types}]
11+
subject: [:enum, {name: :subject_types}],
12+
params: :hstore
1213

1314
destination do
1415
model StatsByDay
15-
take :record_id, :column_id, :hits, :date
16-
key_fields :record_id, :column_id, :date
16+
take :record_id, :column_id, :hits, :date, :params
17+
key_fields :record_id, :column_id, :date, :params
1718
increment_fields :hits
1819
map :hits, to: :value
1920
condition 'target.date = :date'
2021
end
2122

2223
destination do
2324
model StatsTotal
24-
take :record_id, :column_id, :hits
25-
key_fields :record_id, :column_id
25+
take :record_id, :column_id, :hits, :params
26+
key_fields :record_id, :column_id, :params
2627
increment_fields :hits
2728
map :hits, to: :value
2829
end
@@ -52,7 +53,7 @@
5253
RedisCounters.create_counter(Redis.current,
5354
counter_class: RedisCounters::HashCounter,
5455
counter_name: :record_hits_by_day,
55-
group_keys: [:record_id, :column_id, :subject],
56+
group_keys: [:record_id, :column_id, :subject, :params],
5657
partition_keys: [:date]
5758
)
5859
end
@@ -63,35 +64,39 @@
6364

6465
describe '#process!' do
6566
before do
66-
counter.increment(date: prev_date_s, record_id: 1, column_id: 100, subject: '')
67-
counter.increment(date: prev_date_s, record_id: 1, column_id: 200, subject: '')
68-
counter.increment(date: prev_date_s, record_id: 1, column_id: 200, subject: '')
69-
counter.increment(date: prev_date_s, record_id: 2, column_id: 100, subject: nil)
67+
counter.increment(date: prev_date_s, record_id: 1, column_id: 100, subject: '', params: '')
68+
counter.increment(date: prev_date_s, record_id: 1, column_id: 200, subject: '', params: '')
69+
counter.increment(date: prev_date_s, record_id: 1, column_id: 200, subject: '', params: '')
70+
counter.increment(date: prev_date_s, record_id: 2, column_id: 100, subject: nil, params: '')
71+
72+
params = {a: 1}.stringify_keys.to_s[1..-2]
73+
counter.increment(date: prev_date_s, record_id: 3, column_id: 300, subject: nil, params: params)
7074

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

73-
counter.increment(date: date_s, record_id: 1, column_id: 100, subject: '')
74-
counter.increment(date: date_s, record_id: 1, column_id: 200, subject: '')
75-
counter.increment(date: date_s, record_id: 1, column_id: 200, subject: '')
76-
counter.increment(date: date_s, record_id: 2, column_id: 100, subject: nil)
77+
counter.increment(date: date_s, record_id: 1, column_id: 100, subject: '', params: '')
78+
counter.increment(date: date_s, record_id: 1, column_id: 200, subject: '', params: '')
79+
counter.increment(date: date_s, record_id: 1, column_id: 200, subject: '', params: '')
80+
counter.increment(date: date_s, record_id: 2, column_id: 100, subject: nil, params: '')
7781

7882
dumper.process!(counter, date: date)
7983
end
8084

81-
Then { expect(StatsByDay.count).to eq 6 }
85+
Then { expect(StatsByDay.count).to eq 7 }
8286
And { expect(StatsByDay.where(record_id: 1, column_id: 100, date: prev_date).first.hits).to eq 1 }
8387
And { expect(StatsByDay.where(record_id: 1, column_id: 200, date: prev_date).first.hits).to eq 2 }
8488
And { expect(StatsByDay.where(record_id: 2, column_id: 100, date: prev_date).first.hits).to eq 1 }
89+
And { expect(StatsByDay.where(record_id: 3, column_id: 300, date: prev_date).first.params).to eq("a" => "1") }
8590
And { expect(StatsByDay.where(record_id: 1, column_id: 100, date: date).first.hits).to eq 1 }
8691
And { expect(StatsByDay.where(record_id: 1, column_id: 200, date: date).first.hits).to eq 2 }
8792
And { expect(StatsByDay.where(record_id: 2, column_id: 100, date: date).first.hits).to eq 1 }
8893

89-
And { expect(StatsTotal.count).to eq 3 }
94+
And { expect(StatsTotal.count).to eq 4 }
9095
And { expect(StatsTotal.where(record_id: 1, column_id: 100).first.hits).to eq 2 }
9196
And { expect(StatsTotal.where(record_id: 1, column_id: 200).first.hits).to eq 4 }
9297
And { expect(StatsTotal.where(record_id: 2, column_id: 100).first.hits).to eq 2 }
9398

94-
And { expect(StatsAggTotal.count).to eq 2 }
99+
And { expect(StatsAggTotal.count).to eq 3 }
95100
And { expect(StatsAggTotal.where(record_id: 1).first.hits).to eq 6 }
96101
And { expect(StatsAggTotal.where(record_id: 2).first.hits).to eq 2 }
97102

spec/spec_helper.rb

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
require 'bundler/setup'
33
require 'redis_counters/dumpers'
44

5+
if ::ActiveRecord::VERSION::MAJOR < 4
6+
require 'activerecord-postgres-hstore'
7+
require 'simple_hstore_accessor'
8+
end
9+
510
require 'combustion'
611
Combustion.initialize! :active_record
712

0 commit comments

Comments
 (0)