Commit 38c29f07 authored by 刘基明's avatar 刘基明

排序落库

parent 30ed778b
...@@ -38,16 +38,19 @@ public class TopicRankQo { ...@@ -38,16 +38,19 @@ public class TopicRankQo {
@ApiModelProperty(value = "话题下的帖子权重") @ApiModelProperty(value = "话题下的帖子权重")
private Integer themeWeight; private Integer themeWeight;
private Integer hoursTillNow;
/** /**
* TODO 热度计算算法 * 热度计算算法
* *
* @return * @return
*/ */
public Integer getRank() { public Integer getRank() {
double g = 0.3;//时间系数
//顶置话题 //顶置话题
if (isTop > 0) { if (isTop > 0) {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
return this.disscussCount * 3 + viewCount + themeWeight; return (int)((disscussCount * 3 + viewCount)/ Math.pow(hoursTillNow + 1, g)) + themeWeight;
} }
} }
package com.tanpu.community.api.enums;
import org.apache.commons.collections4.SetUtils;
import java.util.HashSet;
public enum RankLogTypeEnum {
TOPIC(1,"话题排序"),THEME(2,"主题排序");
public static final HashSet<String> imageTypeSet = SetUtils.hashSet("jpg", "jpeg", "png");
private Integer code;
private String type;
RankLogTypeEnum(Integer code, String type) {
this.code = code;
this.type = type;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
...@@ -5,6 +5,9 @@ import com.baomidou.mybatisplus.annotation.TableId; ...@@ -5,6 +5,9 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime; import java.time.LocalDateTime;
...@@ -17,9 +20,12 @@ import java.time.LocalDateTime; ...@@ -17,9 +20,12 @@ import java.time.LocalDateTime;
* @author xudong * @author xudong
* @since 2021-07-30 * @since 2021-07-30
*/ */
@TableName("topic_rank_log") @TableName("rank_log")
@ApiModel(value="TopicRankLogEntity对象", description="话题排序日志记录’") @Builder
public class TopicRankLogEntity implements Serializable { @AllArgsConstructor
@NoArgsConstructor
@ApiModel(value="RankLogEntity对象", description="话题排序日志记录’")
public class RankLogEntity implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
...@@ -27,6 +33,9 @@ public class TopicRankLogEntity implements Serializable { ...@@ -27,6 +33,9 @@ public class TopicRankLogEntity implements Serializable {
@TableId(value = "id", type = IdType.AUTO) @TableId(value = "id", type = IdType.AUTO)
private Long id; private Long id;
@ApiModelProperty(value = "1:话题 2:主题")
private Integer type;
@ApiModelProperty(value = "总排序数量") @ApiModelProperty(value = "总排序数量")
private Integer totalCount; private Integer totalCount;
...@@ -37,7 +46,13 @@ public class TopicRankLogEntity implements Serializable { ...@@ -37,7 +46,13 @@ public class TopicRankLogEntity implements Serializable {
private Integer pageSize; private Integer pageSize;
@ApiModelProperty(value = "排序内容") @ApiModelProperty(value = "排序内容")
private String info; private String content;
@ApiModelProperty(value = "花费间隔,单位毫秒")
private Long rankCost;
@ApiModelProperty(value = "排序时间")
private LocalDateTime rankTime;
private LocalDateTime createTime; private LocalDateTime createTime;
...@@ -54,6 +69,14 @@ public class TopicRankLogEntity implements Serializable { ...@@ -54,6 +69,14 @@ public class TopicRankLogEntity implements Serializable {
this.id = id; this.id = id;
} }
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Integer getTotalCount() { public Integer getTotalCount() {
return totalCount; return totalCount;
} }
...@@ -78,12 +101,28 @@ public class TopicRankLogEntity implements Serializable { ...@@ -78,12 +101,28 @@ public class TopicRankLogEntity implements Serializable {
this.pageSize = pageSize; this.pageSize = pageSize;
} }
public String getInfo() { public String getContent() {
return info; return content;
}
public void setContent(String content) {
this.content = content;
}
public Long getRankCost() {
return rankCost;
}
public void setRankCost(Long rankCost) {
this.rankCost = rankCost;
}
public LocalDateTime getRankTime() {
return rankTime;
} }
public void setInfo(String info) { public void setRankTime(LocalDateTime rankTime) {
this.info = info; this.rankTime = rankTime;
} }
public LocalDateTime getCreateTime() { public LocalDateTime getCreateTime() {
...@@ -112,12 +151,15 @@ public class TopicRankLogEntity implements Serializable { ...@@ -112,12 +151,15 @@ public class TopicRankLogEntity implements Serializable {
@Override @Override
public String toString() { public String toString() {
return "TopicRankLogEntity{" + return "RankLogEntity{" +
"id=" + id + "id=" + id +
", type=" + type +
", totalCount=" + totalCount + ", totalCount=" + totalCount +
", pageNumber=" + pageNumber + ", pageNumber=" + pageNumber +
", pageSize=" + pageSize + ", pageSize=" + pageSize +
", info=" + info + ", content=" + content +
", rankCost=" + rankCost +
", rankTime=" + rankTime +
", createTime=" + createTime + ", createTime=" + createTime +
", updateTime=" + updateTime + ", updateTime=" + updateTime +
", deleteTag=" + deleteTag + ", deleteTag=" + deleteTag +
......
package com.tanpu.community.dao.mapper.community; package com.tanpu.community.dao.mapper.community;
import com.tanpu.community.dao.entity.community.TopicRankLogEntity; import com.tanpu.community.dao.entity.community.RankLogEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/** /**
...@@ -11,6 +11,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; ...@@ -11,6 +11,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @author xudong * @author xudong
* @since 2021-07-30 * @since 2021-07-30
*/ */
public interface TopicRankLogMapper extends BaseMapper<TopicRankLogEntity> { public interface RankLogMapper extends BaseMapper<RankLogEntity> {
} }
package com.tanpu.community.service;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.beans.qo.ThemeAnalysDO;
import com.tanpu.community.api.beans.qo.TopicRankQo;
import com.tanpu.community.api.enums.RankLogTypeEnum;
import com.tanpu.community.dao.entity.community.RankLogEntity;
import com.tanpu.community.dao.mapper.community.RankLogMapper;
import com.tanpu.community.util.BizUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class RankLogService {
@Resource
private RankLogMapper rankLogMapper;
private Integer pageSize = 50;
//话题排序日志
public void logTopicRank(List<TopicRankQo> rankList, LocalDateTime logTime, Long cost) {
if (CollectionUtils.isEmpty(rankList)) {
return;
}
//分页插入
for (int i = 0; i * pageSize < rankList.size(); i++) {
int pageStart = i * pageSize;
List<TopicRankQo> sublist = BizUtils.subList(rankList, pageStart, pageSize);
RankLogEntity entity = RankLogEntity.builder().rankTime(logTime)
.type(RankLogTypeEnum.TOPIC.getCode())
.totalCount(rankList.size())
.rankCost(cost)
.content(JsonUtil.toJson(sublist))
.pageNumber(i + 1)
.pageSize(sublist.size())
.build();
rankLogMapper.insert(entity);
}
}
//主题排序日志
public void logThemeRank(List<ThemeAnalysDO> themeList, LocalDateTime logTime, Long cost) {
if (CollectionUtils.isEmpty(themeList)) {
return;
}
//分页插入
for (int i = 0; i * pageSize < themeList.size(); i++) {
int pageStart = i * pageSize;
List<ThemeAnalysDO> sublist = BizUtils.subList(themeList, pageStart, pageSize);
RankLogEntity entity = RankLogEntity.builder().rankTime(logTime)
.type(RankLogTypeEnum.THEME.getCode())
.totalCount(themeList.size())
.rankCost(cost)
.content(JsonUtil.toJson(sublist))
.pageNumber(i + 1)
.pageSize(sublist.size())
.build();
rankLogMapper.insert(entity);
}
}
}
...@@ -9,16 +9,20 @@ import com.tanpu.community.api.enums.VisitTypeEnum; ...@@ -9,16 +9,20 @@ import com.tanpu.community.api.enums.VisitTypeEnum;
import com.tanpu.community.cache.RedisCache; import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.dao.entity.community.ThemeEntity; import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity; import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.feign.fatools.FeignClientForFatools;
import com.tanpu.community.util.BizUtils; import com.tanpu.community.util.BizUtils;
import com.tanpu.community.util.ConvertUtil; import com.tanpu.community.util.ConvertUtil;
import com.tanpu.community.util.TimeUtils;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.*; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USER_INFO; import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USER_INFO;
...@@ -43,7 +47,7 @@ public class RankService { ...@@ -43,7 +47,7 @@ public class RankService {
private RedisCache redisCache; private RedisCache redisCache;
@Resource @Resource
private FeignClientForFatools feignClientForFatools; private RankLogService rankLogService;
//最热 //最热
private List<ThemeAnalysDO> hotestThemes = new ArrayList<>(); private List<ThemeAnalysDO> hotestThemes = new ArrayList<>();
...@@ -56,6 +60,8 @@ public class RankService { ...@@ -56,6 +60,8 @@ public class RankService {
* 计算主题热度排行 * 计算主题热度排行
*/ */
public void rankThemes() { public void rankThemes() {
LocalDateTime start = LocalDateTime.now();
//7天内所有主题进行热度值排序 //7天内所有主题进行热度值排序
List<ThemeEntity> themeEntities = themeService.queryRecentdays(7); List<ThemeEntity> themeEntities = themeService.queryRecentdays(7);
if (CollectionUtils.isEmpty(themeEntities)) { if (CollectionUtils.isEmpty(themeEntities)) {
...@@ -95,6 +101,8 @@ public class RankService { ...@@ -95,6 +101,8 @@ public class RankService {
hotestThemes = map.entrySet().stream() hotestThemes = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.map(e -> e.getKey()).collect(Collectors.toList()); .map(e -> e.getKey()).collect(Collectors.toList());
//落库
rankLogService.logThemeRank(hotestThemes,start, TimeUtils.calMillisTillNow(start));
} }
...@@ -104,6 +112,7 @@ public class RankService { ...@@ -104,6 +112,7 @@ public class RankService {
* @return * @return
*/ */
public void rankTopics() { public void rankTopics() {
LocalDateTime start = LocalDateTime.now();
List<TopicEntity> topicEntities = topicService.queryAll(); List<TopicEntity> topicEntities = topicService.queryAll();
if (CollectionUtils.isEmpty(topicEntities)) { if (CollectionUtils.isEmpty(topicEntities)) {
return; return;
...@@ -145,6 +154,9 @@ public class RankService { ...@@ -145,6 +154,9 @@ public class RankService {
rankList.get(0).setType(TopicStatusEnum.HOTTEST.getCode()); rankList.get(0).setType(TopicStatusEnum.HOTTEST.getCode());
this.rankTopicList = rankList; this.rankTopicList = rankList;
this.rankTopicListTop4 = rankList.stream().limit(4).collect(Collectors.toList()); this.rankTopicListTop4 = rankList.stream().limit(4).collect(Collectors.toList());
//落库
rankLogService.logTopicRank(rankList,start, TimeUtils.calMillisTillNow(start));
return; return;
} }
......
...@@ -218,7 +218,7 @@ public class ThemeService { ...@@ -218,7 +218,7 @@ public class ThemeService {
return themeMapper.selectList(queryWrapper); return themeMapper.selectList(queryWrapper);
} }
//根据话题查询所有的主题Id //根据话题查询所有的主题Id,包括已删除的主题
public List<String> queryThemeIdsByTopic(String topidId) { public List<String> queryThemeIdsByTopic(String topidId) {
if (StringUtils.isEmpty(topidId)) { if (StringUtils.isEmpty(topidId)) {
return Collections.emptyList(); return Collections.emptyList();
......
...@@ -94,6 +94,7 @@ public class ConvertUtil { ...@@ -94,6 +94,7 @@ public class ConvertUtil {
if(TimeUtils.calMinuteTillNow(topicEntity.getCreateTime())<120){ if(TimeUtils.calMinuteTillNow(topicEntity.getCreateTime())<120){
topicRankQo.setType(TopicStatusEnum.NEWEST.getCode()); topicRankQo.setType(TopicStatusEnum.NEWEST.getCode());
} }
topicRankQo.setHoursTillNow((int) TimeUtils.calHoursTillNow(topicEntity.getCreateTime()));
return topicRankQo; return topicRankQo;
} }
......
...@@ -37,12 +37,30 @@ public class TimeUtils { ...@@ -37,12 +37,30 @@ public class TimeUtils {
return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")); return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
} }
//计算迄今时间 //计算迄今分钟
public static long calMinuteTillNow(LocalDateTime start) { public static long calMinuteTillNow(LocalDateTime start) {
Duration between = Duration.between(start, LocalDateTime.now()); Duration between = Duration.between(start, LocalDateTime.now());
return between.toMinutes(); return between.toMinutes();
} }
//计算迄今毫秒数
public static long calMillisTillNow(LocalDateTime start) {
Duration between = Duration.between(start, LocalDateTime.now());
return between.toMillis();
}
//计算迄今天数
public static long calDaysTillNow(LocalDateTime start) {
Duration between = Duration.between(start, LocalDateTime.now());
return between.toDays();
}
//计算迄今天数
public static long calHoursTillNow(LocalDateTime start) {
Duration between = Duration.between(start, LocalDateTime.now());
return between.toHours();
}
//计算n天前的时间 //计算n天前的时间
public static LocalDateTime getDaysBefore(Integer number) { public static LocalDateTime getDaysBefore(Integer number) {
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tanpu.community.dao.mapper.community.TopicRankLogMapper"> <mapper namespace="com.tanpu.community.dao.mapper.community.RankLogMapper">
<!-- 通用查询映射结果 --> <!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.tanpu.community.dao.entity.community.TopicRankLogEntity"> <resultMap id="BaseResultMap" type="com.tanpu.community.dao.entity.community.RankLogEntity">
<id column="id" property="id" /> <id column="id" property="id" />
<result column="type" property="type" />
<result column="total_count" property="totalCount" /> <result column="total_count" property="totalCount" />
<result column="page_number" property="pageNumber" /> <result column="page_number" property="pageNumber" />
<result column="page_size" property="pageSize" /> <result column="page_size" property="pageSize" />
<result column="info" property="info" /> <result column="content" property="content" />
<result column="rank_cost" property="rankCost" />
<result column="rank_time" property="rankTime" />
<result column="create_time" property="createTime" /> <result column="create_time" property="createTime" />
<result column="update_time" property="updateTime" /> <result column="update_time" property="updateTime" />
<result column="delete_tag" property="deleteTag" /> <result column="delete_tag" property="deleteTag" />
......
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