修改 influxdb query函数

This commit is contained in:
caixiang 2022-12-07 16:56:47 +08:00
parent 515df64760
commit 2d9b0ea1f1
4 changed files with 209 additions and 202 deletions

View File

@ -44,13 +44,13 @@ public class SysRoleController {
@GetMapping("page") @GetMapping("page")
@ApiOperation("分页") @ApiOperation("分页")
@ApiImplicitParams({ // @ApiImplicitParams({
@ApiImplicitParam(name = Constant.PAGE, value = "当前页码从1开始", paramType = "query", required = true, dataTypeClass=Integer.class) , // @ApiImplicitParam(name = Constant.PAGE, value = "当前页码从1开始", paramType = "query", required = true, dataTypeClass=Integer.class) ,
@ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataTypeClass=Integer.class) , // @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataTypeClass=Integer.class) ,
@ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataTypeClass=String.class) , // @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataTypeClass=String.class) ,
@ApiImplicitParam(name = Constant.ORDER, value = "排序方式,可选值(asc、desc)", paramType = "query", dataTypeClass=String.class) , // @ApiImplicitParam(name = Constant.ORDER, value = "排序方式,可选值(asc、desc)", paramType = "query", dataTypeClass=String.class) ,
@ApiImplicitParam(name = "name", value = "角色名", paramType = "query", dataTypeClass=String.class) // @ApiImplicitParam(name = "name", value = "角色名", paramType = "query", dataTypeClass=String.class)
}) // })
@PreAuthorize("@ex.hasAuthority('sys:role:page')") @PreAuthorize("@ex.hasAuthority('sys:role:page')")
public Result<PageData<SysRoleDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){ public Result<PageData<SysRoleDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
PageData<SysRoleDTO> page = sysRoleService.page(params); PageData<SysRoleDTO> page = sysRoleService.page(params);

View File

@ -146,14 +146,21 @@ public enum InfluxClient {
List<String> dropedTagNames = param.getDropedTagNames(); List<String> dropedTagNames = param.getDropedTagNames();
Range range = param.getRange(); Range range = param.getRange();
String bucket = param.getBucket(); String bucket = param.getBucket();
String tagName = param.getTag().getTagName();
String tagValue = param.getTag().getTagValue();
PageInfo pageInfo = param.getPageInfo(); PageInfo pageInfo = param.getPageInfo();
String flux = "from(bucket:\""+bucket+"\")"; String flux = "from(bucket:\""+bucket+"\")";
flux += "|> range(start: "+range.getBegin()+",stop:"+range.getEnd()+")"; flux += "|> range(start: "+range.getBegin()+",stop:"+range.getEnd()+")";
flux += "|> filter(fn: (r) => r[\"_measurement\"] == \""+measurement+"\")"; flux += "|> filter(fn: (r) => r[\"_measurement\"] == \""+measurement+"\")";
flux += "|> filter(fn: (r) => r[\""+tagName+"\"] == \""+tagValue+"\")";
if(param.getTag()!=null){
String tagName = param.getTag().getTagName();
String tagValue = param.getTag().getTagValue();
flux += "|> filter(fn: (r) => r[\""+tagName+"\"] == \""+tagValue+"\")";
}
//调整时区查询出的结果 +8个小时 //调整时区查询出的结果 +8个小时
flux += "|> timeShift(duration: 8h)"; flux += "|> timeShift(duration: 8h)";
for(String dropName:dropedTagNames){ for(String dropName:dropedTagNames){

View File

@ -29,8 +29,9 @@ public class S7DemoController {
private static final Logger logger = LoggerFactory.getLogger(S7DemoController.class); private static final Logger logger = LoggerFactory.getLogger(S7DemoController.class);
@PostMapping("/insertBatch") //for 常规计量型
public void insertBatch() throws InterruptedException { @PostMapping("/insertDemoDataForMetering")
public void insertDemoDataForMetering() throws InterruptedException {
List<Event> list = new ArrayList<>(); List<Event> list = new ArrayList<>();
Random r = new Random(); Random r = new Random();
@ -47,17 +48,15 @@ public class S7DemoController {
}else { }else {
event.setArgValue("1"); event.setArgValue("1");
} }
// Double d = r.nextDouble() * 2.5 + 66;
// event.setArgValue(d);
list.add(event); list.add(event);
} }
InfluxClient.Client.batchInsert(list,"Weight"); InfluxClient.Client.batchInsert(list,"Weight");
} }
@PostMapping("/forTestInsertBatch")
public void forTestInsertBatch() throws InterruptedException { //for 常规计数型
@PostMapping("/insertDemoDataForCount")
public void insertDemoDataForCount() throws InterruptedException {
List<Event> list = new ArrayList<>(); List<Event> list = new ArrayList<>();
Random r = new Random(); Random r = new Random();
@ -75,199 +74,199 @@ public class S7DemoController {
} }
InfluxClient.Client.batchInsert(list,"Weight"); InfluxClient.Client.batchInsert(list,"Weight");
} }
@PostMapping("/insertBatchJYD")
public void insertBatchJYD() throws InterruptedException {
List<Event> list = new ArrayList<>();
Random r = new Random();
Instant instant = DataUtils.getBeforeDate(400).toInstant();
for(int j=0;j<10;j++){
for(int i=0;i<99;i++){
Thread.sleep(10);
Event event = new Event();
event.setTime(instant);
event.setTransationId("asas"+i);
event.setArgName("LTWeight");
Double d = r.nextDouble() * 2.5 + 66;
event.setInspectionSheetId(j+"");
event.setArgValue(d.toString());
event.setBatchNum(i+"");
list.add(event);
}
}
InfluxClient.Client.batchInsert(list,"WeightHei");
}
@PostMapping("/insertAndQuery")
public void insertAndQuery() throws InterruptedException {
Event event = new Event();
// long l = System.currentTimeMillis();
// System.out.println("l:"+l);
// event.setTime(new Date(1669874900889l).toInstant());
// //
event.setTime(new Date().toInstant()); // @PostMapping("/insertBatchJYD")
event.setTransationId("4444"); // public void insertBatchJYD() throws InterruptedException {
event.setBatchNum("44"); // List<Event> list = new ArrayList<>();
event.setArgName("forUpdate"); // Random r = new Random();
event.setArgValue("5555.1"); // Instant instant = DataUtils.getBeforeDate(400).toInstant();
InfluxClient.Client.insert(event,"FORUPDATECAIXIANG"); //
// Thread.sleep(50000); // for(int j=0;j<10;j++){
// for(int i=0;i<99;i++){
// Thread.sleep(10);
// Event event = new Event();
// event.setTime(instant);
// event.setTransationId("asas"+i);
// event.setArgName("LTWeight");
// Double d = r.nextDouble() * 2.5 + 66;
// event.setInspectionSheetId(j+"");
// event.setArgValue(d.toString());
// event.setBatchNum(i+"");
// list.add(event);
// }
// }
// InfluxClient.Client.batchInsert(list,"WeightHei");
// }
//
// @PostMapping("/insertAndQuery")
// public void insertAndQuery() throws InterruptedException {
// Event event = new Event();
//
//// long l = System.currentTimeMillis();
//// System.out.println("l:"+l);
//// event.setTime(new Date(1669874900889l).toInstant());
////
// event.setTime(new Date().toInstant());
// event.setTransationId("4444");
// event.setBatchNum("44");
// event.setArgName("forUpdate");
// event.setArgValue("5555.1");
// InfluxClient.Client.insert(event,"FORUPDATECAIXIANG");
//// Thread.sleep(50000);
////
//// event.setArgValue(333.3);
//// InfluxClient.Client.insert(event,"FORUPDATE");
//
//
//// QueryDataParam queryDataParam = new QueryDataParam();
//// queryDataParam.setBucket("qgs-bucket");
//// queryDataParam.setMeasurement("FORUPDATECAIXIANG");
//// List<String> dropNames = new ArrayList<>();
//// dropNames.add("transationId");
//// dropNames.add("inspectionSheetId");
//// queryDataParam.setDropedTagNames(dropNames);
//// queryDataParam.setTag(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);
//// System.out.println();
// }
//
//
//
// @PostMapping("/insertBatchJYDForTest")
// public void insertBatchJYDForTest() throws InterruptedException {
// List<Event> list = new ArrayList<>();
// Random r = new Random();
//
// for(int i=0;i<999;i++){
// Thread.sleep(10);
// Event event = new Event();
// event.setTime(DataUtils.getAfterDate(i).toInstant());
// event.setTransationId("asas"+i);
// event.setArgName("LostDays");
// int i1 = r.nextInt(10);
// if(i1<4){
// event.setArgValue("0");
// }else {
// event.setArgValue("1");
// }
// event.setInspectionSheetId(i+"");
//
// event.setBatchNum(i+"");
// list.add(event);
// }
// InfluxClient.Client.batchInsert(list,"Weights");
// }
//
// @PostMapping("/insertBatchForNew")
// public void insertBatchForNew() throws InterruptedException {
// List<Event> list = new ArrayList<>();
// Random r = new Random();
//
// for(int i=0;i<999;i++){
// Thread.sleep(10);
// Event event = new Event();
// event.setTime(new Date().toInstant());
// event.setTransationId("asas"+i);
// event.setArgName("LiuWeight");
//// int i1 = r.nextInt(10);
//// if(i1<4){
//// event.setArgValue(new Double(0));
//// }else {
//// event.setArgValue(new Double(1));
//// }
// Double d = r.nextDouble() * 2.5 + 66;
// event.setArgValue(d.toString());
// event.setInspectionSheetId(i+"");
//
// event.setBatchNum(i+"");
// list.add(event);
// }
// InfluxClient.Client.batchInsert(list,"Weights");
// }
//
// /**
// * 测试连接是否正常
// *
// * @return
// * true 服务正常健康
// * false 异常
// */
// @PostMapping("/ping")
// public void ping() throws InterruptedException {
// boolean ping = InfluxClient.Client.ping();
// System.out.println(ping);
// }
//
// @PostMapping("/query")
// public void query() throws InterruptedException {
// List<Event> list = new ArrayList<>();
// //
// event.setArgValue(333.3);
// InfluxClient.Client.insert(event,"FORUPDATE");
// QueryDataParam queryDataParam = new QueryDataParam(); // QueryDataParam queryDataParam = new QueryDataParam();
// queryDataParam.setBucket("qgs-bucket"); // queryDataParam.setBucket("qgs-bucket");
// queryDataParam.setMeasurement("FORUPDATECAIXIANG"); // queryDataParam.setMeasurement("ASProcessCompleteEventAS");
// List<String> dropNames = new ArrayList<>(); // List<String> dropNames = new ArrayList<>();
// dropNames.add("transationId"); // dropNames.add("transationId");
// dropNames.add("inspectionSheetId"); // dropNames.add("inspectionSheetId");
// queryDataParam.setDropedTagNames(dropNames); // queryDataParam.setDropedTagNames(dropNames);
// queryDataParam.setTag(new Tag("argName","forUpdate")); // queryDataParam.setTag(new Tag("argName","arg6"));
// queryDataParam.setRange(new Range(DataUtils.getBeforeDate(999).toInstant(),DataUtils.getAfterDate(999).toInstant())); // queryDataParam.setRange(new Range(DataUtils.getBeforeDate(10).toInstant(),Instant.now()));
// queryDataParam.setPageInfo(new PageInfo(1,10)); // queryDataParam.setPageInfo(new PageInfo(1,10));
// List<FluxTable> query = InfluxClient.Client.query(queryDataParam); // List<FluxTable> query = InfluxClient.Client.query(queryDataParam);
// System.out.println(); //
} //
// for (FluxTable fluxTable : query) {
// List<FluxRecord> records = fluxTable.getRecords();
// for (FluxRecord fluxRecord : records) {
@PostMapping("/insertBatchJYDForTest") // System.out.println("value: " + fluxRecord.getValueByKey("_value"));
public void insertBatchJYDForTest() throws InterruptedException {
List<Event> list = new ArrayList<>();
Random r = new Random();
for(int i=0;i<999;i++){
Thread.sleep(10);
Event event = new Event();
event.setTime(DataUtils.getAfterDate(i).toInstant());
event.setTransationId("asas"+i);
event.setArgName("LostDays");
int i1 = r.nextInt(10);
if(i1<4){
event.setArgValue("0");
}else {
event.setArgValue("1");
}
event.setInspectionSheetId(i+"");
event.setBatchNum(i+"");
list.add(event);
}
InfluxClient.Client.batchInsert(list,"Weights");
}
@PostMapping("/insertBatchForNew")
public void insertBatchForNew() throws InterruptedException {
List<Event> list = new ArrayList<>();
Random r = new Random();
for(int i=0;i<999;i++){
Thread.sleep(10);
Event event = new Event();
event.setTime(new Date().toInstant());
event.setTransationId("asas"+i);
event.setArgName("LiuWeight");
// int i1 = r.nextInt(10);
// if(i1<4){
// event.setArgValue(new Double(0));
// }else {
// event.setArgValue(new Double(1));
// } // }
Double d = r.nextDouble() * 2.5 + 66; // }
event.setArgValue(d.toString()); // System.out.println();
event.setInspectionSheetId(i+""); // }
//
event.setBatchNum(i+""); // public static void main(String[] args) {
list.add(event); // List<Event> list = new ArrayList<>();
} //
InfluxClient.Client.batchInsert(list,"Weights"); // QueryDataParam queryDataParam = new QueryDataParam();
} // queryDataParam.setBucket("qgs-bucket");
// queryDataParam.setMeasurement("ASProcessCompleteEventAS");
/** // List<String> dropNames = new ArrayList<>();
* 测试连接是否正常 // dropNames.add("transationId");
* // dropNames.add("inspectionSheetId");
* @return // queryDataParam.setDropedTagNames(dropNames);
* true 服务正常健康 // queryDataParam.setTag(new Tag("argName","arg7"));
* false 异常 // queryDataParam.setRange(new Range(DataUtils.getBeforeDate(10).toInstant(),Instant.now()));
*/ // queryDataParam.setPageInfo(new PageInfo(2,10));
@PostMapping("/ping") // List<FluxTable> query = InfluxClient.Client.query(queryDataParam);
public void ping() throws InterruptedException { //
boolean ping = InfluxClient.Client.ping(); //
System.out.println(ping); // for (FluxTable fluxTable : query) {
} // List<FluxRecord> records = fluxTable.getRecords();
// for (FluxRecord fluxRecord : records) {
@PostMapping("/query") // System.out.println("value: " + fluxRecord.getValueByKey("_value"));
public void query() throws InterruptedException { // }
List<Event> list = new ArrayList<>(); // }
// }
QueryDataParam queryDataParam = new QueryDataParam(); //
queryDataParam.setBucket("qgs-bucket"); //
queryDataParam.setMeasurement("ASProcessCompleteEventAS"); // public Point insert(Event event, String measurement){
List<String> dropNames = new ArrayList<>(); // Point point = Point.measurement(measurement)
dropNames.add("transationId"); // .addTag("transationId", event.getTransationId())
dropNames.add("inspectionSheetId"); // .addTag("argName", event.getArgName())
queryDataParam.setDropedTagNames(dropNames); // .addField("argValue", event.getArgValue())
queryDataParam.setTag(new Tag("argName","arg6")); // .time(event.getTime().toEpochMilli(), WritePrecision.MS);
queryDataParam.setRange(new Range(DataUtils.getBeforeDate(10).toInstant(),Instant.now())); // return point;
queryDataParam.setPageInfo(new PageInfo(1,10)); // }
List<FluxTable> query = InfluxClient.Client.query(queryDataParam); //
// @PostMapping("/insert")
// public void insert() throws InterruptedException {
for (FluxTable fluxTable : query) { // Event event = new Event();
List<FluxRecord> records = fluxTable.getRecords(); // event.setTime(Instant.now());
for (FluxRecord fluxRecord : records) { // event.setTransationId("asasd11");
System.out.println("value: " + fluxRecord.getValueByKey("_value")); // event.setArgName("argName11");
} // event.setArgValue("900001");
} // Point asProcessCompleteEvent = insert(event, "ASProcessCompleteEvent");
System.out.println(); // InfluxClient.Client.insert(event,"ASProcessCompleteEvent");
} // }
public static void main(String[] args) {
List<Event> list = new ArrayList<>();
QueryDataParam queryDataParam = new QueryDataParam();
queryDataParam.setBucket("qgs-bucket");
queryDataParam.setMeasurement("ASProcessCompleteEventAS");
List<String> dropNames = new ArrayList<>();
dropNames.add("transationId");
dropNames.add("inspectionSheetId");
queryDataParam.setDropedTagNames(dropNames);
queryDataParam.setTag(new Tag("argName","arg7"));
queryDataParam.setRange(new Range(DataUtils.getBeforeDate(10).toInstant(),Instant.now()));
queryDataParam.setPageInfo(new PageInfo(2,10));
List<FluxTable> query = InfluxClient.Client.query(queryDataParam);
for (FluxTable fluxTable : query) {
List<FluxRecord> records = fluxTable.getRecords();
for (FluxRecord fluxRecord : records) {
System.out.println("value: " + fluxRecord.getValueByKey("_value"));
}
}
}
public Point insert(Event event, String measurement){
Point point = Point.measurement(measurement)
.addTag("transationId", event.getTransationId())
.addTag("argName", event.getArgName())
.addField("argValue", event.getArgValue())
.time(event.getTime().toEpochMilli(), WritePrecision.MS);
return point;
}
@PostMapping("/insert")
public void insert() throws InterruptedException {
Event event = new Event();
event.setTime(Instant.now());
event.setTransationId("asasd11");
event.setArgName("argName11");
event.setArgValue("900001");
Point asProcessCompleteEvent = insert(event, "ASProcessCompleteEvent");
InfluxClient.Client.insert(event,"ASProcessCompleteEvent");
}
} }

View File

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