Please don't post random slapped-together lines of code that couldn't possibly compile to CodeReview. We strongly prefer code that compiles, even if all the dependencies aren't available to us. This method will never compile.
In canonical Java, there is whitespace after a ,
String building is typically cleaner when done with a StringBuilder.
A simple helper method to generate a filter string would greatly clean up the numerous repetitions of string formatting.
More helper methods would make the whole method read more cleanly.
If you need a generally useful filter, instead of just this one method, make a QueryBuilder class to do the heavy lifting.
Helper methods would look something like:
public List<FluxTable> getAggregatedValueWithTag(String type, String fieldName, String func, TestingTag tag) {
QueryApi queryApi = influxDBClient.getQueryApi();
String flux = new StringBuilder()
.append(from(bucket))
.append(range(0))
.append(filter("_measurement", type))
.append(filter("packageName", tag.getPackageName()))
.append(filter("deviceID", tag.getDeviceID()))
.append(filter("runTime", tag.getRunTime()))
.append(filter("_field", fieldName))
.append(aggregateWindow("1m", func, false))
.append(yield(func))
.toString();
return queryApi.query(flux, org);
}
private String from(String bucket) {
return String.format("from(bucket: \"%s\")\n", bucket);
}
private String range(int start) {
return String.format(" |> range(start: %d)\n", start);
}
private String filter(String name, String value) {
if (value == null) {
return "";
}
return String.format(" |> filter(fn: (r) => r[\"%s\"] == \"%s\")\n", name, value);
}
private String aggregateWindow(String frequency, String function, boolean createEmpty) {
return String.format(" |> aggregateWindow(every: %s, fn: %s, createEmpty: %b)\n", frequency, function, createEmpty);
}
private String yield(String name) {
return String.format(" |> yield(name: \"%s\")", name);
}
A QueryBuilder class would look something like:
public final class QueryBuilder {
private final StringBuilder query = new StringBuilder();
public static QueryBuilder fromBucket(String bucket) {
return new QueryBuilder("bucket", bucket);
}
private QueryBuilder(String fromType, String from) {
append("from(%s: \"%s\")\n", fromType, from);
}
public QueryBuilder range(int start) {
append(" |> range(start: %d)\n", start);
return this;
}
public QueryBuilder filter(String name, String value) {
if (value != null) {
append(" |> filter(fn: (r) => r[\"%s\"] == \"%s\")\n", name, value);
}
return this;
}
public QueryBuilder aggregateWindow(String frequency, String function, boolean createEmpty) {
append(" |> aggregateWindow(every: %s, fn: %s, createEmpty: %b)\n", frequency, function, createEmpty);
return this;
}
public QueryBuilder yield(String name) {
append(" |> yield(name: \"%s\")", name);
return this;
}
public String toString() {
return query.toString();
}
private void append(String formatString, Object... values) {
query.append(String.format(formatString, values));
}
}
and the calling method would then look like:
public List<FluxTable> getAggregatedValueWithTag(String type, String fieldName, String func, TestingTag tag) {
QueryApi queryApi = influxDBClient.getQueryApi();
String flux = QueryBuilder.fromBucket(bucket)
.range(0)
.filter("_measurement", type)
.filter("packageName", tag.getPackageName())
.filter("deviceID", tag.getDeviceID())
.filter("runTime", tag.getRunTime())
.filter("_field", fieldName)
.aggregateWindow("1m", func, false)
.yield(func)
.toString();
return queryApi.query(flux, org);
}