Skip to content

Commit 1382f3e

Browse files
authored
Merge pull request #40 from xiaowing/master
add the "delta" support for SubQuery
2 parents b4e449a + 61a2deb commit 1382f3e

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.aliyun.hitsdb.client.value.request;
2+
3+
/**
4+
* Created By jianhong.hjh
5+
* Date: 2019/08/30
6+
*/
7+
public class DeltaOptions {
8+
9+
private Boolean counter;
10+
11+
private Boolean dropResets;
12+
13+
private Long counterMax;
14+
15+
public static class Builder {
16+
17+
private Boolean counter;
18+
19+
private Boolean dropResets;
20+
21+
private Long counterMax;
22+
23+
public Builder() {
24+
25+
}
26+
27+
public Builder counter(boolean counter) {
28+
this.counter = counter;
29+
return this;
30+
}
31+
32+
public Builder dropResets(boolean dropResets) {
33+
this.dropResets = dropResets;
34+
return this;
35+
}
36+
37+
public Builder counterMax(long counterMax) {
38+
this.counterMax = counterMax;
39+
return this;
40+
}
41+
42+
public DeltaOptions build() {
43+
DeltaOptions rateOptions = new DeltaOptions();
44+
if (counter != null) {
45+
rateOptions.counter = counter;
46+
}
47+
if (dropResets != null) {
48+
rateOptions.dropResets = dropResets;
49+
}
50+
if (counterMax != null) {
51+
rateOptions.counterMax = counterMax;
52+
}
53+
return rateOptions;
54+
}
55+
}
56+
57+
58+
public static Builder newBuilder() {
59+
return new Builder();
60+
}
61+
62+
public Boolean getCounter() {
63+
return counter;
64+
}
65+
66+
public void setCounter(Boolean counter) {
67+
this.counter = counter;
68+
}
69+
70+
public Boolean getDropResets() {
71+
return dropResets;
72+
}
73+
74+
public void setDropResets(Boolean dropResets) {
75+
this.dropResets = dropResets;
76+
}
77+
78+
public Long getCounterMax() {
79+
return counterMax;
80+
}
81+
82+
public void setCounterMax(Long counterMax) {
83+
this.counterMax = counterMax;
84+
}
85+
}

