Skip to content

Commit a28c488

Browse files
committed
fix: add precision in query method
Signed-off-by: zhiheng123 <[email protected]>
1 parent 1c74863 commit a28c488

File tree

6 files changed

+149
-7
lines changed

6 files changed

+149
-7
lines changed

opengemini-client-api/src/main/java/io/opengemini/client/api/Precision.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,27 @@
66

77
@Getter
88
public enum Precision {
9-
PRECISIONNANOSECOND("PrecisionNanoSecond", TimeUnit.NANOSECONDS),
9+
PRECISIONNANOSECOND("PrecisionNanoSecond", TimeUnit.NANOSECONDS, "ns"),
1010

11-
PRECISIONMICROSECOND("PrecisionMicrosecond", TimeUnit.MICROSECONDS),
11+
PRECISIONMICROSECOND("PrecisionMicrosecond", TimeUnit.MICROSECONDS, "u"),
1212

13-
PRECISIONMILLISECOND("PrecisionMillisecond", TimeUnit.MILLISECONDS),
13+
PRECISIONMILLISECOND("PrecisionMillisecond", TimeUnit.MILLISECONDS, "ms"),
1414

15-
PRECISIONSECOND("PrecisionSecond", TimeUnit.SECONDS),
15+
PRECISIONSECOND("PrecisionSecond", TimeUnit.SECONDS, "s"),
1616

17-
PRECISIONMINUTE("PrecisionMinute", TimeUnit.MINUTES),
17+
PRECISIONMINUTE("PrecisionMinute", TimeUnit.MINUTES, "m"),
1818

19-
PRECISIONHOUR("PrecisionHour", TimeUnit.HOURS);
19+
PRECISIONHOUR("PrecisionHour", TimeUnit.HOURS, "h");
2020

2121
private final String description;
2222

2323
private final TimeUnit timeUnit;
2424

25-
Precision(String description, TimeUnit timeUnit) {
25+
private final String epoch;
26+
27+
Precision(String description, TimeUnit timeUnit, String epoch) {
2628
this.description = description;
2729
this.timeUnit = timeUnit;
30+
this.epoch = epoch;
2831
}
2932
}

opengemini-client-api/src/main/java/io/opengemini/client/api/Query.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,18 @@ public class Query {
2323
*/
2424
private String retentionPolicy;
2525

26+
/*
27+
* the precision of the time in query result
28+
*/
29+
private Precision precision;
30+
2631
public Query(String command) {
2732
this.command = command;
2833
}
2934

35+
public Query(String command, String database, String retentionPolicy) {
36+
this.command = command;
37+
this.database = database;
38+
this.retentionPolicy = retentionPolicy;
39+
}
3040
}

opengemini-client-asynchttpclient/src/test/java/io/opengemini/client/asynchttpclient/OpenGeminiAsyncHttpClientTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import io.opengemini.client.api.OpenGeminiException;
55
import io.opengemini.client.api.Point;
66
import io.opengemini.client.api.Pong;
7+
import io.opengemini.client.api.Precision;
78
import io.opengemini.client.api.Query;
89
import io.opengemini.client.api.QueryResult;
910
import io.opengemini.client.api.RetentionPolicy;
1011
import io.opengemini.client.api.RpConfig;
1112
import io.opengemini.client.api.Series;
13+
import lombok.SneakyThrows;
1214
import org.junit.jupiter.api.AfterEach;
1315
import org.junit.jupiter.api.Assertions;
1416
import org.junit.jupiter.api.BeforeEach;
@@ -288,4 +290,45 @@ private static Point testPoint(String measurementName, int valueIndex, int field
288290
testPoint.setFields(fields);
289291
return testPoint;
290292
}
293+
294+
@SneakyThrows
295+
@Test
296+
void testQueryPrecision() {
297+
String databaseName = "query_precision_0001";
298+
CompletableFuture<Void> createdb = openGeminiAsyncHttpClient.createDatabase(databaseName);
299+
createdb.get();
300+
301+
String measurementName = "query_precision_ms_0001";
302+
Point testPoint1 = testPoint(measurementName, 1, 1);
303+
Point testPoint2 = testPoint(measurementName, 2, 1);
304+
Point testPoint3 = testPoint(measurementName, 3, 1);
305+
306+
CompletableFuture<Void> writeRsp = openGeminiAsyncHttpClient.write(databaseName,
307+
Arrays.asList(testPoint1, testPoint2, testPoint3));
308+
writeRsp.get();
309+
Thread.sleep(3000);
310+
311+
Query selectQuery = new Query("select * from " + measurementName, databaseName, "");
312+
CompletableFuture<QueryResult> rst = openGeminiAsyncHttpClient.query(selectQuery);
313+
QueryResult queryResult = rst.get();
314+
315+
Series x = queryResult.getResults().get(0).getSeries().get(0);
316+
Object timeValue = x.getValues().get(0).get(0);
317+
Assertions.assertInstanceOf(String.class, timeValue);
318+
String timeValueStr = (String) timeValue;
319+
Assertions.assertTrue(timeValueStr.startsWith("20") && timeValueStr.endsWith("Z"));
320+
321+
selectQuery = new Query("select * from " + measurementName, databaseName, "", Precision.PRECISIONNANOSECOND);
322+
rst = openGeminiAsyncHttpClient.query(selectQuery);
323+
queryResult = rst.get();
324+
325+
x = queryResult.getResults().get(0).getSeries().get(0);
326+
timeValue = x.getValues().get(0).get(0);
327+
Assertions.assertInstanceOf(Long.class, timeValue);
328+
long timeValueDouble = (Long) timeValue;
329+
Assertions.assertTrue(timeValueDouble > 1724778721457052741L);
330+
331+
CompletableFuture<Void> dropdb = openGeminiAsyncHttpClient.dropDatabase(databaseName);
332+
dropdb.get();
333+
}
291334
}

