use of org.apache.inlong.manager.common.enums.AuditQuerySource in project incubator-inlong by apache.
the class AuditServiceImpl method listByCondition.
/**
* Query audit data for list by condition
*
* @param request The audit request of query condition
* @return The result of query
*/
@Override
public List<AuditVO> listByCondition(AuditRequest request) throws IOException {
LOGGER.info("begin query audit list request={}", request);
Preconditions.checkNotNull(request, "request is null");
List<AuditVO> result = new ArrayList<>();
AuditQuerySource querySource = AuditQuerySource.valueOf(auditQuerySource);
for (String auditId : request.getAuditIds()) {
if (AuditQuerySource.MYSQL == querySource) {
String format = "%Y-%m-%d %H:%i:00";
// Support min agg at now
DateTimeFormatter forPattern = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime dtDate = forPattern.parseDateTime(request.getDt());
String eDate = dtDate.plusDays(1).toString(forPattern);
List<Map<String, Object>> sumList = auditEntityMapper.sumByLogTs(request.getInlongGroupId(), request.getInlongStreamId(), auditId, request.getDt(), eDate, format);
List<AuditInfo> auditSet = sumList.stream().map(s -> {
AuditInfo vo = new AuditInfo();
vo.setLogTs((String) s.get("logTs"));
vo.setCount(((BigDecimal) s.get("total")).longValue());
return vo;
}).collect(Collectors.toList());
result.add(new AuditVO(auditId, auditSet));
} else if (AuditQuerySource.ELASTICSEARCH == querySource) {
String index = String.format("%s_%s", request.getDt().replaceAll("-", ""), auditId);
if (elasticsearchApi.indexExists(index)) {
SearchResponse response = elasticsearchApi.search(toAuditSearchRequest(index, request.getInlongGroupId(), request.getInlongStreamId()));
final List<Aggregation> aggregations = response.getAggregations().asList();
if (CollectionUtils.isNotEmpty(aggregations)) {
ParsedTerms terms = (ParsedTerms) aggregations.get(0);
if (CollectionUtils.isNotEmpty(terms.getBuckets())) {
List<AuditInfo> auditSet = terms.getBuckets().stream().map(bucket -> {
AuditInfo vo = new AuditInfo();
vo.setLogTs(bucket.getKeyAsString());
vo.setCount((long) ((ParsedSum) bucket.getAggregations().asList().get(0)).getValue());
return vo;
}).collect(Collectors.toList());
result.add(new AuditVO(auditId, auditSet));
}
}
} else {
LOGGER.warn("Elasticsearch index={} not exists", index);
}
}
}
LOGGER.info("success to query audit list for request={}", request);
return result;
}
Aggregations