Commit 4f1a595f by shenjunjie

2022/8/10 9:44

parent 9277c5d1
......@@ -77,6 +77,7 @@ public class EventDaoImpl extends BaseMongoDaoImpl<Event> implements EventDao {
Aggregation.group("patternDate").count().as("eventCount"));
AggregationResults<JSONObject> aggregate = mongoTemplate.aggregate(agg, getAggreeCollection(), JSONObject.class);
List<JSONObject> mappedResults = aggregate.getMappedResults();
// TODO
return new HashMap<>();
}
......
......@@ -92,6 +92,7 @@ public class Channel extends ChannelIndex {
channel.setCTime(new Date().getTime());
channel.setProjectId(channelIndex.getProjectId());
channel.setLinkedGroupId(channelIndex.getLinkedGroupId());
channel.setContendId(channelIndex.getContendId());
channel.setPlatform(channelIndex.getPlatform());
channel.setRealSource(channelIndex.getRealSource());
channel.setSource(channelIndex.getSource());
......
......@@ -219,4 +219,10 @@ public interface ChannelService {
* @return 稿件信息
*/
List<ExportAppChannelArticleDTO> downloadArticlesByTime(Long startTime, Long endTime, String channelId, String contendId);
/**
* 计算渠道倾向及指数
* @param channelId 渠道ID
*/
void calculateChannelEmotionIndex(String channelId);
}
......@@ -668,6 +668,37 @@ public class ChannelServiceImpl implements ChannelService {
return futureList.stream().map(CompletableFuture::join).collect(Collectors.toList());
}
@Override
public void calculateChannelEmotionIndex(String channelId) {
try {
Channel channel = channelDao.findOneById(channelId);
// 数据格式变化未含有contendId部分
if (null == channel.getContendId()) {
channel.setContendId("0");
}
if (hasEmotion(channel)) {
//该渠道发布正面稿件
Long positiveCount = markCountByEmotion(channel, EmotionEnum.POSITIVE.getName());
//该渠道发布中性稿件
Long neutralCount = markCountByEmotion(channel, EmotionEnum.NEUTRAL.getName());
//该渠道发布负面稿件
Long negativeCount = markCountByEmotion(channel, EmotionEnum.NEGATIVE.getName());
//是否友好渠道
boolean isPositive = false;
}
} catch (Exception e) {
log.error("calculateChannelEmotionIndex-", e);
}
}
private Long markCountByEmotion(Channel channel, String emotion) throws IOException {
BoolQueryBuilder postFilter = MarkDataServiceImpl.projectLinkedGroupContendIdQuery(channel.getProjectId(), channel.getLinkedGroupId(), channel.getContendId());
postFilter.must(QueryBuilders.termQuery("brandkbs_mark_cache_maps.name.keyword", emotion));
return esClientDao.count(postFilter);
}
private Map<String, List<ChannelIndex.Article>> getSourceContendMap(String channelId, Collection<String> contendIds, String platform, String keyword, Long startTime, Long endTime) {
try {
String projectId = UserThreadLocal.getProjectId();
......@@ -704,9 +735,8 @@ public class ChannelServiceImpl implements ChannelService {
res.put("negativeCount", negativeCount);
res.put("articleTotal", total);
// 做分母时不能为0
total = 0 == total ? 1 : total;
res.put("positivePercent", new BigDecimal((double) positiveCount * 100 / total).setScale(1, RoundingMode.UP));
res.put("negativePercent", new BigDecimal((double) negativeCount * 100 / total).setScale(1, RoundingMode.UP));
res.put("positivePercent", BigDecimal.valueOf((double) positiveCount * 100 / (0 == total ? 1 : total)).setScale(1, RoundingMode.UP));
res.put("negativePercent", BigDecimal.valueOf((double) negativeCount * 100 / (0 == total ? 1 : total)).setScale(1, RoundingMode.UP));
return Pair.of(total, res);
}
......@@ -743,23 +773,21 @@ public class ChannelServiceImpl implements ChannelService {
private List<JSONObject> spreadingTendData(Long startTime, Long endTime, List<ChannelIndex.Article> articleList, String timePattern) {
// 按日分组并根据id去重保留最近标注时间
Map<Long, List<ChannelIndex.Article>> dateListMap = partition(startTime, endTime, timePattern, articleList);
List<JSONObject> collect = dateListMap.entrySet().stream().sorted(Comparator.comparingLong(Map.Entry::getKey)).map(e -> {
return dateListMap.entrySet().stream().sorted(Comparator.comparingLong(Map.Entry::getKey)).map(e -> {
JSONObject spreadJson = new JSONObject();
spreadJson.put("time", e.getKey());
spreadJson.put("sum", e.getValue().size());
return spreadJson;
}).collect(Collectors.toList());
Collections.reverse(collect);
return collect;
}
private List<JSONObject> spreadingTendEvent(Long startTime, Long endTime, Channel channel, String projectId, String contendId, String timePattern) {
String linkedGroupId = projectService.getProjectByContendId(contendId).getBrandLinkedGroupId();
Map<Long, List<Event>> eventCount;
Map<Long, List<Event>> eventCount = completeTimes(startTime, endTime, timePattern);
if (Constant.MONTH_PATTERN.equals(timePattern)) {
eventCount = eventDao.getEventMonth(new ChannelIndex(projectId, linkedGroupId, channel), startTime, endTime);
eventCount.putAll(eventDao.getEventMonth(new ChannelIndex(projectId, linkedGroupId, channel), startTime, endTime));
} else {
eventCount = eventDao.getEventDay(new ChannelIndex(projectId, linkedGroupId, channel), startTime, endTime);
eventCount.putAll(eventDao.getEventDay(new ChannelIndex(projectId, linkedGroupId, channel), startTime, endTime));
}
return eventCount.entrySet().stream().sorted(Comparator.comparingLong(Map.Entry::getKey)).map(e -> {
JSONObject spreadJson = new JSONObject();
......@@ -804,11 +832,7 @@ public class ChannelServiceImpl implements ChannelService {
* @return
*/
private Map<Long, List<ChannelIndex.Article>> partition(Long startTime, Long endTime, String timePattern, List<ChannelIndex.Article> articles) {
Map<Long, List<ChannelIndex.Article>> res = new LinkedHashMap<>();
// 自动补全时间段
for (Long timeKey : Tools.parseToDayList(startTime, endTime)) {
res.put(timeKey, Lists.newArrayList());
}
Map<Long, List<ChannelIndex.Article>> res = completeTimes(startTime, endTime, timePattern);
for (ChannelIndex.Article article : articles) {
// 按日分组
long key = Tools.truncDate(new Date(article.getTime()), timePattern).getTime();
......@@ -899,4 +923,42 @@ public class ChannelServiceImpl implements ChannelService {
return new String[]{null, null};
}
private boolean hasEmotion(Channel channel) {
if ("0".equals(channel.getContendId())) {
return true;
}
AbstractProject project = projectService.getProjectByContendId(channel.getContendId());
if (project instanceof Contend) {
//有情感倾向更新渠道指数
return ((Contend) project).isHasEmotion();
}
return project instanceof Project;
}
/**
* 自动补全时间段
*
* @param startTime
* @param endTime
* @param timePattern
* @return
*/
private <T> Map<Long, List<T>> completeTimes(Long startTime, Long endTime, String timePattern) {
Map<Long, List<T>> res = new LinkedHashMap<>();
if (Constant.DAY_PATTERN.equals(timePattern)) {
for (Long timeKey : Tools.parseToDayList(startTime, endTime)) {
res.put(timeKey, Lists.newArrayList());
}
} else if (Constant.MONTH_PATTERN.equals(timePattern)) {
for (Long timeKey : Tools.parseToMonthList(startTime, endTime)) {
res.put(timeKey, Lists.newArrayList());
}
}
return res;
}
private long judgeSpecEventCount(String channelId, List<Integer> eventEmotions, int articleEmotion) {
return 0;
}
}
......@@ -64,7 +64,6 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
......@@ -358,7 +357,7 @@ public class MarkDataServiceImpl implements MarkDataService {
result.put("negTopTitle", topTitle);
List<JSONObject> articlePlatformProportion = getMarkPlatformProportion(startTime, endTime, projectId, linkedGroupId, true);
result.put("platformRank", articlePlatformProportion.stream().filter(articlePlatform -> articlePlatform.getLongValue("count") > 0).sorted((o1, o2) -> o2.getLong("count").compareTo(o1.getLong("count"))).collect(Collectors.toList()));
redisUtil.setExpire(redisKey, JSON.toJSONString(result), 1, TimeUnit.HOURS);
redisUtil.setExpire(redisKey, JSON.toJSONString(result));
return result;
} catch (IOException e) {
ExceptionCast.cast(CommonCodeEnum.FAIL.message("es查询异常"), e);
......@@ -404,7 +403,7 @@ public class MarkDataServiceImpl implements MarkDataService {
result.put("lastPosCount", oldEmotions.get(0).getLongValue("count"));
result.put("lastNeuCount", oldEmotions.get(1).getLongValue("count"));
result.put("lastNegCount", oldEmotions.get(2).getLongValue("count"));
redisUtil.setExpire(redisKey, JSON.toJSONString(result), 1, TimeUnit.HOURS);
redisUtil.setExpire(redisKey, JSON.toJSONString(result));
return result;
} catch (IOException e) {
ExceptionCast.cast(CommonCodeEnum.FAIL.message("es查询异常"), e);
......@@ -433,6 +432,7 @@ public class MarkDataServiceImpl implements MarkDataService {
} catch (IOException e) {
ExceptionCast.cast(CommonCodeEnum.FAIL.message("es查询异常"));
}
redisUtil.setExpire(redisKey, JSON.toJSONString(result));
return result;
}
......@@ -753,7 +753,7 @@ public class MarkDataServiceImpl implements MarkDataService {
line.put("time", day.getLongValue("date"));
lineList.add(line);
});
redisUtil.setExpire(redisKey, JSON.toJSONString(lineList), 1, TimeUnit.HOURS);
redisUtil.setExpire(redisKey, JSON.toJSONString(result));
return lineList;
}
......@@ -946,7 +946,7 @@ public class MarkDataServiceImpl implements MarkDataService {
List<JSONObject> platformsCount = getPlatformsCount(startTime, endTime, null, null, projectId, linkedGroupId, platformList);
long articlesCount = platformsCount.stream().mapToLong(platform -> platform.getLongValue("count")).sum();
List<JSONObject> resultList = platformsCount.stream().peek(platform -> platform.put("proportion", 0 == articlesCount ? 0 : platform.getLongValue("count") * 1.0 / articlesCount)).collect(Collectors.toList());
redisUtil.setExpire(redisKey, JSON.toJSONString(resultList), 1, TimeUnit.HOURS);
redisUtil.setExpire(redisKey, JSON.toJSONString(resultList));
return resultList;
}
......@@ -1076,7 +1076,7 @@ public class MarkDataServiceImpl implements MarkDataService {
return QueryBuilders.boolQuery().must(QueryBuilders.termQuery("brandkbs_cache_maps.project_id.keyword", projectId)).must(QueryBuilders.termQuery("brandkbs_cache_maps.linked_group_id.keyword", linkedGroupId));
}
private static BoolQueryBuilder projectLinkedGroupContendIdQuery(String projectId, String linkedGroupId, String contendId) {
protected static BoolQueryBuilder projectLinkedGroupContendIdQuery(String projectId, String linkedGroupId, String contendId) {
return QueryBuilders.boolQuery().must(QueryBuilders.termQuery("brandkbs_cache_maps.key.keyword", Tools.concat(projectId, linkedGroupId, contendId)));
}
......
......@@ -674,6 +674,31 @@ public class Tools {
* @param endTime 结束时间
* @return 按日分割的map集合
*/
public static List<Long> parseToMonthList(Long startTime, Long endTime) {
Date start = new Date(startTime);
Date end = new Date(endTime);
start = Tools.truncDate(start, MONTH_PATTERN);
end = Tools.truncDate(end, MONTH_PATTERN);
Period periodDays = new Period(start.getTime(), end.getTime(), PeriodType.months());
int months = periodDays.getMonths();
if (months < 0) {
return Collections.emptyList();
}
List<Long> resList = new ArrayList<>(months);
for (int i = 0; i <= months; i++) {
resList.add(DateUtils.addMonths(start, i).getTime());
}
Collections.reverse(resList);
return resList;
}
/**
* 解析时间转换成按日的集合
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 按日分割的map集合
*/
public static List<Map<String, String>> parseToDays(Long startTime, Long endTime) {
FastDateFormat df = DAY_FORMAT;
Date start = Tools.truncDate(new Date(startTime), Constant.DAY_PATTERN);
......
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