opengemini-client-common/src/main/java/io/opengemini/client/common/BaseClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ protected String getQueryUrl(Query query) {
5858
if (query.getRetentionPolicy() != null) {
5959
queryUrl += "&rp=" + query.getRetentionPolicy();
6060
}
61+
62+
if (query.getPrecision() != null) {
63+
queryUrl += "&epoch=" + query.getPrecision().getEpoch();
64+
}
6165
return queryUrl;
6266
}
6367
}

opengemini-client-jdk/src/test/java/io/opengemini/client/jdk/OpenGeminiJdkClientTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.opengemini.client.api.OpenGeminiException;
77
import io.opengemini.client.api.Point;
88
import io.opengemini.client.api.Pong;
9+
import io.opengemini.client.api.Precision;
910
import io.opengemini.client.api.Query;
1011
import io.opengemini.client.api.QueryResult;
1112
import io.opengemini.client.api.RetentionPolicy;
@@ -284,4 +285,41 @@ void ping() throws Exception {
284285
Assertions.assertNotNull(pong.getVersion());
285286
}
286287

288+
@SneakyThrows
289+
@Test
290+
void testQueryPrecision() {
291+
String databaseName = "query_precision_0001";
292+
CompletableFuture<Void> createdb = openGeminiJdkClient.createDatabase(databaseName);
293+
createdb.get();
294+
295+
String measurementName = "query_precision_ms_0001";
296+
Point testPoint = generalTestPoint(measurementName, 1, 1);
297+
298+
CompletableFuture<Void> writeRsp = openGeminiJdkClient.write(databaseName, testPoint);
299+
writeRsp.get();
300+
Thread.sleep(3000);
301+
302+
Query selectQuery = new Query("select * from " + measurementName, databaseName, "");
303+
CompletableFuture<QueryResult> rst = openGeminiJdkClient.query(selectQuery);
304+
QueryResult queryResult = rst.get();
305+
306+
Series x = queryResult.getResults().get(0).getSeries().get(0);
307+
Object timeValue = x.getValues().get(0).get(0);
308+
Assertions.assertInstanceOf(String.class, timeValue);
309+
String timeValueStr = (String) timeValue;
310+
Assertions.assertTrue(timeValueStr.startsWith("20") && timeValueStr.endsWith("Z"));
311+
312+
selectQuery = new Query("select * from " + measurementName, databaseName, "", Precision.PRECISIONNANOSECOND);
313+
rst = openGeminiJdkClient.query(selectQuery);
314+
queryResult = rst.get();
315+
316+
x = queryResult.getResults().get(0).getSeries().get(0);
317+
timeValue = x.getValues().get(0).get(0);
318+
Assertions.assertInstanceOf(Long.class, timeValue);
319+
long timeValueDouble = (Long) timeValue;
320+
Assertions.assertTrue(timeValueDouble > 1724778721457052741L);
321+
322+
CompletableFuture<Void> dropdb = openGeminiJdkClient.dropDatabase(databaseName);
323+
dropdb.get();
324+
}
287325
}

