mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 05:26:17 +02:00
Refactored statistics caclulation
This commit is contained in:
parent
bd12a73898
commit
e73faae879
4 changed files with 111 additions and 29 deletions
|
@ -36,6 +36,7 @@ module Repp
|
||||||
data: { name: search_params[:end_date],
|
data: { name: search_params[:end_date],
|
||||||
domains: serialize_growth_rate_result(domains_by_rar),
|
domains: serialize_growth_rate_result(domains_by_rar),
|
||||||
market_share: serialize_growth_rate_result(market_share_by_rar) } }
|
market_share: serialize_growth_rate_result(market_share_by_rar) } }
|
||||||
|
|
||||||
render_success(data: result)
|
render_success(data: result)
|
||||||
end
|
end
|
||||||
# rubocop:enable Metrics/MethodLength
|
# rubocop:enable Metrics/MethodLength
|
||||||
|
@ -49,9 +50,9 @@ module Repp
|
||||||
|
|
||||||
def set_date_params
|
def set_date_params
|
||||||
@date_to = to_date(search_params[:end_date]).end_of_month
|
@date_to = to_date(search_params[:end_date]).end_of_month
|
||||||
@date_from = to_date(search_params[:start_date] || '01.05')
|
@date_from = to_date(search_params[:start_date] || '01.00')
|
||||||
@date_compare_to = to_date(search_params[:compare_to_end_date]).end_of_month
|
@date_compare_to = to_date(search_params[:compare_to_end_date]).end_of_month
|
||||||
@date_compare_from = to_date(search_params[:compare_to_start_date] || '01.05')
|
@date_compare_from = to_date(search_params[:compare_to_start_date] || '01.00')
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_date(date_param)
|
def to_date(date_param)
|
||||||
|
@ -78,15 +79,49 @@ module Repp
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# def domains_by_registrar(date_to, date_from)
|
||||||
|
# log_domains_del = log_domains(event: 'destroy', date_to: date_to, date_from: date_from)
|
||||||
|
# log_domains_trans = log_domains(event: 'update', date_to: date_to, date_from: date_from)
|
||||||
|
# logged_domains = log_domains_trans.map { |ld| ld.object['name'] } +
|
||||||
|
# log_domains_del.map { |ld| ld.object['name'] }
|
||||||
|
# domains_grouped = ::Domain.where('created_at <= ? AND created_at >= ?', date_to, date_from)
|
||||||
|
# .where.not(name: logged_domains.uniq)
|
||||||
|
# .group(:registrar_id).count.stringify_keys
|
||||||
|
|
||||||
|
# summarize([group(log_domains_del), group(log_domains_trans), domains_grouped])
|
||||||
|
# end
|
||||||
|
|
||||||
def domains_by_registrar(date_to, date_from)
|
def domains_by_registrar(date_to, date_from)
|
||||||
log_domains_del = log_domains(event: 'destroy', date_to: date_to, date_from: date_from)
|
domain_versions = ::Version::DomainVersion.where('object_changes IS NOT NULL')
|
||||||
log_domains_trans = log_domains(event: 'update', date_to: date_to, date_from: date_from)
|
.where('created_at >= ? AND created_at <= ?', date_from, date_to)
|
||||||
logged_domains = log_domains_trans.map { |ld| ld.object['name'] } +
|
registrar_counts = Hash.new(0)
|
||||||
log_domains_del.map { |ld| ld.object['name'] }
|
processed_domains = []
|
||||||
domains_grouped = ::Domain.where('created_at <= ? AND created_at >= ?', date_to, date_from)
|
domain_versions.find_each(batch_size: 1000) do |v|
|
||||||
.where.not(name: logged_domains.uniq)
|
registrar_ids = v.object_changes['registrar_id']
|
||||||
|
next if registrar_ids.nil? || registrar_ids.empty?
|
||||||
|
|
||||||
|
case v.event
|
||||||
|
when 'create'
|
||||||
|
processed_domains << v.object_changes['name'][1]
|
||||||
|
registrar_counts[registrar_ids[1].to_s] += 1
|
||||||
|
when 'update'
|
||||||
|
if processed_domains.include?(v.object['name'])
|
||||||
|
registrar_counts[registrar_ids[0].to_s] -= 1
|
||||||
|
registrar_counts[registrar_ids[1].to_s] += 1
|
||||||
|
else
|
||||||
|
registrar_counts[registrar_ids[1].to_s] += 1
|
||||||
|
processed_domains << v.object['name']
|
||||||
|
end
|
||||||
|
when 'destroy'
|
||||||
|
registrar_counts[registrar_ids[0].to_s] -= 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
current_domains_grouped = ::Domain.where('created_at <= ? AND created_at >= ?', date_to, date_from)
|
||||||
|
.where.not(name: processed_domains.uniq)
|
||||||
.group(:registrar_id).count.stringify_keys
|
.group(:registrar_id).count.stringify_keys
|
||||||
summarize([group(log_domains_del), group(log_domains_trans), domains_grouped])
|
|
||||||
|
summarize([registrar_counts, current_domains_grouped])
|
||||||
end
|
end
|
||||||
|
|
||||||
def summarize(arr)
|
def summarize(arr)
|
||||||
|
@ -94,9 +129,9 @@ module Repp
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_domains(event:, date_to:, date_from:)
|
def log_domains(event:, date_to:, date_from:)
|
||||||
domains = ::Version::DomainVersion.where(event: event)
|
domain_version = ::Version::DomainVersion.where(event: event)
|
||||||
domains.where!("object_changes ->> 'registrar_id' IS NOT NULL") if event == 'update'
|
domain_version.where!("object_changes ->> 'registrar_id' IS NOT NULL") if event == 'update'
|
||||||
domains.where('created_at > ?', date_to)
|
domain_version.where('created_at > ?', date_to)
|
||||||
.where("object ->> 'created_at' <= ?", date_to)
|
.where("object ->> 'created_at' <= ?", date_to)
|
||||||
.where("object ->> 'created_at' >= ?", date_from)
|
.where("object ->> 'created_at' >= ?", date_from)
|
||||||
.select("DISTINCT ON (object ->> 'name') object, created_at")
|
.select("DISTINCT ON (object ->> 'name') object, created_at")
|
||||||
|
|
4
test/fixtures/domains.yml
vendored
4
test/fixtures/domains.yml
vendored
|
@ -12,7 +12,7 @@ shop:
|
||||||
period: 1
|
period: 1
|
||||||
period_unit: m
|
period_unit: m
|
||||||
uuid: 1b3ee442-e8fe-4922-9492-8fcb9dccc69c
|
uuid: 1b3ee442-e8fe-4922-9492-8fcb9dccc69c
|
||||||
created_at: <%= 2.days.ago.to_s :db %>
|
created_at: <%= 2.months.ago.to_s :db %>
|
||||||
|
|
||||||
airport:
|
airport:
|
||||||
name: airport.test
|
name: airport.test
|
||||||
|
@ -51,7 +51,7 @@ metro:
|
||||||
period: 1
|
period: 1
|
||||||
period_unit: m
|
period_unit: m
|
||||||
uuid: ef97cb80-333b-4893-b9df-163f2b452798
|
uuid: ef97cb80-333b-4893-b9df-163f2b452798
|
||||||
created_at: <%= 2.days.ago.to_s :db %>
|
created_at: <%= 3.months.ago.to_s :db %>
|
||||||
|
|
||||||
hospital:
|
hospital:
|
||||||
name: hospital.test
|
name: hospital.test
|
||||||
|
|
46
test/fixtures/log_domains.yml
vendored
46
test/fixtures/log_domains.yml
vendored
|
@ -6,3 +6,49 @@ one:
|
||||||
registrant_id: <%= ActiveRecord::FixtureSet.identify(:john) %>
|
registrant_id: <%= ActiveRecord::FixtureSet.identify(:john) %>
|
||||||
updated_at: <%= Time.zone.parse('2010-07-05') %>
|
updated_at: <%= Time.zone.parse('2010-07-05') %>
|
||||||
created_at: <%= Time.zone.parse('2010-07-05') %>
|
created_at: <%= Time.zone.parse('2010-07-05') %>
|
||||||
|
|
||||||
|
transfer_one:
|
||||||
|
item_id: <%= ActiveRecord::FixtureSet.identify(:shop) %>
|
||||||
|
item_type: Domain
|
||||||
|
event: update
|
||||||
|
object:
|
||||||
|
name: 'shop.test'
|
||||||
|
object_changes:
|
||||||
|
registrar_id: [<%= ActiveRecord::FixtureSet.identify(:goodnames) %>, <%= ActiveRecord::FixtureSet.identify(:bestnames) %>]
|
||||||
|
created_at: <%= 2.days.ago.to_s :db %>
|
||||||
|
|
||||||
|
create_one:
|
||||||
|
item_id: 1111111
|
||||||
|
item_type: Domain
|
||||||
|
event: create
|
||||||
|
object_changes:
|
||||||
|
name: [null, 'deleted.test']
|
||||||
|
registrar_id: [null, <%= ActiveRecord::FixtureSet.identify(:goodnames) %>]
|
||||||
|
created_at: <%= 3.months.ago.to_s :db %>
|
||||||
|
|
||||||
|
destroy_one:
|
||||||
|
item_id: 1111111
|
||||||
|
item_type: Domain
|
||||||
|
event: destroy
|
||||||
|
object_changes:
|
||||||
|
name: ['deleted.test', null]
|
||||||
|
registrar_id: [<%= ActiveRecord::FixtureSet.identify(:goodnames) %>, null]
|
||||||
|
created_at: <%= 2.days.ago.to_s :db %>
|
||||||
|
|
||||||
|
create_two:
|
||||||
|
item_id: <%= ActiveRecord::FixtureSet.identify(:library) %>
|
||||||
|
item_type: Domain
|
||||||
|
event: create
|
||||||
|
object_changes:
|
||||||
|
name: [null, 'library.test']
|
||||||
|
registrar_id: [null, <%= ActiveRecord::FixtureSet.identify(:bestnames) %>]
|
||||||
|
created_at: <%= 2.days.ago.to_s :db %>
|
||||||
|
|
||||||
|
create_three:
|
||||||
|
item_id: <%= ActiveRecord::FixtureSet.identify(:airport) %>
|
||||||
|
item_type: Domain
|
||||||
|
event: create
|
||||||
|
object_changes:
|
||||||
|
name: [null, 'airport.test']
|
||||||
|
registrar_id: [null, <%= ActiveRecord::FixtureSet.identify(:bestnames) %>]
|
||||||
|
created_at: <%= 2.days.ago.to_s :db %>
|
|
@ -19,8 +19,8 @@ class ReppV1StatsMarketShareTest < ActionDispatch::IntegrationTest
|
||||||
assert_equal 1000, json[:code]
|
assert_equal 1000, json[:code]
|
||||||
assert_equal 'Command completed successfully', json[:message]
|
assert_equal 'Command completed successfully', json[:message]
|
||||||
|
|
||||||
assert json[:data].is_a? Array
|
assert_equal json[:data], [{ name: @user.registrar.name, y: 4, sliced: true, selected: true },
|
||||||
assert json[:data][0].is_a? Hash
|
{ name: 'Good Names', y: 2 }]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_shows_market_share_growth_rate_data
|
def test_shows_market_share_growth_rate_data
|
||||||
|
@ -34,10 +34,11 @@ class ReppV1StatsMarketShareTest < ActionDispatch::IntegrationTest
|
||||||
assert_equal 1000, json[:code]
|
assert_equal 1000, json[:code]
|
||||||
assert_equal 'Command completed successfully', json[:message]
|
assert_equal 'Command completed successfully', json[:message]
|
||||||
|
|
||||||
data = json[:data]
|
assert_equal json[:data], prev_data: { name: prev_date,
|
||||||
assert data[:data].is_a? Hash
|
domains: [['Best Names', 1], ['Good Names', 2]],
|
||||||
assert data[:prev_data].is_a? Hash
|
market_share: [['Best Names', 33.3], ['Good Names', 66.7]] },
|
||||||
assert_equal data[:data][:name], @today
|
data: { name: @today,
|
||||||
assert data[:data][:domains].is_a? Array
|
domains: [['Best Names', 4], ['Good Names', 2]],
|
||||||
|
market_share: [['Best Names', 66.7], ['Good Names', 33.3]] }
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Add table
Add a link
Reference in a new issue