You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by lt...@apache.org on 2019/12/03 12:24:02 UTC

[incubator-iotdb] branch add_grafana_time_precision created (now 5ce657d)

This is an automated email from the ASF dual-hosted git repository.

lta pushed a change to branch add_grafana_time_precision
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git.


      at 5ce657d  add timestamp precision properties

This branch includes the following new commits:

     new 5ce657d  add timestamp precision properties

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-iotdb] 01/01: add timestamp precision properties

Posted by lt...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

lta pushed a commit to branch add_grafana_time_precision
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git

commit 5ce657d510fd3a4dff4e51cd3252768c0ba93a15
Author: lta <li...@163.com>
AuthorDate: Tue Dec 3 20:23:40 2019 +0800

    add timestamp precision properties
---
 grafana/conf/application.properties                |  5 ++-
 .../iotdb/web/grafana/dao/impl/BasicDaoImpl.java   | 36 ++++++++++++++++++++--
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/grafana/conf/application.properties b/grafana/conf/application.properties
index 2fb9f49..b2b982f 100644
--- a/grafana/conf/application.properties
+++ b/grafana/conf/application.properties
@@ -21,4 +21,7 @@ spring.datasource.url=jdbc:iotdb://127.0.0.1:6667/
 spring.datasource.username=root
 spring.datasource.password=root
 spring.datasource.driver-class-name=org.apache.iotdb.jdbc.IoTDBDriver
-server.port=8888
\ No newline at end of file
+server.port=8888
+# Use this value to set timestamp precision as "ms", "us" or "ns", which must to be same with the timestamp
+# precision of Apache IoTDB engine.
+timestamp_precision=ms
\ No newline at end of file
diff --git a/grafana/src/main/java/org/apache/iotdb/web/grafana/dao/impl/BasicDaoImpl.java b/grafana/src/main/java/org/apache/iotdb/web/grafana/dao/impl/BasicDaoImpl.java
index 6bf195c..fe0e12a 100644
--- a/grafana/src/main/java/org/apache/iotdb/web/grafana/dao/impl/BasicDaoImpl.java
+++ b/grafana/src/main/java/org/apache/iotdb/web/grafana/dao/impl/BasicDaoImpl.java
@@ -18,6 +18,10 @@
  */
 package org.apache.iotdb.web.grafana.dao.impl;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
 import java.sql.ResultSet;
@@ -25,6 +29,7 @@ import java.sql.SQLException;
 import java.time.ZonedDateTime;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Properties;
 import org.apache.iotdb.jdbc.Constant;
 import org.apache.iotdb.tsfile.utils.Pair;
 import org.apache.iotdb.web.grafana.bean.TimeValues;
@@ -45,11 +50,34 @@ public class BasicDaoImpl implements BasicDao {
 
   private static final Logger logger = LoggerFactory.getLogger(BasicDaoImpl.class);
 
+  private static final String CONFIG_PROPERTY_FILE = "application.properties";
+
   private final JdbcTemplate jdbcTemplate;
 
+  private static long TIMESTAMP_RADIX = 1L;
+
   @Autowired
   public BasicDaoImpl(JdbcTemplate jdbcTemplate) {
     this.jdbcTemplate = jdbcTemplate;
+    try (InputStream inputStream = new FileInputStream(new File(CONFIG_PROPERTY_FILE))) {
+      Properties properties = new Properties();
+      properties.load(inputStream);
+      String tsPrecision = properties.getProperty("timestamp_precision", "ms");
+      switch (tsPrecision) {
+        case "us":
+          TIMESTAMP_RADIX = 1000;
+          break;
+        case "ns":
+          TIMESTAMP_RADIX = 1000_000;
+          break;
+        default:
+          TIMESTAMP_RADIX = 1;
+      }
+      logger.info("Use timestamp precision {}", tsPrecision);
+    } catch (IOException e) {
+      logger.error("Can not find properties [timestamp_precision], use default value [ms]");
+      TIMESTAMP_RADIX = 1;
+    }
   }
 
   @Override
@@ -57,7 +85,8 @@ public class BasicDaoImpl implements BasicDao {
     ConnectionCallback<Object> connectionCallback = new ConnectionCallback<Object>() {
       public Object doInConnection(Connection connection) throws SQLException {
         DatabaseMetaData databaseMetaData = connection.getMetaData();
-        ResultSet resultSet = databaseMetaData.getColumns(Constant.CATALOG_TIMESERIES, "root.*", "root.*", null);
+        ResultSet resultSet = databaseMetaData
+            .getColumns(Constant.CATALOG_TIMESERIES, "root.*", "root.*", null);
         logger.info("Start to get timeseries");
         List<String> columnsName = new ArrayList<>();
         while (resultSet.next()) {
@@ -75,7 +104,8 @@ public class BasicDaoImpl implements BasicDao {
     Long from = zonedCovertToLong(timeRange.left);
     Long to = zonedCovertToLong(timeRange.right);
     String sql = "SELECT " + s.substring(s.lastIndexOf('.') + 1) + " FROM root."
-        + s.substring(0, s.lastIndexOf('.')) + " WHERE time > " + from + " and time < " + to;
+        + s.substring(0, s.lastIndexOf('.')) + " WHERE time > " + from * TIMESTAMP_RADIX
+        + " and time < " + to * TIMESTAMP_RADIX;
     logger.info(sql);
     List<TimeValues> rows = null;
     try {
@@ -103,7 +133,7 @@ public class BasicDaoImpl implements BasicDao {
     @Override
     public TimeValues mapRow(ResultSet resultSet, int i) throws SQLException {
       TimeValues tv = new TimeValues();
-      tv.setTime(resultSet.getLong("Time"));
+      tv.setTime(resultSet.getLong("Time") / TIMESTAMP_RADIX);
       String valueString = resultSet.getString(columnName);
       if (valueString != null) {
         if (TRUE_STR.equalsIgnoreCase(valueString)) {