-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2426 from internetee/create-stats-endpoints
Created market share chart data endpoint
- Loading branch information
Showing
6 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
module Repp | ||
module V1 | ||
class StatsController < BaseController | ||
api :get, '/repp/v1/stats/market_share_distribution' | ||
desc 'Get market share and distribution of registrars' | ||
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) | ||
.where(from_condition).where(to_condition) | ||
grouped = registrars.group(:name).count | ||
|
||
result = grouped.map do |key, value| | ||
hash = { name: key.strip, y: value } | ||
hash.merge!({ sliced: true, selected: true }) if current_user.registrar.name == key | ||
hash | ||
end | ||
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_date, String, required: true, desc: 'Comparison date' | ||
end | ||
def market_share_growth_rate | ||
registrars = ::Registrar.where(test_registrar: false).joins(:domains) | ||
|
||
domains_by_rar = registrars.where(from_condition).where(to_condition).group(:name).count | ||
prev_domains_by_rar = registrars.where(compare_to_condition).group(:name).count | ||
|
||
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_date], | ||
domains: serialize(prev_domains_by_rar), | ||
market_share: serialize(prev_market_share_by_rar) }, | ||
data: { name: search_params[:end_date], | ||
domains: serialize(domains_by_rar), | ||
market_share: serialize(market_share_by_rar) } } | ||
render_success(data: result) | ||
end | ||
# rubocop:enable Metrics/MethodLength | ||
|
||
private | ||
|
||
def search_params | ||
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 >= '#{to_date(search_params[:start_date])}'" | ||
end | ||
|
||
def to_condition | ||
return unless search_params[:end_date] | ||
|
||
"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 | ||
|
||
def calculate_market_share(domains_by_rar) | ||
sum = domains_by_rar.values.sum | ||
domains_by_rar.transform_values do |v| | ||
value = v.to_f / sum * 100.0 | ||
value < 0.1 ? value.round(3) : value.round(1) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'test_helper' | ||
|
||
class ReppV1StatsMarketShareTest < ActionDispatch::IntegrationTest | ||
def setup | ||
@user = users(:api_bestnames) | ||
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}") | ||
token = "Basic #{token}" | ||
|
||
@auth_headers = { 'Authorization' => token } | ||
@today = Time.zone.today.strftime('%m.%y') | ||
end | ||
|
||
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 | ||
assert_equal 1000, json[:code] | ||
assert_equal 'Command completed successfully', json[:message] | ||
|
||
assert json[:data].is_a? Array | ||
assert json[:data][0].is_a? Hash | ||
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 |