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],
|
||||
domains: serialize_growth_rate_result(domains_by_rar),
|
||||
market_share: serialize_growth_rate_result(market_share_by_rar) } }
|
||||
|
||||
render_success(data: result)
|
||||
end
|
||||
# rubocop:enable Metrics/MethodLength
|
||||
|
@ -49,9 +50,9 @@ module Repp
|
|||
|
||||
def set_date_params
|
||||
@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_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
|
||||
|
||||
def to_date(date_param)
|
||||
|
@ -78,15 +79,49 @@ module Repp
|
|||
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)
|
||||
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])
|
||||
domain_versions = ::Version::DomainVersion.where('object_changes IS NOT NULL')
|
||||
.where('created_at >= ? AND created_at <= ?', date_from, date_to)
|
||||
registrar_counts = Hash.new(0)
|
||||
processed_domains = []
|
||||
domain_versions.find_each(batch_size: 1000) do |v|
|
||||
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
|
||||
|
||||
summarize([registrar_counts, current_domains_grouped])
|
||||
end
|
||||
|
||||
def summarize(arr)
|
||||
|
@ -94,14 +129,14 @@ module Repp
|
|||
end
|
||||
|
||||
def log_domains(event:, date_to:, date_from:)
|
||||
domains = ::Version::DomainVersion.where(event: event)
|
||||
domains.where!("object_changes ->> 'registrar_id' IS NOT NULL") if event == 'update'
|
||||
domains.where('created_at > ?', date_to)
|
||||
.where("object ->> 'created_at' <= ?", date_to)
|
||||
.where("object ->> 'created_at' >= ?", date_from)
|
||||
.select("DISTINCT ON (object ->> 'name') object, created_at")
|
||||
.order(Arel.sql("object ->> 'name', created_at desc"))
|
||||
end
|
||||
domain_version = ::Version::DomainVersion.where(event: event)
|
||||
domain_version.where!("object_changes ->> 'registrar_id' IS NOT NULL") if event == 'update'
|
||||
domain_version.where('created_at > ?', date_to)
|
||||
.where("object ->> 'created_at' <= ?", date_to)
|
||||
.where("object ->> 'created_at' >= ?", date_from)
|
||||
.select("DISTINCT ON (object ->> 'name') object, created_at")
|
||||
.order(Arel.sql("object ->> 'name', created_at desc"))
|
||||
end
|
||||
|
||||
def group(domains)
|
||||
domains.group_by { |ld| ld.object['registrar_id'].to_s }
|
||||
|
|
4
test/fixtures/domains.yml
vendored
4
test/fixtures/domains.yml
vendored
|
@ -12,7 +12,7 @@ shop:
|
|||
period: 1
|
||||
period_unit: m
|
||||
uuid: 1b3ee442-e8fe-4922-9492-8fcb9dccc69c
|
||||
created_at: <%= 2.days.ago.to_s :db %>
|
||||
created_at: <%= 2.months.ago.to_s :db %>
|
||||
|
||||
airport:
|
||||
name: airport.test
|
||||
|
@ -51,7 +51,7 @@ metro:
|
|||
period: 1
|
||||
period_unit: m
|
||||
uuid: ef97cb80-333b-4893-b9df-163f2b452798
|
||||
created_at: <%= 2.days.ago.to_s :db %>
|
||||
created_at: <%= 3.months.ago.to_s :db %>
|
||||
|
||||
hospital:
|
||||
name: hospital.test
|
||||
|
|
48
test/fixtures/log_domains.yml
vendored
48
test/fixtures/log_domains.yml
vendored
|
@ -5,4 +5,50 @@ one:
|
|||
object:
|
||||
registrant_id: <%= ActiveRecord::FixtureSet.identify(:john) %>
|
||||
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 'Command completed successfully', json[:message]
|
||||
|
||||
assert json[:data].is_a? Array
|
||||
assert json[:data][0].is_a? Hash
|
||||
assert_equal json[:data], [{ name: @user.registrar.name, y: 4, sliced: true, selected: true },
|
||||
{ name: 'Good Names', y: 2 }]
|
||||
end
|
||||
|
||||
def test_shows_market_share_growth_rate_data
|
||||
|
@ -34,10 +34,11 @@ class ReppV1StatsMarketShareTest < ActionDispatch::IntegrationTest
|
|||
assert_equal 1000, json[:code]
|
||||
assert_equal 'Command completed successfully', json[:message]
|
||||
|
||||
data = json[:data]
|
||||
assert data[:data].is_a? Hash
|
||||
assert data[:prev_data].is_a? Hash
|
||||
assert_equal data[:data][:name], @today
|
||||
assert data[:data][:domains].is_a? Array
|
||||
assert_equal json[:data], prev_data: { name: prev_date,
|
||||
domains: [['Best Names', 1], ['Good Names', 2]],
|
||||
market_share: [['Best Names', 33.3], ['Good Names', 66.7]] },
|
||||
data: { name: @today,
|
||||
domains: [['Best Names', 4], ['Good Names', 2]],
|
||||
market_share: [['Best Names', 66.7], ['Good Names', 33.3]] }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue