This commit is contained in:
LuoYe 2021-08-17 17:56:15 +08:00
parent 201ede424f
commit cfeab18dd0
6 changed files with 93 additions and 128 deletions

View File

@ -57,6 +57,7 @@ public class OutlookLogController {
@Autowired @Autowired
InfluxDBClient influxDBClient; InfluxDBClient influxDBClient;
@Value("${spring.influx.org:''}") @Value("${spring.influx.org:''}")
private String org; private String org;
@ -64,8 +65,6 @@ public class OutlookLogController {
private String bucket; private String bucket;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${page.size}") @Value("${page.size}")
private int pageSize; private int pageSize;
@ -90,71 +89,4 @@ public class OutlookLogController {
return ResultUtil.success(logVo); return ResultUtil.success(logVo);
} }
@GetMapping("/save")
public String save(){
ArrayList<Point> arrayList = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
OutlookLog outlookLog = new OutlookLog();
Point point = Point.measurement(bucket)
.addTag("githubid", "22121")
.addField("aaaaaa1", i)
.addField("aaaaaa2", i)
.addField("aaaaaa3", i)
.time(Instant.now().toEpochMilli(), WritePrecision.MS);
arrayList.add(point);
}
try (WriteApi writeApi = influxDBClient.getWriteApi()) {
// writeApi.writePoint(bucket, org, point);
writeApi.writePoints(bucket,org, arrayList);
}
return "ok";
}
@GetMapping("/find")
public void find(){
String flux = "from(bucket:\"e5\") |> range(start: 0)" +
"|> filter(fn: (r) => r[\"_measurement\"] == \"e5s\")" +
// "|> filter(fn: (r) => r[\"_field\"] == \"aaaaaa1\")" +
"|> limit(n: 100)";
QueryApi queryApi = influxDBClient.getQueryApi();
List<FluxTable> tables = queryApi.query(flux,org);
for (FluxTable fluxTable : tables) {
List<FluxRecord> records = fluxTable.getRecords();
for (FluxRecord fluxRecord : records) {
System.out.println(fluxRecord.getField());
System.out.println(fluxRecord.getTime() + " ->" + fluxRecord.getValueByKey("_value"));
}
}
/*queryApi.query(flux,org,(cancellable, fluxRecord) -> {
//
// The callback to consume a FluxRecord.
//
// cancelable - object has the cancel method to stop asynchronous query
//
System.out.println(fluxRecord.getTime() + ": " + fluxRecord.getValueByKey("_value"));
}, throwable -> {
//
// The callback to consume any error notification.
//
System.out.println("Error occurred: " + throwable.getMessage());
}, () -> {
//
// The callback to consume a notification about successfully end of stream.
//
System.out.println("Query completed");
});*/
}
} }

View File

