@@ -73,6 +73,11 @@ public JobStatusCommand(final Subparser parser) {
73
73
.help ("Print full hostnames, job and container id's." );
74
74
}
75
75
76
+ interface HostStatusDisplayer {
77
+ void matchedStatus (final JobStatus jobStatus , final Iterable <String > matchingHosts ,
78
+ final Map <String , TaskStatus > taskStatuses );
79
+ }
80
+
76
81
@ Override
77
82
int run (Namespace options , HeliosClient client , PrintStream out , final boolean json )
78
83
throws ExecutionException , InterruptedException {
@@ -103,12 +108,58 @@ int run(Namespace options, HeliosClient client, PrintStream out, final boolean j
103
108
statuses .putAll (allAsMap (futures ));
104
109
105
110
if (json ) {
106
- out . println ( Json . asPrettyStringUnchecked ( statuses ) );
111
+ showJsonStatuses ( out , hostPattern , jobIds , statuses );
107
112
return 0 ;
108
113
}
109
114
110
115
final JobStatusTable table = jobStatusTable (out , full );
111
116
117
+ final boolean noHostMatchedEver = showStatusesForHosts (hostPattern , jobIds , statuses ,
118
+ new HostStatusDisplayer () {
119
+ @ Override
120
+ public void matchedStatus (JobStatus jobStatus , Iterable <String > matchingHosts ,
121
+ Map <String , TaskStatus > taskStatuses ) {
122
+ displayTask (full , table , jobStatus .getJob ().getId (), jobStatus , taskStatuses ,
123
+ matchingHosts );
124
+ }
125
+ });
126
+
127
+ if (noHostMatchedEver ) {
128
+ out .printf ("host pattern %s matched no jobs%n" , hostPattern );
129
+ return 1 ;
130
+ }
131
+
132
+ table .print ();
133
+
134
+ return 0 ;
135
+ }
136
+
137
+ private void showJsonStatuses (PrintStream out , final String hostPattern , final Set <JobId > jobIds ,
138
+ final Map <JobId , JobStatus > statuses ) {
139
+ if (Strings .isNullOrEmpty (hostPattern )) {
140
+ out .println (Json .asPrettyStringUnchecked (statuses ));
141
+ return ;
142
+ }
143
+
144
+ final Map <JobId , JobStatus > returnStatuses = Maps .newTreeMap ();
145
+ showStatusesForHosts (hostPattern , jobIds , statuses , new HostStatusDisplayer () {
146
+ @ Override
147
+ public void matchedStatus (JobStatus jobStatus , Iterable <String > matchingHosts ,
148
+ Map <String , TaskStatus > taskStatuses ) {
149
+ for (final String host : matchingHosts ) {
150
+ final Map <String , Deployment > deployments = jobStatus .getDeployments ();
151
+ final Deployment deployment = (deployments == null ) ? null : deployments .get (host );
152
+ if (deployment != null ) {
153
+ returnStatuses .put (jobStatus .getJob ().getId (), jobStatus );
154
+ }
155
+ }
156
+ }
157
+ });
158
+ out .println (Json .asPrettyStringUnchecked (returnStatuses ));
159
+ }
160
+
161
+ private boolean showStatusesForHosts (final String hostPattern , final Set <JobId > jobIds ,
162
+ final Map <JobId , JobStatus > statuses , final HostStatusDisplayer statusDisplayer ) {
112
163
boolean noHostMatchedEver = true ;
113
164
114
165
for (final JobId jobId : Ordering .natural ().sortedCopy (jobIds )) {
@@ -133,21 +184,20 @@ int run(Namespace options, HeliosClient client, PrintStream out, final boolean j
133
184
noHostMatchedEver = false ;
134
185
}
135
186
136
- for (final String host : matchingHosts ) {
137
- final Map <String , Deployment > deployments = jobStatus .getDeployments ();
138
- final TaskStatus ts = taskStatuses .get (host );
139
- final Deployment deployment = (deployments == null ) ? null : deployments .get (host );
140
- table .task (jobId , formatHostname (full , host ), ts , deployment );
141
- }
142
- }
187
+ statusDisplayer .matchedStatus (jobStatus , matchingHosts , taskStatuses );
143
188
144
- if (noHostMatchedEver ) {
145
- out .printf ("host pattern %s matched no hosts%n" , hostPattern );
146
- return 1 ;
147
189
}
190
+ return noHostMatchedEver ;
191
+ }
148
192
149
- table .print ();
150
-
151
- return 0 ;
193
+ private void displayTask (final boolean full , final JobStatusTable table , final JobId jobId ,
194
+ final JobStatus jobStatus , final Map <String , TaskStatus > taskStatuses ,
195
+ final Iterable <String > matchingHosts ) {
196
+ for (final String host : matchingHosts ) {
197
+ final Map <String , Deployment > deployments = jobStatus .getDeployments ();
198
+ final TaskStatus ts = taskStatuses .get (host );
199
+ final Deployment deployment = (deployments == null ) ? null : deployments .get (host );
200
+ table .task (jobId , formatHostname (full , host ), ts , deployment );
201
+ }
152
202
}
153
203
}
0 commit comments