提交 690b55f0 作者: 925993793@qq.com

热度计算

上级 491cb7ce
...@@ -8,14 +8,6 @@ public class Constants { ...@@ -8,14 +8,6 @@ public class Constants {
public static final String SUBJECT_ANALYSIS_PRE = "SUBJECT_ANALYSIS::"; public static final String SUBJECT_ANALYSIS_PRE = "SUBJECT_ANALYSIS::";
//传播路径 //传播路径
public static final String PROPAGATION_KEY = "PROPAGATION_PATH::"; public static final String PROPAGATION_KEY = "PROPAGATION_PATH::";
/**
* 关键词数据入缓存 key前缀
*/
public static final String KEY_WORDS_TO_REDIS_PREFIX = "KEY_WORDS_TO_REDIS::";
public static final String MAX_TOTAL_HOT_SCORE = "MAX_TOTAL_HOT_SCORE";
public static final String MAX_MEDIA_HOT_SCORE = "MAX_MEDIA_HOT_SCORE";
public static final String MAX_WECHAT_HOT_SCORE = "MAX_WECHAT_HOT_SCORE";
public static final String MAX_OTHER_HOT_SCORE = "MAX_OTHER_HOT_SCORE";
public static String[] FETCH_FIELDS_STATISTIC = {"id", "subjectId","title","origin", "publishDate", "sourceAddress"}; public static String[] FETCH_FIELDS_STATISTIC = {"id", "subjectId","title","origin", "publishDate", "sourceAddress"};
public static String[] FETCH_FIELDS_DATA = {"id", "subjectId", "title", "content", "publishDate", "origin", "sourceAddress"}; public static String[] FETCH_FIELDS_DATA = {"id", "subjectId", "title", "content", "publishDate", "origin", "sourceAddress"};
......
...@@ -42,7 +42,7 @@ public class KafkaConsumer { ...@@ -42,7 +42,7 @@ public class KafkaConsumer {
* *
* @param record 接受的kafka数据 * @param record 接受的kafka数据
*/ */
// @KafkaListener(topics = {Constants.VIEWPOINT_RECEIVE_TOPIC}) @KafkaListener(topics = {Constants.VIEWPOINT_RECEIVE_TOPIC})
public void viewPointAnalysis(ConsumerRecord<String, String> record) { public void viewPointAnalysis(ConsumerRecord<String, String> record) {
String value = record.value(); String value = record.value();
if (StringUtils.isNotEmpty(value)) { if (StringUtils.isNotEmpty(value)) {
...@@ -74,7 +74,7 @@ public class KafkaConsumer { ...@@ -74,7 +74,7 @@ public class KafkaConsumer {
* *
* @param record 接受的kafka数据 * @param record 接受的kafka数据
*/ */
// @KafkaListener(topics = {Constants.EVENT_CONTEXT_RECEIVE_TOPIC}) @KafkaListener(topics = {Constants.EVENT_CONTEXT_RECEIVE_TOPIC})
public void eventContext(ConsumerRecord<String, String> record) { public void eventContext(ConsumerRecord<String, String> record) {
String value = record.value(); String value = record.value();
if (StringUtils.isNotEmpty(value)) { if (StringUtils.isNotEmpty(value)) {
...@@ -98,7 +98,7 @@ public class KafkaConsumer { ...@@ -98,7 +98,7 @@ public class KafkaConsumer {
* *
* @param record 接受的kafka数据 * @param record 接受的kafka数据
*/ */
// @KafkaListener(topics = {Constants.FAKE_EVENT_CONTEXT_RECEIVE_TOPIC}) @KafkaListener(topics = {Constants.FAKE_EVENT_CONTEXT_RECEIVE_TOPIC})
public void eventContext_fake(ConsumerRecord<String, String> record) { public void eventContext_fake(ConsumerRecord<String, String> record) {
String value = record.value(); String value = record.value();
if (StringUtils.isNotEmpty(value)) { if (StringUtils.isNotEmpty(value)) {
...@@ -132,7 +132,7 @@ public class KafkaConsumer { ...@@ -132,7 +132,7 @@ public class KafkaConsumer {
* @author lkg * @author lkg
* @date 2024/4/12 * @date 2024/4/12
*/ */
// @KafkaListener(topics = {Constants.EVENT_REPORT_RECEIVE_TOPIC}) @KafkaListener(topics = {Constants.EVENT_REPORT_RECEIVE_TOPIC})
public void eventReport(ConsumerRecord<String, String> record) { public void eventReport(ConsumerRecord<String, String> record) {
String value = record.value(); String value = record.value();
EventAnalysisReport eventAnalysisReport = JSONObject.parseObject(value, EventAnalysisReport.class); EventAnalysisReport eventAnalysisReport = JSONObject.parseObject(value, EventAnalysisReport.class);
......
...@@ -3,21 +3,22 @@ package com.zzsn.event.task; ...@@ -3,21 +3,22 @@ package com.zzsn.event.task;
import cn.hutool.core.date.DateField; import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.zzsn.event.constant.Constants; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zzsn.event.entity.Event; import com.zzsn.event.entity.Event;
import com.zzsn.event.service.EsStatisticsService; import com.zzsn.event.service.EsStatisticsService;
import com.zzsn.event.service.IEventService; import com.zzsn.event.service.IEventService;
import com.zzsn.event.util.RedisUtil; import com.zzsn.event.util.CalculateUtil;
import com.zzsn.event.vo.HotVO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/** /**
* *
...@@ -34,9 +35,6 @@ public class EventHotTask { ...@@ -34,9 +35,6 @@ public class EventHotTask {
@Autowired @Autowired
private EsStatisticsService esStatisticsService; private EsStatisticsService esStatisticsService;
@Autowired
private RedisUtil redisUtil;
@Value("${scoreRule.weekScore}") @Value("${scoreRule.weekScore}")
Integer weekScore; Integer weekScore;
@Value("${scoreRule.monthScore}") @Value("${scoreRule.monthScore}")
...@@ -56,46 +54,53 @@ public class EventHotTask { ...@@ -56,46 +54,53 @@ public class EventHotTask {
@Scheduled(cron = "0 0 0/3 * * ?") @Scheduled(cron = "0 0 0/3 * * ?")
public void hot() { public void hot() {
List<Event> list = eventService.list(); List<Event> list = eventService.list();
List<HotVO> countList = new ArrayList<>();
for (Event event : list) { for (Event event : list) {
CompletableFuture.runAsync(() -> { CompletableFuture<HotVO> async = CompletableFuture.supplyAsync(() -> {
HotVO hotVO = new HotVO();
String id = event.getId(); String id = event.getId();
//total int count = computeScore(id);
Integer totalScore = computeScore(id); hotVO.setId(id);
Integer totalHot = getHot(Constants.MAX_TOTAL_HOT_SCORE + "::" + id, totalScore); hotVO.setCount(count);
event.setTotalHot(totalHot); return hotVO;
/*//media });
Integer mediaScore = 0; try {
Integer mediaHot = getHot(Constants.MAX_MEDIA_HOT_SCORE + "::" + id, mediaScore); HotVO hotVO = async.get();
//wechat countList.add(hotVO);
Integer wechatScore = 0; } catch (Exception e) {
Integer wechatHot = getHot(Constants.MAX_WECHAT_HOT_SCORE + "::" + id, wechatScore); e.printStackTrace();
//others }
Integer otherScore = 0; }
Integer otherHot = getHot(Constants.MAX_OTHER_HOT_SCORE + "::" + id, otherScore); countList.sort(Comparator.comparingInt(HotVO::getCount));
event.setMediaHot(mediaHot); Integer min = countList.get(0).getCount();
event.setWechatHot(wechatHot); Integer max = countList.get(countList.size() - 1).getCount();
event.setOtherHot(otherHot);*/ Map<String, List<HotVO>> hotMap = countList.stream().collect(Collectors.groupingBy(HotVO::getId));
//最小-最大归一化(Min-Max Normalization)
for (Event event : list) {
List<HotVO> hotVOS = hotMap.get(event.getId());
if (CollectionUtils.isNotEmpty(hotVOS)) {
HotVO hotVO = hotVOS.get(0);
Integer hot = normalize(hotVO.getCount(), min, max);
event.setTotalHot(hot);
eventService.updateById(event); eventService.updateById(event);
log.info("专题-{}-热度计算完成", event.getEventName()); log.info("专题-{}-热度计算完成", event.getEventName());
}); }
} }
} }
private Integer getHot(String key, Integer score) { //归一化计算
Object total = redisUtil.get(key); private Integer normalize(Integer value, Integer min,Integer max) {
if (null != total) { int normalizeValue = 0;
Integer maxTotalScore = (Integer) total; if (value > 0) {
if (score > maxTotalScore) { int param1 = (value - min) * 100;
redisUtil.set(key, score); int param2 = (max - min);
return 100; String divide = CalculateUtil.divide(String.valueOf(param1), String.valueOf(param2), 0);
} else { if (StringUtils.isNotEmpty(divide)) {
return score / maxTotalScore * 100; normalizeValue = Integer.parseInt(divide);
} }
} else { }
redisUtil.set(key, score); return normalizeValue;
return 100;
}
} }
/** /**
......
package com.zzsn.event.vo;
import lombok.Data;
/**
* recursion
*
* @author lkg
* @date 2024/5/9
*/
@Data
public class HotVO {
private String id;
private Integer count;
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论