mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 05:26:17 +02:00
Added market share growth rate data endpoint
This commit is contained in:
parent
261e2d3162
commit
dc4c3cf616
6 changed files with 90 additions and 11 deletions
|
@ -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|
|
||||
|
@ -16,22 +19,67 @@ module Repp
|
|||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue