合并暂降模块代码

This commit is contained in:
2022-07-14 09:43:29 +08:00
parent 014c766806
commit 638f5b8f93
56 changed files with 3133 additions and 86 deletions

View File

@@ -0,0 +1,118 @@
package com.njcn.event;
import com.njcn.event.pojo.PqsEventDetail;
import com.njcn.event.pojo.PqsOnlinerateAggregate;
import com.njcn.event.pojo.PqsEventDetailCount;
import com.njcn.influxdb.utils.InfluxDbUtils;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.influxdb.querybuilder.SelectQueryImpl;
import org.influxdb.querybuilder.SelectionQueryImpl;
import org.influxdb.querybuilder.WhereNested;
import org.influxdb.querybuilder.WhereQueryImpl;
import org.influxdb.querybuilder.clauses.Clause;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.List;
import static com.njcn.influxdb.param.InfluxDBPublicParam.DATABASE;
import static com.njcn.influxdb.param.InfluxDBPublicParam.PQS_EVENT_DETAIL;
import static org.influxdb.querybuilder.BuiltQuery.QueryBuilder.*;
import static org.influxdb.querybuilder.FunctionFactory.sum;
@RunWith(SpringRunner.class)
@SpringBootTest
public class EventBootApplicationTest {
@Autowired
private InfluxDbUtils influxDbUtils;
// TODO https://github.com/influxdata/influxdb-java/blob/master/QUERY_BUILDER.md
@Test
public void queryList() {
// or 条件数据
List<Clause> clauses = getClauses();
SelectQueryImpl selectQuery = select().column("line_id").column("eventass_index").from(DATABASE, PQS_EVENT_DETAIL);
WhereQueryImpl<SelectQueryImpl> where = selectQuery.where();
// WHERE (line_id = '1' OR line_id = '2' OR line_id = '3') 加上前后()
whereAndNested(clauses, where);
// AND time >= '2022-05-01T00:00:00Z' AND time <= '2022-09-01T00:00:00Z' tz('Asia/Shanghai');
where.and(gte("time", "2022-05-01T00:00:00Z")).and(lte("time", "2022-09-01T00:00:00Z"));
where.tz("Asia/Shanghai");
QueryResult result = influxDbUtils.query(selectQuery.getCommand());
InfluxDBResultMapper influxDBResultMapper = new InfluxDBResultMapper();
List<PqsEventDetail> re = influxDBResultMapper.toPOJO(result, PqsEventDetail.class);
Assert.assertTrue(re.size() > 0);
}
@Test
public void queryCount() {
// or 条件数据
List<Clause> clauses = getClauses();
SelectQueryImpl selectQuery = select().count("eventass_index").from(DATABASE, PQS_EVENT_DETAIL);
WhereQueryImpl<SelectQueryImpl> where = selectQuery.where();
// WHERE (line_id = '1' OR line_id = '2' OR line_id = '3') 加上前后()
whereAndNested(clauses, where);
// AND time >= '2022-05-01T00:00:00Z' AND time <= '2022-09-01T00:00:00Z' tz('Asia/Shanghai');
where.and(gte("time", "2022-05-01T00:00:00Z")).and(lte("time", "2022-09-01T00:00:00Z"));
where.tz("Asia/Shanghai");
QueryResult result = influxDbUtils.query(selectQuery.getCommand());
InfluxDBResultMapper influxDBResultMapper = new InfluxDBResultMapper();
List<PqsEventDetailCount> re = influxDBResultMapper.toPOJO(result, PqsEventDetailCount.class);
Assert.assertTrue(re.size() > 0);
}
@Test
public void queryAggregate() {
// SELECT (SUM(onlinemin) / (SUM(onlinemin) + SUM(offlinemin))) * 100 FROM pqs_onlinerate
SelectionQueryImpl select = select();
SelectionQueryImpl sum = select.op(op(sum("onlinemin"), "/", op(sum("onlinemin"), "+", sum("offlinemin"))), "*", 100)
.as("value");
SelectQueryImpl selectQuery = sum.from(DATABASE, "pqs_onlinerate");
WhereQueryImpl<SelectQueryImpl> where = selectQuery.where();
// AND time >= '2022-05-01T00:00:00Z' AND time <= '2022-09-01T00:00:00Z' tz('Asia/Shanghai');
where.and(gte("time", "2022-05-01T00:00:00Z")).and(lte("time", "2022-09-01T00:00:00Z"));
where.tz("Asia/Shanghai");
QueryResult result = influxDbUtils.query(selectQuery.getCommand());
InfluxDBResultMapper influxDBResultMapper = new InfluxDBResultMapper();
List<PqsOnlinerateAggregate> re = influxDBResultMapper.toPOJO(result, PqsOnlinerateAggregate.class);
Assert.assertTrue(re.size() > 0);
}
private List<Clause> getClauses() {
Clause c1 = eq("line_id", "5e467a40023b299070682eb21f2ec9a1");
Clause c2 = eq("line_id", "183245996f303ebfd80eeb3377cecdc2");
Clause c3 = eq("line_id", "0d46f54420246e999d5c68b3133f668c");
return Arrays.asList(c1, c2, c3);
}
private void whereAndNested(List<Clause> clauses, WhereQueryImpl<SelectQueryImpl> whereQuery) {
WhereNested<WhereQueryImpl<SelectQueryImpl>> andNested = whereQuery.andNested();
for (Clause clause : clauses) {
andNested.or(clause);
}
andNested.close();
}
}

View File

@@ -0,0 +1,81 @@
package com.njcn.event.pojo;
import lombok.Data;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Measurement;
import java.time.Instant;
import static com.njcn.influxdb.param.InfluxDBPublicParam.PQS_EVENT_DETAIL;
@Data
@Measurement(name = PQS_EVENT_DETAIL)
public class PqsEventDetail {
@Column(name = "line_id")
private String lineId;
@Column(name = "time")
private Instant time;
@Column(name = "event_describe")
private String eventDescribe;
@Column(name = "wave_type")
private Integer waveType;
@Column(name = "persist_time")
private Double persistTime;
@Column(name = "event_value")
private Double eventValue;
@Column(name = "event_reason")
private String eventReason;
@Column(name = "event_type")
private String eventType;
@Column(name = "eventass_index")
private String eventassIndex;
@Column(name = "dq_time")
private Integer dqTime;
@Column(name = "deal_time")
private String dealTime;
@Column(name = "deal_flag")
private Integer dealFlag;
@Column(name = "num")
private Integer num;
@Column(name = "file_flag")
private Integer fileFlag;
@Column(name = "first_time")
private String firstTime;
@Column(name = "first_type")
private String firstType;
@Column(name = "first_ms")
private Integer firstMs;
@Column(name = "wave_name")
private String waveName;
@Column(name = "energy")
private Double energy;
@Column(name = "severity")
private Double severity;
@Column(name = "sagsource")
private String sagSource;
@Column(name = "create_time")
private String createTime;
}

View File

@@ -0,0 +1,20 @@
package com.njcn.event.pojo;
import lombok.Data;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Measurement;
import java.time.Instant;
import static com.njcn.influxdb.param.InfluxDBPublicParam.PQS_EVENT_DETAIL;
@Data
@Measurement(name = PQS_EVENT_DETAIL)
public class PqsEventDetailCount {
@Column(name = "time")
private Instant time;
@Column(name = "count")
private Integer count;
}

View File

@@ -0,0 +1,18 @@
package com.njcn.event.pojo;
import lombok.Data;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Measurement;
import java.time.Instant;
@Data
@Measurement(name = "pqs_onlinerate")
public class PqsOnlinerateAggregate {
@Column(name = "time")
private Instant time;
@Column(name = "value")
private Double value;
}