新增 influxdb 多条件查询

This commit is contained in:
caixiang 2022-12-12 11:12:01 +08:00
parent 1798ba8d1d
commit ff63c194ee
18 changed files with 134 additions and 88 deletions

View File

@ -182,41 +182,6 @@ public class Main {
return day;
}
private static List<FluxTable> query(QueryDataParam param,InfluxDBClient influxDBClient){
String measurement = param.getMeasurement();
List<String> dropedTagName = param.getDropedTagNames();
Range range = param.getRange();
String bucket = param.getBucket();
String tagName = param.getTag().getTagName();
String tagValue = param.getTag().getTagValue();
PageInfo pageInfo = param.getPageInfo();
String flux = "from(bucket:\""+bucket+"\")";
flux += "|> range(start: "+range.getBegin().toString()+",stop:"+range.getEnd().toString()+") \n";
flux += "|> filter(fn: (r) => r[\"_measurement\"] == \""+measurement+"\") \n";
flux += "|> filter(fn: (r) => r[\""+tagName+"\"] == \""+tagValue+"\") \n";
for(String dropName:dropedTagName){
flux += "|> drop(columns: [\""+ dropName +"\"]) \n";
}
flux += "|> sort(columns: [\"_time\"], desc: true) \n";
flux += "|> window(every: 1y) \n";
if(pageInfo!=null){
flux += "|> limit(n: "+pageInfo.getSize()+", offset: "+(pageInfo.getCurrent()-1)* pageInfo.getSize()+")";
}
QueryApi queryApi = influxDBClient.getQueryApi();
List<FluxTable> tables = queryApi.query(flux);
for (FluxTable fluxTable : tables) {
List<FluxRecord> records = fluxTable.getRecords();
for (FluxRecord fluxRecord : records) {
System.out.println("time: "+fluxRecord.getTime() +" key:"+fluxRecord.getField()+" value: " + fluxRecord.getValueByKey("_value")+" measurement: " + fluxRecord.getMeasurement());
}
}
return tables;
}
}

View File

