Skip to content

Commit

Permalink
Merge pull request #511 from senid231/fix-custom-data-totals-1-9
Browse files Browse the repository at this point in the history
Admin UI fix custom data totals, auth logs add default filter 1.9
  • Loading branch information
dmitry-sinina authored Aug 1, 2019
2 parents 6aba829 + d1a41bf commit e66cf30
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 81 deletions.
5 changes: 5 additions & 0 deletions app/admin/cdr/auth_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
config.batch_actions = false
config.sort_order = 'request_time_desc'

with_default_params do
params[:q] = { request_time_gteq_datetime: 1.days.ago.to_date.strftime('%F') }
'Only records from yesterday are displayed by default'
end

acts_as_export :id,
:request_time,
[:gateway_name, proc { |row| row.gateway.try(:name) }],
Expand Down
8 changes: 3 additions & 5 deletions app/admin/logs/api_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
scope :all, default: true
scope :failed, show_count: false

before_action only: [:index] do
if params['q'].blank?
params['q'] = { created_at_gteq: 1.days.ago } # only 1 last days by default
flash.now[:notice] = 'Only records for last day are displayed by default'
end
with_default_params do
params[:q] = { created_at_gteq: 1.days.ago } # only 1 last days by default
'Only records for last day are displayed by default'
end

controller do
Expand Down
2 changes: 1 addition & 1 deletion app/admin/reports/custom_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def scoped_collection

column :calls_count, sortable: :agg_calls_count, footer: lambda {
strong do
text_node @footer_data[:agg_calls_count].to_s
text_node @footer_data.agg_calls_count.to_s
text_node ' calls'
end
}, &:agg_calls_count
Expand Down
8 changes: 1 addition & 7 deletions app/admin/reports/realtime/bad_routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@
filter :internal_disconnect_code
filter :internal_disconnect_reason

before_action only: [:index] do
params[:q] ||= {}
if params[:q][:time_interval_eq].blank?
params[:q][:time_interval_eq] = Report::Realtime::Base::DEFAULT_INTERVAL
flash.now[:notice_message] = "Records for time interval #{Report::Realtime::Base::DEFAULT_INTERVAL} seconds are displayed by default"
end
end
with_default_realtime_interval

controller do
def scoped_collection
Expand Down
8 changes: 1 addition & 7 deletions app/admin/reports/realtime/not_authenticated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@
collection: Report::Realtime::Base::INTERVALS,
input_html: { class: 'chosen' }, include_blank: false

before_action only: [:index] do
params[:q] ||= {}
if params[:q][:time_interval_eq].blank?
params[:q][:time_interval_eq] = Report::Realtime::Base::DEFAULT_INTERVAL
flash.now[:notice_message] = "Records for time interval #{Report::Realtime::Base::DEFAULT_INTERVAL} seconds are displayed by default"
end
end
with_default_realtime_interval

controller do
def scoped_collection
Expand Down
8 changes: 1 addition & 7 deletions app/admin/reports/realtime/origination_performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
collection: proc { Contractor.select(:id, :name).reorder(:name) },
input_html: { class: 'chosen' }

before_action only: [:index] do
params[:q] ||= {}
if params[:q][:time_interval_eq].blank?
params[:q][:time_interval_eq] = Report::Realtime::Base::DEFAULT_INTERVAL
flash.now[:notice_message] = "Records for time interval #{Report::Realtime::Base::DEFAULT_INTERVAL} seconds are displayed by default"
end
end
with_default_realtime_interval

controller do
def scoped_collection
Expand Down
8 changes: 1 addition & 7 deletions app/admin/reports/realtime/termination_distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
as: :select, collection: proc { Contractor.select(:id, :name).reorder(:name) },
input_html: { class: 'chosen' }

before_action only: [:index] do
params[:q] ||= {}
if params[:q][:time_interval_eq].blank?
params[:q][:time_interval_eq] = Report::Realtime::Base::DEFAULT_INTERVAL
flash.now[:notice_message] = "Records for time interval #{Report::Realtime::Base::DEFAULT_INTERVAL} seconds are displayed by default"
end
end
with_default_realtime_interval

controller do
def scoped_collection
Expand Down
5 changes: 4 additions & 1 deletion app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ def send_balance_notifications_to_emails
contacts_for_balance_notifications.map(&:email).join(',')
end

Totals = Struct.new(:total_balance)

def self.totals
except(:eager_load).select('sum(balance) as total_balance').take
row = extending(ActsAsTotalsRelation).totals_row_by('sum(balance) as total_balance')
Totals.new(*row)
end

def contacts_for_invoices
Expand Down
9 changes: 8 additions & 1 deletion app/models/billing/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,15 @@ def file_name
"#{id}_#{start_date}_#{end_date}"
end

Totals = Struct.new(:total_amount, :total_calls_count, :total_calls_duration)

