Created market share chart data endpoint

This commit is contained in:
Sergei Tsõganov 2022-08-25 10:57:36 +03:00
parent 0745efc766
commit 0c960fea67
2 changed files with 43 additions and 0 deletions

View file

@ -0,0 +1,38 @@
module Repp
module V1
class StatsController < BaseController
api :get, '/repp/v1/stats/market_share'
desc 'Get market share and distribution of registrars'
def market_share
registrars = ::Registrar.where(test_registrar: false).joins(:domains)
registrars = registrars.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
private
def search_params
params.permit(:q, q: %i[start_date end_date]).fetch(:q, {}) || {}
end
def from_condition
return unless search_params[:start_date]
"domains.created_at >= '#{Date.strptime(search_params[:start_date], '%m.%y')}'"
end
def to_condition
return unless search_params[:end_date]
"domains.created_at <= '#{Date.strptime(search_params[:end_date], '%m.%y').end_of_month}'"
end
end
end
end

View file

@ -99,6 +99,11 @@ Rails.application.routes.draw do
end
resources :auctions, only: %i[index]
resources :retained_domains, only: %i[index]
resources :stats do
collection do
get '/market_share', to: 'stats#market_share'
end
end
namespace :registrar do
resources :notifications, only: [:index, :show, :update] do
collection do