|
| 1 | +/*Copyright ©2024 APIJSON(https://github.com/APIJSON) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
| 15 | +package apijson.influxdb; |
| 16 | + |
| 17 | +import apijson.JSONResponse; |
| 18 | +import apijson.NotNull; |
| 19 | +import apijson.RequestMethod; |
| 20 | +import apijson.StringUtil; |
| 21 | +import apijson.orm.AbstractParser; |
| 22 | +import apijson.orm.SQLConfig; |
| 23 | +import com.alibaba.fastjson.JSONObject; |
| 24 | +import org.influxdb.BatchOptions; |
| 25 | +import org.influxdb.InfluxDB; |
| 26 | +import org.influxdb.InfluxDBFactory; |
| 27 | +import org.influxdb.dto.Query; |
| 28 | +import org.influxdb.dto.QueryResult; |
| 29 | + |
| 30 | +import java.math.BigDecimal; |
| 31 | +import java.sql.SQLException; |
| 32 | +import java.util.*; |
| 33 | + |
| 34 | +import static apijson.orm.AbstractSQLExecutor.KEY_RAW_LIST; |
| 35 | + |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Lemon |
| 39 | + * @see DemoSQLExecutor 重写 execute 方法: |
| 40 | + * \@Override |
| 41 | + * public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception { |
| 42 | + * if (config.isMilvus()) { |
| 43 | + * return MilvusUtil.execute(config, null, unknownType); |
| 44 | + * } |
| 45 | + * |
| 46 | + * return super.execute(config, unknownType); |
| 47 | + * } |
| 48 | + */ |
| 49 | +public class InfluxDBUtil { |
| 50 | + public static final String TAG = "MilvusUtil"; |
| 51 | + |
| 52 | + public static <T> JSONObject execute(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception { |
| 53 | + if (RequestMethod.isQueryMethod(config.getMethod())) { |
| 54 | + List<JSONObject> list = executeQuery(config, sql, unknownType); |
| 55 | + JSONObject result = list == null || list.isEmpty() ? null : list.get(0); |
| 56 | + if (result == null) { |
| 57 | + result = new JSONObject(true); |
| 58 | + } |
| 59 | + |
| 60 | + if (list != null && list.size() > 1) { |
| 61 | + result.put(KEY_RAW_LIST, list); |
| 62 | + } |
| 63 | + |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + return executeUpdate(config, sql); |
| 68 | + } |
| 69 | + |
| 70 | + public static <T> int execUpdate(SQLConfig<T> config, String sql) throws Exception { |
| 71 | + JSONObject result = executeUpdate(config, sql); |
| 72 | + return result.getIntValue(JSONResponse.KEY_COUNT); |
| 73 | + } |
| 74 | + |
| 75 | + public static <T> JSONObject executeUpdate(SQLConfig<T> config, String sql) throws Exception { |
| 76 | + InfluxDB influxDB = InfluxDBFactory.connect(config.getDBUri(), config.getDBAccount(), config.getDBPassword()); |
| 77 | + influxDB.setDatabase(config.getSchema()); |
| 78 | + |
| 79 | + influxDB.enableBatch( |
| 80 | + BatchOptions.DEFAULTS |
| 81 | + .threadFactory(runnable -> { |
| 82 | + Thread thread = new Thread(runnable); |
| 83 | + thread.setDaemon(true); |
| 84 | + return thread; |
| 85 | + }) |
| 86 | + ); |
| 87 | + |
| 88 | + Runtime.getRuntime().addShutdownHook(new Thread(influxDB::close)); |
| 89 | + |
| 90 | + influxDB.write(StringUtil.isEmpty(sql) ? config.getSQL(false) : sql); |
| 91 | + |
| 92 | + JSONObject result = AbstractParser.newSuccessResult(); |
| 93 | + |
| 94 | + RequestMethod method = config.getMethod(); |
| 95 | + if (method == RequestMethod.POST) { |
| 96 | + List<List<Object>> values = config.getValues(); |
| 97 | + result.put(JSONResponse.KEY_COUNT, values == null ? 0 : values.size()); |
| 98 | + } else { |
| 99 | + String idKey = config.getIdKey(); |
| 100 | + Object id = config.getId(); |
| 101 | + Object idIn = config.getIdIn(); |
| 102 | + if (id != null) { |
| 103 | + result.put(idKey, id); |
| 104 | + } |
| 105 | + if (idIn != null) { |
| 106 | + result.put(idKey + "[]", idIn); |
| 107 | + } |
| 108 | + |
| 109 | + if (method == RequestMethod.PUT) { |
| 110 | + Map<String, Object> content = config.getContent(); |
| 111 | + result.put(JSONResponse.KEY_COUNT, content == null ? 0 : content.size()); |
| 112 | + } else { |
| 113 | + result.put(JSONResponse.KEY_COUNT, id == null && idIn instanceof Collection ? ((Collection<?>) idIn).size() : 1); // FIXME 直接 SQLAuto 传 Flux/InfluxQL INSERT 如何取数量? |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return result; |
| 118 | + } |
| 119 | + |
| 120 | + public static <T> List<JSONObject> executeQuery(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception { |
| 121 | + InfluxDB influxDB = InfluxDBFactory.connect(config.getDBUri(), config.getDBAccount(), config.getDBPassword()); |
| 122 | + influxDB.setDatabase(config.getSchema()); |
| 123 | + QueryResult qr = influxDB.query(new Query(StringUtil.isEmpty(sql) ? config.getSQL(false) : sql)); |
| 124 | + |
| 125 | + String err = qr == null ? null : qr.getError(); |
| 126 | + if (StringUtil.isNotEmpty(err, true)) { |
| 127 | + throw new SQLException(err); |
| 128 | + } |
| 129 | + |
| 130 | + List<QueryResult.Result> list = qr == null ? null : qr.getResults(); |
| 131 | + if (list == null) { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + List<JSONObject> resultList = new ArrayList<>(); |
| 136 | + |
| 137 | + for (int i = 0; i < list.size(); i++) { |
| 138 | + QueryResult.Result qyrt = list.get(i); |
| 139 | + List<QueryResult.Series> seriesList = qyrt.getSeries(); |
| 140 | + if (seriesList == null || seriesList.isEmpty()) { |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + for (int j = 0; j < seriesList.size(); j++) { |
| 145 | + QueryResult.Series series = seriesList.get(j); |
| 146 | + List<List<Object>> valuesList = series.getValues(); |
| 147 | + if (valuesList == null || valuesList.isEmpty()) { |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + List<String> columns = series.getColumns(); |
| 152 | + for (int k = 0; k < valuesList.size(); k++) { |
| 153 | + |
| 154 | + List<Object> values = valuesList.get(k); |
| 155 | + JSONObject obj = new JSONObject(true); |
| 156 | + if (values != null) { |
| 157 | + for (int l = 0; l < values.size(); l++) { |
| 158 | + obj.put(columns.get(l), values.get(l)); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + resultList.add(obj); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return resultList; |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments