From 1e4d6e829d6471d60868f9a8d1ddbcde9b044914 Mon Sep 17 00:00:00 2001 From: Simon Howe Date: Wed, 23 Mar 2016 15:36:55 +0100 Subject: [PATCH] Hide empty topo's that set hidden_if_empty=true. --- client/app/scripts/stores/app-store.js | 8 ++++++-- .../scripts/utils/__tests__/topology-utils-test.js | 14 ++++++++++++++ client/app/scripts/utils/topology-utils.js | 5 +++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/client/app/scripts/stores/app-store.js b/client/app/scripts/stores/app-store.js index 0f1f49360..cd79d84d2 100644 --- a/client/app/scripts/stores/app-store.js +++ b/client/app/scripts/stores/app-store.js @@ -6,7 +6,8 @@ import { Store } from 'flux/utils'; import AppDispatcher from '../dispatcher/app-dispatcher'; import ActionTypes from '../constants/action-types'; import { EDGE_ID_SEPARATOR } from '../constants/naming'; -import { findTopologyById, setTopologyUrlsById, updateTopologyIds } from '../utils/topology-utils'; +import { findTopologyById, setTopologyUrlsById, updateTopologyIds, + filterHiddenTopologies } from '../utils/topology-utils'; const makeList = List; const makeMap = Map; @@ -62,8 +63,11 @@ const topologySorter = topology => topology.get('rank'); // adds ID field to topology (based on last part of URL path) and save urls in // map for easy lookup function processTopologies(nextTopologies) { + // filter out hidden topos + const visibleTopologies = filterHiddenTopologies(nextTopologies); + // add IDs to topology objects in-place - const topologiesWithId = updateTopologyIds(nextTopologies); + const topologiesWithId = updateTopologyIds(visibleTopologies); // cache URLs by ID topologyUrlsById = setTopologyUrlsById(topologyUrlsById, topologiesWithId); diff --git a/client/app/scripts/utils/__tests__/topology-utils-test.js b/client/app/scripts/utils/__tests__/topology-utils-test.js index 7686eee92..7010bb414 100644 --- a/client/app/scripts/utils/__tests__/topology-utils-test.js +++ b/client/app/scripts/utils/__tests__/topology-utils-test.js @@ -106,4 +106,18 @@ describe('TopologyUtils', () => { expect(nodes.n2.degree).toEqual(0); expect(nodes.n3.degree).toEqual(0); }); + + describe('filterHiddenTopologies', () => { + it('should filter out empty topos that set hidden_if_empty=true', () => { + const topos = [ + {id: 'a', hidden_if_empty: true, stats: {node_count: 0, filtered_nodes:0}}, + {id: 'b', hidden_if_empty: true, stats: {node_count: 1, filtered_nodes:0}}, + {id: 'c', hidden_if_empty: true, stats: {node_count: 0, filtered_nodes:1}}, + {id: 'd', hidden_if_empty: false, stats: {node_count: 0, filtered_nodes:0}} + ]; + + const res = TopologyUtils.filterHiddenTopologies(topos); + expect(res.map(t => t.id)).toEqual(['b', 'c', 'd']); + }); + }) }); diff --git a/client/app/scripts/utils/topology-utils.js b/client/app/scripts/utils/topology-utils.js index 4489aa5e4..5d0e5dd23 100644 --- a/client/app/scripts/utils/topology-utils.js +++ b/client/app/scripts/utils/topology-utils.js @@ -50,3 +50,8 @@ export function setTopologyUrlsById(topologyUrlsById, topologies) { }); return urlMap; } + +export function filterHiddenTopologies(topologies) { + return topologies.filter(t => (!t.hidden_if_empty || t.stats.node_count > 0 || + t.stats.filtered_nodes > 0)); +}