2022-07-04 08:32:21 +08:00
|
|
|
package com.cnbm.influx.controller;
|
|
|
|
|
2022-07-13 16:41:43 +08:00
|
|
|
import com.cnbm.common.spc.util.DataUtils;
|
2022-11-25 10:12:33 +08:00
|
|
|
import com.cnbm.common.utils.Result;
|
|
|
|
import com.cnbm.common.vo.R;
|
2022-07-04 08:32:21 +08:00
|
|
|
import com.cnbm.influx.config.InfluxClient;
|
2022-07-04 14:49:49 +08:00
|
|
|
import com.cnbm.influx.param.PageInfo;
|
|
|
|
import com.cnbm.influx.param.QueryDataParam;
|
|
|
|
import com.cnbm.influx.param.Range;
|
|
|
|
import com.cnbm.influx.param.Tag;
|
2022-07-04 08:32:21 +08:00
|
|
|
import com.cnbm.influx.template.Event;
|
|
|
|
import com.influxdb.client.domain.WritePrecision;
|
|
|
|
import com.influxdb.client.write.Point;
|
2022-07-04 14:49:49 +08:00
|
|
|
import com.influxdb.query.FluxRecord;
|
|
|
|
import com.influxdb.query.FluxTable;
|
2022-07-04 08:32:21 +08:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
2022-08-09 14:57:55 +08:00
|
|
|
|
|
|
|
import java.text.DateFormat;
|
2022-07-04 08:32:21 +08:00
|
|
|
import java.time.Instant;
|
2022-07-04 14:49:49 +08:00
|
|
|
import java.util.ArrayList;
|
2022-08-09 14:57:55 +08:00
|
|
|
import java.util.Date;
|
2022-07-04 14:49:49 +08:00
|
|
|
import java.util.List;
|
2022-07-21 09:50:01 +08:00
|
|
|
import java.util.Random;
|
2022-07-04 08:32:21 +08:00
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/influx")
|
|
|
|
public class S7DemoController {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(S7DemoController.class);
|
|
|
|
|
|
|
|
|
2022-07-04 14:49:49 +08:00
|
|
|
@PostMapping("/insertBatch")
|
|
|
|
public void insertBatch() throws InterruptedException {
|
|
|
|
List<Event> list = new ArrayList<>();
|
2022-07-21 09:50:01 +08:00
|
|
|
Random r = new Random();
|
2022-07-04 08:32:21 +08:00
|
|
|
|
2022-07-21 09:50:01 +08:00
|
|
|
for(int i=0;i<999;i++){
|
|
|
|
Thread.sleep(10);
|
2022-07-04 14:49:49 +08:00
|
|
|
Event event = new Event();
|
|
|
|
event.setTime(Instant.now());
|
|
|
|
event.setTransationId("asas"+i);
|
2022-07-21 09:50:01 +08:00
|
|
|
event.setArgName("LTWeight");
|
|
|
|
Double d = r.nextDouble() * 2.5 + 66;
|
|
|
|
event.setArgValue(d);
|
2022-11-25 10:12:33 +08:00
|
|
|
|
|
|
|
list.add(event);
|
|
|
|
}
|
|
|
|
InfluxClient.Client.batchInsert(list,"Weight");
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/forTestInsertBatch")
|
|
|
|
public void forTestInsertBatch() throws InterruptedException {
|
|
|
|
List<Event> list = new ArrayList<>();
|
|
|
|
Random r = new Random();
|
|
|
|
|
|
|
|
for(int i=0;i<99;i++){
|
|
|
|
Thread.sleep(10);
|
|
|
|
Event event = new Event();
|
|
|
|
event.setTime(Instant.now());
|
|
|
|
event.setTransationId("forbatch"+i);
|
|
|
|
event.setArgName("LTWeight");
|
|
|
|
Double d = r.nextDouble() * 2.5 + 66;
|
|
|
|
event.setArgValue(d);
|
|
|
|
event.setBatchNum(4);
|
|
|
|
event.setInspectionSheetId(i+"");
|
2022-07-04 14:49:49 +08:00
|
|
|
list.add(event);
|
|
|
|
}
|
2022-07-21 09:50:01 +08:00
|
|
|
InfluxClient.Client.batchInsert(list,"Weight");
|
2022-07-04 14:49:49 +08:00
|
|
|
}
|
2022-07-04 08:32:21 +08:00
|
|
|
|
2022-07-29 10:28:14 +08:00
|
|
|
@PostMapping("/insertBatchJYD")
|
|
|
|
public void insertBatchJYD() throws InterruptedException {
|
|
|
|
List<Event> list = new ArrayList<>();
|
|
|
|
Random r = new Random();
|
2022-08-09 14:57:55 +08:00
|
|
|
Instant instant = DataUtils.getBeforeDate(400).toInstant();
|
2022-07-29 10:28:14 +08:00
|
|
|
|
|
|
|
for(int j=0;j<10;j++){
|
|
|
|
for(int i=0;i<99;i++){
|
|
|
|
Thread.sleep(10);
|
|
|
|
Event event = new Event();
|
2022-08-09 14:57:55 +08:00
|
|
|
event.setTime(instant);
|
2022-07-29 10:28:14 +08:00
|
|
|
event.setTransationId("asas"+i);
|
|
|
|
event.setArgName("LTWeight");
|
|
|
|
Double d = r.nextDouble() * 2.5 + 66;
|
2022-07-29 10:38:43 +08:00
|
|
|
event.setInspectionSheetId(j+"");
|
2022-07-29 10:28:14 +08:00
|
|
|
event.setArgValue(d);
|
2022-08-09 14:57:55 +08:00
|
|
|
event.setBatchNum(i);
|
2022-07-29 10:28:14 +08:00
|
|
|
list.add(event);
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 14:57:55 +08:00
|
|
|
InfluxClient.Client.batchInsert(list,"WeightHei");
|
|
|
|
}
|
|
|
|
|
2022-11-25 10:12:33 +08:00
|
|
|
|
|
|
|
|
2022-08-09 14:57:55 +08:00
|
|
|
@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(new Double(0));
|
|
|
|
}else {
|
|
|
|
event.setArgValue(new Double(1));
|
|
|
|
}
|
2022-11-25 10:12:33 +08:00
|
|
|
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();
|
2022-08-09 14:57:55 +08:00
|
|
|
|
2022-11-25 10:12:33 +08:00
|
|
|
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);
|
2022-08-09 14:57:55 +08:00
|
|
|
event.setInspectionSheetId(i+"");
|
|
|
|
|
|
|
|
event.setBatchNum(i);
|
|
|
|
list.add(event);
|
|
|
|
}
|
2022-11-25 10:12:33 +08:00
|
|
|
InfluxClient.Client.batchInsert(list,"Weights");
|
2022-07-29 10:28:14 +08:00
|
|
|
}
|
|
|
|
|
2022-07-13 16:41:43 +08:00
|
|
|
/**
|
|
|
|
* 测试连接是否正常
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* true 服务正常健康
|
|
|
|
* false 异常
|
|
|
|
*/
|
|
|
|
@PostMapping("/ping")
|
|
|
|
public void ping() throws InterruptedException {
|
|
|
|
boolean ping = InfluxClient.Client.ping();
|
|
|
|
System.out.println(ping);
|
|
|
|
}
|
|
|
|
|
2022-07-04 14:49:49 +08:00
|
|
|
@PostMapping("/query")
|
|
|
|
public void query() throws InterruptedException {
|
|
|
|
List<Event> list = new ArrayList<>();
|
2022-07-04 08:32:21 +08:00
|
|
|
|
2022-07-04 14:49:49 +08:00
|
|
|
QueryDataParam queryDataParam = new QueryDataParam();
|
|
|
|
queryDataParam.setBucket("qgs-bucket");
|
|
|
|
queryDataParam.setMeasurement("ASProcessCompleteEventAS");
|
2022-07-29 10:28:14 +08:00
|
|
|
List<String> dropNames = new ArrayList<>();
|
|
|
|
dropNames.add("transationId");
|
2022-07-29 10:38:43 +08:00
|
|
|
dropNames.add("inspectionSheetId");
|
2022-07-29 10:28:14 +08:00
|
|
|
queryDataParam.setDropedTagNames(dropNames);
|
2022-07-04 14:49:49 +08:00
|
|
|
queryDataParam.setTag(new Tag("argName","arg6"));
|
2022-07-13 16:41:43 +08:00
|
|
|
queryDataParam.setRange(new Range(DataUtils.getBeforeDate(10).toInstant(),Instant.now()));
|
2022-07-04 14:49:49 +08:00
|
|
|
queryDataParam.setPageInfo(new PageInfo(1,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"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
List<Event> list = new ArrayList<>();
|
|
|
|
|
|
|
|
QueryDataParam queryDataParam = new QueryDataParam();
|
|
|
|
queryDataParam.setBucket("qgs-bucket");
|
|
|
|
queryDataParam.setMeasurement("ASProcessCompleteEventAS");
|
2022-07-29 10:28:14 +08:00
|
|
|
List<String> dropNames = new ArrayList<>();
|
|
|
|
dropNames.add("transationId");
|
2022-07-29 10:38:43 +08:00
|
|
|
dropNames.add("inspectionSheetId");
|
2022-07-29 10:28:14 +08:00
|
|
|
queryDataParam.setDropedTagNames(dropNames);
|
2022-07-04 14:49:49 +08:00
|
|
|
queryDataParam.setTag(new Tag("argName","arg7"));
|
2022-07-13 16:41:43 +08:00
|
|
|
queryDataParam.setRange(new Range(DataUtils.getBeforeDate(10).toInstant(),Instant.now()));
|
2022-07-04 14:49:49 +08:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 08:32:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-08-09 14:57:55 +08:00
|
|
|
|
2022-07-04 08:32:21 +08:00
|
|
|
@PostMapping("/insert")
|
|
|
|
public void insert() throws InterruptedException {
|
|
|
|
Event event = new Event();
|
|
|
|
event.setTime(Instant.now());
|
|
|
|
event.setTransationId("asasd11");
|
|
|
|
event.setArgName("argName11");
|
2022-07-04 14:49:49 +08:00
|
|
|
event.setArgValue(900001d);
|
2022-07-04 08:32:21 +08:00
|
|
|
Point asProcessCompleteEvent = insert(event, "ASProcessCompleteEvent");
|
2022-07-04 14:49:49 +08:00
|
|
|
InfluxClient.Client.insert(event,"ASProcessCompleteEvent");
|
2022-07-04 08:32:21 +08:00
|
|
|
}
|
|
|
|
}
|