From 0c960fea677d796df03c3535ab5887ba3d315fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergei=20Ts=C3=B5ganov?= Date: Thu, 25 Aug 2022 10:57:36 +0300 Subject: [PATCH] Created market share chart data endpoint --- app/controllers/repp/v1/stats_controller.rb | 38 +++++++++++++++++++++ config/routes.rb | 5 +++ 2 files changed, 43 insertions(+) create mode 100644 app/controllers/repp/v1/stats_controller.rb diff --git a/app/controllers/repp/v1/stats_controller.rb b/app/controllers/repp/v1/stats_controller.rb new file mode 100644 index 000000000..f53c24a16 --- /dev/null +++ b/app/controllers/repp/v1/stats_controller.rb @@ -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 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 180a4687d..22a4dc9c6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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