Skip to content

Commit

Permalink
Merge pull request #2629 from internetee/fix-statistics-1
Browse files Browse the repository at this point in the history
Fixed statistics
  • Loading branch information
vohmar authored Dec 21, 2023
2 parents bd12a73 + 7736e1c commit 397a98c
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ plugins:
enabled: false
rubocop:
enabled: true
channel: rubocop-1-18-3
channel: rubocop-1-45
checks:
Rubocop/Style/ClassAndModuleChildren:
enabled: false
Expand Down
145 changes: 110 additions & 35 deletions app/controllers/repp/v1/stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class StatsController < BaseController # rubocop:disable Metrics/ClassLength
param :end_date, String, required: true, desc: 'Period end date'
end
def market_share_distribution
domains_by_rar = domains_by_registrar(@date_to, @date_from)
domains_by_rar = domains_by_registrar(@date_to)
result = serialize_distribution_result(domains_by_rar)
render_success(data: result)
end
Expand All @@ -22,8 +22,8 @@ def market_share_distribution
param :compare_to_end_date, String, required: true, desc: 'Comparison date'
end
def market_share_growth_rate
domains_by_rar = domains_by_registrar(@date_to, @date_from)
prev_domains_by_rar = domains_by_registrar(@date_compare_to, @date_compare_from)
domains_by_rar = domains_by_registrar(@date_to)
prev_domains_by_rar = domains_by_registrar(@date_compare_to)

set_zero_values!(domains_by_rar, prev_domains_by_rar)

Expand All @@ -36,22 +36,21 @@ def market_share_growth_rate
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

private

def search_params
params.permit(:q, q: %i[start_date end_date compare_to_end_date compare_to_start_date])
params.permit(:q, q: %i[end_date compare_to_end_date])
.fetch(:q, {}) || {}
end

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_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')
end

def to_date(date_param)
Expand All @@ -78,35 +77,108 @@ def calculate_market_share(domains_by_rar)
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 summarize(arr)
arr.inject { |memo, el| memo.merge(el) { |_, old_v, new_v| old_v + new_v } }
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

def group(domains)
domains.group_by { |ld| ld.object['registrar_id'].to_s }
.transform_values(&:count)
# rubocop:disable Metrics/MethodLength
def domains_by_registrar(date_to)
sql = <<-SQL
SELECT
registrar_id,
SUM(domain_count) AS total_domain_count
FROM (
-- Query for current domains for each registrar
SELECT
registrar_id::text AS registrar_id,
COUNT(*) AS domain_count
FROM
domains
GROUP BY
registrar_id
UNION ALL
-- Query for 'create' events and count domains created by registrar
SELECT
(object_changes->'registrar_id'->>1)::text AS registrar_id,
COUNT(*) * -1 AS domain_count
FROM
log_domains
WHERE
event = 'create'
AND created_at > :date_to
GROUP BY
registrar_id
UNION ALL
-- Query for 'update' events and count domains transferred to a new registrar
SELECT
(object_changes->'registrar_id'->>1)::text AS registrar_id,
COUNT(*) * -1 AS domain_count
FROM
log_domains
WHERE
event = 'update'
AND object_changes->'registrar_id' IS NOT NULL
AND created_at > :date_to
GROUP BY
registrar_id
UNION ALL
-- Query for 'update' events and count domains transferred from an old registrar
SELECT
(object_changes->'registrar_id'->>0)::text AS registrar_id,
COUNT(*) AS domain_count
FROM
log_domains
WHERE
event = 'update'
AND object_changes->'registrar_id' IS NOT NULL
AND created_at > :date_to
GROUP BY
registrar_id
UNION ALL
-- Query for 'destroy' events and count the number of domains destroyed associated with each registrar
SELECT
(object_changes->'registrar_id'->>0)::text AS registrar_id,
COUNT(*) AS domain_count
FROM
log_domains
WHERE
event = 'destroy'
AND object_changes->'registrar_id' IS NOT NULL
AND created_at > :date_to
GROUP BY
registrar_id
UNION ALL
-- Query for 'destroy' events and count the number of domains destroyed associated with each registrar
SELECT
(object->'registrar_id')::text AS registrar_id,
COUNT(*) AS domain_count
FROM
log_domains
WHERE
event = 'destroy'
AND object_changes IS NULL
AND created_at > :date_to
GROUP BY
registrar_id
) AS combined
GROUP BY
registrar_id;
SQL

ActiveRecord::Base.connection.execute(
ActiveRecord::Base.send(:sanitize_sql_array, [sql, { date_to: date_to }])
).each_with_object({}) do |row, hash|
hash[row['registrar_id']] = row['total_domain_count'].to_i
end
end
# rubocop:enable Metrics/MethodLength

def registrar_names
@registrar_names ||= ::Registrar.where(test_registrar: false)
Expand All @@ -120,7 +192,10 @@ def serialize_distribution_result(result)

name = registrar_names[key]
hash = { name: name, y: value }
hash.merge!({ sliced: true, selected: true }) if current_user.registrar.name == name
if current_user.registrar.name == name
hash[:sliced] = true
hash[:selected] = true
end
hash
end.compact
end
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/domains.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/files/domain_versions.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Name,Registrant,Registrar,Action,Created at
,John,,destroy,2023-12-04 22:00:00
hospital.test,John,Good Names,create,2023-12-04 22:00:00
library.test,Acme Ltd,Best Names,create,2023-12-04 22:00:00
shop.test,John,Best Names,update,2023-12-04 22:00:00
invalid.test,any,Best Names,create,2023-12-04 22:00:00
airport.test,John,Best Names,create,2023-12-04 22:00:00
shop.test,John,Good Names,create,2023-10-04 21:00:00
cinema.test,John,Good Names,create,2023-09-04 21:00:00
metro.test,Jack,Good Names,create,2023-09-04 21:00:00
,test_code,Best Names,update,2018-04-23 15:50:48
,John,,update,2010-07-04 21:00:00
94 changes: 93 additions & 1 deletion test/fixtures/log_domains.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,96 @@ 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') %>

