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-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;
|
|
|
|
import java.time.Instant;
|
2022-07-04 14:49:49 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
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-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();
|
|
|
|
|
|
|
|
for(int j=0;j<10;j++){
|
|
|
|
for(int i=0;i<99;i++){
|
|
|
|
Thread.sleep(10);
|
|
|
|
Event event = new Event();
|
|
|
|
event.setTime(Instant.now());
|
|
|
|
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);
|
|
|
|
list.add(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InfluxClient.Client.batchInsert(list,"Weight");
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
@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
|
|
|
}
|
|
|
|
}
|