From 69e5a479445de0b42dd9bfc3255cdd84a4f665df Mon Sep 17 00:00:00 2001 From: macejmic Date: Mon, 29 Jun 2020 18:19:07 +0200 Subject: [PATCH] [Dashboard] print ajax widget content once loaded --- .../service/navigations_controller.rb | 5 ++ .../provider/admin/dashboards_helper.rb | 5 +- app/presenters/dashboard_widget_presenter.rb | 12 ++-- .../admin/dashboards/_service.html.slim | 7 +- .../widgets/_new_accounts.html.slim | 70 ++++++++++--------- .../widgets/_service_hits.html.slim | 50 ++++++------- .../_service_integration_errors.html.slim | 9 ++- .../_service_navigations.html.slim} | 1 + config/routes.rb | 1 + features/old/menu/dashboard.feature | 1 + features/old/stats/provider_side.feature | 1 + features/step_definitions/dashboard_steps.rb | 4 ++ 12 files changed, 93 insertions(+), 73 deletions(-) create mode 100644 app/controllers/provider/admin/dashboard/service/navigations_controller.rb rename app/views/provider/admin/dashboards/{_service_navigation.html.slim => widgets/_service_navigations.html.slim} (99%) diff --git a/app/controllers/provider/admin/dashboard/service/navigations_controller.rb b/app/controllers/provider/admin/dashboard/service/navigations_controller.rb new file mode 100644 index 0000000000..9fbf562a27 --- /dev/null +++ b/app/controllers/provider/admin/dashboard/service/navigations_controller.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class Provider::Admin::Dashboard::Service::NavigationsController < Provider::Admin::Dashboard::Service::BaseController + respond_to :json +end diff --git a/app/helpers/provider/admin/dashboards_helper.rb b/app/helpers/provider/admin/dashboards_helper.rb index e75951eb3d..8e22545e1c 100644 --- a/app/helpers/provider/admin/dashboards_helper.rb +++ b/app/helpers/provider/admin/dashboards_helper.rb @@ -6,8 +6,9 @@ module Provider::Admin::DashboardsHelper # @param name [Symbol] # @param params [Hash] - def dashboard_widget(name, params = {}) - widget = DashboardWidgetPresenter.new(name, params) + # @param variables [Hash] + def dashboard_widget(name, params = {}, variables = {}) + widget = DashboardWidgetPresenter.new(name, params, variables) widget.render end diff --git a/app/presenters/dashboard_widget_presenter.rb b/app/presenters/dashboard_widget_presenter.rb index b37d5fb61e..36b707a16e 100644 --- a/app/presenters/dashboard_widget_presenter.rb +++ b/app/presenters/dashboard_widget_presenter.rb @@ -1,14 +1,16 @@ class DashboardWidgetPresenter include ::Draper::ViewHelpers - attr_reader :name, :params, :value, :previous_value, :percentual_change, :has_history, :current + attr_reader :name, :params, :variables, :value, :previous_value, :percentual_change, :has_history, :current # TODO: clearly this should be just one attribute, and controllers should inject own structure attr_reader :chart, :items - def initialize(name, params = {}) + def initialize(name, params = {}, variables = {}) @name = name - @params = params.respond_to?(:permit!) ? params.dup.permit!.freeze : params.freeze + @params = params.respond_to?(:permit!) ? params.dup.permit! : params + @params = @params.symbolize_keys.with_indifferent_access + @variables = variables @data = nil @value = spinner @previous_value = nil @@ -29,7 +31,7 @@ def render_chart end def url(params = self.params) - h.polymorphic_url([:provider, :admin, :dashboard, name], params) + h.polymorphic_url([:provider, :admin, :dashboard, name], params.symbolize_keys) end def path @@ -78,6 +80,6 @@ def spinner end def locals - { widget: self } + { widget: self }.merge(variables) end end diff --git a/app/views/provider/admin/dashboards/_service.html.slim b/app/views/provider/admin/dashboards/_service.html.slim index 30ae11d1e7..662983e65e 100644 --- a/app/views/provider/admin/dashboards/_service.html.slim +++ b/app/views/provider/admin/dashboards/_service.html.slim @@ -8,9 +8,10 @@ section.DashboardSection.DashboardSection--service class=('DashboardSection--tog - else i.fa.fa-puzzle-piece> = friendly_service_name(service) - = render 'service_navigation', service: service + + = dashboard_widget :service_navigations, { service_id: service.to_param }, service: service // service level widgets - if can?(:manage, :analytics) - = dashboard_widget :service_hits, service_id: service.to_param - = dashboard_widget :service_top_traffic, service_id: service.to_param + = dashboard_widget :service_hits, { service_id: service.to_param }, service: service + = dashboard_widget :service_top_traffic, { service_id: service.to_param }, service: service diff --git a/app/views/provider/admin/dashboards/widgets/_new_accounts.html.slim b/app/views/provider/admin/dashboards/widgets/_new_accounts.html.slim index 7b1a3bc7a1..66de09dcb1 100644 --- a/app/views/provider/admin/dashboards/widgets/_new_accounts.html.slim +++ b/app/views/provider/admin/dashboards/widgets/_new_accounts.html.slim @@ -1,41 +1,43 @@ article.DashboardWidget id=widget.id - = widget.render_chart + - if widget.loaded? + = widget.render_chart - header.DashboardWidget-badge + header.DashboardWidget-badge - - if widget.value == 0 && widget.current == 0 && !widget.has_history + - if widget.value == 0 && widget.current == 0 && !widget.has_history - h1.DashboardWidget-title Signups + h1.DashboardWidget-title Signups - p - ' Make it easy for developers to sign up using your - = link_to 'Developer Portal', provider_admin_cms_root_path - | . - - if current_account.site_access_code.present? p - ' When you're ready to open to the world, remember to - = link_to 'remove the Access Code', admin_site_dns_path + ' Make it easy for developers to sign up using your + = link_to 'Developer Portal', provider_admin_cms_root_path | . - - - else - - h1.DashboardWidget-title - - = link_to admin_buyers_accounts_path(search: { created_within: [current_range.first, current_range.last] }), class: 'DashboardWidget-link' - strong data-title-count='true' => number_to_human(widget.value) - | Signups - - .DashboardWidget-percentageInfo data-toggle-visibility='true' - - if widget.has_history - = link_to admin_buyers_accounts_path(search: { created_within: [previous_range.first, previous_range.last] }), class: "DashboardWidget-link #{sign_class(widget)}" - = number_to_percentage(number_to_human(widget.percentual_change), precision: 0) - - else - = link_to admin_buyers_accounts_path(search: { created_within: [current_range.last, current_range.last] }), class: 'DashboardWidget-link DashboardWidget-link--today' - = number_to_human(widget.current) - - - if widget.loaded? - span.DashboardWidget-intro.DashboardWidget-intro--primary data-title-intro='true' - | last 30 days - - span.DashboardWidget-intro.DashboardWidget-intro--secondary data-toggle-visibility='true' - = widget.has_history ? 'vs. previous 30 days' : 'today' + - if current_account.site_access_code.present? + p + ' When you're ready to open to the world, remember to + = link_to 'remove the Access Code', admin_site_dns_path + | . + + - else + + h1.DashboardWidget-title + + = link_to admin_buyers_accounts_path(search: { created_within: [current_range.first, current_range.last] }), class: 'DashboardWidget-link' + strong data-title-count='true' => number_to_human(widget.value) + | Signups + + .DashboardWidget-percentageInfo data-toggle-visibility='true' + - if widget.has_history + = link_to admin_buyers_accounts_path(search: { created_within: [previous_range.first, previous_range.last] }), class: "DashboardWidget-link #{sign_class(widget)}" + = number_to_percentage(number_to_human(widget.percentual_change), precision: 0) + - else + = link_to admin_buyers_accounts_path(search: { created_within: [current_range.last, current_range.last] }), class: 'DashboardWidget-link DashboardWidget-link--today' + = number_to_human(widget.current) + + span.DashboardWidget-intro.DashboardWidget-intro--primary data-title-intro='true' + | last 30 days + + span.DashboardWidget-intro.DashboardWidget-intro--secondary data-toggle-visibility='true' + = widget.has_history ? 'vs. previous 30 days' : 'today' + - else + = widget.spinner diff --git a/app/views/provider/admin/dashboards/widgets/_service_hits.html.slim b/app/views/provider/admin/dashboards/widgets/_service_hits.html.slim index 5e17350174..aa20be5e16 100644 --- a/app/views/provider/admin/dashboards/widgets/_service_hits.html.slim +++ b/app/views/provider/admin/dashboards/widgets/_service_hits.html.slim @@ -1,38 +1,40 @@ article.DashboardWidget id = widget.id - = widget.render_chart + - if widget.loaded? + = widget.render_chart + - widget_params = widget.params.symbolize_keys - header.DashboardWidget-badge + header.DashboardWidget-badge - - if widget.value == 0 && widget.current == 0 && !widget.has_history + - if widget.value == 0 && widget.current == 0 && !widget.has_history - h1.DashboardWidget-title Hits + h1.DashboardWidget-title Hits - p - ' In order to show Hits for the #{friendly_service_name(service)} you need to have at least one application sending traffic to it. - p - ' Consider - => link_to 'making some test calls', edit_admin_service_integration_path(widget.params['service_id'], :anchor => 'staging') - | from the Integration page to get a feel for what you'd see here. + p + ' In order to show Hits for the #{friendly_service_name(service)} you need to have at least one application sending traffic to it. + p + ' Consider + => link_to 'making some test calls', edit_admin_service_integration_path(widget_params['service_id'], :anchor => 'staging') + | from the Integration page to get a feel for what you'd see here. - - else - - h1.DashboardWidget-title + - else - = link_to admin_service_stats_usage_path(widget.params.merge(since: current_range.first)), class: 'DashboardWidget-link' - strong data-title-count='true' => number_to_human(widget.value) - | Hits + h1.DashboardWidget-title + = link_to admin_service_stats_usage_path(widget_params.merge(since: current_range.first)), class: 'DashboardWidget-link' + strong data-title-count='true' => number_to_human(widget.value) + | Hits - .DashboardWidget-percentageInfo data-toggle-visibility='true' - - if widget.has_history - = link_to admin_service_stats_usage_path(widget.params.merge(since: previous_range.first)), class: "DashboardWidget-link #{sign_class(widget)}" - = number_to_percentage(number_to_human(widget.percentual_change), precision: 0) - - else - = link_to admin_service_stats_usage_path(widget.params.merge(since: current_range.last, until: current_range.last, period: 'day')), class: 'DashboardWidget-link' - = number_to_human(widget.current) + .DashboardWidget-percentageInfo data-toggle-visibility='true' + - if widget.has_history + = link_to admin_service_stats_usage_path(widget_params.merge(since: previous_range.first)), class: "DashboardWidget-link #{sign_class(widget)}" + = number_to_percentage(number_to_human(widget.percentual_change), precision: 0) + - else + = link_to admin_service_stats_usage_path(widget_params.merge(since: current_range.last, until: current_range.last, period: 'day')), class: 'DashboardWidget-link' + = number_to_human(widget.current) - - if widget.loaded? span.DashboardWidget-intro.DashboardWidget-intro--primary data-title-intro='true' | last 30 days span.DashboardWidget-intro.DashboardWidget-intro--secondary data-toggle-visibility='true' = widget.has_history ? 'vs. previous 30 days' : 'today' + - else + = widget.spinner diff --git a/app/views/provider/admin/dashboards/widgets/_service_integration_errors.html.slim b/app/views/provider/admin/dashboards/widgets/_service_integration_errors.html.slim index 3f554c074f..53ea23b857 100644 --- a/app/views/provider/admin/dashboards/widgets/_service_integration_errors.html.slim +++ b/app/views/provider/admin/dashboards/widgets/_service_integration_errors.html.slim @@ -1,6 +1,5 @@ -- loaded = widget.loaded? && widget.value -- if loaded - == dashboard_secondary_link 'Integration Errors', - admin_service_errors_path(widget.params[:service_id]), - icon_name: 'exclamation-circle', +- if widget.loaded? && widget.value + == dashboard_secondary_link 'Integration Errors', + admin_service_errors_path(widget.params[:service_id]), + icon_name: 'exclamation-circle', notice: true diff --git a/app/views/provider/admin/dashboards/_service_navigation.html.slim b/app/views/provider/admin/dashboards/widgets/_service_navigations.html.slim similarity index 99% rename from app/views/provider/admin/dashboards/_service_navigation.html.slim rename to app/views/provider/admin/dashboards/widgets/_service_navigations.html.slim index 06be667688..947120fa6c 100644 --- a/app/views/provider/admin/dashboards/_service_navigation.html.slim +++ b/app/views/provider/admin/dashboards/widgets/_service_navigations.html.slim @@ -14,6 +14,7 @@ nav.DashboardNavigation admin_service_stats_usage_path(service), icon_name: 'bar-chart' + - if widget.loaded? - if service.cinstances.any? li.DashboardNavigation-list-item // Applications diff --git a/config/routes.rb b/config/routes.rb index 2436714109..4a726d1cd8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -231,6 +231,7 @@ def call(env) namespace :service, path: 'service/:service_id', as: :service do resource :hits, only: [:show] resource :integration_errors, only: [:show] + resource :navigations, only: [:show] resource :top_traffic, only: [:show], controller: :top_traffic end end diff --git a/features/old/menu/dashboard.feature b/features/old/menu/dashboard.feature index 5885efb911..a432dbdec8 100644 --- a/features/old/menu/dashboard.feature +++ b/features/old/menu/dashboard.feature @@ -6,6 +6,7 @@ Feature: Dashboard Background: Given a provider "foo.example.com" And current domain is the admin domain of provider "foo.example.com" + And All Dashboard widgets are loaded Scenario: Audience widget When I log in as provider "foo.example.com" diff --git a/features/old/stats/provider_side.feature b/features/old/stats/provider_side.feature index b528df82a0..df8b510026 100644 --- a/features/old/stats/provider_side.feature +++ b/features/old/stats/provider_side.feature @@ -11,6 +11,7 @@ Feature: Provider stats And provider "foo.example.com" has multiple applications enabled And current domain is the admin domain of provider "foo.example.com" And all the rolling updates features are off + And All Dashboard widgets are loaded Scenario: Stats access When I log in as provider "foo.example.com" diff --git a/features/step_definitions/dashboard_steps.rb b/features/step_definitions/dashboard_steps.rb index 2fe4a731ff..5d94a2615e 100644 --- a/features/step_definitions/dashboard_steps.rb +++ b/features/step_definitions/dashboard_steps.rb @@ -58,3 +58,7 @@ def top_traffic_for_name(name, opts = {}) search_bar = find("##{tab}_search").find('input[type="search"]') search_bar.send_keys query end + +When 'All Dashboard widgets are loaded' do + DashboardWidgetPresenter.any_instance.stubs(:loaded?).returns(true) +end