From d92e08f3f62880dcada6d597dd5fef7189a0b3d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Mon, 14 Aug 2017 20:10:44 -0700 Subject: [PATCH] Don't reload filters until entire applyFilterList() block was completed setFilters() will trigger fetch of the new alerts with updated filter, it's being called after each filter is added to the filter bar, so each added filter ends up with AJAX call to /alerts.json, this means that when someone clicks on a history entry with complex rule it will trigger multiple subsequent requests. What's worse first request will have bigger response body (since it filters less) so it's likely that it will finish after the next request, leaving invalid alerts on the grid. This disable setFilters() if we apply filters in batch mode. --- assets/static/filters.js | 42 +++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/assets/static/filters.js b/assets/static/filters.js index 79bac98bf..638dd234c 100644 --- a/assets/static/filters.js +++ b/assets/static/filters.js @@ -56,23 +56,6 @@ function addFilter(text) { $(selectors.filter).tagsinput("add", text); } -function applyFilterList(filterList) { - // we need to add filters one by one, this would reload alerts on every - // add() so let's pause reloads and resume once we're done with updating - // filters - unsee.pause(); - // disable history appends as it would record each new filter in the - // history - appendsEnabled = false; - $(selectors.filter).tagsinput("removeAll"); - for (var i = 0; i < filterList.length; i++) { - $(selectors.filter).tagsinput("add", filterList[i]); - } - // enable everything again - appendsEnabled = true; - unsee.resume(); -} - function clearFilters() { $(selectors.filter).tagsinput("removeAll"); } @@ -146,6 +129,24 @@ function setFilters() { unsee.triggerReload(); } +function applyFilterList(filterList) { + // we need to add filters one by one, this would reload alerts on every + // add() so let's pause reloads and resume once we're done with updating + // filters + unsee.pause(); + // disable history appends as it would record each new filter in the + // history + appendsEnabled = false; + $(selectors.filter).tagsinput("removeAll"); + for (var i = 0; i < filterList.length; i++) { + $(selectors.filter).tagsinput("add", filterList[i]); + } + // enable everything again + appendsEnabled = true; + setFilters(); + unsee.resume(); +} + function init(historyStore) { historyStorage = historyStore; var initialFilter; @@ -179,7 +180,12 @@ function init(historyStore) { }); $(selectors.filter).on("itemAdded itemRemoved", function(event) { - setFilters(); + if (appendsEnabled) { + // if history appends are disabled then don't set filters yet + // we disable appends to have batch filter updates so we shouldn't + // set filters yet + setFilters(); + } // add counter badge to new tag if (event.type == "itemAdded") { addBadge(event.item);