From ce98e6c4ae3868435455eb9d55a43a5e8eb37640 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Fri, 6 Oct 2023 12:19:26 +0300 Subject: [PATCH] optimise statistic data calculations --- Gemfile | 1 + Gemfile.lock | 4 +- app/controllers/repp/v1/stats_controller.rb | 50 ++++++++++++++++++--- script/benchmarks/static_benchmark.rb | 24 ++++++++++ 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 script/benchmarks/static_benchmark.rb diff --git a/Gemfile b/Gemfile index 0f2e855ab3..4349c82238 100644 --- a/Gemfile +++ b/Gemfile @@ -106,3 +106,4 @@ gem 'pg_query', '>= 0.9.0' # token gem 'jwt' +gem 'benchmark-ips' diff --git a/Gemfile.lock b/Gemfile.lock index 4fe330f693..80e678f153 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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 @@ -604,4 +606,4 @@ DEPENDENCIES wkhtmltopdf-binary (~> 0.12.6.1) BUNDLED WITH - 2.4.19 + 2.4.20 diff --git a/app/controllers/repp/v1/stats_controller.rb b/app/controllers/repp/v1/stats_controller.rb index 052cdcbc45..22c7a59933 100644 --- a/app/controllers/repp/v1/stats_controller.rb +++ b/app/controllers/repp/v1/stats_controller.rb @@ -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]) @@ -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) @@ -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 } diff --git a/script/benchmarks/static_benchmark.rb b/script/benchmarks/static_benchmark.rb new file mode 100644 index 0000000000..08484d9125 --- /dev/null +++ b/script/benchmarks/static_benchmark.rb @@ -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