Skip to content

Commit 00e7f3b

Browse files
committed
fix step into + add tests
1 parent 0c65c7f commit 00e7f3b

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

web/src/components/__tests-data__/scopes.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export const ScopeDefSample: ScopeConfigDef[] = [
77
shortName: 'Cl',
88
description: 'Cluster name or identifier',
99
labels: ['K8S_ClusterName'],
10-
feature: 'multiCluster'
10+
feature: 'multiCluster',
11+
stepInto: 'zone'
1112
},
1213
{
1314
id: 'zone',
@@ -16,23 +17,26 @@ export const ScopeDefSample: ScopeConfigDef[] = [
1617
description: 'Availability zone',
1718
labels: ['SrcK8S_Zone', 'DstK8S_Zone'],
1819
feature: 'zones',
19-
groups: ['clusters']
20+
groups: ['clusters'],
21+
stepInto: 'host'
2022
},
2123
{
2224
id: 'host',
2325
name: 'Node',
2426
shortName: 'Nd',
2527
description: 'Node on which the resources are running',
2628
labels: ['SrcK8S_HostName', 'DstK8S_HostName'],
27-
groups: ['clusters', 'zones', 'clusters+zones']
29+
groups: ['clusters', 'zones', 'clusters+zones'],
30+
stepInto: 'resource'
2831
},
2932
{
3033
id: 'namespace',
3134
name: 'Namespace',
3235
shortName: 'NS',
3336
description: 'Resource namespace',
3437
labels: ['SrcK8S_Namespace', 'DstK8S_Namespace'],
35-
groups: ['clusters', 'clusters+zones', 'clusters+hosts', 'zones', 'zones+hosts', 'hosts']
38+
groups: ['clusters', 'clusters+zones', 'clusters+hosts', 'zones', 'zones+hosts', 'hosts'],
39+
stepInto: 'owner'
3640
},
3741
{
3842
id: 'owner',
@@ -58,7 +62,8 @@ export const ScopeDefSample: ScopeConfigDef[] = [
5862
'hosts',
5963
'hosts+namespaces',
6064
'namespaces'
61-
]
65+
],
66+
stepInto: 'resource'
6267
},
6368
{
6469
id: 'resource',

web/src/components/tabs/netflow-topology/2d/topology-content.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ export const TopologyContent: React.FC<TopologyContentProps> = ({
206206
const onStepInto = React.useCallback(
207207
(data: Decorated<ElementData>) => {
208208
const groupTypes: TopologyGroupTypes = metricScope;
209-
const scope = getStepInto(metricScope, scopes);
209+
const scope = getStepInto(
210+
metricScope,
211+
scopes.map(sc => sc.id)
212+
);
210213
if (data.nodeType && data.peer && scope) {
211214
setMetricScope(scope);
212215
setOptions({ ...options, groupTypes });

web/src/model/__tests__/topology.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { ScopeDefSample } from '../../components/__tests-data__/scopes';
2-
import { getGroupName, getGroupsForScope } from '../scope';
2+
import { ContextSingleton } from '../../utils/context';
3+
import { getGroupName, getGroupsForScope, getStepInto } from '../scope';
34

45
describe('Check enabled groups', () => {
6+
beforeEach(() => {
7+
ContextSingleton.setScopes(ScopeDefSample);
8+
});
9+
510
it('should get group from scope', () => {
611
let groups = getGroupsForScope('cluster', ScopeDefSample);
712
expect(groups).toEqual(['none']);
@@ -41,4 +46,18 @@ describe('Check enabled groups', () => {
4146
name = getGroupName('namespaces+zzz', ScopeDefSample, (s: string) => s);
4247
expect(name).toEqual('Namespace + invalid zzz');
4348
});
49+
50+
it('should get next scope', () => {
51+
let next = getStepInto('cluster', ['cluster', 'zone', 'host', 'namespace', 'owner', 'resource']);
52+
expect(next).toEqual('zone');
53+
54+
next = getStepInto('cluster', ['cluster', 'host']);
55+
expect(next).toEqual('host');
56+
57+
next = getStepInto('cluster', ['cluster']);
58+
expect(next).toEqual(undefined);
59+
60+
next = getStepInto('resource', ['cluster', 'zone', 'host', 'namespace', 'owner', 'resource']);
61+
expect(next).toEqual(undefined);
62+
});
4463
});

web/src/model/scope.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type ScopeConfigDef = {
1313
groups?: string[];
1414
filter?: string;
1515
filters?: string[];
16-
stepInto?: string;
16+
stepInto?: FlowScope;
1717
};
1818

1919
export const getScopeName = (sc: ScopeConfigDef | undefined, t: (k: string) => string) => {
@@ -77,6 +77,11 @@ export const getDirectionnalScopes = () => {
7777
return ContextSingleton.getScopes().filter(sc => isDirectionnal(sc));
7878
};
7979

80-
export const getStepInto = (scopeId: FlowScope, scopes: ScopeConfigDef[]): FlowScope | undefined => {
81-
return scopes.find(sc => sc.id === scopeId)?.stepInto;
80+
export const getStepInto = (scopeId: FlowScope, allowedIds: FlowScope[]): FlowScope | undefined => {
81+
const next = ContextSingleton.getScopes().find(sc => sc.id === scopeId)?.stepInto;
82+
if (!next || allowedIds.includes(next)) {
83+
return next;
84+
}
85+
// recursively get next scope as some may be hidden due to selected features
86+
return getStepInto(next, allowedIds);
8287
};

web/src/model/topology.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,11 @@ export const generateDataModel = (
546546
};
547547

548548
const peerToNodeData = (p: TopologyMetricPeer): NodeData => {
549-
const canStepInto = getStepInto(metricScope, scopes) !== undefined;
549+
const canStepInto =
550+
getStepInto(
551+
metricScope,
552+
scopes.map(sc => sc.id)
553+
) !== undefined;
550554
switch (metricScope) {
551555
case 'owner':
552556
return p.owner ? { peer: p, nodeType: 'owner', canStepInto } : { peer: p, nodeType: 'unknown' };

0 commit comments

Comments
 (0)