opengemini-client-okhttp/src/test/java/io/opengemini/client/okhttp/OpenGeminiOkhttpClientTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import io.opengemini.client.api.OpenGeminiException;
55
import io.opengemini.client.api.Point;
66
import io.opengemini.client.api.Pong;
7+
import io.opengemini.client.api.Precision;
78
import io.opengemini.client.api.Query;
89
import io.opengemini.client.api.QueryResult;
910
import io.opengemini.client.api.RetentionPolicy;
1011
import io.opengemini.client.api.RpConfig;
1112
import io.opengemini.client.api.Series;
13+
import lombok.SneakyThrows;
1214
import org.junit.jupiter.api.AfterEach;
1315
import org.junit.jupiter.api.Assertions;
1416
import org.junit.jupiter.api.BeforeEach;
@@ -287,4 +289,46 @@ private static Point testPoint(String measurementName, int valueIndex, int field
287289
testPoint.setFields(fields);
288290
return testPoint;
289291
}
292+
293+
294+
@SneakyThrows
295+
@Test
296+
void testQueryPrecision() {
297+
String databaseName = "query_precision_0001";
298+
CompletableFuture<Void> createdb = openGeminiOkhttpClient.createDatabase(databaseName);
299+
createdb.get();
300+
301+
String measurementName = "query_precision_ms_0001";
302+
Point testPoint1 = testPoint(measurementName, 1, 1);
303+
Point testPoint2 = testPoint(measurementName, 2, 1);
304+
Point testPoint3 = testPoint(measurementName, 3, 1);
305+
306+
CompletableFuture<Void> writeRsp = openGeminiOkhttpClient.write(databaseName,
307+
Arrays.asList(testPoint1, testPoint2, testPoint3));
308+
writeRsp.get();
309+
Thread.sleep(3000);
310+
311+
Query selectQuery = new Query("select * from " + measurementName, databaseName, "");
312+
CompletableFuture<QueryResult> rst = openGeminiOkhttpClient.query(selectQuery);
313+
QueryResult queryResult = rst.get();
314+
315+
Series x = queryResult.getResults().get(0).getSeries().get(0);
316+
Object timeValue = x.getValues().get(0).get(0);
317+
Assertions.assertInstanceOf(String.class, timeValue);
318+
String timeValueStr = (String) timeValue;
319+
Assertions.assertTrue(timeValueStr.startsWith("20") && timeValueStr.endsWith("Z"));
320+
321+
selectQuery = new Query("select * from " + measurementName, databaseName, "", Precision.PRECISIONNANOSECOND);
322+
rst = openGeminiOkhttpClient.query(selectQuery);
323+
queryResult = rst.get();
324+
325+
x = queryResult.getResults().get(0).getSeries().get(0);
326+
timeValue = x.getValues().get(0).get(0);
327+
Assertions.assertInstanceOf(Long.class, timeValue);
328+
long timeValueDouble = (Long) timeValue;
329+
Assertions.assertTrue(timeValueDouble > 1724778721457052741L);
330+
331+
CompletableFuture<Void> dropdb = openGeminiOkhttpClient.dropDatabase(databaseName);
332+
dropdb.get();
333+
}
290334
}

0 commit comments

Comments
 (0)