@ -2,10 +2,14 @@ package io.qyi.e5.outlook_log.entity;
import java.io.Serializable; import java.io.Serializable;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.influxdb.annotations.Column;
import com.influxdb.annotations.Measurement;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
@ -19,42 +23,43 @@ import lombok.experimental.Accessors;
* @since 2020-03-03 * @since 2020-03-03
*/ */
@Data @Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true) @Accessors(chain = true)
@Measurement(name = "OutlookLog")
public class OutlookLog implements Serializable { public class OutlookLog implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/** /**
* github_id * github_id
*/ */
private Integer githubId; // private Integer githubId;
/** /**
* outlook_id * outlook_id
*/ */
private Integer outlookId; // private Integer outlookId;
/** /**
* 调用时间 * 调用时间
*/ */
private Timestamp callTime; @Column(timestamp = true)
private Instant callTime;
/** /**
* 调用结果 * 调用结果
*/ */
@Column
private Integer result; private Integer result;
/** /**
* 如果有错误原因则记录 * 如果有错误原因则记录
*/ */
@Column
private String msg; private String msg;
/** /**
* 原始错误消息 * 原始错误消息
*/ */
@Column
private String originalMsg; private String originalMsg;

View File

@ -1,24 +0,0 @@
package io.qyi.e5.outlook_log.mapper;
import io.qyi.e5.outlook_log.entity.OutlookLog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* <p>
* Mapper 接口
* </p>
*
* @author 落叶
* @since 2020-03-03
*/
public interface OutlookLogMapper extends BaseMapper<OutlookLog> {
@Select("select * from e5.d_#{githubId}_#{outlookId}")
List<OutlookLog> findAllList(@Param("githubId") int githubId,@Param("outlookId") int outlookId);
}

View File

@ -13,9 +13,8 @@ import java.util.List;
* @author 落叶 * @author 落叶
* @since 2020-03-03 * @since 2020-03-03
*/ */
public interface IOutlookLogService extends IService<OutlookLog> { public interface IOutlookLogService {
void addLog(int githubId,int outlookId, String msg,int result,String original_msg); void addLog(int githubId,int outlookId, String msg,int result,String original_msg);
int deleteInfo(int github_id); int deleteInfo(int github_id);
List<OutlookLog> findAllList(int githubId, int outlookId); List<OutlookLog> findAllList(int githubId, int outlookId);
} }

View File

@ -1,11 +1,8 @@
package io.qyi.e5.outlook_log.service.impl; package io.qyi.e5.outlook_log.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.qyi.e5.outlook_log.entity.OutlookLog; import io.qyi.e5.outlook_log.entity.OutlookLog;
import io.qyi.e5.outlook_log.mapper.OutlookLogMapper;
import io.qyi.e5.outlook_log.service.IOutlookLogService; import io.qyi.e5.outlook_log.service.IOutlookLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@ -19,30 +16,32 @@ import java.util.List;
* @since 2020-03-03 * @since 2020-03-03
*/ */
@Service @Service
@DS("td") public class OutlookLogServiceImpl implements IOutlookLogService {
public class OutlookLogServiceImpl extends ServiceImpl<OutlookLogMapper, OutlookLog> implements IOutlookLogService {
@Override @Override
public void addLog(int githubId,int outlookId, String msg, int result,String original_msg) { public void addLog(int githubId,int outlookId, String msg, int result,String original_msg) {
OutlookLog outlookLog = new OutlookLog(); // OutlookLog outlookLog = new OutlookLog();
outlookLog.setGithubId(githubId) // outlookLog.setGithubId(githubId)
.setOutlookId(outlookId) // .setOutlookId(outlookId)
.setResult(result) // .setResult(result)
// .setCallTime((int) (System.currentTimeMillis() / 1000)) // .setMsg(msg)
.setMsg(msg) // .setOriginalMsg(original_msg);
.setOriginalMsg(original_msg);
baseMapper.insert(outlookLog); // baseMapper.insert(outlookLog);
} }
@Override @Override
public int deleteInfo(int github_id) { public int deleteInfo(int github_id) {
QueryWrapper<OutlookLog> outlookLogQueryWrapper = new QueryWrapper<>(); QueryWrapper<OutlookLog> outlookLogQueryWrapper = new QueryWrapper<>();
outlookLogQueryWrapper.eq("github_id", github_id); outlookLogQueryWrapper.eq("github_id", github_id);
return baseMapper.delete(outlookLogQueryWrapper); // return baseMapper.delete(outlookLogQueryWrapper);
return 0;
} }
@Override @Override
public List<OutlookLog> findAllList(int githubId, int outlookId) { public List<OutlookLog> findAllList(int githubId, int outlookId) {
return baseMapper.findAllList(githubId, outlookId); // return baseMapper.findAllList(githubId, outlookId);
return null;
} }
} }

View File

@ -1,22 +1,76 @@
import io.qyi.e5.bean.influx2.InfluxdbConfig; import com.influxdb.LogLevel;
import io.qyi.e5.outlook_log.service.IOutlookLogService; import com.influxdb.client.*;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
import io.qyi.e5.outlook_log.entity.OutlookLog;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.springframework.cglib.beans.BeanMap;
import org.mockito.Mock;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class influxdb2Test { public class influxdb2Test {
InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://127.0.0.1:8086", "o-oFFLbRCFHmGFMMYR7kLFGb4jTUXkJkTCmPBeZxn32prCKxwVpS-FM3pLyCvv0gVao-Cm6c_s2Yl-7Ud_xH_Q==".toCharArray()
);
@InjectMocks private String org = "luoye";
InfluxdbConfig influxdbConfig;
@Mock @Test
IOutlookLogService outlookLogService; public void save(){
influxDBClient.setLogLevel(LogLevel.BASIC);
WriteOptions writeOptions = WriteOptions.builder()
.batchSize(5000)
.flushInterval(1000)
.bufferLimit(10000)
.jitterInterval(1000)
.retryInterval(5000)
.build();
List<OutlookLog> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
OutlookLog outlookLog = new OutlookLog();
outlookLog.setResult(0).setMsg(i + "- ok").setOriginalMsg("加入成功").setCallTime(Instant.now());
list.add(outlookLog);
}
OutlookLog outlookLog = new OutlookLog();
outlookLog.setResult(0).setMsg(i + "- ok").setOriginalMsg("加入成功").setCallTime(Instant.now());
BeanMap.create(outlookLog).getBean();
try (WriteApi writeApi = influxDBClient.getWriteApi(writeOptions)) {
// writeApi.writeMeasurement();
// writeApi.writeMeasurements("e5", org ,WritePrecision.NS,list);
Point point = Point
.measurement("githubId_123")
.addTag("githubId","123465")
.addFields(fields)
.time(Instant.now(), WritePrecision.NS);
}
influxDBClient.close();
}
@Test @Test
public void test1(){ public void find(){
// outlookLogService.findAllList(1000, 2000); String flux = "from(bucket:\"e5\") |> range(start: 0)" +
influxdbConfig.influxDBClient(); "|> filter(fn: (r) => r[\"_measurement\"] == \"OutlookLog\")" +
// "|> filter(fn: (r) => r[\"_field\"] == \"aaaaaa1\")" +
"|> limit(n: 100)";
QueryApi queryApi = influxDBClient.getQueryApi();
List<FluxTable> tables = queryApi.query(flux,org);
for (FluxTable fluxTable : tables) {
List<FluxRecord> records = fluxTable.getRecords();
for (FluxRecord fluxRecord : records) {
System.out.println(fluxRecord.getField());
System.out.println(fluxRecord.getTime() + " ->" + fluxRecord.getValueByKey("_value"));
} }
}
influxDBClient.close();
}
} }