Removed date_from attribute

This commit is contained in:
Sergei Tsoganov 2023-12-19 09:38:00 +02:00
parent 0ca07a50fb
commit 7db8b5d970
5 changed files with 78 additions and 200 deletions

View file

@ -9,7 +9,7 @@ module Repp
param :end_date, String, required: true, desc: 'Period end date' param :end_date, String, required: true, desc: 'Period end date'
end end
def market_share_distribution 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) result = serialize_distribution_result(domains_by_rar)
render_success(data: result) render_success(data: result)
end end
@ -22,8 +22,8 @@ module Repp
param :compare_to_end_date, String, required: true, desc: 'Comparison date' param :compare_to_end_date, String, required: true, desc: 'Comparison date'
end end
def market_share_growth_rate def market_share_growth_rate
domains_by_rar = domains_by_registrar(@date_to, @date_from) domains_by_rar = domains_by_registrar(@date_to)
prev_domains_by_rar = domains_by_registrar(@date_compare_to, @date_compare_from) prev_domains_by_rar = domains_by_registrar(@date_compare_to)
set_zero_values!(domains_by_rar, prev_domains_by_rar) set_zero_values!(domains_by_rar, prev_domains_by_rar)
@ -50,9 +50,7 @@ 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.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.00')
end end
def to_date(date_param) def to_date(date_param)
@ -79,25 +77,25 @@ module Repp
end end
end end
def domains_by_registrar(date_to, date_from) # rubocop:disable Metrics/MethodLength
def domains_by_registrar(date_to)
sql = <<-SQL sql = <<-SQL
SELECT SELECT
registrar_id, registrar_id,
SUM(domain_count) AS total_domain_count SUM(domain_count) AS total_domain_count
FROM ( FROM (
-- Count domains from the domains table, excluding those in created_domains -- Query for current domains for each registrar
SELECT SELECT
registrar_id::text AS registrar_id, registrar_id::text AS registrar_id,
COUNT(*) AS domain_count COUNT(*) AS domain_count
FROM FROM
domains domains
WHERE
created_at >= :date_from
GROUP BY GROUP BY
registrar_id registrar_id
UNION ALL UNION ALL
-- Query for 'create' events and count domains created by registrar
SELECT SELECT
(object_changes->'registrar_id'->>1)::text AS registrar_id, (object_changes->'registrar_id'->>1)::text AS registrar_id,
COUNT(*) * -1 AS domain_count COUNT(*) * -1 AS domain_count
@ -111,7 +109,7 @@ module Repp
UNION ALL UNION ALL
-- Query for 'update' events and count domains transferred to a new registrar only for domains in created_domains -- Query for 'update' events and count domains transferred to a new registrar
SELECT SELECT
(object_changes->'registrar_id'->>1)::text AS registrar_id, (object_changes->'registrar_id'->>1)::text AS registrar_id,
COUNT(*) * -1 AS domain_count COUNT(*) * -1 AS domain_count
@ -126,7 +124,7 @@ module Repp
UNION ALL UNION ALL
-- Query for 'update' events and count domains transferred from an old registrar only for domains in created_domains -- Query for 'update' events and count domains transferred from an old registrar
SELECT SELECT
(object_changes->'registrar_id'->>0)::text AS registrar_id, (object_changes->'registrar_id'->>0)::text AS registrar_id,
COUNT(*) AS domain_count COUNT(*) AS domain_count
@ -141,7 +139,7 @@ module Repp
UNION ALL UNION ALL
-- Query for 'destroy' events and count the number of domains destroyed associated with each registrar only for domains in created_domains -- Query for 'destroy' events and count the number of domains destroyed associated with each registrar
SELECT SELECT
(object_changes->'registrar_id'->>0)::text AS registrar_id, (object_changes->'registrar_id'->>0)::text AS registrar_id,
COUNT(*) AS domain_count COUNT(*) AS domain_count
@ -158,181 +156,13 @@ module Repp
registrar_id; registrar_id;
SQL SQL
results = ActiveRecord::Base.connection.execute( ActiveRecord::Base.connection.execute(
ActiveRecord::Base.send(:sanitize_sql_array, [sql, date_from: date_from, date_to: date_to]) ActiveRecord::Base.send(:sanitize_sql_array, [sql, date_to: date_to])
).each_with_object({}) do |row, hash| ).each_with_object({}) do |row, hash|
hash[row['registrar_id']] = row['total_domain_count'].to_i hash[row['registrar_id']] = row['total_domain_count'].to_i
end end
results
end
# def domains_by_registrar(date_to, date_from)
# p 'Count the number of domains created by each registrar'
# sql = <<-SQL
# WITH created_domains AS (
# SELECT
# (object_changes->'registrar_id'->>1)::text AS registrar_id,
# (object_changes->'name'->>1)::text AS domain_name
# FROM
# log_domains
# WHERE
# event = 'create'
# AND created_at BETWEEN :date_from AND :date_to
# )
# SELECT
# registrar_id,
# SUM(domain_count) AS total_domain_count
# FROM (
# -- Count domains from created_domains
# SELECT
# registrar_id,
# COUNT(*) AS domain_count
# FROM
# created_domains
# GROUP BY
# registrar_id
# UNION ALL
# -- Query for 'update' events and count domains transferred to a new registrar only for domains in created_domains
# SELECT
# (object_changes->'registrar_id'->>1)::text AS registrar_id,
# COUNT(*) AS domain_count
# FROM
# log_domains
# WHERE
# event = 'update'
# AND object_changes->'registrar_id' IS NOT NULL
# AND (object->'name')::text IN (SELECT domain_name FROM created_domains)
# AND created_at BETWEEN :date_from AND :date_to
# GROUP BY
# registrar_id
# UNION ALL
# -- Query for 'update' events and count domains transferred from an old registrar only for domains in created_domains
# SELECT
# (object_changes->'registrar_id'->>0)::text AS registrar_id,
# COUNT(*) * -1 AS domain_count
# FROM
# log_domains
# WHERE
# event = 'update'
# AND object_changes->'registrar_id' IS NOT NULL
# AND (object->'name')::text IN (SELECT domain_name FROM created_domains)
# AND created_at BETWEEN :date_from AND :date_to
# GROUP BY
# registrar_id
# UNION ALL
# -- Query for 'destroy' events and count the number of domains destroyed associated with each registrar only for domains in created_domains
# SELECT
# (object_changes->'registrar_id'->>0)::text AS registrar_id,
# COUNT(*) * -1 AS domain_count
# FROM
# log_domains
# WHERE
# event = 'destroy'
# AND (object_changes->'name'->>0)::text IN (SELECT domain_name FROM created_domains)
# AND created_at BETWEEN :date_from AND :date_to
# GROUP BY
# registrar_id
# UNION ALL
# -- Count domains from the domains table, excluding those in created_domains
# SELECT
# registrar_id::text AS registrar_id,
# COUNT(*) AS domain_count
# FROM
# domains
# WHERE
# name NOT IN (SELECT domain_name FROM created_domains)
# AND created_at BETWEEN :date_from AND :date_to
# GROUP BY
# registrar_id
# ) AS combined
# GROUP BY
# registrar_id;
# SQL
# results = ActiveRecord::Base.connection.execute(
# ActiveRecord::Base.send(:sanitize_sql_array, [sql, date_from: date_from, date_to: date_to])
# ).each_with_object({}) do |row, hash|
# hash[row['registrar_id']] = row['total_domain_count'].to_i
# end
# p results
# 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)
# domain_versions = ::Version::DomainVersion.where('object_changes IS NOT NULL')
# .where('created_at >= ? AND created_at <= ?', date_from, date_to)
# .select(:event, :object, :object_changes)
# registrar_counts = Hash.new(0)
# processed_domains = []
# Rails.logger.info "Processing total #{domain_versions.size} log_domain records"
# domain_versions.each 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)
arr.inject { |memo, el| memo.merge(el) { |_, old_v, new_v| old_v + new_v } }
end
def log_domains(event:, date_to:, date_from:)
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 }
.transform_values(&:count)
end end
# rubocop:enable Metrics/MethodLength
def registrar_names def registrar_names
@registrar_names ||= ::Registrar.where(test_registrar: false) @registrar_names ||= ::Registrar.where(test_registrar: false)
@ -346,7 +176,10 @@ module Repp
name = registrar_names[key] name = registrar_names[key]
hash = { name: name, y: value } 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 hash
end.compact end.compact
end end

View file

@ -1,8 +1,12 @@
Name,Registrant,Registrar,Action,Created at Name,Registrant,Registrar,Action,Created at
,John,,destroy,2023-12-04 22:00:00 ,John,,destroy,2023-12-04 22:00:00
shop.test,John,Best Names,update,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 library.test,Acme Ltd,Best Names,create,2023-12-04 22:00:00
metro.test,Jack,Good Names,create,2023-09-04 21: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 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 ,test_code,Best Names,update,2018-04-23 15:50:48
,John,,update,2010-07-04 21:00:00 ,John,,update,2010-07-04 21:00:00

1 Name Registrant Registrar Action Created at
2 John destroy 2023-12-04 22:00:00
3 shop.test hospital.test John Best Names Good Names update create 2023-12-04 22:00:00
4 library.test Acme Ltd Best Names create 2023-12-04 22:00:00
5 metro.test shop.test Jack John Good Names Best Names create update 2023-09-04 21:00:00 2023-12-04 22:00:00
6 invalid.test any Best Names create 2023-12-04 22:00:00
7 airport.test John Best Names create 2023-12-04 22:00:00
8 shop.test John Good Names create 2023-10-04 21:00:00
9 cinema.test John Good Names create 2023-09-04 21:00:00
10 metro.test Jack Good Names create 2023-09-04 21:00:00
11 test_code Best Names update 2018-04-23 15:50:48
12 John update 2010-07-04 21:00:00

View file

@ -7,6 +7,46 @@ one:
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') %>
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: transfer_one:
item_id: <%= ActiveRecord::FixtureSet.identify(:shop) %> item_id: <%= ActiveRecord::FixtureSet.identify(:shop) %>
item_type: Domain item_type: Domain
@ -18,7 +58,7 @@ transfer_one:
registrar_id: [<%= ActiveRecord::FixtureSet.identify(:goodnames) %>, <%= ActiveRecord::FixtureSet.identify(:bestnames) %>] registrar_id: [<%= ActiveRecord::FixtureSet.identify(:goodnames) %>, <%= ActiveRecord::FixtureSet.identify(:bestnames) %>]
created_at: <%= Time.zone.parse('2023-12-05') %> created_at: <%= Time.zone.parse('2023-12-05') %>
create_one: create_five:
item_id: 1111111 item_id: 1111111
item_type: Domain item_type: Domain
event: create event: create
@ -39,7 +79,7 @@ destroy_one:
registrar_id: [<%= ActiveRecord::FixtureSet.identify(:goodnames) %>, null] registrar_id: [<%= ActiveRecord::FixtureSet.identify(:goodnames) %>, null]
created_at: <%= Time.zone.parse('2023-12-05') %> created_at: <%= Time.zone.parse('2023-12-05') %>
create_two: create_six:
item_id: <%= ActiveRecord::FixtureSet.identify(:library) %> item_id: <%= ActiveRecord::FixtureSet.identify(:library) %>
item_type: Domain item_type: Domain
event: create event: create
@ -49,7 +89,7 @@ create_two:
registrant_id: [null, <%= ActiveRecord::FixtureSet.identify(:acme_ltd) %>] registrant_id: [null, <%= ActiveRecord::FixtureSet.identify(:acme_ltd) %>]
created_at: <%= Time.zone.parse('2023-12-05') %> created_at: <%= Time.zone.parse('2023-12-05') %>
create_three: create_seven:
item_id: <%= ActiveRecord::FixtureSet.identify(:metro) %> item_id: <%= ActiveRecord::FixtureSet.identify(:metro) %>
item_type: Domain item_type: Domain
event: create event: create

View file

@ -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_equal json[:data], [{ name: @user.registrar.name, y: 4, sliced: true, selected: true }, assert_equal json[:data], [{ name: 'Good Names', y: 2 },
{ name: 'Good Names', y: 2 }] { name: @user.registrar.name, y: 4, sliced: true, selected: true }]
end end
def test_shows_market_share_growth_rate_data def test_shows_market_share_growth_rate_data
@ -35,10 +35,10 @@ class ReppV1StatsMarketShareTest < ActionDispatch::IntegrationTest
assert_equal 'Command completed successfully', json[:message] assert_equal 'Command completed successfully', json[:message]
assert_equal json[:data], prev_data: { name: prev_date, assert_equal json[:data], prev_data: { name: prev_date,
domains: [['Best Names', 1], ['Good Names', 2]], domains: [['Good Names', 3], ['Best Names', 0]],
market_share: [['Best Names', 33.3], ['Good Names', 66.7]] }, market_share: [['Good Names', 100.0], ['Best Names', 0.0]] },
data: { name: @today, data: { name: @today,
domains: [['Best Names', 4], ['Good Names', 2]], domains: [['Good Names', 2], ['Best Names', 4]],
market_share: [['Best Names', 66.7], ['Good Names', 33.3]] } market_share: [['Good Names', 33.3], ['Best Names', 66.7]] }
end end
end end

View file

@ -24,13 +24,14 @@ class PaperTrailLearningTest < ActiveSupport::TestCase
def test_returns_version_count_on_domains def test_returns_version_count_on_domains
@domain = domains(:airport) @domain = domains(:airport)
assert_difference -> { @domain.versions.count }, 1 do
@domain.save @domain.save
end
assert_equal 1, @domain.versions.count
@domain.name = 'domain.test' @domain.name = 'domain.test'
assert_difference -> { @domain.versions.count }, 1 do
@domain.save! @domain.save!
assert_equal 2, @domain.versions.count end
end end
def test_returns_version_count_on_users def test_returns_version_count_on_users