合并暂降模块代码
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
package com.njcn.event.influxdb;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import com.njcn.event.pojo.po.EventDetail;
|
||||
import com.njcn.event.pojo.vo.EventDetailCount;
|
||||
import com.njcn.influxdb.utils.InfluxDbUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.influxdb.dto.QueryResult;
|
||||
import org.influxdb.querybuilder.SelectQueryImpl;
|
||||
import org.influxdb.querybuilder.SelectionQueryImpl;
|
||||
import org.influxdb.querybuilder.clauses.Clause;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
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.eq;
|
||||
|
||||
/**
|
||||
* 表PqsEventDetail 查询封装
|
||||
*
|
||||
* @author : zhaojun
|
||||
* @version : 1.0.0
|
||||
* @date : 2022年07月04日 18:36
|
||||
*/
|
||||
@Component
|
||||
public class PqsEventDetailQuery extends QueryBuilder {
|
||||
protected PqsEventDetailQuery(InfluxDbUtils influxDbUtils) {
|
||||
super(influxDbUtils);
|
||||
}
|
||||
|
||||
/**
|
||||
* line_id or条件
|
||||
*
|
||||
* @param lineIds line_id
|
||||
* @return line_id or条件
|
||||
*/
|
||||
public static List<Clause> lineIdOr(List<String> lineIds) {
|
||||
List<Clause> lineIdOr = new ArrayList<>();
|
||||
for (String lineId : lineIds) {
|
||||
Clause clause = eq("line_id", lineId);
|
||||
lineIdOr.add(clause);
|
||||
}
|
||||
return lineIdOr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定查询数据库和表
|
||||
*
|
||||
* @param column select count 条件
|
||||
* @see SelectQueryImpl
|
||||
*/
|
||||
private SelectQueryImpl fromTable(SelectionQueryImpl column) {
|
||||
return column.from(DATABASE, PQS_EVENT_DETAIL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询方法
|
||||
* SELECT在包括一个tag时,必须至少指定一个field
|
||||
*
|
||||
* @param columnNames 显示的字段名,查询需要指定,减少数据加载
|
||||
* @param or or条件
|
||||
* @param and and条件
|
||||
* @return 结果
|
||||
*/
|
||||
public List<EventDetail> selectList(List<String> columnNames, List<Clause> or, List<Clause> and) {
|
||||
SelectionQueryImpl selectAppendColumnNames = selectAppendColumnNames(columnNames);
|
||||
SelectQueryImpl selectAppendTable = fromTable(selectAppendColumnNames);
|
||||
QueryResult query = getQueryResult(or, and, selectAppendTable);
|
||||
return toPOJO(query, EventDetail.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count方法
|
||||
*
|
||||
* @param columnNames 显示的字段名,查询需要指定,减少数据加载
|
||||
* @param or or条件
|
||||
* @param and and条件
|
||||
* @return 结果
|
||||
*/
|
||||
public Integer selectCount(List<String> columnNames, List<Clause> or, List<Clause> and) {
|
||||
SelectionQueryImpl selectAppendColumnNames = countAppendColumnNames(columnNames);
|
||||
SelectQueryImpl selectAppendTable = fromTable(selectAppendColumnNames);
|
||||
QueryResult query = getQueryResult(or, and, selectAppendTable);
|
||||
List<EventDetailCount> eventDetailCounts = toPOJO(query, EventDetailCount.class);
|
||||
if (CollUtil.isEmpty(eventDetailCounts)) {
|
||||
return 0;
|
||||
}
|
||||
return eventDetailCounts.get(0).getCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* where条件 时间和reason_type
|
||||
*
|
||||
* @param beginOfDay 开始时间
|
||||
* @param endOfDay 结束时间
|
||||
* @param type 字段 reason_type
|
||||
* @param reasonId 字段 reason_type 值
|
||||
* @return 构建查询and条件
|
||||
*/
|
||||
public static List<Clause> timeAndType(String beginOfDay, String endOfDay, String type, String reasonId) {
|
||||
List<Clause> clauses = timeAnd(beginOfDay, endOfDay);
|
||||
clauses.add(eq(type, reasonId));
|
||||
return clauses;
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计lineIndex的数据
|
||||
*
|
||||
* @param eventDetailList influxdb数据
|
||||
* @return 个数
|
||||
*/
|
||||
public static Function<List<String>, Long> countLineIndexes(List<EventDetail> eventDetailList) {
|
||||
return lineIndexes -> eventDetailList.stream().filter(t -> lineIndexes.contains(t.getLineId())).count();
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计eventass_index字段非空的个数
|
||||
*
|
||||
* @return 个数
|
||||
*/
|
||||
public static Function<List<EventDetail>, Long> countEventassIndexIsNoEmpty() {
|
||||
return eventDetails -> eventDetails.stream().filter(t -> StringUtils.isNotEmpty(t.getEventassIndex())).count();
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间段内的数据
|
||||
*
|
||||
* @param eventDetailList influxdb数据
|
||||
* @return 数据
|
||||
*/
|
||||
public static BiFunction<DateTime, DateTime, List<EventDetail>> getBetweenTimeEventDetailList(List<EventDetail> eventDetailList) {
|
||||
return (beginTime, endTime) -> eventDetailList.stream().filter(eventDetail -> {
|
||||
Instant timeId = eventDetail.getTimeId();
|
||||
return timeId.isAfter(beginTime.toInstant()) && timeId.isBefore(endTime.toInstant());
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.njcn.event.influxdb;
|
||||
|
||||
import com.njcn.event.pojo.po.PqsOnlinerate;
|
||||
import com.njcn.influxdb.utils.InfluxDbUtils;
|
||||
import org.influxdb.dto.QueryResult;
|
||||
import org.influxdb.querybuilder.SelectQueryImpl;
|
||||
import org.influxdb.querybuilder.SelectionQueryImpl;
|
||||
import org.influxdb.querybuilder.clauses.Clause;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.njcn.influxdb.param.InfluxDBPublicParam.DATABASE;
|
||||
import static com.njcn.influxdb.param.InfluxDBPublicParam.PQS_ONLINERATE;
|
||||
import static org.influxdb.querybuilder.BuiltQuery.QueryBuilder.eq;
|
||||
|
||||
@Component
|
||||
public class PqsOnlinerateQuery extends QueryBuilder {
|
||||
|
||||
protected PqsOnlinerateQuery(InfluxDbUtils influxDbUtils) {
|
||||
super(influxDbUtils);
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定查询数据库和表
|
||||
*
|
||||
* @param column select count 条件
|
||||
* @see SelectQueryImpl
|
||||
*/
|
||||
private SelectQueryImpl fromTable(SelectionQueryImpl column) {
|
||||
return column.from(DATABASE, PQS_ONLINERATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* dev_id or条件
|
||||
*
|
||||
* @param devIds dev_id
|
||||
* @return dev_id or条件
|
||||
*/
|
||||
public static List<Clause> devIdOr(List<String> devIds) {
|
||||
List<Clause> devIdOr = new ArrayList<>();
|
||||
for (String lineId : devIds) {
|
||||
Clause clause = eq("dev_id", lineId);
|
||||
devIdOr.add(clause);
|
||||
}
|
||||
return devIdOr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询方法
|
||||
* SELECT在包括一个tag时,必须至少指定一个field
|
||||
*
|
||||
* @param columnNames 显示的字段名,查询需要指定,减少数据加载
|
||||
* @param or or条件
|
||||
* @param and and条件
|
||||
* @return 结果
|
||||
*/
|
||||
public List<PqsOnlinerate> selectList(List<String> columnNames, List<Clause> or, List<Clause> and) {
|
||||
SelectionQueryImpl selectAppendColumnNames = selectAppendColumnNames(columnNames);
|
||||
SelectQueryImpl selectAppendTable = fromTable(selectAppendColumnNames);
|
||||
QueryResult query = getQueryResult(or, and, selectAppendTable);
|
||||
return toPOJO(query, PqsOnlinerate.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package com.njcn.event.influxdb;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.njcn.influxdb.utils.InfluxDbUtils;
|
||||
import org.influxdb.InfluxDBMapperException;
|
||||
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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.influxdb.querybuilder.BuiltQuery.QueryBuilder.*;
|
||||
|
||||
/**
|
||||
* 查询抽象
|
||||
*
|
||||
* @author : zhaojun
|
||||
* @version : 1.0.0
|
||||
* @date : 2022年07月04日 18:36
|
||||
*/
|
||||
public abstract class QueryBuilder {
|
||||
|
||||
/* 查询工具类 */
|
||||
private final InfluxDbUtils influxDbUtils;
|
||||
private final InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
|
||||
|
||||
protected QueryBuilder(InfluxDbUtils influxDbUtils) {
|
||||
this.influxDbUtils = influxDbUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据转换
|
||||
*
|
||||
* @param queryResult 查询结果
|
||||
* @param clazz 泛型
|
||||
* @param <T> 泛型T
|
||||
* @return 查询结果映射
|
||||
* @throws InfluxDBMapperException 异常
|
||||
*/
|
||||
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz) throws InfluxDBMapperException {
|
||||
return resultMapper.toPOJO(queryResult, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询方法
|
||||
*
|
||||
* @param command sql
|
||||
* @return 查询结果
|
||||
*/
|
||||
public QueryResult query(String command) {
|
||||
return influxDbUtils.query(command);
|
||||
}
|
||||
|
||||
/**
|
||||
* select语句拼接查询字段
|
||||
*
|
||||
* @param columnNames 查询字段
|
||||
* @see SelectionQueryImpl
|
||||
*/
|
||||
public SelectionQueryImpl selectAppendColumnNames(List<String> columnNames) {
|
||||
SelectionQueryImpl selectionQuery = select();
|
||||
for (String columnName : columnNames) {
|
||||
selectionQuery.column(columnName);
|
||||
}
|
||||
return selectionQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count语句拼接查询字段
|
||||
*
|
||||
* @param columnNames 查询字段
|
||||
* @see SelectionQueryImpl
|
||||
*/
|
||||
public SelectionQueryImpl countAppendColumnNames(List<String> columnNames) {
|
||||
SelectionQueryImpl selectionQuery = select();
|
||||
for (String columnName : columnNames) {
|
||||
selectionQuery.count(columnName);
|
||||
}
|
||||
return selectionQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装sql语句,包括or语句和and语句
|
||||
*
|
||||
* @param or or条件
|
||||
* @param and and条件
|
||||
* @return 查询结果
|
||||
* @see SelectionQueryImpl
|
||||
*/
|
||||
public QueryResult getQueryResult(List<Clause> or, List<Clause> and, SelectQueryImpl selectQuery) {
|
||||
WhereQueryImpl<SelectQueryImpl> where = selectQuery.where();
|
||||
whereAndNested(where, or);
|
||||
appendWhereOther(where, and);
|
||||
return query(selectQuery.getCommand());
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接or条件
|
||||
*
|
||||
* @param clauses or条件
|
||||
* @see WhereQueryImpl
|
||||
*/
|
||||
public void whereAndNested(WhereQueryImpl<SelectQueryImpl> whereQuery, List<Clause> clauses) {
|
||||
WhereNested<WhereQueryImpl<SelectQueryImpl>> andNested = whereQuery.andNested();
|
||||
if (CollUtil.isNotEmpty(clauses)) {
|
||||
for (Clause clause : clauses) {
|
||||
andNested.or(clause);
|
||||
}
|
||||
andNested.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接and条件
|
||||
*
|
||||
* @param clauses and条件
|
||||
* @see WhereQueryImpl
|
||||
*/
|
||||
public void appendWhereOther(WhereQueryImpl<SelectQueryImpl> where, List<Clause> clauses) {
|
||||
if (CollUtil.isNotEmpty(clauses)) {
|
||||
for (Clause clause : clauses) {
|
||||
where.and(clause);
|
||||
}
|
||||
}
|
||||
where.tz("Asia/Shanghai");
|
||||
}
|
||||
|
||||
/**
|
||||
* time and条件
|
||||
*
|
||||
* @param gteTime 开始时间
|
||||
* @param lteTime 结束时间
|
||||
* @return time and条件
|
||||
*/
|
||||
public static List<Clause> timeAnd(String gteTime, String lteTime) {
|
||||
List<Clause> clauses = new ArrayList<>(15);
|
||||
clauses.add(gte("time", gteTime));
|
||||
clauses.add(lte("time", lteTime));
|
||||
return clauses;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式成查询开始时间段
|
||||
*
|
||||
* @param time 时间
|
||||
* @return yyyy-MM-dd 00:00:00
|
||||
*/
|
||||
public static String beginOfDay(String time) {
|
||||
DateTime date = DateUtil.parse(time);
|
||||
DateTime dateTime = DateUtil.beginOfDay(date);
|
||||
return DateUtil.formatDateTime(dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式成查询结束时间段
|
||||
*
|
||||
* @param time 时间
|
||||
* @return yyyy-MM-dd 23:59:59
|
||||
*/
|
||||
public static String endOfDay(String time) {
|
||||
DateTime date = DateUtil.parse(time);
|
||||
DateTime dateTime = DateUtil.endOfDay(date);
|
||||
return DateUtil.formatDateTime(dateTime);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user