|
@@ -10,9 +10,14 @@ import com.influxdb.client.write.Point;
|
|
|
import com.influxdb.query.FluxRecord;
|
|
|
import com.influxdb.query.FluxTable;
|
|
|
import com.platform.common.exception.BusinessException;
|
|
|
+import com.platform.common.util.BeanConverterUtil;
|
|
|
import com.platform.common.util.DateUtils;
|
|
|
+import com.platform.dao.entity.remote.RemoteOpc;
|
|
|
import com.platform.dao.influxdb.InfluxDBService;
|
|
|
+import com.platform.dao.influxdb.builder.InfluxDbBuilder;
|
|
|
+import com.platform.dao.mapper.remote.RemoteOpcMapper;
|
|
|
import com.platform.dao.util.influxDB.InfluxDBFluxExpression;
|
|
|
+import com.platform.dao.vo.query.remote.RemoteOpcVO;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -21,6 +26,7 @@ import org.springframework.stereotype.Service;
|
|
|
import java.text.ParseException;
|
|
|
import java.time.Instant;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.OffsetDateTime;
|
|
|
import java.time.ZoneOffset;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
@@ -33,15 +39,16 @@ public class InfluxDBServiceImpl implements InfluxDBService {
|
|
|
|
|
|
@Autowired
|
|
|
private InfluxDBClient influxDBClient;
|
|
|
+ @Autowired
|
|
|
+ private RemoteOpcMapper remoteOpcMapper;
|
|
|
|
|
|
/**
|
|
|
* *
|
|
|
- * @param tagName 标签名称:positionNum的值
|
|
|
- * @param fields 字段
|
|
|
+ *
|
|
|
+ * @param vo
|
|
|
*/
|
|
|
@Override
|
|
|
- public void writeOne(String tagName, Map<String, Object> fields) {
|
|
|
-
|
|
|
+ public void writeOne(RemoteOpcVO vo) {
|
|
|
System.out.println("=============开始插入数据=============");
|
|
|
WriteOptions writeOptions = WriteOptions.builder()
|
|
|
.batchSize(5000)
|
|
@@ -51,13 +58,8 @@ public class InfluxDBServiceImpl implements InfluxDBService {
|
|
|
.retryInterval(5000)
|
|
|
.build();
|
|
|
try (WriteApi writeApi = influxDBClient.getWriteApi(writeOptions)) {
|
|
|
- Point point = Point
|
|
|
- .measurement(InfluxDBFluxExpression.measurement)
|
|
|
- .addTag(InfluxDBFluxExpression.tag, tagName)
|
|
|
- .addFields(fields)
|
|
|
- .time(Instant.now(), WritePrecision.NS);
|
|
|
- writeApi.writePoint(InfluxDBFluxExpression.bucket, InfluxDBFluxExpression.org, point);
|
|
|
- }catch(Exception e){
|
|
|
+ writeApi.writePoint(InfluxDBFluxExpression.bucket, InfluxDBFluxExpression.org, InfluxDbBuilder.bulidOnePoint(vo));
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
throw new BusinessException("db写入报错");
|
|
|
|
|
@@ -67,14 +69,23 @@ public class InfluxDBServiceImpl implements InfluxDBService {
|
|
|
/**
|
|
|
* 批量数据写入
|
|
|
*/
|
|
|
- public void writeBatch(List<Point> list){
|
|
|
- /*Point point = Point
|
|
|
- .measurement(InfluxDBFluxExpression.measurement)
|
|
|
- .addTag(InfluxDBFluxExpression.tag, InfluxDBFluxExpression.tag)
|
|
|
- .addField(InfluxDBFluxExpression.field, 23.43234543)
|
|
|
- .time(Instant.now(), WritePrecision.NS);
|
|
|
- list.add(point);*/
|
|
|
- influxDBClient.getWriteApi().writePoints(list);
|
|
|
+ @Override
|
|
|
+ public void writeBatch(List<RemoteOpcVO> resultList) {
|
|
|
+ System.out.println("=============开始插入数据=============");
|
|
|
+ WriteOptions writeOptions = WriteOptions.builder()
|
|
|
+ .batchSize(5000)
|
|
|
+ .flushInterval(1000)
|
|
|
+ .bufferLimit(10000)
|
|
|
+ .jitterInterval(1000)
|
|
|
+ .retryInterval(5000)
|
|
|
+ .build();
|
|
|
+ try (WriteApi writeApi = influxDBClient.getWriteApi(writeOptions)) {
|
|
|
+ writeApi.writePoints(InfluxDBFluxExpression.bucket, InfluxDBFluxExpression.org, InfluxDbBuilder.bulidBatchPoint(resultList));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("db写入报错");
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -84,37 +95,58 @@ public class InfluxDBServiceImpl implements InfluxDBService {
|
|
|
public void delete(LocalDateTime start, LocalDateTime stop) {
|
|
|
DeletePredicateRequest deletePredicateRequest = new DeletePredicateRequest();
|
|
|
// deletePredicateRequest.start(start.atOffset(ZoneOffset.ofHours(0)).minusDays(5));
|
|
|
- deletePredicateRequest.start(start.atOffset(ZoneOffset.ofHours(0)));
|
|
|
- deletePredicateRequest.stop(stop.atOffset(ZoneOffset.ofHours(0)));
|
|
|
+ deletePredicateRequest.start(start.atOffset(ZoneOffset.ofHours(-8)).minusDays(5));
|
|
|
+ deletePredicateRequest.stop(stop.atOffset(ZoneOffset.ofHours(-8)));
|
|
|
influxDBClient.getDeleteApi().delete(deletePredicateRequest, InfluxDBFluxExpression.bucket, InfluxDBFluxExpression.org);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 查询数据: 查询某个点位,某个时间段的数据*
|
|
|
- * @param tagName positionNum值
|
|
|
- * @param start 开始时间
|
|
|
- * @param stop 结束时间
|
|
|
+ * 查询数据: 查询某个点位,某个时间段的数据*,表格数据,图表数据
|
|
|
+ *
|
|
|
+ * @param id positionNum值
|
|
|
+ * @param type 1: 5分钟
|
|
|
*/
|
|
|
@Override
|
|
|
- public void select(String tagName, LocalDateTime start, LocalDateTime stop){
|
|
|
+ public RemoteOpcVO select(String id, Integer type) {
|
|
|
+ RemoteOpc remoteOpc = remoteOpcMapper.selectByPrimaryKey(id);
|
|
|
+ RemoteOpcVO vo = BeanConverterUtil.copyObjectProperties(remoteOpc, RemoteOpcVO.class);
|
|
|
StringBuffer stringBuilder = new StringBuffer();
|
|
|
try {
|
|
|
+
|
|
|
+ LocalDateTime start = LocalDateTime.now().minusHours(8);
|
|
|
+ LocalDateTime stop = LocalDateTime.now().minusHours(8);
|
|
|
+ if (type == 1) {// 5分钟
|
|
|
+ start = start.minusMinutes(5);
|
|
|
+ } else if (type == 2) {// 5分钟
|
|
|
+ start = start.minusMinutes(15);
|
|
|
+ }else if (type == 3) {// 5分钟
|
|
|
+ start = start.minusHours(1);
|
|
|
+ }else if (type == 4) {// 5分钟
|
|
|
+ start = start.minusHours(3);
|
|
|
+ }else if (type == 5) {// 5分钟
|
|
|
+ start = start.minusHours(6);
|
|
|
+ }else if (type == 6) {// 5分钟
|
|
|
+ start = start.minusHours(12);
|
|
|
+ }else if (type == 7) {// 5分钟
|
|
|
+ start = start.minusHours(24);
|
|
|
+ }else if (type == 8) {// 5分钟
|
|
|
+ start = start.minusDays(2);
|
|
|
+ }else if (type == 9) {// 5分钟
|
|
|
+ start = start.minusDays(7);
|
|
|
+ }else{
|
|
|
+ start = start.minusDays(30);
|
|
|
+ }
|
|
|
InfluxDBFluxExpression.appendCommonFlux(stringBuilder, InfluxDBFluxExpression.bucket, InfluxDBFluxExpression.measurement, DateUtils.localDateTimeToUTC(start), DateUtils.localDateTimeToUTC(stop));
|
|
|
} catch (ParseException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- InfluxDBFluxExpression.appendTagFlux(stringBuilder, tagName);
|
|
|
+ InfluxDBFluxExpression.appendTagFlux(stringBuilder, remoteOpc.getPositionNum());
|
|
|
InfluxDBFluxExpression.appendTimeShiftFlux(stringBuilder);
|
|
|
log.info("查询sql :{}", stringBuilder);
|
|
|
// 通过时间分组 查询时间段的数据
|
|
|
- List<FluxTable> tables = influxDBClient.getQueryApi().query(stringBuilder.toString());
|
|
|
- List<Map<String, Object>> list = new ArrayList<>();
|
|
|
- for (FluxTable table : tables) {
|
|
|
- List<FluxRecord> records = table.getRecords();
|
|
|
- for (FluxRecord record : records) {
|
|
|
- log.info("{}---{}---{}---{}", record.getMeasurement(),record.getField(),record.getValue(),record.getTime());
|
|
|
- }
|
|
|
- }
|
|
|
+ List<FluxTable> tables = influxDBClient.getQueryApi().query(stringBuilder.toString(), InfluxDBFluxExpression.org);
|
|
|
+ vo.setDataJsonStr(InfluxDbBuilder.bulidRemoteOpcVO(tables));
|
|
|
+ return vo;
|
|
|
}
|
|
|
|
|
|
/* @Override
|