create_one:
item_id: <%= ActiveRecord::FixtureSet.identify(:shop) %>
item_type: Domain
event: create
object_changes:
name: [null, 'shop.test']
registrar_id: [null, <%= ActiveRecord::FixtureSet.identify(:goodnames) %>]
registrant_id: [null, <%= ActiveRecord::FixtureSet.identify(:john) %>]
created_at: <%= Time.zone.parse('2023-10-05') %>

create_two:
item_id: <%= ActiveRecord::FixtureSet.identify(:airport) %>
item_type: Domain
event: create
object_changes:
name: [null, 'airport.test']
registrar_id: [null, <%= ActiveRecord::FixtureSet.identify(:bestnames) %>]
registrant_id: [null, <%= ActiveRecord::FixtureSet.identify(:john) %>]
created_at: <%= Time.zone.parse('2023-12-05') %>

create_three:
item_id: <%= ActiveRecord::FixtureSet.identify(:hospital) %>
item_type: Domain
event: create
object_changes:
name: [null, 'hospital.test']
registrar_id: [null, <%= ActiveRecord::FixtureSet.identify(:goodnames) %>]
registrant_id: [null, <%= ActiveRecord::FixtureSet.identify(:john) %>]
created_at: <%= Time.zone.parse('2023-12-05') %>

create_four:
item_id: <%= ActiveRecord::FixtureSet.identify(:invalid) %>
item_type: Domain
event: create
object_changes:
name: [null, 'invalid.test']
registrar_id: [null, <%= ActiveRecord::FixtureSet.identify(:bestnames) %>]
registrant_id: [null, <%= ActiveRecord::FixtureSet.identify(:invalid) %>]
created_at: <%= Time.zone.parse('2023-12-05') %>

transfer_one:
item_id: <%= ActiveRecord::FixtureSet.identify(:shop) %>
item_type: Domain
event: update
object:
name: 'shop.test'
registrant_id: <%= ActiveRecord::FixtureSet.identify(:john) %>
object_changes:
registrar_id: [<%= ActiveRecord::FixtureSet.identify(:goodnames) %>, <%= ActiveRecord::FixtureSet.identify(:bestnames) %>]
created_at: <%= Time.zone.parse('2023-12-05') %>

create_five:
item_id: 1111111
item_type: Domain
event: create
object_changes:
name: [null, 'cinema.test']
registrar_id: [null, <%= ActiveRecord::FixtureSet.identify(:goodnames) %>]
registrant_id: [null, <%= ActiveRecord::FixtureSet.identify(:john) %>]
created_at: <%= Time.zone.parse('2023-09-05') %>

destroy_one:
item_id: 1111111
item_type: Domain
event: destroy
object:
registrant_id: <%= ActiveRecord::FixtureSet.identify(:john) %>
object_changes:
name: ['cinema.test', null]
registrar_id: [<%= ActiveRecord::FixtureSet.identify(:goodnames) %>, null]
created_at: <%= Time.zone.parse('2023-12-05') %>

create_six:
item_id: <%= ActiveRecord::FixtureSet.identify(:library) %>
item_type: Domain
event: create
object_changes:
name: [null, 'library.test']
registrar_id: [null, <%= ActiveRecord::FixtureSet.identify(:bestnames) %>]
registrant_id: [null, <%= ActiveRecord::FixtureSet.identify(:acme_ltd) %>]
created_at: <%= Time.zone.parse('2023-12-05') %>

create_seven:
item_id: <%= ActiveRecord::FixtureSet.identify(:metro) %>
item_type: Domain
event: create
object_changes:
name: [null, 'metro.test']
registrar_id: [null, <%= ActiveRecord::FixtureSet.identify(:goodnames) %>]
registrant_id: [null, <%= ActiveRecord::FixtureSet.identify(:jack) %>]
created_at: <%= Time.zone.parse('2023-09-05') %>
17 changes: 9 additions & 8 deletions test/integration/repp/v1/stats/market_share_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def test_shows_market_share_distribution_data
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: 'Good Names', y: 2 },
{ name: @user.registrar.name, y: 4, sliced: true, selected: true }]
end

def test_shows_market_share_growth_rate_data
Expand All @@ -34,10 +34,11 @@ def test_shows_market_share_growth_rate_data
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: [['Good Names', 3], ['Best Names', 0]],
market_share: [['Good Names', 100.0], ['Best Names', 0.0]] },
data: { name: @today,
domains: [['Good Names', 2], ['Best Names', 4]],
market_share: [['Good Names', 33.3], ['Best Names', 66.7]] }
end
end
end
11 changes: 6 additions & 5 deletions test/learning/paper_trail_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ def test_returns_version_list

def test_returns_version_count_on_domains
@domain = domains(:airport)
@domain.save

assert_equal 1, @domain.versions.count
assert_difference -> { @domain.versions.count }, 1 do
@domain.save
end

@domain.name = 'domain.test'
@domain.save!
assert_equal 2, @domain.versions.count
assert_difference -> { @domain.versions.count }, 1 do
@domain.save!
end
end

def test_returns_version_count_on_users
Expand Down

0 comments on commit 397a98c

Please sign in to comment.