From d1a41bf8f41899b8f6514da443be49d920bb0760 Mon Sep 17 00:00:00 2001 From: Denis Talakevich Date: Wed, 31 Jul 2019 21:30:58 +0300 Subject: [PATCH] Admin UI auth logs add default filter --- app/admin/cdr/auth_logs.rb | 5 ++++ app/admin/logs/api_log.rb | 8 ++---- app/admin/reports/realtime/bad_routing.rb | 8 +----- .../reports/realtime/not_authenticated.rb | 8 +----- .../realtime/origination_performance.rb | 8 +----- .../realtime/termination_distribution.rb | 8 +----- config/initializers/yeti.rb | 1 + lib/resource_dsl/with_default_params.rb | 28 +++++++++++++++++++ 8 files changed, 41 insertions(+), 33 deletions(-) create mode 100644 lib/resource_dsl/with_default_params.rb diff --git a/app/admin/cdr/auth_logs.rb b/app/admin/cdr/auth_logs.rb index 1c4f3b580..233a6314f 100644 --- a/app/admin/cdr/auth_logs.rb +++ b/app/admin/cdr/auth_logs.rb @@ -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) }], diff --git a/app/admin/logs/api_log.rb b/app/admin/logs/api_log.rb index 066ee88d5..06189d2ae 100644 --- a/app/admin/logs/api_log.rb +++ b/app/admin/logs/api_log.rb @@ -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 diff --git a/app/admin/reports/realtime/bad_routing.rb b/app/admin/reports/realtime/bad_routing.rb index bff133280..1df6ee38f 100644 --- a/app/admin/reports/realtime/bad_routing.rb +++ b/app/admin/reports/realtime/bad_routing.rb @@ -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 diff --git a/app/admin/reports/realtime/not_authenticated.rb b/app/admin/reports/realtime/not_authenticated.rb index f291c53df..cf27422d8 100644 --- a/app/admin/reports/realtime/not_authenticated.rb +++ b/app/admin/reports/realtime/not_authenticated.rb @@ -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 diff --git a/app/admin/reports/realtime/origination_performance.rb b/app/admin/reports/realtime/origination_performance.rb index c7b839afa..8f594d36e 100644 --- a/app/admin/reports/realtime/origination_performance.rb +++ b/app/admin/reports/realtime/origination_performance.rb @@ -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 diff --git a/app/admin/reports/realtime/termination_distribution.rb b/app/admin/reports/realtime/termination_distribution.rb index 8c3733b15..aa31731f5 100644 --- a/app/admin/reports/realtime/termination_distribution.rb +++ b/app/admin/reports/realtime/termination_distribution.rb @@ -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 diff --git a/config/initializers/yeti.rb b/config/initializers/yeti.rb index 720941f30..4ccd6f544 100644 --- a/config/initializers/yeti.rb +++ b/config/initializers/yeti.rb @@ -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) diff --git a/lib/resource_dsl/with_default_params.rb b/lib/resource_dsl/with_default_params.rb new file mode 100644 index 000000000..4a92ec98a --- /dev/null +++ b/lib/resource_dsl/with_default_params.rb @@ -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