Skip to content

Commit

Permalink
[Dashboard] optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
macejmic committed May 4, 2020
1 parent f6b5bb2 commit e573945
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile.base
Expand Up @@ -166,6 +166,7 @@ gem 'slim-rails', '~> 3.2'
gem 'ruby-openid'

gem 'draper', '~> 3.0'
gem 'activerecord-precounter'

group :development do
gem 'listen'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -153,6 +153,8 @@ GEM
activerecord (~> 5.0.0)
arel (~> 7.1.4)
ruby-plsql (>= 0.5.0)
activerecord-precounter (0.3.3)
activerecord (>= 5)
activesupport (5.0.7.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
Expand Down Expand Up @@ -807,6 +809,7 @@ DEPENDENCIES
activemerchant (~> 1.77.0)
activemodel-serializers-xml
activerecord-oracle_enhanced-adapter (~> 1.7.0)
activerecord-precounter
acts-as-taggable-on (~> 4.0)
acts_as_list (~> 0.9.17)
acts_as_tree
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.prod.lock
Expand Up @@ -154,6 +154,8 @@ GEM
activerecord (~> 5.0.0)
arel (~> 7.1.4)
ruby-plsql (>= 0.5.0)
activerecord-precounter (0.3.3)
activerecord (>= 5)
activesupport (5.0.7.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
Expand Down Expand Up @@ -808,6 +810,7 @@ DEPENDENCIES
activemerchant (~> 1.77.0)
activemodel-serializers-xml
activerecord-oracle_enhanced-adapter (~> 1.7.0)
activerecord-precounter
acts-as-taggable-on (~> 4.0)
acts_as_list (~> 0.9.17)
acts_as_tree
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/provider/admin/dashboards_controller.rb
Expand Up @@ -13,7 +13,9 @@ def show
# .scoped(:include => [ :message => [ :sender ]])
#
# but 'Cannot eagerly load the polymorphic association :sender'
@services = current_user.accessible_services
@services = current_user.accessible_services.includes(:proxy, :cinstances, :account)
ActiveRecord::Precounter.new(@services).precount(:unread_alerts, :api_docs_services)

@messages_presenter = current_presenter
@unread_messages_presenter = unread_messages_presenter
end
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/menu_helper.rb
Expand Up @@ -85,6 +85,8 @@ def forcibly_denied_switch?(switch)
end

def api_selector_services
return @api_selector_services if defined? @api_selector_services

@api_selector_services ||= (site_account.provider? && logged_in? ? current_user.accessible_services : Service.none).decorate
@api_selector_services.concat(current_user.account.backend_apis.decorate) if current_account.provider_can_use?(:api_as_product)
@api_selector_services
Expand Down
3 changes: 2 additions & 1 deletion app/helpers/provider/admin/dashboards_helper.rb
Expand Up @@ -40,7 +40,8 @@ def dashboard_secondary_link(link_text, path, options = {})
end

def dashboard_collection_link(singular_name, collection, path, options = {})
link_text = pluralize(number_to_human(collection.size), singular_name, options.fetch(:plural, nil))
collection_size = collection.is_a?(Integer) ? collection : collection.size
link_text = pluralize(number_to_human(collection_size), singular_name, options.fetch(:plural, nil))
dashboard_navigation_link(link_text, path, options)
end

Expand Down
4 changes: 3 additions & 1 deletion app/lib/logic/provider_settings.rb
Expand Up @@ -29,8 +29,10 @@ def service_items_in_menu?
end

def multiservice?
return @multiservice_flag if defined? @multiservice_flag

account = buyer? ? provider_account : self
account.multiple_accessible_services?
@multiservice_flag = account.multiple_accessible_services?
end

def multiple_accessible_services?(scope = nil)
Expand Down
1 change: 1 addition & 0 deletions app/models/alert.rb
Expand Up @@ -12,6 +12,7 @@ class Alert < ApplicationRecord

belongs_to :cinstance
belongs_to :account
belongs_to :service

has_one :user_account, through: :cinstance

Expand Down
2 changes: 2 additions & 0 deletions app/models/service.rb
Expand Up @@ -110,6 +110,8 @@ def cinstance
has_many :top_level_metrics, -> { includes(:children).top_level }, class_name: 'Metric'

has_many :service_tokens, inverse_of: :service, dependent: :destroy
has_many :alerts, inverse_of: :service
has_many :unread_alerts, -> { unread }, class_name: 'Alert', inverse_of: :service

scope :accessible, -> { where.not(state: DELETE_STATE) }
scope :deleted, -> { where(state: DELETE_STATE) }
Expand Down
Expand Up @@ -23,11 +23,10 @@ nav.DashboardNavigation
admin_service_applications_path(service),
icon_name: 'cubes'

- unread_alerts = current_account.buyer_alerts.by_service(service).unread
- if can?(:manage, :monitoring) && unread_alerts.any?
- if can?(:manage, :monitoring) && service.unread_alerts_count > 0
li.DashboardNavigation-list-item
= dashboard_collection_link 'Alert',
unread_alerts,
service.unread_alerts_count,
admin_service_alerts_path(service),
icon_name: 'exclamation-triangle',
notice: true
Expand All @@ -37,15 +36,15 @@ nav.DashboardNavigation
li.DashboardNavigation-list-item
// Subscriptions (would be nice to have API specific links)
=> dashboard_collection_link 'Subscription',
service.service_contracts,
service.service_contracts_count,
admin_buyers_service_contracts_path(search: {service_id: service.id}),
icon_name: 'paperclip'

// Active docs
- if can?(:manage, :plans)
li.DashboardNavigation-list-item
= dashboard_collection_link 'ActiveDoc',
service.api_docs_services,
service.api_docs_services_count,
admin_service_api_docs_path(service),
icon_name: 'file-text'
// Integration
Expand Down

0 comments on commit e573945

Please sign in to comment.