Commit 1aad1048 by 陈健智

舆情分析趋势图调整

parent 2782ec85
......@@ -2390,9 +2390,9 @@ public class MarkDataServiceImpl implements MarkDataService {
DateHistogramAggregationBuilder daysAggregationBuilder;
// 天级以小时为颗粒度,周、月级以天作为颗粒度
if (endTime - startTime <= Constant.ONE_DAY){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("stime").calendarInterval(DateHistogramInterval.HOUR);
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("stime").calendarInterval(DateHistogramInterval.HOUR).offset("16h");
}else {
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("stime").calendarInterval(DateHistogramInterval.DAY);
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("stime").calendarInterval(DateHistogramInterval.DAY).offset("16h");
}
String projectId = UserThreadLocal.getProjectId();
// project plan query
......@@ -2857,6 +2857,9 @@ public class MarkDataServiceImpl implements MarkDataService {
public JSONObject getSpreadTendency(MarkSearchDTO dto) {
JSONObject res = new JSONObject();
try {
Long[] times = Tools.formatSpreadTendencyTime(dto.getStartTime(), dto.getEndTime());
dto.setStartTime(times[0]);
dto.setEndTime(times[1]);
String projectId = UserThreadLocal.getProjectId();
Long endTime = dto.getEndTime();
String planId = dto.getPlanId();
......@@ -3244,6 +3247,9 @@ public class MarkDataServiceImpl implements MarkDataService {
JSONObject res = new JSONObject();
try {
defaultXhsCustomSearchDTO(dto);
Long[] times = Tools.formatSpreadTendencyTime(dto.getStartTime(), dto.getEndTime());
dto.setStartTime(times[0]);
dto.setEndTime(times[1]);
// 总趋势图
Pair<String, List<LineVO>> spreadTendency = getEmotionSpreadTendency(dto, null);
// 负面趋势图
......@@ -4434,12 +4440,19 @@ public class MarkDataServiceImpl implements MarkDataService {
String[] indexes = esClientDao.getIndexes();
// 聚合请求
TermsAggregationBuilder platformAggregationBuilder = AggregationBuilders.terms("platformAgg").field("platform_id").order(BucketOrder.count(false));
DateHistogramAggregationBuilder daysAggregationBuilder;
// 天级以小时为颗粒度,其他以天作为颗粒度
if (dto.getEndTime() - dto.getStartTime() <= Constant.ONE_DAY){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.HOUR);
}else {
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.DAY);
DateHistogramAggregationBuilder daysAggregationBuilder = null;
long timeRange = dto.getEndTime() - dto.getStartTime();
// 时间间隔≤7天,数据颗粒度为“小时级”
if (timeRange <= Constant.ONE_WEEK){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.HOUR).offset("16h");
}
// 7天<时间间隔≤180天,数据颗粒度为“天级”
if (timeRange > Constant.ONE_WEEK && timeRange <= 6 * Constant.ONE_MONTH){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.DAY).offset("16h");
}
// 180天<时间间隔,数据颗粒度为“月级”
if (timeRange > 6 * Constant.ONE_MONTH){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.MONTH).offset("-8h");
}
// query
BoolQueryBuilder query = yuqingAnalyzeQuery(dto);
......@@ -4459,6 +4472,20 @@ public class MarkDataServiceImpl implements MarkDataService {
long count = timeBucket.getDocCount();
line.add(new LineVO(count, time));
}
// 由于结束点为开区间,将结束点数据补完
if (CollectionUtils.isNotEmpty(line)){
line.get(0).setDate(dto.getStartTime());
MarkSearchDTO searchDTO = Tools.convertMap(dto, MarkSearchDTO.class);
searchDTO.setPlatforms(Collections.singletonList(platformId));
searchDTO.setStartTime(dto.getEndTime());
searchDTO.setEndTime(dto.getEndTime() + 1);
Long count = 0L;
try {
count = esClientDao.count(indexes, yuqingAnalyzeQuery(searchDTO), null);
} catch (IOException ignore) {
}
line.add(new LineVO(count, dto.getEndTime()));
}
res.put(GlobalPojo.getPlatformNameById(platformId), line);
});
return res;
......@@ -4473,15 +4500,21 @@ public class MarkDataServiceImpl implements MarkDataService {
private Pair<String, List<LineVO>> getEmotionSpreadTendency(MarkSearchDTO dto, String emotion) throws IOException {
List<LineVO> res = new ArrayList<>();
String[] indexes = esClientDao.getIndexes();
DateHistogramAggregationBuilder daysAggregationBuilder;
// 天级以小时为颗粒度,其他以天作为颗粒度
if (dto.getEndTime() - dto.getStartTime() <= Constant.ONE_DAY){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.HOUR);
}else {
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.DAY);
DateHistogramAggregationBuilder daysAggregationBuilder = null;
long timeRange = dto.getEndTime() - dto.getStartTime();
// 时间间隔≤7天,数据颗粒度为“小时级”
if (timeRange <= Constant.ONE_WEEK){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.HOUR).offset("16h");
}
// 7天<时间间隔≤180天,数据颗粒度为“天级”
if (timeRange > Constant.ONE_WEEK && timeRange <= 6 * Constant.ONE_MONTH){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.DAY).offset("16h");
}
// 180天<时间间隔,数据颗粒度为“月级”
if (timeRange > 6 * Constant.ONE_MONTH){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.MONTH).offset("-8h");
}
TermsAggregationBuilder titleAggregationBuilder = AggregationBuilders.terms("titleAgg").field("agg_title.keyword").order(BucketOrder.count(false));
// dto.setPlatforms(null);
// query
BoolQueryBuilder query = yuqingAnalyzeQuery(dto, emotion, null);
// response
......@@ -4496,6 +4529,15 @@ public class MarkDataServiceImpl implements MarkDataService {
long count = bucket.getDocCount();
res.add(new LineVO(count, time));
});
// 由于结束点为开区间,将结束点数据补完
if (CollectionUtils.isNotEmpty(res)){
res.get(0).setDate(dto.getStartTime());
MarkSearchDTO secondDTO = Tools.convertMap(dto, MarkSearchDTO.class);
secondDTO.setStartTime(dto.getEndTime());
secondDTO.setEndTime(dto.getEndTime() + 1);
Long count = esClientDao.count(indexes, yuqingAnalyzeQuery(secondDTO, emotion, null), null);
res.add(new LineVO(count, dto.getEndTime()));
}
// 走势图最高点的聚合标题
String maxAggTitle = null;
Optional<? extends Histogram.Bucket> maxBucket = buckets.stream().max(Comparator.comparingLong(MultiBucketsAggregation.Bucket::getDocCount));
......
......@@ -611,6 +611,9 @@ public class SearchWholeServiceImpl implements SearchWholeService {
public JSONObject getSearchWholeSpreadTendency(SearchFilterDTO dto) {
JSONObject res = new JSONObject();
try {
Long[] times = Tools.formatSpreadTendencyTime(dto.getStartTime(), dto.getEndTime());
dto.setStartTime(times[0]);
dto.setEndTime(times[1]);
List<LineVO> totalSpreadTendency = getTotalSpreadTendency(dto);
Map<String, List<LineVO>> platformSpreadTendency = getPlatformSpreadTendency(dto);
BaseMap firstArticle = getFirstArticle(dto, totalSpreadTendency);
......@@ -858,12 +861,19 @@ public class SearchWholeServiceImpl implements SearchWholeService {
private List<LineVO> getTotalSpreadTendency(SearchFilterDTO dto) throws IOException {
List<LineVO> res = new ArrayList<>();
String[] indexes = esClientDao.getAllIndexes(dto.getStartTime(), dto.getEndTime());
DateHistogramAggregationBuilder daysAggregationBuilder;
// 天级以小时为颗粒度,其他以天作为颗粒度
if (dto.getEndTime() - dto.getStartTime() <= Constant.ONE_DAY){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.HOUR);
}else {
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.DAY);
DateHistogramAggregationBuilder daysAggregationBuilder = null;
long timeRange = dto.getEndTime() - dto.getStartTime();
// 时间间隔≤7天,数据颗粒度为“小时级”
if (timeRange <= Constant.ONE_WEEK){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.HOUR).offset("16h");
}
// 7天<时间间隔≤180天,数据颗粒度为“天级”
if (timeRange > Constant.ONE_WEEK && timeRange <= 6 * Constant.ONE_MONTH){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.DAY).offset("16h");
}
// 180天<时间间隔,数据颗粒度为“月级”
if (timeRange > 6 * Constant.ONE_MONTH){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.MONTH).offset("-8h");
}
// query
BoolQueryBuilder query = searchWholeAnalyzeQuery(dto);
......@@ -879,6 +889,15 @@ public class SearchWholeServiceImpl implements SearchWholeService {
long count = bucket.getDocCount();
res.add(new LineVO(count, time));
});
// 由于结束点为开区间,将结束点数据补完
if (CollectionUtils.isNotEmpty(res)){
res.get(0).setDate(dto.getStartTime());
SearchFilterDTO searchFilterDTO = Tools.convertMap(dto, SearchFilterDTO.class);
searchFilterDTO.setStartTime(dto.getEndTime());
searchFilterDTO.setEndTime(dto.getEndTime() + 1);
Long count = esClientDao.count(indexes, searchWholeAnalyzeQuery(searchFilterDTO), null);
res.add(new LineVO(count, dto.getEndTime()));
}
return res;
}
......@@ -897,12 +916,19 @@ public class SearchWholeServiceImpl implements SearchWholeService {
// 平台聚合
Script script = new Script("doc['c5'].value +'_' +doc['foreign'].value");
TermsAggregationBuilder platformAggregationBuilder = AggregationBuilders.terms("platformAgg").script(script).order(BucketOrder.count(false));
DateHistogramAggregationBuilder daysAggregationBuilder;
// 天级以小时为颗粒度,其他以天作为颗粒度
if (dto.getEndTime() - dto.getStartTime() <= Constant.ONE_DAY){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.HOUR);
}else {
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.DAY);
DateHistogramAggregationBuilder daysAggregationBuilder = null;
long timeRange = dto.getEndTime() - dto.getStartTime();
// 时间间隔≤7天,数据颗粒度为“小时级”
if (timeRange <= Constant.ONE_WEEK){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.HOUR).offset("16h");
}
// 7天<时间间隔≤180天,数据颗粒度为“天级”
if (timeRange > Constant.ONE_WEEK && timeRange <= 6 * Constant.ONE_MONTH){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.DAY).offset("16h");
}
// 180天<时间间隔,数据颗粒度为“月级”
if (timeRange > 6 * Constant.ONE_MONTH){
daysAggregationBuilder = AggregationBuilders.dateHistogram("timeAgg").field("time").calendarInterval(DateHistogramInterval.MONTH).offset("-8h");
}
// query
BoolQueryBuilder query = searchWholeAnalyzeQuery(dto);
......@@ -936,9 +962,24 @@ public class SearchWholeServiceImpl implements SearchWholeService {
});
}
});
// 合并数据
platformLines.forEach((k1, v1) -> {
List<LineVO> line = new ArrayList<>();
v1.forEach((k2, v2) -> line.add(new LineVO(v2, Long.valueOf(k2))));
// 结束点数据补充完全
if (CollectionUtils.isNotEmpty(line)){
line.get(0).setDate(dto.getStartTime());
SearchFilterDTO searchFilterDTO = Tools.convertMap(dto, SearchFilterDTO.class);
searchFilterDTO.setPlatforms(Collections.singletonList(GlobalPojo.getPlatformIdByName(k1)));
searchFilterDTO.setStartTime(dto.getEndTime());
searchFilterDTO.setEndTime(dto.getEndTime() + 1);
Long count = 0L;
try {
count = esClientDao.count(indexes, searchWholeAnalyzeQuery(searchFilterDTO), null);
} catch (IOException ignore) {
}
line.add(new LineVO(count, dto.getEndTime()));
}
res.put(k1, line);
});
return res;
......
......@@ -1397,4 +1397,29 @@ public class Tools {
long ss = BigDecimal.valueOf(temp).divideAndRemainder(BigDecimal.valueOf(60))[1].setScale(0, RoundingMode.HALF_UP).longValue();
return ((mm < 10 ? ("0" + mm) : mm) + ":" + (ss < 10 ? ("0" + ss) : ss));
}
public static Long[] formatSpreadTendencyTime(long sTime, long eTime){
long timeRange = eTime - sTime;
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(new Date(sTime));
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(new Date(eTime));
calendar1.set(Calendar.MINUTE, 0);
calendar1.set(Calendar.SECOND, 0);
calendar1.set(Calendar.MILLISECOND, 0);
calendar2.set(Calendar.MINUTE, 0);
calendar2.set(Calendar.SECOND, 0);
calendar2.set(Calendar.MILLISECOND, 0);
if (timeRange > Constant.ONE_WEEK && timeRange <= 6 * Constant.ONE_MONTH){
calendar1.set(Calendar.HOUR_OF_DAY, 0);
calendar2.set(Calendar.HOUR_OF_DAY, 0);
}
if (timeRange > 6 * Constant.ONE_MONTH){
calendar1.set(Calendar.HOUR_OF_DAY, 0);
calendar2.set(Calendar.HOUR_OF_DAY, 0);
calendar1.set(Calendar.DAY_OF_MONTH, 1);
calendar2.set(Calendar.DAY_OF_MONTH, 1);
}
return new Long[]{calendar1.getTime().getTime(), calendar2.getTime().getTime()};
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment