From 84dd20d3971b734b63e4168ec22e8844e16aa84a Mon Sep 17 00:00:00 2001 From: Stephen Humphries <59188756+sjahu@users.noreply.github.com> Date: Mon, 30 Aug 2021 15:31:41 -0400 Subject: [PATCH] Persist live poll status in browser localStorage (#4976) * Persist live poll status in browser localStorage * Prefix localStorage variables with 'sidekiq' * Minor fix for default handling in localStorage timeInterval parsing --- lib/sidekiq/web/helpers.rb | 13 +---- test/test_web.rb | 12 ----- test/test_web_helpers.rb | 15 ++++++ web/assets/javascripts/application.js | 75 ++++++++++++++++++-------- web/assets/javascripts/dashboard.js | 10 ++-- web/assets/stylesheets/application.css | 4 +- web/views/_poll_link.erb | 7 +-- web/views/layout.erb | 2 +- 8 files changed, 80 insertions(+), 58 deletions(-) diff --git a/lib/sidekiq/web/helpers.rb b/lib/sidekiq/web/helpers.rb index 3810fb92..237f1400 100644 --- a/lib/sidekiq/web/helpers.rb +++ b/lib/sidekiq/web/helpers.rb @@ -70,17 +70,6 @@ module Sidekiq @head_html.join if defined?(@head_html) end - def poll_path - if current_path != "" && params["poll"] - path = root_path + current_path - query_string = to_query_string(params.slice(*params.keys - %w[page poll])) - path += "?#{query_string}" unless query_string.empty? - path - else - "" - end - end - def text_direction get_locale["TextDirection"] || "ltr" end @@ -203,7 +192,7 @@ module Sidekiq [score.to_f, jid] end - SAFE_QPARAMS = %w[page poll direction] + SAFE_QPARAMS = %w[page direction] # Merge options with current params, filter safe params, and stringify to query string def qparams(options) diff --git a/test/test_web.rb b/test/test_web.rb index 16bceb26..7b177660 100644 --- a/test/test_web.rb +++ b/test/test_web.rb @@ -428,12 +428,6 @@ describe Sidekiq::Web do assert_match(/#{msg['args'][2]}/, last_response.body) end - it 'calls updatePage() once when polling' do - get '/busy?poll=true' - assert_equal 200, last_response.status - assert_equal 1, last_response.body.scan('data-poll-path="/busy').count - end - it 'escape job args and error messages' do # on /retries page params = add_xss_retry @@ -647,12 +641,6 @@ describe Sidekiq::Web do assert_equal 200, last_response.status assert_match(/#{params.first['args'][2]}/, last_response.body) end - - it 'handles bad query input' do - get '/queues/foo?page=B "B "H>B" } + end + end + assert_equal "direction=H%3EB&page=B%3CH", obj.qparams("page" => "B 0) { // set up live poll only when button is present + $(document).on("click", ".live-poll", function() { + if (localStorage.sidekiqLivePoll == "enabled") { + localStorage.sidekiqLivePoll = "disabled"; + clearTimeout(livePollTimer); + livePollTimer = null; + } else { + localStorage.sidekiqLivePoll = "enabled"; + livePollCallback(); + } + + updateLivePollButton(); + }); + + updateLivePollButton(); + if (localStorage.sidekiqLivePoll == "enabled") { + scheduleLivePoll(); + } + } }); function updateFuzzyTimes(locale) { @@ -50,27 +66,44 @@ function updateFuzzyTimes(locale) { t.cancel(); } -function updatePage(url) { +function updateLivePollButton() { + if (localStorage.sidekiqLivePoll == "enabled") { + $('.live-poll-stop').show(); + $('.live-poll-start').hide(); + } else { + $('.live-poll-start').show(); + $('.live-poll-stop').hide(); + } +} + +function livePollCallback() { + clearTimeout(livePollTimer); + $.ajax({ - url: url, + url: document.url, dataType: 'html' - }).done(function(data) { - $data = $(data) + }).done( + replacePage + ).complete( + scheduleLivePoll + ) +} - var $page = $data.filter('#page') - $('#page').replaceWith($page) +function scheduleLivePoll() { + let ti = parseInt(localStorage.sidekiqTimeInterval) || 5000; + livePollTimer = setTimeout(livePollCallback, ti); +} - var $header_status = $data.find('.status') - $('.status').replaceWith($header_status) +function replacePage(data) { + $data = $(data) - updateFuzzyTimes($('body').data('locale')); + var $page = $data.filter('#page') + $('#page').replaceWith($page) - var ti = parseInt(localStorage.timeInterval) || 2000; - setTimeout(function(){updatePage(url)}, ti) - }).fail(function() { - var ti = parseInt(localStorage.timeInterval) || 2000; - setTimeout(function(){updatePage(url)}, ti) - }) + var $header_status = $data.find('.status') + $('.status').replaceWith($header_status) + + updateFuzzyTimes($('body').data('locale')); } $(function() { diff --git a/web/assets/javascripts/dashboard.js b/web/assets/javascripts/dashboard.js index 16864248..c4d50892 100644 --- a/web/assets/javascripts/dashboard.js +++ b/web/assets/javascripts/dashboard.js @@ -17,7 +17,7 @@ var nodes=vis.selectAll("path").data(series.stack.filter(function(d){return d.y! var poller; var realtimeGraph = function(updatePath) { - var timeInterval = parseInt(localStorage.timeInterval || '5000'); + var timeInterval = parseInt(localStorage.sidekiqTimeInterval) || 5000; var graphElement = document.getElementById("realtime"); var graph = new Rickshaw.Graph( { @@ -246,14 +246,14 @@ var setSliderLabel = function(val) { $(function(){ renderGraphs(); - if (typeof localStorage.timeInterval !== 'undefined'){ - $('div.interval-slider input').val(localStorage.timeInterval); - setSliderLabel(localStorage.timeInterval); + if (typeof localStorage.sidekiqTimeInterval !== 'undefined'){ + $('div.interval-slider input').val(localStorage.sidekiqTimeInterval); + setSliderLabel(localStorage.sidekiqTimeInterval); } $(document).on('change', 'div.interval-slider input', function(){ clearInterval(poller); - localStorage.timeInterval = $(this).val(); + localStorage.sidekiqTimeInterval = $(this).val(); setSliderLabel($(this).val()); resetGraphs(); renderGraphs(); diff --git a/web/assets/stylesheets/application.css b/web/assets/stylesheets/application.css index 0655e2b2..e459b70e 100644 --- a/web/assets/stylesheets/application.css +++ b/web/assets/stylesheets/application.css @@ -98,10 +98,10 @@ header.row .pagination { .poll-wrapper { margin: 9px; } -#live-poll.active { +.live-poll.active { background-color: #009300; } -#live-poll.active:hover { +.live-poll.active:hover { background-color: #777; } .summary_bar ul { diff --git a/web/views/_poll_link.erb b/web/views/_poll_link.erb index d6aba179..8bc57995 100644 --- a/web/views/_poll_link.erb +++ b/web/views/_poll_link.erb @@ -1,7 +1,4 @@ <% if current_path != '' %> - <% if params[:poll] %> - <%= t('StopPolling') %> - <% else %> - <%= t('LivePoll') %> - <% end %> + <%= t('LivePoll') %> + <%= t('StopPolling') %> <% end %> diff --git a/web/views/layout.erb b/web/views/layout.erb index da948fce..3cbe9c2b 100644 --- a/web/views/layout.erb +++ b/web/views/layout.erb @@ -22,7 +22,7 @@ <%= display_custom_head %> - + <%= erb :_nav %>