-
Notifications
You must be signed in to change notification settings - Fork 960
/
BasicDaoImpl.java
157 lines (141 loc) · 5.32 KB
/
BasicDaoImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.iotdb.web.grafana.dao.impl;
import org.apache.iotdb.tsfile.utils.Pair;
import org.apache.iotdb.web.grafana.bean.TimeValues;
import org.apache.iotdb.web.grafana.dao.BasicDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Created by dell on 2017/7/17.
*/
@Repository
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
public List<String> getMetaData() {
ConnectionCallback<Object> connectionCallback = new ConnectionCallback<Object>() {
public Object doInConnection(Connection connection) throws SQLException {
Statement statement = connection.createStatement();
statement.execute("show timeseries root.*");
ResultSet resultSet = statement.getResultSet();
logger.info("Start to get timeseries");
List<String> columnsName = new ArrayList<>();
while (resultSet.next()) {
String timeseries = resultSet.getString(1);
columnsName.add(timeseries.substring(5));
}
return columnsName;
}
};
return (List<String>) jdbcTemplate.execute(connectionCallback);
}
@Override
public List<TimeValues> querySeries(String s, Pair<ZonedDateTime, ZonedDateTime> timeRange) {
Long from = zonedCovertToLong(timeRange.left);
Long to = zonedCovertToLong(timeRange.right);
// How many rows will the result have?
String sql = String.format("SELECT %s FROM root.%s WHERE time > %d and time < %d",
s.substring(s.lastIndexOf('.') + 1), s.substring(0, s.lastIndexOf('.')),
from * TIMESTAMP_RADIX, to * TIMESTAMP_RADIX);
logger.info(sql);
List<TimeValues> rows = null;
try {
rows = jdbcTemplate.query(sql, new TimeValuesRowMapper("root." + s));
} catch (Exception e) {
logger.error(e.getMessage());
}
return rows;
}
private Long zonedCovertToLong(ZonedDateTime time) {
return time.toInstant().toEpochMilli();
}
static class TimeValuesRowMapper implements RowMapper<TimeValues> {
static final String TRUE_STR = "true";
static final String FALSE_STR = "false";
String columnName;
TimeValuesRowMapper(String columnName) {
this.columnName = columnName;
}
@Override
public TimeValues mapRow(ResultSet resultSet, int i) throws SQLException {
TimeValues tv = new TimeValues();
tv.setTime(resultSet.getLong("Time") / TIMESTAMP_RADIX);
String valueString = resultSet.getString(columnName);
if (valueString != null) {
if (TRUE_STR.equalsIgnoreCase(valueString)) {
tv.setValue(1);
} else if (FALSE_STR.equalsIgnoreCase(valueString)) {
tv.setValue(0);
} else {
try {
tv.setValue(Float.parseFloat(resultSet.getString(columnName)));
} catch (Exception e) {
logger.error("Can not parse the value {}", resultSet.getString(columnName));
tv.setValue(0);
}
}
}
return tv;
}
}
}