diff --git a/ym-influx/src/main/java/com/cnbm/influx/config/InfluxClient.java b/ym-influx/src/main/java/com/cnbm/influx/config/InfluxClient.java index 5e58b13..0a144ca 100644 --- a/ym-influx/src/main/java/com/cnbm/influx/config/InfluxClient.java +++ b/ym-influx/src/main/java/com/cnbm/influx/config/InfluxClient.java @@ -141,13 +141,18 @@ public enum InfluxClient { public List query(QueryDataParam param){ String measurement = param.getMeasurement(); - List dropedTagNames = param.getDropedTagNames(); - Range range = param.getRange(); + + String bucket = param.getBucket(); - PageInfo pageInfo = param.getPageInfo(); + String flux = "from(bucket:\""+bucket+"\")"; - flux += "|> range(start: "+range.getBegin()+",stop:"+range.getEnd()+")"; + + if(param.getRange() != null){ + Range range = param.getRange(); + flux += "|> range(start: "+range.getBegin()+",stop:"+range.getEnd()+")"; + } + flux += "|> filter(fn: (r) => r[\"_measurement\"] == \""+measurement+"\")"; if(param.getTags()!=null && param.getTags().size()>0){ @@ -161,12 +166,16 @@ public enum InfluxClient { //调整时区,查询出的结果 +8个小时 //flux += "|> timeShift(duration: 8h)"; - - for(String dropName:dropedTagNames){ - flux += "|> drop(columns: [\""+dropName+"\"])"; + if(param.getDropedTagNames() != null){ + List dropedTagNames = param.getDropedTagNames(); + for(String dropName:dropedTagNames){ + flux += "|> drop(columns: [\""+dropName+"\"])"; + } } + flux += "|> sort(columns: [\"_time\"], desc: true)"; - if(pageInfo!=null){ + if(param.getPageInfo() != null){ + PageInfo pageInfo = param.getPageInfo(); flux += "|> limit(n: "+pageInfo.getSize()+", offset: "+(pageInfo.getCurrent()-1)* pageInfo.getSize()+")"; } return queryApi.query(flux); diff --git a/ym-influx/src/main/java/com/cnbm/influx/controller/InfluxData.java b/ym-influx/src/main/java/com/cnbm/influx/controller/InfluxData.java new file mode 100644 index 0000000..1d22bd0 --- /dev/null +++ b/ym-influx/src/main/java/com/cnbm/influx/controller/InfluxData.java @@ -0,0 +1,25 @@ +package com.cnbm.influx.controller; + +import com.cnbm.influx.template.Event; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.time.Instant; +import java.util.List; + +/** + * @Desc: "" + * @Author: caixiang + * @DATE: 2022/12/9 15:53 + */ +@Data +@ApiModel(value = "Events") +public class InfluxData { + @ApiModelProperty("ID (transationId、argName、argValue、time 必填)") + private List events; + + @ApiModelProperty("measurement(相当于mysql 中的table,一台工艺设备一个measurement)") + private String measurement; + +} diff --git a/ym-influx/src/main/java/com/cnbm/influx/controller/SPCController.java b/ym-influx/src/main/java/com/cnbm/influx/controller/SPCController.java new file mode 100644 index 0000000..ebd608c --- /dev/null +++ b/ym-influx/src/main/java/com/cnbm/influx/controller/SPCController.java @@ -0,0 +1,49 @@ +package com.cnbm.influx.controller; + +import com.cnbm.common.spc.util.DataUtils; +import com.cnbm.common.utils.Result; +import com.cnbm.influx.config.InfluxClient; +import com.cnbm.influx.constant.Constant; +import com.cnbm.influx.param.QueryDataParam; +import com.cnbm.influx.param.Range; +import com.cnbm.influx.param.Tag; +import com.cnbm.influx.template.Event; +import com.influxdb.query.FluxTable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.time.Instant; +import java.util.*; + +@RestController +@RequestMapping("/spcData") +public class SPCController { + private static final Logger logger = LoggerFactory.getLogger(SPCController.class); + + + @PostMapping("/getData") + public Result getData(@RequestBody QueryDataParam param) throws InterruptedException { + List query = InfluxClient.Client.query(param); + return new Result>().ok(query); + } + + + @PostMapping("/insertOrUpdate") + public Result insertBatchForNew(@RequestBody InfluxData data){ + if(data.getEvents()== null || data.getMeasurement()== null){ + return new Result().error("传入events 或者 measurement为空"); + } + try { + InfluxClient.Client.batchInsert(data.getEvents(),data.getMeasurement()); + }catch (Exception e){ + return new Result().ok("influxdb 操作异常"); + } + return new Result().error("成功"); + } + + +} diff --git a/ym-influx/src/main/java/com/cnbm/influx/controller/TIMETest.java b/ym-influx/src/main/java/com/cnbm/influx/controller/TIMETest.java index e4e0e43..c4e58f1 100644 --- a/ym-influx/src/main/java/com/cnbm/influx/controller/TIMETest.java +++ b/ym-influx/src/main/java/com/cnbm/influx/controller/TIMETest.java @@ -1,10 +1,12 @@ package com.cnbm.influx.controller; +import com.cnbm.influx.template.Event; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.time.Instant; +import java.util.List; /** * @Desc: "" @@ -14,7 +16,9 @@ import java.time.Instant; @Data @ApiModel(value = "检验样本 DTO对象") public class TIMETest { - @ApiModelProperty(value = "时间2") + + + @ApiModelProperty(value = "Events") private Instant time2; }