Skip to content

Commit

Permalink
optimise statistic data calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Oct 6, 2023
1 parent c7c8ae9 commit ce98e6c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ gem 'pg_query', '>= 0.9.0'

# token
gem 'jwt'
gem 'benchmark-ips'
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ GEM
aws-sigv4 (1.2.4)
aws-eventstream (~> 1, >= 1.0.2)
bcrypt (3.1.16)
benchmark-ips (2.12.0)
bindata (2.4.14)
bootsnap (1.9.3)
msgpack (~> 1.0)
Expand Down Expand Up @@ -539,6 +540,7 @@ DEPENDENCIES
airbrake
apipie-rails (~> 0.6.0)
aws-sdk-sesv2 (~> 1.19)
benchmark-ips
bootsnap (>= 1.1.0)
bootstrap-sass (~> 3.4)
cancancan
Expand Down Expand Up @@ -604,4 +606,4 @@ DEPENDENCIES
wkhtmltopdf-binary (~> 0.12.6.1)

BUNDLED WITH
2.4.19
2.4.20
50 changes: 45 additions & 5 deletions app/controllers/repp/v1/stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def market_share_growth_rate
end
# rubocop:enable Metrics/MethodLength

private
# private

def search_params
params.permit(:q, q: %i[start_date end_date compare_to_end_date compare_to_start_date])
Expand Down Expand Up @@ -81,17 +81,52 @@ def calculate_market_share(domains_by_rar)
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'] }
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])
s = summarize([group(log_domains_del), group(log_domains_trans), domains_grouped])
puts 'registrar 1 --------'
puts s
puts '--------------------'
end

def domains_by_registrar2(date_to, date_from)
# log_domains_del = log_domains2(event: 'destroy', date_to: date_to, date_from: date_from)
# log_domains_trans = log_domains2(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'] }

logged_domains = log_domains3(date_to: date_to, date_from: 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)

domains_grouped = ::Domain.where('created_at <= ? AND created_at >= ?', date_to, date_from)
.where.not(name: logged_domains.uniq)
.group(:registrar_id).size.stringify_keys

# s = summarize([group2(logged_domains), domains_grouped])
s = summarize([group(log_domains_del), group(log_domains_trans), domains_grouped])
puts 'registrar 2 --------'
puts s
puts '--------------------'
end

def summarize(arr)
arr.inject { |memo, el| memo.merge(el) { |_, old_v, new_v| old_v + new_v } }
end

def log_domains3(date_to:, date_from:)
query = ::Version::DomainVersion.where(event: ['destroy', 'update'])

query = query.where('created_at > ?', date_to)
.where("object ->> 'created_at' <= ?", date_to)
.where("object ->> 'created_at' >= ?", date_from)
.where("(event = 'update' AND object_changes ->> 'registrar_id' IS NOT NULL) OR event = 'destroy'")
.select("DISTINCT ON (object ->> 'name') object, created_at")
.order(Arel.sql("object ->> 'name', created_at desc"))

query
end

def log_domains(event:, date_to:, date_from:)
domains = ::Version::DomainVersion.where(event: event)
Expand All @@ -101,13 +136,18 @@ def log_domains(event:, date_to:, date_from:)
.where("object ->> 'created_at' >= ?", date_from)
.select("DISTINCT ON (object ->> 'name') object, created_at")
.order(Arel.sql("object ->> 'name', created_at desc"))
end
end

def group(domains)
domains.group_by { |ld| ld.object['registrar_id'].to_s }
.transform_values(&:count)
end

def group2(domains)
domains.group_by { |ld| ld.object['registrar_id'].to_s }
.transform_values(&:size)
end

def registrar_names
@registrar_names ||= ::Registrar.where(test_registrar: false)
.map { |r| { "#{r.id}": r.name }.with_indifferent_access }
Expand Down
24 changes: 24 additions & 0 deletions script/benchmarks/static_benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require_relative "../../config/environment"

# Any benchmarking setup goes here...



Benchmark.ips do |x|
x.report("before") do
date_to = Date.strptime("09.23", '%m.%y')
date_from = Date.strptime("01.22", '%m.%y')

Repp::V1::StatsController.new.domains_by_registrar(date_to, date_from)
end
x.report("after") do
date_to = Date.strptime("09.23", '%m.%y')
date_from = Date.strptime("01.22", '%m.%y')

Repp::V1::StatsController.new.domains_by_registrar2(date_to, date_from)
end

x.compare!
end

0 comments on commit ce98e6c

Please sign in to comment.