def self.totals
except(:eager_load).select('sum(amount) as total_amount, sum(calls_count) as total_calls_count, sum(calls_duration) as total_calls_duration').take
row = extending(ActsAsTotalsRelation).totals_row_by(
'sum(amount) as total_amount',
'sum(calls_count) as total_calls_count',
'sum(calls_duration) as total_calls_duration'
)
Totals.new(*row)
end

def contacts_for_invoices
Expand Down
10 changes: 10 additions & 0 deletions app/models/concerns/acts_as_totals_relation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module ActsAsTotalsRelation
def totals_row_by(*select_sql)
safe_select_sql = select_sql.map { |sql| Arel.sql(sql) }
except(:preload, :includes, :eager_load, :limit, :offset, :select, :order)
.pluck(safe_select_sql)
.first
end
end
17 changes: 11 additions & 6 deletions app/models/report/custom_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,17 @@ def self.report_columns
column_names.select { |column| column.start_with?('agg_') }
end

Totals = Struct.new(:agg_calls_count, :agg_calls_duration, :agg_acd, :agg_customer_price, :agg_vendor_price, :agg_profit)

def self.totals
select("sum(agg_calls_count)::integer as agg_calls_count,
sum(agg_calls_duration) as agg_calls_duration,
coalesce(sum(agg_calls_duration)::float/nullif(sum(agg_calls_count),0),0) as agg_acd,
sum(agg_customer_price) as agg_customer_price,
sum(agg_vendor_price) as agg_vendor_price,
sum(agg_profit) as agg_profit").take
row = extending(ActsAsTotalsRelation).totals_row_by(
'sum(agg_calls_count)::integer as agg_calls_count',
'sum(agg_calls_duration) as agg_calls_duration',
'coalesce(sum(agg_calls_duration)::float/nullif(sum(agg_calls_count),0),0) as agg_acd',
'sum(agg_customer_price) as agg_customer_price',
'sum(agg_vendor_price) as agg_vendor_price',
'sum(agg_profit) as agg_profit'
)
Totals.new(*row)
end
end
28 changes: 18 additions & 10 deletions app/models/report/customer_traffic_data_by_destination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,25 @@ def display_name
id.to_s
end

Totals = Struct.new(
:calls_count, :success_calls_count, :short_calls_count, :calls_duration, :origination_cost,
:termination_cost, :profit, :first_call_at, :last_call_at, :agg_acd
)

def self.totals
select("sum(calls_count)::int as calls_count,
sum(success_calls_count)::int as success_calls_count,
sum(short_calls_count)::int as short_calls_count,
sum(calls_duration) as calls_duration,
sum(origination_cost) as origination_cost,
sum(termination_cost) as termination_cost,
sum(profit) as profit,
min(first_call_at) as first_call_at,
max(last_call_at) as last_call_at,
coalesce(sum(calls_duration)::float/nullif(sum(success_calls_count),0),0) as agg_acd").take
row = extending(ActsAsTotalsRelation).totals_row_by(
'sum(calls_count)::int as calls_count',
'sum(success_calls_count)::int as success_calls_count',
'sum(short_calls_count)::int as short_calls_count',
'sum(calls_duration) as calls_duration',
'sum(origination_cost) as origination_cost',
'sum(termination_cost) as termination_cost',
'sum(profit) as profit',
'min(first_call_at) as first_call_at',
'max(last_call_at) as last_call_at',
'coalesce(sum(calls_duration)::float/nullif(sum(success_calls_count),0),0) as agg_acd'
)
Totals.new(*row)
end

def self.report_records
Expand Down
28 changes: 18 additions & 10 deletions app/models/report/customer_traffic_data_by_vendor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,24 @@ def display_name
id.to_s
end

Totals = Struct.new(
:calls_count, :success_calls_count, :short_calls_count, :calls_duration, :origination_cost,
:termination_cost, :profit, :first_call_at, :last_call_at, :agg_acd
)

def self.totals
select("sum(calls_count)::int as calls_count,
sum(success_calls_count)::int as success_calls_count,
sum(short_calls_count)::int as short_calls_count,
sum(calls_duration) as calls_duration,
sum(origination_cost) as origination_cost,
sum(termination_cost) as termination_cost,
sum(profit) as profit,
min(first_call_at) as first_call_at,
max(last_call_at) as last_call_at,
coalesce(sum(calls_duration)::float/nullif(sum(success_calls_count),0),0) as agg_acd").take
row = extending(ActsAsTotalsRelation).totals_row_by(
'sum(calls_count)::int as calls_count',
'sum(success_calls_count)::int as success_calls_count',
'sum(short_calls_count)::int as short_calls_count',
'sum(calls_duration) as calls_duration',
'sum(origination_cost) as origination_cost',
'sum(termination_cost) as termination_cost',
'sum(profit) as profit',
'min(first_call_at) as first_call_at',
'max(last_call_at) as last_call_at',
'coalesce(sum(calls_duration)::float/nullif(sum(success_calls_count),0),0) as agg_acd'
)
Totals.new(*row)
end
end
28 changes: 18 additions & 10 deletions app/models/report/customer_traffic_data_full.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,24 @@ def display_name
id.to_s
end

Totals = Struct.new(
:calls_count, :success_calls_count, :short_calls_count, :calls_duration, :origination_cost,
:termination_cost, :profit, :first_call_at, :last_call_at, :agg_acd
)

def self.totals
select("sum(calls_count)::int as calls_count,
sum(success_calls_count)::int as success_calls_count,
sum(short_calls_count)::int as short_calls_count,
sum(calls_duration) as calls_duration,
sum(origination_cost) as origination_cost,
sum(termination_cost) as termination_cost,
sum(profit) as profit,
min(first_call_at) as first_call_at,
max(last_call_at) as last_call_at,
coalesce(sum(calls_duration)::float/nullif(sum(success_calls_count),0),0) as agg_acd").take
row = extending(ActsAsTotalsRelation).totals_row_by(
'sum(calls_count)::int as calls_count',
'sum(success_calls_count)::int as success_calls_count',
'sum(short_calls_count)::int as short_calls_count',
'sum(calls_duration) as calls_duration',
'sum(origination_cost) as origination_cost',
'sum(termination_cost) as termination_cost',
'sum(profit) as profit',
'min(first_call_at) as first_call_at',
'max(last_call_at) as last_call_at',
'coalesce(sum(calls_duration)::float/nullif(sum(success_calls_count),0),0) as agg_acd'
)
Totals.new(*row)
end
end
26 changes: 17 additions & 9 deletions app/models/report/vendor_traffic_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ def display_name
id.to_s
end

Totals = Struct.new(
:calls_count, :success_calls_count, :short_calls_count, :calls_duration, :origination_cost,
:termination_cost, :profit, :first_call_at, :last_call_at
)

def self.totals
select("sum(calls_count)::int as calls_count,
sum(success_calls_count)::int as success_calls_count,
sum(short_calls_count)::int as short_calls_count,
sum(calls_duration) as calls_duration,
sum(origination_cost) as origination_cost,
sum(termination_cost) as termination_cost,
sum(profit) as profit,
min(first_call_at) as first_call_at,
max(last_call_at) as last_call_at").to_a.first
row = extending(ActsAsTotalsRelation).totals_row_by(
'sum(calls_count)::int as calls_count',
'sum(success_calls_count)::int as success_calls_count',
'sum(short_calls_count)::int as short_calls_count',
'sum(calls_duration) as calls_duration',
'sum(origination_cost) as origination_cost',
'sum(termination_cost) as termination_cost',
'sum(profit) as profit',
'min(first_call_at) as first_call_at',
'max(last_call_at) as last_call_at'
)
Totals.new(*row)
end
end
1 change: 1 addition & 0 deletions config/initializers/yeti.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
ActiveAdmin::ResourceDSL.send :include, ResourceDSL::ActsAsDelayedJobLock
ActiveAdmin::ResourceDSL.send :include, ResourceDSL::ActsAsFilterByRoutingTagIds
ActiveAdmin::ResourceDSL.send :include, ResourceDSL::ActsAsBelongsTo
ActiveAdmin::ResourceDSL.send :include, ResourceDSL::WithDefaultParams

# ActiveAdmin::CSVBuilder.send(:include, Yeti::CSVBuilder)

Expand Down
28 changes: 28 additions & 0 deletions lib/resource_dsl/with_default_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module ResourceDSL
module WithDefaultParams
# @param opts[:if] [Proc] - passes params into proc, block will be executed if proc returns true
# @param opts[:flash_type] [Proc] - passes params into proc, block will be executed if proc returns true
# yield in context of controller
# @yieldreturn message that will be shown in flash.now
def with_default_params(opts = {}, &block)
if_proc = opts[:if] || proc { |q: nil, **_| q.blank? }
flash_type = opts.fetch(:flash_type, :notice_message)
before_action only: [:index] do
if instance_exec(params.to_unsafe_h, &if_proc)
message = instance_exec(&block)
flash.now[flash_type] = message if message.present?
end
end
end

def with_default_realtime_interval
with_default_params if: proc { |q: nil, **_| q.blank? || q[:time_interval_eq].blank? } do
params[:q] ||= {}
params[:q][:time_interval_eq] = Report::Realtime::Base::DEFAULT_INTERVAL
"Records for time interval #{Report::Realtime::Base::DEFAULT_INTERVAL} seconds are displayed by default"
end
end
end
end

0 comments on commit e66cf30

Please sign in to comment.