367 lines
13 KiB
Java
367 lines
13 KiB
Java
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;
|
|
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.client.domain.WritePrecision;
|
|
import com.influxdb.client.write.Point;
|
|
import com.influxdb.query.FluxRecord;
|
|
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("/influx")
|
|
public class S7DemoController {
|
|
private static final Logger logger = LoggerFactory.getLogger(S7DemoController.class);
|
|
|
|
|
|
//for 常规计数型
|
|
@PostMapping("/insertDemoDataForCount")
|
|
public void insertDemoDataForCount() 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("failDay");
|
|
|
|
int i1 = r.nextInt(10);
|
|
if(i1<4){
|
|
event.setArgValue("0");
|
|
}else {
|
|
event.setArgValue("1");
|
|
}
|
|
list.add(event);
|
|
}
|
|
InfluxClient.Client.batchInsert(list,"Weight");
|
|
}
|
|
|
|
public static void main(String[] args) throws InterruptedException {
|
|
long l1 = System.currentTimeMillis();
|
|
System.out.println(l1);
|
|
Date date = new Date(l1);
|
|
Thread.sleep(1000);
|
|
Date date2 = new Date(l1);
|
|
System.out.println();
|
|
}
|
|
|
|
@PostMapping("/insertDemoOne")
|
|
public void insertDemoOne(){
|
|
|
|
List<Event> list = new ArrayList<>();
|
|
Event event = new Event();
|
|
event.setTime(new Date(1670554110451L).toInstant());
|
|
event.setArgName("failDayDay");
|
|
event.setArgValue("30089");
|
|
event.setSampleNumber("10001");
|
|
list.add(event);
|
|
|
|
Event event2 = new Event();
|
|
event2.setTime(new Date(1670555006702L).toInstant());
|
|
event2.setArgName("failDayDay");
|
|
event2.setArgValue("40088");
|
|
event2.setSampleNumber("10001");
|
|
list.add(event2);
|
|
InfluxClient.Client.batchInsert(list,"Weight");
|
|
}
|
|
@PostMapping("/readDemoOne")
|
|
public void readDemoOne() {
|
|
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","1601123134690340865")));
|
|
queryDataParam.setBucket(Constant.bucket);
|
|
List<FluxTable> query = InfluxClient.Client.query(queryDataParam);
|
|
|
|
}
|
|
|
|
@PostMapping("/getFlux")
|
|
public Result<Instant> getFlux() {
|
|
List<String> dropNames = new ArrayList<>();
|
|
dropNames.add("transationId");
|
|
dropNames.add("inspectionSheetId");
|
|
// 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.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("/saveFlux1")
|
|
public Result<Instant> saveFlux1() {
|
|
|
|
List<Event> list = new ArrayList<>();
|
|
Event event2 = new Event();
|
|
Instant instant = new Date().toInstant();
|
|
event2.setTime(instant);
|
|
event2.setArgName("failDayDay");
|
|
event2.setArgValue("");
|
|
event2.setSampleNumber("10001");
|
|
list.add(event2);
|
|
InfluxClient.Client.batchInsert(list,"Weight");
|
|
return new Result<Instant>().ok(instant);
|
|
}
|
|
|
|
@PostMapping("/saveFlux2")
|
|
public void saveFlux2(@RequestBody TIMETest timeTest) {
|
|
|
|
List<Event> list = new ArrayList<>();
|
|
Event event2 = new Event();
|
|
event2.setTime(timeTest.getTime2());
|
|
event2.setArgName("failDayDay");
|
|
event2.setArgValue("40091");
|
|
event2.setSampleNumber("10001");
|
|
list.add(event2);
|
|
InfluxClient.Client.batchInsert(list,"Weight");
|
|
System.out.println();
|
|
}
|
|
|
|
|
|
//for 常规计量型
|
|
@PostMapping("/insertDemoDataForMetering")
|
|
public void insertDemoDataForMetering() 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.toString());
|
|
|
|
event.setInspectionSheetId(i+"");
|
|
list.add(event);
|
|
}
|
|
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());
|
|
// 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<>();
|
|
//
|
|
// 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","arg6"));
|
|
// queryDataParam.setRange(new Range(DataUtils.getBeforeDate(10).toInstant(),Instant.now()));
|
|
// 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");
|
|
// 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");
|
|
// }
|
|
}
|