Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimise statistic data calculations #2619

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
93 changes: 84 additions & 9 deletions app/controllers/repp/v1/stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ def market_share_distribution
render_success(data: result)
end

# rubocop:disable Metrics/MethodLength
api :get, '/repp/v1/stats/market_share_growth_rate'
desc 'Get market share and growth rate of registrars'
param :q, Hash, required: true, desc: 'Period parameters for data' do
param :end_date, String, required: true, desc: 'Period end date'
param :compare_to_end_date, String, required: true, desc: 'Comparison date'
end
# # rubocop:disable Metrics/MethodLength
# api :get, '/repp/v1/stats/market_share_growth_rate'
# desc 'Get market share and growth rate of registrars'
# param :q, Hash, required: true, desc: 'Period parameters for data' do
# param :end_date, String, required: true, desc: 'Period end date'
# 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)

puts '---------'
puts domains_by_rar
puts prev_domains_by_rar
puts '---------'

set_zero_values!(domains_by_rar, prev_domains_by_rar)

market_share_by_rar = calculate_market_share(domains_by_rar)
Expand All @@ -36,11 +41,81 @@ 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)
# render_success(data: result)
end

# rubocop:disable Metrics/MethodLength
# api :get, '/repp/v1/stats/market_share_growth_rate2'
# desc 'Get market share and growth rate of registrars'
# param :q, Hash, required: true, desc: 'Period parameters for data' do
# param :end_date, String, required: true, desc: 'Period end date'
# param :compare_to_end_date, String, required: true, desc: 'Comparison date'
# end
def market_share_growth_rate2
# domains_by_rar = domains_by_registrar(@date_to, @date_from)
# prev_domains_by_rar = domains_by_registrar(@date_compare_to, @date_compare_from)

query = ActiveRecord::Base.send(:sanitize_sql_array, ["
SELECT \"object_changes\" -> 'registrar_id' ->> 1 AS \"registrar_id\", COUNT(*) AS \"domain_count\"
FROM \"log_domains\"
WHERE (\"event\" = 'create' OR \"event\" = 'update' OR \"event\" = 'delete')
AND \"created_at\" BETWEEN ? AND ?
AND (
\"event\" != 'update' OR
(\"event\" = 'update' AND \"object_changes\" ->> 'registrar_id' IS NOT NULL)
)
GROUP BY \"object_changes\" -> 'registrar_id' ->> 1", @date_from, @date_to])


result = ActiveRecord::Base.connection.execute(query)

# Преобразование результата в хэш
domains_by_rar = {}
result.each do |row|
domains_by_rar[row['registrar_id']] = row['domain_count'].to_i
end

query = ActiveRecord::Base.send(:sanitize_sql_array, ["
SELECT \"object_changes\" -> 'registrar_id' ->> 1 AS \"registrar_id\", COUNT(*) AS \"domain_count\"
FROM \"log_domains\"
WHERE (\"event\" = 'create' OR \"event\" = 'update' OR \"event\" = 'delete')
AND \"created_at\" BETWEEN ? AND ?
AND (
\"event\" != 'update' OR
(\"event\" = 'update' AND \"object_changes\" ->> 'registrar_id' IS NOT NULL)
)
GROUP BY \"object_changes\" -> 'registrar_id' ->> 1", @date_compare_from, @date_compare_to])


result = ActiveRecord::Base.connection.execute(query)

# Преобразование результата в хэш
prev_domains_by_rar = {}
result.each do |row|
prev_domains_by_rar[row['registrar_id']] = row['domain_count'].to_i
end

puts '---------'
puts domains_by_rar
puts prev_domains_by_rar
puts '---------'

set_zero_values!(domains_by_rar, prev_domains_by_rar)

market_share_by_rar = calculate_market_share(domains_by_rar)
prev_market_share_by_rar = calculate_market_share(prev_domains_by_rar)

result = { prev_data: { name: search_params[:compare_to_end_date],
domains: serialize_growth_rate_result(prev_domains_by_rar),
market_share: serialize_growth_rate_result(prev_market_share_by_rar) },
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
# private

def search_params
params.permit(:q, q: %i[start_date end_date compare_to_end_date compare_to_start_date])
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
collection do
get '/market_share_distribution', to: 'stats#market_share_distribution'
get '/market_share_growth_rate', to: 'stats#market_share_growth_rate'
get '/market_share_growth_rate2', to: 'stats#market_share_growth_rate2'
end
end
resources :api_users, only: %i[index show update create destroy] do
Expand Down
33 changes: 33 additions & 0 deletions script/benchmarks/static_benchmark_ips.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative "../../config/environment"
require 'benchmark/ips'

Benchmark.ips do |x|
x.warmup = 2
x.time = 5

controller = Repp::V1::StatsController.new
controller.params = ActionController::Parameters.new(
q: {
end_date: '10.23',
compare_to_end_date: '10.22'
}
)
# date_to = Date.strptime("10.23", '%m.%y').end_of_month
# date_from = Date.strptime("01.22", '%m.%y').end_of_month

controller.set_date_params # Вызывает метод, который устанавливает переменные экземпляра

x.report("before") do
ActiveRecord::Base.uncached do
res = controller.market_share_growth_rate
end
end

x.report("after") do
ActiveRecord::Base.uncached do
res = controller.market_share_growth_rate2
end
end

x.compare!
end
23 changes: 23 additions & 0 deletions script/benchmarks/static_benchmark_time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require_relative "../../config/environment"
require 'benchmark/ips' # Подразумевается, что gem 'benchmark-ips' установлен

date_to = Date.strptime("10.23", '%m.%y').end_of_month
date_from = Date.strptime("01.22", '%m.%y').end_of_month
controller = Repp::V1::StatsController.new

time_before = Benchmark.realtime do
ActiveRecord::Base.uncached do
res = controller.log_domains(event: 'update', date_to: date_to, date_from: date_from)
end
end

time_after = Benchmark.realtime do
ActiveRecord::Base.uncached do
res = controller.log_domains(event: 'update', date_to: date_to, date_from: date_from)
end
end

puts "Time for 'before': #{time_before} seconds"
puts "Time for 'after': #{time_after} seconds"
Loading