@ -1,10 +1,7 @@
package com.cnbm.influx.config;
import com.cnbm.influx.constant.Constant;
import com.cnbm.influx.param.PageInfo;
import com.cnbm.influx.param.QueryDataGroupByTimeParam;
import com.cnbm.influx.param.QueryDataParam;
import com.cnbm.influx.param.Range;
import com.cnbm.influx.param.*;
import com.cnbm.influx.template.Event;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
@ -141,6 +138,7 @@ public enum InfluxClient {
}
public List<FluxTable> query(QueryDataParam param){
String measurement = param.getMeasurement();
List<String> dropedTagNames = param.getDropedTagNames();
@ -152,10 +150,12 @@ public enum InfluxClient {
flux += "|> range(start: "+range.getBegin()+",stop:"+range.getEnd()+")";
flux += "|> filter(fn: (r) => r[\"_measurement\"] == \""+measurement+"\")";
if(param.getTag()!=null){
String tagName = param.getTag().getTagName();
String tagValue = param.getTag().getTagValue();
flux += "|> filter(fn: (r) => r[\""+tagName+"\"] == \""+tagValue+"\")";
if(param.getTags()!=null && param.getTags().size()>0){
for(Tag tag:param.getTags()){
String tagName = tag.getTagName();
String tagValue = tag.getTagValue();
flux += "|> filter(fn: (r) => r[\""+tagName+"\"] == \""+tagValue+"\")";
}
}
//调整时区查询出的结果 +8个小时
@ -177,16 +177,28 @@ public enum InfluxClient {
List<String> dropedTagNames = param.getDropedTagNames();
Range range = param.getRange();
String bucket = param.getBucket();
String tagName = param.getTag().getTagName();
String tagValue = param.getTag().getTagValue();
String groupName = param.getGroupName();
PageInfo pageInfo = param.getPageInfo();
String flux = "from(bucket:\""+bucket+"\")";
flux += "|> range(start: "+range.getBegin()+",stop:"+range.getEnd()+")";
flux += "|> filter(fn: (r) => r[\"_measurement\"] == \""+measurement+"\")";
flux += "|> filter(fn: (r) => r[\""+tagName+"\"] == \""+tagValue+"\")";
flux += "|> timeShift(duration: 8h)";
// String tagName = param.getTag().getTagName();
// String tagValue = param.getTag().getTagValue();
// flux += "|> filter(fn: (r) => r[\""+tagName+"\"] == \""+tagValue+"\")";
if(param.getTags()!=null && param.getTags().size()>0){
for(Tag tag:param.getTags()){
String tagName = tag.getTagName();
String tagValue = tag.getTagValue();
flux += "|> filter(fn: (r) => r[\""+tagName+"\"] == \""+tagValue+"\")";
}
}
// flux += "|> timeShift(duration: 8h)";
flux += "|> group(columns: [\""+groupName+"\"], mode: \"except\")";
for(String dropName:dropedTagNames){
flux += "|> drop(columns: [\""+dropName+"\"])";
@ -203,15 +215,25 @@ public enum InfluxClient {
List<String> dropedTagNames = param.getDropedTagNames();
Range range = param.getRange();
String bucket = param.getBucket();
String tagName = param.getTag().getTagName();
String tagValue = param.getTag().getTagValue();
PageInfo pageInfo = param.getPageInfo();
String flux = "from(bucket:\""+bucket+"\")";
flux += "|> range(start: "+range.getBegin()+",stop:"+range.getEnd()+")";
flux += "|> filter(fn: (r) => r[\"_measurement\"] == \""+measurement+"\")";
flux += "|> filter(fn: (r) => r[\""+tagName+"\"] == \""+tagValue+"\")";
flux += "|> timeShift(duration: 8h)";
// String tagName = param.getTag().getTagName();
// String tagValue = param.getTag().getTagValue();
// flux += "|> filter(fn: (r) => r[\""+tagName+"\"] == \""+tagValue+"\")";
// flux += "|> timeShift(duration: 8h)";
if(param.getTags()!=null && param.getTags().size()>0){
for(Tag tag:param.getTags()){
String tagName = tag.getTagName();
String tagValue = tag.getTagValue();
flux += "|> filter(fn: (r) => r[\""+tagName+"\"] == \""+tagValue+"\")";
}
}
for(String dropName:dropedTagNames){
flux += "|> drop(columns: [\""+dropName+"\"])";
}

View File

@ -15,6 +15,7 @@ import com.influxdb.query.FluxTable;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@ -69,7 +70,7 @@ public class Main {
dropNames.add("transationId");
dropNames.add("inspectionSheetId");
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTag(new Tag("argName","forUpdate"));
queryDataParam.setTags(Arrays.asList(new Tag("argName","forUpdate")));
queryDataParam.setRange(new Range(DataUtils.getBeforeDate(999).toInstant(),DataUtils.getAfterDate(999).toInstant()));
queryDataParam.setPageInfo(new PageInfo(1,10));
List<FluxTable> query = InfluxClient.Client.query(queryDataParam);

View File

@ -1,6 +1,7 @@
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.PageInfo;
@ -15,14 +16,12 @@ 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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.*;
@RestController
@RequestMapping("/influx")
@ -100,24 +99,47 @@ public class S7DemoController {
public void readDemoOne() throws InterruptedException {
List<String> dropNames = new ArrayList<>();
dropNames.add("transationId");
// dropNames.add("inspectionSheetId");
// dropNames.add("batchNum");
QueryDataParam queryDataParam = new QueryDataParam();
queryDataParam.setMeasurement("PID2");
queryDataParam.setRange(new Range(DataUtils.getBeforeDate(999).toInstant(), DataUtils.getAfterDate(999).toInstant() ));
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTags(Arrays.asList(new Tag("argName","LTWeight"),new Tag("inspectionSheetId","1601096578382766082")));
queryDataParam.setBucket(Constant.bucket);
List<FluxTable> query = InfluxClient.Client.query(queryDataParam);
}
@PostMapping("/getFlux")
public Result<Instant> getFlux() throws InterruptedException {
List<String> dropNames = new ArrayList<>();
dropNames.add("transationId");
dropNames.add("inspectionSheetId");
dropNames.add("batchNum");
// dropNames.add("batchNum");
QueryDataParam queryDataParam = new QueryDataParam();
queryDataParam.setMeasurement(Constant.measurement);
queryDataParam.setRange(new Range(DataUtils.getBeforeDate(999).toInstant(), DataUtils.getAfterDate(999).toInstant() ));
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTag(new Tag("argName","failDayDay"));
queryDataParam.setTags(Arrays.asList(new Tag("argName","failDayDay")));
queryDataParam.setBucket(Constant.bucket);
List<FluxTable> query = InfluxClient.Client.query(queryDataParam);
Instant time = query.get(0).getRecords().get(0).getTime();
return new Result<Instant>().ok(time);
}
@PostMapping("/saveFlux")
public void saveFlux(@RequestBody TIMETest timeTest) throws InterruptedException {
List<Event> list = new ArrayList<>();
Event event2 = new Event();
event2.setTime(time);
event2.setTime(timeTest.getTime2());
event2.setArgName("failDayDay");
event2.setArgValue("40089");
event2.setArgValue("40091");
event2.setSampleNumber("10001");
list.add(event2);
InfluxClient.Client.batchInsert(list,"Weight");

View File

@ -0,0 +1,20 @@
package com.cnbm.influx.controller;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.time.Instant;
/**
* @Desc: ""
* @Author: caixiang
* @DATE: 2022/12/9 15:53
*/
@Data
@ApiModel(value = "检验样本 DTO对象")
public class TIMETest {
@ApiModelProperty(value = "时间2")
private Instant time2;
}

View File

@ -25,7 +25,8 @@ import java.util.List;
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class QueryDataGroupByTimeParam extends BaseParam{
private Tag tag;
private List<Tag> tags;
//查询的时候需要忽略的字段(transationId是唯一标识会对 最终的查询结果集产生影响)
private List<String> dropedTagNames;
private String bucket;

View File

@ -25,8 +25,9 @@ import java.util.List;
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class QueryDataParam extends BaseParam{
//如果tag不传就是查询所有
private Tag tag;
//如果tag不传就是查询所有, 多条件查询influxdb数据
private List<Tag> tags;
//private Tag tag;
//查询的时候需要忽略的字段(transationId是唯一标识会对 最终的查询结果集产生影响)
private List<String> dropedTagNames;
private String bucket;

View File

@ -1,8 +1,8 @@
package com.cnbm.processInspection.controller;
import com.alibaba.fastjson.JSONObject;
import com.cnbm.admin.annotation.LogOperation;
import com.cnbm.basic.dto.FeaturesProcedureDTO;
import com.cnbm.basic.dto.ProductFeaturesDTO;
import com.cnbm.common.constant.Constant;
import com.cnbm.common.page.PageData;
@ -13,8 +13,8 @@ import com.cnbm.common.validator.ValidatorUtils;
import com.cnbm.common.validator.group.AddGroup;
import com.cnbm.common.validator.group.DefaultGroup;
import com.cnbm.common.validator.group.UpdateGroup;
import com.cnbm.influx.param.QueryDataParam;
import com.cnbm.influx.template.Event;
import com.cnbm.processInspection.dto.InspectionSampleDTO;
import com.cnbm.processInspection.dto.InspectionSheetDTO;
import com.cnbm.processInspection.entity.InspectionSheet;

View File

@ -35,6 +35,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
@ -172,7 +173,9 @@ public class ProcessInspectionController {
dropNames.add("inspectionSheetId");
dropNames.add("argName");
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTag(new Tag("argName","LTWeight"));
queryDataParam.setTags(Arrays.asList(new Tag("argName","LTWeight")));
queryDataParam.setRange(new Range(DataUtils.getBeforeDate(100).toInstant(),Instant.now()));
// queryDataParam.setPageInfo(new PageInfo(1,10));
List<FluxTable> query = InfluxClient.Client.queryByGroup(queryDataParam);

View File

@ -5,6 +5,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDateTime;
/**
@ -30,7 +31,7 @@ public class InspectionSampleDTO implements Serializable {
private String batchNum;
@ApiModelProperty(value = "取样时间")
private LocalDateTime sampleTime;
private Instant sampleTime;
@ApiModelProperty(value = "样本号")
private String sampleNumber;

View File

@ -15,6 +15,7 @@ import com.influxdb.query.FluxTable;
import lombok.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@ -104,9 +105,9 @@ public class CGraph {
List<String> dropNames = new ArrayList<>();
dropNames.add("transationId");
dropNames.add("inspectionSheetId");
dropNames.add("batchNum");
// dropNames.add("batchNum");
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTag(new Tag("argName",argName));
queryDataParam.setTags(Arrays.asList(new Tag("argName",argName)));
List<FluxTable> query = InfluxClient.Client.queryGroupByTime(queryDataParam);
//1. 先从fluxdb 里面提取原始数据

View File

@ -12,6 +12,7 @@ import com.influxdb.query.FluxTable;
import lombok.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@ -96,10 +97,10 @@ public class NPGraph {
List<String> dropNames = new ArrayList<>();
dropNames.add("transationId");
dropNames.add("inspectionSheetId");
dropNames.add("batchNum");
//dropNames.add("batchNum");
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTag(new Tag("argName",argName));
queryDataParam.setTags(Arrays.asList(new Tag("argName",argName)));
List<FluxTable> query = InfluxClient.Client.queryGroupByTime(queryDataParam);
//1. 先从fluxdb 里面提取原始数据
List<Double> originData = new ArrayList<>();

View File

@ -15,6 +15,7 @@ import com.influxdb.query.FluxTable;
import lombok.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@ -109,9 +110,9 @@ public class PGraph {
List<String> dropNames = new ArrayList<>();
dropNames.add("transationId");
dropNames.add("inspectionSheetId");
dropNames.add("batchNum");
//dropNames.add("batchNum");
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTag(new Tag("argName",argName));
queryDataParam.setTags(Arrays.asList(new Tag("argName",argName)));
List<FluxTable> query = InfluxClient.Client.queryGroupByTime(queryDataParam);
//1. 先从fluxdb 里面提取原始数据

View File

@ -15,6 +15,7 @@ import com.influxdb.query.FluxTable;
import lombok.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@ -108,10 +109,10 @@ public class UGraph {
List<String> dropNames = new ArrayList<>();
dropNames.add("transationId");
dropNames.add("inspectionSheetId");
dropNames.add("batchNum");
//dropNames.add("batchNum");
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTag(new Tag("argName",argName));
queryDataParam.setTags(Arrays.asList(new Tag("argName",argName)));
List<FluxTable> query = InfluxClient.Client.queryGroupByTime(queryDataParam);
//1. 先从fluxdb 里面提取原始数据
//计算p bar

View File

@ -17,6 +17,7 @@ import com.influxdb.query.FluxTable;
import lombok.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@ -102,9 +103,9 @@ public class MeanRGraph {
List<String> dropNames = new ArrayList<>();
dropNames.add("transationId");
dropNames.add("inspectionSheetId");
dropNames.add("batchNum");
//dropNames.add("batchNum");
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTag(new Tag("argName",argName));
queryDataParam.setTags(Arrays.asList(new Tag("argName",argName)));
List<FluxTable> query = InfluxClient.Client.query(queryDataParam);
if(query.size() == 0){

View File

@ -18,6 +18,7 @@ import com.influxdb.query.FluxTable;
import lombok.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@ -99,9 +100,9 @@ public class MeanStandardDeviationGraph {
List<String> dropNames = new ArrayList<>();
dropNames.add("transationId");
dropNames.add("inspectionSheetId");
dropNames.add("batchNum");
//dropNames.add("batchNum");
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTag(new Tag("argName",argName));
queryDataParam.setTags(Arrays.asList(new Tag("argName",argName)));
List<FluxTable> query = InfluxClient.Client.query(queryDataParam);
//1. 先从fluxdb 里面提取原始数据

View File

@ -15,6 +15,7 @@ import com.influxdb.query.FluxTable;
import lombok.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
@ -96,9 +97,9 @@ public class XMRGraph {
List<String> dropNames = new ArrayList<>();
dropNames.add("transationId");
dropNames.add("inspectionSheetId");
dropNames.add("batchNum");
//dropNames.add("batchNum");
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTag(new Tag("argName",argName));
queryDataParam.setTags(Arrays.asList(new Tag("argName",argName)));
List<FluxTable> query = InfluxClient.Client.query(queryDataParam);

View File

@ -209,7 +209,9 @@ public class InspectionSheetServiceImpl extends CrudServiceImpl<InspectionSheetM
queryDataParam.setMeasurement(workingProcedureName);
//queryDataParam.setInspectionSheetId(inspectionSheetId);
queryDataParam.setBucket("qgs-bucket");
queryDataParam.setTag(new Tag("inspectionSheetId", inspectionSheetId));
//queryDataParam.setTag(new Tag("inspectionSheetId", inspectionSheetId));
List<String> dropNames = new ArrayList<>();
dropNames.add("transationId");
queryDataParam.setDropedTagNames(dropNames);
@ -238,7 +240,7 @@ public class InspectionSheetServiceImpl extends CrudServiceImpl<InspectionSheetM
InspectionSampleDTO sampleDTO = new InspectionSampleDTO();
sampleDTO.setSampleNumber(sampleNumber);
sampleDTO.setSampleTime(LocalDateTime.ofInstant(events.get(0).getTime(), ZoneId.systemDefault()));
sampleDTO.setSampleTime(events.get(0).getTime());
sampleDTO.setBatchNum(dto.getBatchNumber());
sampleDTO.setInspectionSheetId(events.get(0).getInspectionSheetId());
@ -264,8 +266,9 @@ public class InspectionSheetServiceImpl extends CrudServiceImpl<InspectionSheetM
String sampleNumber = dto.getSampleNumber();
String jsonData = dto.getJsonData();
JSONObject json = JSON.parseObject(jsonData);
LocalDateTime sampleTime = dto.getSampleTime();
Instant eventTime = sampleTime.toInstant(ZoneOffset.UTC);
Instant time = dto.getSampleTime();
List<Event> list = new ArrayList<>();
for (Map.Entry entry : json.entrySet()) {
String key = entry.getKey().toString();
@ -275,7 +278,7 @@ public class InspectionSheetServiceImpl extends CrudServiceImpl<InspectionSheetM
// v= Double.valueOf(entry.getValue().toString());
// }catch (Exception e){
// }
list.add(newEvent(eventTime, inspectionSheetId, key, v, sampleNumber));
list.add(newEvent(time, inspectionSheetId, key, v, sampleNumber));
}
InfluxClient.Client.batchInsert(list, workingProcedureName);
}