19
19
# under the License.
20
20
21
21
''' logicalplan.py '''
22
- from collections import defaultdict
23
- from heron .common .src .python .utils .log import Log
24
- import heron .tools .explorer .src .python .args as args
25
- import heron .tools .common .src .python .access .tracker_access as tracker_access
26
- from tabulate import tabulate
27
-
22
+ import sys
28
23
29
- def create_parser (subparsers ):
30
- """ create parser """
31
- components_parser = subparsers .add_parser (
32
- 'components' ,
33
- help = 'Display information of a topology\' s components' ,
34
- usage = "%(prog)s cluster/[role]/[env] topology-name [options]" ,
35
- add_help = False )
36
- args .add_cluster_role_env (components_parser )
37
- args .add_topology_name (components_parser )
38
- args .add_spouts (components_parser )
39
- args .add_bolts (components_parser )
40
- args .add_verbose (components_parser )
41
- args .add_tracker_url (components_parser )
42
- args .add_config (components_parser )
43
- components_parser .set_defaults (subcommand = 'components' )
24
+ from collections import defaultdict
44
25
45
- return subparsers
26
+ from heron . common . src . python . utils . log import Log
46
27
28
+ import heron .tools .common .src .python .access .tracker_access as tracker_access
47
29
48
- # pylint: disable=misplaced-bare-raise
49
- def parse_topo_loc (cl_args ):
50
- """ parse topology location """
51
- try :
52
- topo_loc = cl_args ['cluster/[role]/[env]' ].split ('/' )
53
- topo_loc .append (cl_args ['topology-name' ])
54
- if len (topo_loc ) != 4 :
55
- raise
56
- return topo_loc
57
- except Exception :
58
- Log .error ('Error: invalid topology location' )
59
- raise
30
+ from tabulate import tabulate
60
31
61
32
62
- def to_table (components , topo_info ):
33
+ def to_table (components , topo_info , component_filter ):
63
34
""" normalize raw logical plan info to table """
64
35
inputs , outputs = defaultdict (list ), defaultdict (list )
65
36
for ctype , component in list (components .items ()):
@@ -76,6 +47,10 @@ def to_table(components, topo_info):
76
47
# stages is an int so keep going
77
48
if ctype == "stages" :
78
49
continue
50
+ if component_filter == "spouts" and ctype != "spouts" :
51
+ continue
52
+ if component_filter == "bolts" and ctype != "bolts" :
53
+ continue
79
54
for component_name , component_info in list (component .items ()):
80
55
row = [ctype [:- 1 ], component_name ]
81
56
if ctype == 'spouts' :
@@ -89,59 +64,13 @@ def to_table(components, topo_info):
89
64
return info , header
90
65
91
66
92
- def filter_bolts (table , header ):
93
- """ filter to keep bolts """
94
- bolts_info = []
95
- for row in table :
96
- if row [0 ] == 'bolt' :
97
- bolts_info .append (row )
98
- return bolts_info , header
99
-
100
-
101
- def filter_spouts (table , header ):
102
- """ filter to keep spouts """
103
- spouts_info = []
104
- for row in table :
105
- if row [0 ] == 'spout' :
106
- spouts_info .append (row )
107
- return spouts_info , header
108
-
109
-
110
- # pylint: disable=unused-argument,superfluous-parens
111
- def run (cl_args , compo_type ):
67
+ def run (component_type : str , cluster : str , role : str , environment : str , topology : str ):
112
68
""" run command """
113
- cluster , role , env = cl_args ['cluster' ], cl_args ['role' ], cl_args ['environ' ]
114
- topology = cl_args ['topology-name' ]
115
- spouts_only , bolts_only = cl_args ['spout' ], cl_args ['bolt' ]
116
69
try :
117
- components = tracker_access .get_logical_plan (cluster , env , topology , role )
118
- topo_info = tracker_access .get_topology_info (cluster , env , topology , role )
119
- table , header = to_table (components , topo_info )
120
- if spouts_only == bolts_only :
121
- print (tabulate (table , headers = header ))
122
- elif spouts_only :
123
- table , header = filter_spouts (table , header )
124
- print (tabulate (table , headers = header ))
125
- else :
126
- table , header = filter_bolts (table , header )
127
- print (tabulate (table , headers = header ))
128
- return True
70
+ components = tracker_access .get_logical_plan (cluster , environment , topology , role )
71
+ topo_info = tracker_access .get_topology_info (cluster , environment , topology , role )
129
72
except :
130
- Log .error ("Fail to connect to tracker: \' %s\' " , cl_args ["tracker_url" ])
131
- return False
132
-
133
-
134
-
135
- def run_components (command , parser , cl_args , unknown_args ):
136
- """ run components command """
137
- return run (cl_args , 'all' )
138
-
139
-
140
- def run_spouts (command , parser , cl_args , unknown_args ):
141
- """ run spouts command """
142
- return run (cl_args , 'spouts' )
143
-
144
-
145
- def run_bolts (command , parser , cl_args , unknown_args ):
146
- """ run bolts command """
147
- return run (cl_args , 'bolts' )
73
+ Log .error ("Fail to connect to tracker" )
74
+ sys .exit (1 )
75
+ table , header = to_table (components , topo_info , component_type )
76
+ print (tabulate (table , headers = header ))
0 commit comments