Skip to content

Commit

Permalink
Merge pull request #56 from yunionio/monitor-group-by
Browse files Browse the repository at this point in the history
feat(monitor): add groupBy helper
  • Loading branch information
zexi authored Dec 26, 2023
2 parents 6166333 + 4d2738e commit 8c5f582
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.yunionyun.mcp</groupId>
<artifactId>mcclient</artifactId>
<version>3.2.11</version>
<version>3.2.13</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,39 @@ public MetricQueryInput addMetric(MetricQuery q) {
return this;
}


private MetricQueryInput addMetric(String measurement, String field, String alias, String filterKey, String filterValue) {
MetricQuery query = MetricQuery.newMetricQuery(measurement, field, alias, filterKey, filterValue);
private MetricQueryInput addMetric(String measurement, String field, String alias, String filterKey, String filterValue, List<MetricQueryPart> groupBy) {
List<MetricQueryModelTag> tags = new ArrayList<>();
if (filterKey != null && !filterKey.isEmpty()) {
tags.add(MetricQueryModelTag.Equal(filterKey, filterValue));
}
MetricQuery query = MetricQuery.newMetricQuery(measurement, field, alias, tags, groupBy);
return this.addMetric(query);
}

public MetricQueryInput addMetric(String measurement, String field, String alias, String filterKey, String filterValue) {
return this.addMetric(measurement, field, alias, filterKey, filterValue, null);
}

public MetricQueryInput addMetric(String measurement, String field) {
return this.addMetric(measurement, field, "", "", "");
}

public MetricQueryInput addMetric(String measurement, String field, String filterKey, String filterValue, List<MetricQueryPart> groupBy) {
return this.addMetric(measurement, field, "", filterKey, filterValue, groupBy);
}

public MetricQueryInput addMetric(String measurement, String field, String filterKey, String filterValue) {
return this.addMetric(measurement, field, "", filterKey, filterValue);
}

public static List<MetricQueryPart> GroupBy(String... fields) {
List<MetricQueryPart> parts = new ArrayList<>();
for (String field : fields) {
parts.add(new MetricQueryPart(MetricQueryPart.TYPE_TAG, field));
}
return parts;
}

public String getFrom() {
return from;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class MetricQueryPart {
public static final String TYPE_FIELD = "field";
public static final String TYPE_MEAN = "mean";
public static final String TYPE_ALIAS = "alias";
public static final String TYPE_TAG = "tag";

private String type;
private List<String> params;
Expand All @@ -15,7 +16,7 @@ private MetricQueryPart(String type) {
this.type = type;
}

private MetricQueryPart(String type, String val) {
public MetricQueryPart(String type, String val) {
List<String> params = new ArrayList<>();
params.add(val);
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import junit.framework.TestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UnifiedMonitorManagerTest extends TestCase {

Expand All @@ -22,7 +24,7 @@ public void test() {
// UnifiedMonitorManager man = new UnifiedMonitorManager();
// Session session = cli.newSession("YunionHQ", null, EndpointType.PublicURL, token);
// MetricQueryInput input = MetricQueryInput.newQuery("1h", "5m");
// input.addMetric("vm_cpu", "usage_active", "vm_id", "021e9e2b-ae4e-4bc9-8a29-d80225db379e");
// input.addMetric("disk", "free", "host", "office-controller", MetricQueryInput.GroupBy("path", "host", "host_ip"));
// JSONObject jsonObject = man.PerformQuery(session, input);
// System.out.printf("jsonObject: " + jsonObject.toJSONString(jsonObject, SerializerFeature.PrettyFormat));
// } catch (JSONClientException e) {
Expand Down

0 comments on commit 8c5f582

Please sign in to comment.