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 68d245f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 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
13 changes: 12 additions & 1 deletion 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 @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions script/benchmarks/static_benchmark.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 68d245f

Please sign in to comment.