Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit 6b1636a

Browse files
committed
Merge pull request #194 from spotify/drewc/json-helios-status
[cli] status command with --host --json only return jobs on those hosts
2 parents 13d4544 + b941133 commit 6b1636a

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

helios-system-tests/src/main/java/com/spotify/helios/system/QueryFailureTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ public void testJobStatusHostFilter() throws Exception {
8484
assertEquals(CreateJobResponse.Status.OK, created.getStatus());
8585

8686
final String result = cli("status", "--host", "framazama");
87-
assertThat(result, containsString("matched no hosts"));
87+
assertThat(result, containsString("matched no jobs"));
8888
}
8989
}

helios-tools/src/main/java/com/spotify/helios/cli/command/JobStatusCommand.java

+64-14
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public JobStatusCommand(final Subparser parser) {
7373
.help("Print full hostnames, job and container id's.");
7474
}
7575

76+
interface HostStatusDisplayer {
77+
void matchedStatus(final JobStatus jobStatus, final Iterable<String> matchingHosts,
78+
final Map<String, TaskStatus> taskStatuses);
79+
}
80+
7681
@Override
7782
int run(Namespace options, HeliosClient client, PrintStream out, final boolean json)
7883
throws ExecutionException, InterruptedException {
@@ -103,12 +108,58 @@ int run(Namespace options, HeliosClient client, PrintStream out, final boolean j
103108
statuses.putAll(allAsMap(futures));
104109

105110
if (json) {
106-
out.println(Json.asPrettyStringUnchecked(statuses));
111+
showJsonStatuses(out, hostPattern, jobIds, statuses);
107112
return 0;
108113
}
109114

110115
final JobStatusTable table = jobStatusTable(out, full);
111116

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) {
112163
boolean noHostMatchedEver = true;
113164

114165
for (final JobId jobId : Ordering.natural().sortedCopy(jobIds)) {
@@ -133,21 +184,20 @@ int run(Namespace options, HeliosClient client, PrintStream out, final boolean j
133184
noHostMatchedEver = false;
134185
}
135186

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);
143188

144-
if (noHostMatchedEver) {
145-
out.printf("host pattern %s matched no hosts%n", hostPattern);
146-
return 1;
147189
}
190+
return noHostMatchedEver;
191+
}
148192

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+
}
152202
}
153203
}

0 commit comments

Comments
 (0)