Skip to content

Commit

Permalink
Added market share growth rate data endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
maricavor committed Sep 5, 2022
1 parent 478c358 commit ba8848d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 11 deletions.
60 changes: 54 additions & 6 deletions app/controllers/repp/v1/stats_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module Repp
module V1
class StatsController < BaseController
api :get, '/repp/v1/stats/market_share'
api :get, '/repp/v1/stats/market_share_distribution'
desc 'Get market share and distribution of registrars'
def market_share
param :q, Hash, required: true, desc: 'Period parameters for data' do
param :end_date, String, required: true, desc: 'Period end date'
end
def market_share_distribution
registrars = ::Registrar.where(test_registrar: false).joins(:domains)
registrars = registrars.where(from_condition).where(to_condition)
.where(from_condition).where(to_condition)
grouped = registrars.group(:name).count

result = grouped.map do |key, value|
Expand All @@ -16,22 +19,67 @@ def market_share
render_success(data: result)
end

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_date, String, required: true, desc: 'Comparison date'
end
def market_share_growth_rate
registrars = ::Registrar.where(test_registrar: false).joins(:domains)
.where(from_condition)

domains_by_registrar = registrars.where(to_condition).group(:name).count
prev_domains_by_registrar = registrars.where(compare_to_condition).group(:name).count

set_zero_values!(domains_by_registrar, prev_domains_by_registrar)

result = { prev_data: { name: search_params[:compare_to_date],
domains: serialize(prev_domains_by_registrar) },
data: { name: search_params[:end_date],
domains: serialize(domains_by_registrar) } }
render_success(data: result)
end

private

def search_params
params.permit(:q, q: %i[start_date end_date]).fetch(:q, {}) || {}
params.permit(:q, q: %i[start_date end_date compare_to_date])
.fetch(:q, {}) || {}
end

def from_condition
return unless search_params[:start_date]

"domains.created_at >= '#{Date.strptime(search_params[:start_date], '%m.%y')}'"
"domains.created_at >= '#{to_date(search_params[:start_date])}'"
end

def to_condition
return unless search_params[:end_date]

"domains.created_at <= '#{Date.strptime(search_params[:end_date], '%m.%y').end_of_month}'"
"domains.created_at <= '#{to_date(search_params[:end_date]).end_of_month}'"
end

def compare_to_condition
return unless search_params[:compare_to_date]

"domains.created_at <= '#{to_date(search_params[:compare_to_date]).end_of_month}'"
end

def to_date(date_param)
Date.strptime(date_param, '%m.%y')
end

def serialize(rars)
rars.map { |key, value| [key, value] }
end

def set_zero_values!(cur, prev)
cur_dup = cur.dup
cur_dup.each_key do |k|
cur_dup[k] = prev[k] || 0
end
prev.clear.merge!(cur_dup)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ en:
date_long: "%d. %B %Y"
filename: "%Y-%m-%d_%H.%M"
date:
month_names:
[~, January, February, March, April, May, June, July, August, September, October, November, December]
formats:
default: "%Y-%m-%d"
month_year: "%B, %Y"

activerecord:
errors:
Expand Down
7 changes: 5 additions & 2 deletions config/locales/et.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ et:
password: 'Parool'

date:
# Don't forget the nil at the beginning; there's no such thing as a 0th month
month_names: [~, Jaanuar, Veebruar, Märts, Aprill, Mai, Juuni, Juuli, August, September, Oktoober, November, Detsember]
month_names:
[~, Jaanuar, Veebruar, Märts, April, Mai, Juuni, Juuli, August, September, Oktoober, November, Detsember]
formats:
default: "%Y-%m-%d"
month_year: "%B, %Y"
emails: "Meillaadressid"
invoice:
title: 'Arve'
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
resources :retained_domains, only: %i[index]
resources :stats do
collection do
get '/market_share', to: 'stats#market_share'
get '/market_share_distribution', to: 'stats#market_share_distribution'
get '/market_share_growth_rate', to: 'stats#market_share_growth_rate'
end
end
namespace :registrar do
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/domains.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ shop:
period: 1
period_unit: m
uuid: 1b3ee442-e8fe-4922-9492-8fcb9dccc69c
created_at: <%= 2.days.ago.to_s :db %>

airport:
name: airport.test
Expand All @@ -24,6 +25,7 @@ airport:
period: 1
period_unit: m
uuid: 2df2c1a1-8f6a-490a-81be-8bdf29866880
created_at: <%= 2.days.ago.to_s :db %>

library:
name: library.test
Expand All @@ -36,6 +38,7 @@ library:
period: 1
period_unit: m
uuid: 647bcc48-8d5e-4a04-8ce5-2a3cd17b6eab
created_at: <%= 2.days.ago.to_s :db %>

metro:
name: metro.test
Expand All @@ -48,6 +51,7 @@ metro:
period: 1
period_unit: m
uuid: ef97cb80-333b-4893-b9df-163f2b452798
created_at: <%= 2.days.ago.to_s :db %>

hospital:
name: hospital.test
Expand Down
24 changes: 22 additions & 2 deletions test/integration/repp/v1/stats/market_share_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ def setup
token = "Basic #{token}"

@auth_headers = { 'Authorization' => token }
@today = Time.zone.today.strftime('%m.%y')
end

def test_shows_market_share_data
get '/repp/v1/stats/market_share', headers: @auth_headers
def test_shows_market_share_distribution_data
get '/repp/v1/stats/market_share_distribution', headers: @auth_headers,
params: { q: { end_date: @today } }
json = JSON.parse(response.body, symbolize_names: true)

assert_response :ok
Expand All @@ -22,4 +24,22 @@ def test_shows_market_share_data
assert_equal json[:data][0][:name], 'Best Names'
assert json[:data][0][:selected]
end

def test_shows_market_share_growth_rate_data
prev_date = Time.zone.today.last_month.strftime('%m.%y')
get '/repp/v1/stats/market_share_growth_rate', headers: @auth_headers,
params: { q: { end_date: @today,
compare_to_date: prev_date } }
json = JSON.parse(response.body, symbolize_names: true)

assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Command completed successfully', json[:message]

data = json[:data]
assert data[:data].is_a? Hash
assert data[:prev_data].is_a? Hash
assert_equal data[:data][:name], @today
assert data[:data][:domains].is_a? Array
end
end

0 comments on commit ba8848d

Please sign in to comment.