Skip to content

Commit

Permalink
Update executions metrics response
Browse files Browse the repository at this point in the history
add -%/--format output
remove --json, defer to RD_FORMAT
allow RD_FORMAT=xml or --xml
  • Loading branch information
gschueler committed Jan 16, 2019
1 parent 3601f96 commit 412507f
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.rundeck.client.api.model.*;
import org.rundeck.client.api.model.executions.MetricsResponse;
import org.rundeck.client.api.model.metrics.EndpointListResult;
import org.rundeck.client.api.model.metrics.HealthCheckStatus;
import org.rundeck.client.api.model.metrics.MetricsData;
Expand Down Expand Up @@ -1208,7 +1209,7 @@ Call<BulkToggleJobScheduleResponse> bulkDisableJobSchedule(
*/
@Headers("Accept: application/json")
@GET("project/{project}/executions/metrics")
Call<ResponseBody> executionMetricsJSON(
Call<MetricsResponse> executionMetrics(
@Path("project") String project,
@QueryMap Map<String, String> options,
@Query("jobIdListFilter") List<String> jobIdListFilter,
Expand All @@ -1228,7 +1229,7 @@ Call<ResponseBody> executionMetricsJSON(
*/
@Headers("Accept: application/json")
@GET("executions/metrics")
Call<ResponseBody> executionMetricsJSON(
Call<MetricsResponse> executionMetrics(
@QueryMap Map<String, String> options,
@Query("jobIdListFilter") List<String> jobIdListFilter,
@Query("excludeJobIdListFilter") List<String> xjobIdListFilter,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.rundeck.client.api.model.executions;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.rundeck.client.util.DataOutput;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

import java.util.HashMap;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)

@Root(strict = false, name = "result")
@Data
public class MetricsResponse
implements DataOutput
{
private Long total;
private Long failed;
@JsonProperty(value = "failed-with-retry")
@Element(name = "failed-with-retry")
private Long failedWithRetry;
private Long succeeded;
@JsonProperty(value = "duration-avg")
@Element(name = "duration-avg")
private String durationAvg;
@JsonProperty(value = "duration-min")
@Element(name = "duration-min")
private String durationMin;
@JsonProperty(value = "duration-max")
@Element(name = "duration-max")
private String durationMax;

@Override
public Map<?, ?> asMap() {
HashMap<String, Object> data = new HashMap<>();
data.put("total", total);
data.put("failed-with-retry", failedWithRetry);
data.put("succeeded", succeeded);
data.put("failed", failed);
data.put("duration-avg", durationAvg);
data.put("duration-min", durationMin);
data.put("duration-max", durationMax);
return data;
}
}
13 changes: 13 additions & 0 deletions rd-api-client/src/main/java/org/rundeck/client/util/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class Format {
private Format() {
}

public static String format(String format, DataOutput data, final String start, final String end) {
return format(format, data.asMap(), start, end);
}

public static String format(String format, Map<?, ?> data, final String start, final String end) {
Pattern pat = Pattern.compile(Pattern.quote(start) + "([\\w.]+)" + Pattern.quote(end));
Matcher matcher = pat.matcher(format);
Expand Down Expand Up @@ -79,6 +83,15 @@ private static Object descend(final Map<?, ?> data, final String... found) {
return (Map<?, ?> map) -> format(format, map, start, end);
}

public static <X extends DataOutput> Function<X, String> dataFormatter(
String format,
final String start,
final String end
)
{
return formatter(format, DataOutput::asMap, start, end);
}

@SuppressWarnings("SameParameterValue")
public static <X> Function<X, String> formatter(
String format,
Expand Down
167 changes: 82 additions & 85 deletions src/main/java/org/rundeck/client/tool/commands/Executions.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import okhttp3.ResponseBody;
import org.rundeck.client.api.RundeckApi;
import org.rundeck.client.api.model.*;
import org.rundeck.client.api.model.executions.MetricsResponse;
import org.rundeck.client.tool.RdApp;
import org.rundeck.client.tool.options.*;
import org.rundeck.client.util.Format;
Expand Down Expand Up @@ -643,110 +644,106 @@ private static BooleanSupplier waitUnlessInterrupt(final int millis) {
}

@CommandLineInterface(application = "metrics")
interface MetricsCmd extends QueryOptions {
interface MetricsCmd
extends QueryOptions
{

@Option(
longName = "xml",
description = "Get the result in raw xml. Note: cannot be combined with RD_FORMAT env variable.")
boolean isRawXML();

@Option(
longName = "xml",
description = "Get the result in raw xml.")
boolean isRawXML();

@Option(
longName = "json",
description = "Get the result in raw json.")
boolean isRawJSON();
@Option(shortName = "%",
longName = "outformat",
description =
"Output format specifier for execution metrics data. You can use \"%key\" where key is one "
+ "of: total,failed-with-retry,failed,succeeded,duration-avg,duration-min,duration-max. E.g. "
+ "\"%total %failed %succeeded\"")
String getOutputFormat();

boolean isOutputFormat();
}


@Command(description = "Obtain metrics over the result set of an execution query.")
public void metrics(MetricsCmd options, CommandOutput out) throws IOException, InputError {

// Check parameters.
if(options.isRawJSON() && options.isRawXML()) {
throw new InputError("You must specify either --xml or --json.");
}

Map<String, String> query = createQueryParams(options, null, null);

Map<String, Object> result;

// Case project wire.
if (options.isProject()) {

// Raw XML
if(options.isRawXML()) {
ResponseBody response = apiCall(api -> api.executionMetricsXML(
options.getProject(),
query,
options.getJobIdList(),
options.getExcludeJobIdList(),
options.getJobList(),
options.getExcludeJobList()
));
out.output(response.string());
return;
// Check parameters.
if (!"xml".equalsIgnoreCase(getAppConfig().getString("RD_FORMAT", null)) && options.isRawXML()) {
throw new InputError("You cannot use RD_FORMAT env var with --xml");
}

// Get raw Json.
ResponseBody response = apiCall(api -> api.executionMetricsJSON(
options.getProject(),
query,
options.getJobIdList(),
options.getExcludeJobIdList(),
options.getJobList(),
options.getExcludeJobList()
));

if(options.isRawJSON()) {
out.output(response.string());
return;
}
Map<String, String> query = createQueryParams(options, null, null);

result = JSON.readValue(response.string(), new TypeReference<Map<String, Object>>() {});
MetricsResponse result;

}
// Case project wire.
if (options.isProject()) {

// Case system-wide
else {
// Raw XML
if ("XML".equalsIgnoreCase(getAppConfig().getString("RD_FORMAT", null)) || options.isRawXML()) {
ResponseBody response = apiCall(api -> api.executionMetricsXML(
options.getProject(),
query,
options.getJobIdList(),
options.getExcludeJobIdList(),
options.getJobList(),
options.getExcludeJobList()
));
out.output(response.string());
return;
}

// Raw XML
if(options.isRawXML()) {
ResponseBody response = apiCall(api -> api.executionMetricsXML(
query,
options.getJobIdList(),
options.getExcludeJobIdList(),
options.getJobList(),
options.getExcludeJobList()
));
out.output(response.string());
return;
}
// Get response.
result = apiCall(api -> api.executionMetrics(
options.getProject(),
query,
options.getJobIdList(),
options.getExcludeJobIdList(),
options.getJobList(),
options.getExcludeJobList()
));

}

// Case system-wide
else {

// Raw XML
if ("XML".equalsIgnoreCase(getAppConfig().getString("RD_FORMAT", null)) || options.isRawXML()) {
ResponseBody response = apiCall(api -> api.executionMetricsXML(
query,
options.getJobIdList(),
options.getExcludeJobIdList(),
options.getJobList(),
options.getExcludeJobList()
));
out.output(response.string());
return;
}

// Get raw Json.
ResponseBody response = apiCall(api -> api.executionMetricsJSON(
query,
options.getJobIdList(),
options.getExcludeJobIdList(),
options.getJobList(),
options.getExcludeJobList()
));
// Get raw Json.
result = apiCall(api -> api.executionMetrics(
query,
options.getJobIdList(),
options.getExcludeJobIdList(),
options.getJobList(),
options.getExcludeJobList()
));

if(options.isRawJSON()) {
out.output(response.string());
return;
}

result = JSON.readValue(response.string(), new TypeReference<Map<String, Object>>() {});

}

if (result.get("total") == null) {
out.info("No results.");
return;
}

out.info(String.format("Showing stats for a resultset of %s executions.", result.get("total")));
result.forEach((k, v) -> out.output(String.format("%-13s %s", k + ":", v)));
if (!options.isOutputFormat()) {
if (result.getTotal() == null || result.getTotal() < 1) {
out.info("No results.");
return;
}
out.info(String.format("Showing stats for a resultset of %d executions.", result.getTotal()));
out.output(result);
return;
}
out.output(Format.format(options.getOutputFormat(), result, "%", ""));
}


Expand Down

0 comments on commit 412507f

Please sign in to comment.