From 68d245fa88c2737179dffc9958da384307d11322 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 | 13 ++++++++++- script/benchmarks/static_benchmark.rb | 26 +++++++++++++++++++++ 4 files changed, 42 insertions(+), 2 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..cd7f576d4c 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]) @@ -103,6 +103,17 @@ def log_domains(event:, date_to:, date_from:) .order(Arel.sql("object ->> 'name', created_at desc")) end + def log_domains2(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) diff --git a/script/benchmarks/static_benchmark.rb b/script/benchmarks/static_benchmark.rb new file mode 100644 index 0000000000..91b6bb3a20 --- /dev/null +++ b/script/benchmarks/static_benchmark.rb @@ -0,0 +1,26 @@ +# 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("10.23", '%m.%y').end_of_month + date_from = Date.strptime("01.22", '%m.%y').end_of_month + + res = Repp::V1::StatsController.new.log_domains(event: 'update', date_to: date_to, date_from: date_from) + puts res.size + end + x.report("after") do + date_to = Date.strptime("10.23", '%m.%y').end_of_month + date_from = Date.strptime("01.22", '%m.%y').end_of_month + + res = Repp::V1::StatsController.new.log_domains2(event: 'update', date_to: date_to, date_from: date_from) + puts res.size + end + + x.compare! +end