src/main/java/com/aliyun/hitsdb/client/value/request/SubQuery.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public class SubQuery {
1919
private String downsample;
2020
private Boolean rate;
2121
private RateOptions rateOptions;
22+
/**
23+
* the delta field of SubQuery is supported at the Server side from v.2.5.9
24+
* @since v0.2.5
25+
*/
26+
private Boolean delta;
27+
private DeltaOptions deltaOptions;
28+
2229
private Map<String, String> tags;
2330
private Granularity granularityType;
2431
private String granularity;
@@ -36,6 +43,8 @@ public static class Builder {
3643
private String downsample;
3744
private Boolean rate;
3845
private RateOptions rateOptions;
46+
private Boolean delta;
47+
private DeltaOptions deltaOptions;
3948
private Integer limit;
4049
private Integer offset;
4150
private String dpValue;
@@ -64,6 +73,17 @@ public Builder rate(RateOptions rateOptions) {
6473
return this;
6574
}
6675

76+
public Builder delta() {
77+
this.delta = true;
78+
return this;
79+
}
80+
81+
public Builder delta(DeltaOptions deltaOptions) {
82+
this.delta = true;
83+
this.deltaOptions = deltaOptions;
84+
return this;
85+
}
86+
6787
public Builder limit() {
6888
this.limit = 0;
6989
return this;
@@ -192,6 +212,13 @@ public Builder rate(Boolean rate) {
192212
return this;
193213
}
194214

215+
public Builder delta(Boolean delta) {
216+
if (delta != null) {
217+
this.delta = delta;
218+
}
219+
return this;
220+
}
221+
195222
public Builder limit(Integer limit) {
196223
if (limit != null) {
197224
this.limit = limit;
@@ -294,10 +321,18 @@ public SubQuery build() {
294321
subQuery.downsample = this.downsample;
295322
subQuery.metric = this.metric;
296323
subQuery.tags = this.tags;
324+
325+
if ((this.delta != null) && (this.delta == true) && (this.rate != null) && (this.rate == true)) {
326+
throw new IllegalArgumentException("\"delta\" and \"rate\" cannot be specified at the same time");
327+
}
297328
subQuery.rate = this.rate;
298329
if (this.rateOptions != null) {
299330
subQuery.rateOptions = this.rateOptions;
300331
}
332+
subQuery.delta = this.delta;
333+
if (this.deltaOptions != null) {
334+
subQuery.deltaOptions = this.deltaOptions;
335+
}
301336
subQuery.granularityType = this.granularityType;
302337
subQuery.realTimeSeconds = this.realTimeSeconds;
303338
subQuery.explicitTags = this.explicitTags;
@@ -442,4 +477,12 @@ public String getDpValue() {
442477
public String getPreDpValue() {
443478
return preDpValue;
444479
}
480+
481+
public Boolean getDelta() {
482+
return delta;
483+
}
484+
485+
public DeltaOptions getDeltaOptions() {
486+
return deltaOptions;
487+
}
445488
}

src/test/java/com/aliyun/hitsdb/client/value/TestQuery.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.HashMap;
77
import java.util.Map;
88

9+
import com.aliyun.hitsdb.client.value.request.DeltaOptions;
910
import org.junit.Assert;
1011
import org.junit.Test;
1112

@@ -255,4 +256,40 @@ public void testQueryMsResolution() throws ParseException {
255256
Assert.assertEquals(json,
256257
"{\"end\":1501564455,\"msResolution\":true,\"queries\":[{\"aggregator\":\"sum\",\"index\":0,\"metric\":\"test1\",\"rate\":true,\"tags\":{\"tagk1\":\"tagv1\",\"tagk2\":\"tagv2\"}},{\"aggregator\":\"avg\",\"index\":1,\"metric\":\"test2\",\"rate\":true,\"tags\":{\"tagk1\":\"tagv1\",\"tagk2\":\"tagv2\"}}],\"start\":1501560855}");
257258
}
259+
260+
@Test
261+
public void testQueryDelta() throws ParseException {
262+
String strDate = "2017-08-01 13:14:15";
263+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
264+
Date date = sdf.parse(strDate);
265+
266+
int endTime = (int) (date.getTime() / 1000);
267+
int startTime = endTime - 60 * 60;
268+
269+
SubQuery subQuery1 = SubQuery.metric("test1").aggregator(Aggregator.NONE).delta().tag("tagk1", "tagv1")
270+
.tag("tagk2", "tagv2").build();
271+
SubQuery subQuery2 = SubQuery.metric("test1").aggregator(Aggregator.NONE)
272+
.delta(DeltaOptions.newBuilder().counter(true).counterMax(100).dropResets(true).build())
273+
.tag("tagk1", "tagv1").tag("tagk2", "tagv2").build();
274+
275+
Query query = Query.timeRange(startTime, endTime)
276+
.sub(subQuery1).sub(subQuery2).build();
277+
String json = query.toJSON();
278+
Assert.assertEquals("{\"end\":1501564455,\"queries\":[{\"aggregator\":\"none\",\"delta\":true,\"index\":0,\"metric\":\"test1\",\"tags\":{\"tagk1\":\"tagv1\",\"tagk2\":\"tagv2\"}},{\"aggregator\":\"none\",\"delta\":true,\"deltaOptions\":{\"counter\":true,\"counterMax\":100,\"dropResets\":true},\"index\":1,\"metric\":\"test1\",\"tags\":{\"tagk1\":\"tagv1\",\"tagk2\":\"tagv2\"}}],\"start\":1501560855}",
279+
json);
280+
}
281+
282+
@Test(expected = IllegalArgumentException.class)
283+
public void testQueryDeltaAndRate() throws ParseException {
284+
String strDate = "2017-08-01 13:14:15";
285+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
286+
Date date = sdf.parse(strDate);
287+
288+
int endTime = (int) (date.getTime() / 1000);
289+
int startTime = endTime - 60 * 60;
290+
291+
SubQuery subQuery1 = SubQuery.metric("test1").aggregator(Aggregator.NONE).delta().rate().tag("tagk1", "tagv1")
292+
.tag("tagk2", "tagv2").build();
293+
294+
}
258295
}

0 commit comments

Comments
 (0)