Commit 9ee50458 by shenjunjie

2022/10/8 17:58

parent 28093d2d
......@@ -19,29 +19,29 @@ import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* @author cjz
* @ClassName AppCrisisController
* @Description 提供前台危机相关信息展示
* @author cjz
* @date 2022-07-19 16:17
*/
@RestController
@RequestMapping("/app/hot")
@Api(tags = "前台热点库",description = "提供前台热点相关信息展示")
@Api(tags = "前台热点库", description = "提供前台热点相关信息展示")
@Auth(role = RoleEnum.CUSTOMER)
public class AppHotController extends BaseController {
private static final long ONE_HOUR = 1L;
......@@ -63,6 +63,10 @@ public class AppHotController extends BaseController {
@Autowired
private MarkDataService markDataService;
@Resource(name = "esSearchExecutor")
ThreadPoolTaskExecutor executor;
/**
* weibo 热搜
* weibo-topic 话题
......@@ -74,19 +78,19 @@ public class AppHotController extends BaseController {
*/
@ApiOperation("热点库-列表")
@GetMapping("/getHotList")
public ResponseResult getHotList(@RequestParam(value = "sortType",defaultValue = "realTime") String sortType,
@RequestParam(value = "type",defaultValue = "weibo") String type){
public ResponseResult getHotList(@RequestParam(value = "sortType", defaultValue = "realTime") String sortType,
@RequestParam(value = "type", defaultValue = "weibo") String type) {
ResponseEntity<JSONObject> jsonObjectResponseEntity = restTemplate.getForEntity(trendsListUrl, JSONObject.class, sortType, type);
Object data = jsonObjectResponseEntity.getBody().get("data");
String redisKey = RedisKeyPrefix.HOT_LIST+type;
String redisKey = RedisKeyPrefix.HOT_LIST + type;
String result = redisUtil.get(redisKey);
//当舆论场崩溃时从缓存里获取
if(Objects.nonNull(data)){
if (Objects.nonNull(data)) {
if (Objects.isNull(result)) {
redisUtil.setExpire(redisKey, JSON.toJSONString(data), ONE_HOUR, TimeUnit.HOURS);
}
return ResponseResult.success(data);
}else{
} else {
if (Objects.nonNull(result)) {
return ResponseResult.success(result);
}
......@@ -96,19 +100,19 @@ public class AppHotController extends BaseController {
@ApiOperation("热点库-热点榜单")
@GetMapping("/hot")
public ResponseResult hot(){
ResponseEntity<String> responseEntity = restTemplate.getForEntity(longTimeInListSearchUrl, String.class,"weibo");
public ResponseResult hot() {
ResponseEntity<String> responseEntity = restTemplate.getForEntity(longTimeInListSearchUrl, String.class, "weibo");
JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
String redisKey = RedisKeyPrefix.HOT_RANK_LIST;
String result = redisUtil.get(redisKey);
//当舆论场崩溃时从缓存里获取
if(jsonObject.getBoolean("state")){
if (jsonObject.getBoolean("state")) {
Object data = jsonObject.get("data");
if (Objects.isNull(result)) {
redisUtil.setExpire(redisKey, JSON.toJSONString(data), ONE_HOUR, TimeUnit.HOURS);
}
return ResponseResult.success(data);
}else{
} else {
if (Objects.nonNull(result)) {
return ResponseResult.success(result);
}
......@@ -129,19 +133,22 @@ public class AppHotController extends BaseController {
markDataService.getMarkTopTitle(startDate.getTime(), endDate.getTime(), null, projectId, linkedGroupId, Constant.PRIMARY_CONTEND_ID, size, include);
List<JSONObject> resultList = markTopTitleList.stream().map(map -> {
JSONObject resultJsonObject = new JSONObject();
resultJsonObject.put("title", map.getKey());
resultJsonObject.put("num", map.getValue());
JSONObject json = new JSONObject();
json.put("title", map.getKey());
json.put("num", map.getValue());
return json;
}).collect(Collectors.toList());
CompletableFuture.allOf(resultList.stream().map(json -> CompletableFuture.supplyAsync(() -> {
try {
BaseMap firstArticle = markDataService.getFirstArticle(startDate.getTime(), endDate.getTime(), map.getKey(), projectId, linkedGroupId, Constant.PRIMARY_CONTEND_ID, include);
resultJsonObject.put("content", firstArticle.getContent());
resultJsonObject.put("url", firstArticle.getUrl());
resultJsonObject.put("realSource", firstArticle.getRealSource());
resultJsonObject.put("emotion", firstArticle.getEmotion());
BaseMap firstArticle = markDataService.getFirstArticle(startDate.getTime(), endDate.getTime(), json.getString("title"), projectId, linkedGroupId, Constant.PRIMARY_CONTEND_ID, include);
json.put("content", firstArticle.getContent());
json.put("url", firstArticle.getUrl());
json.put("realSource", firstArticle.getRealSource());
json.put("emotion", firstArticle.getEmotion());
} catch (IOException ignored) {
}
return resultJsonObject;
}).collect(Collectors.toList());
return null;
}, executor)).toArray(CompletableFuture[]::new)).join();
return ResponseResult.success(resultList);
}
}
......@@ -26,9 +26,9 @@ public class Behavior extends AbstractBaseMongo {
*/
private String projectId;
/**
* nickName
* nickname
*/
private String nickName;
private String nickname;
/**
* IP地址
*/
......
......@@ -13,7 +13,6 @@ import com.zhiwei.brandkbs2.service.BehaviorService;
import com.zhiwei.brandkbs2.util.MongoUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.data.mongodb.core.query.Criteria;
......@@ -26,7 +25,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
......@@ -78,7 +76,8 @@ public class BehaviorServiceImpl implements BehaviorService {
Query query = new Query();
query.addCriteria(Criteria.where("cTime").gte(startTime).lt(endTime).and("backstage").is(backstage));
if (StringUtils.isNotEmpty(nickname)) {
List<User> userList = userDao.findList(Query.query(Criteria.where("nickname").regex(Pattern.compile("^.*" + nickname + ".*$", Pattern.CASE_INSENSITIVE))));
userDao.addKeywordFuzz(query,nickname,"nickname");
List<User> userList = userDao.findList(query);
// 根据nickname未查询
if (CollectionUtils.isEmpty(userList)) {
return PageVO.createPageVo(0, page, 0, size, Collections.emptyList());
......@@ -108,7 +107,7 @@ public class BehaviorServiceImpl implements BehaviorService {
behaviorList.forEach(behavior -> {
ExportBehaviorDTO exportBehaviorDTO = new ExportBehaviorDTO();
Integer roleId = getRoleId(behavior.getUserId(), behavior.getProjectId());
exportBehaviorDTO.setNickname(behavior.getNickName());
exportBehaviorDTO.setNickname(behavior.getNickname());
exportBehaviorDTO.setIp(behavior.getIp());
exportBehaviorDTO.setTime(new Date(behavior.getCTime()));
exportBehaviorDTO.setController(behavior.getController());
......
......@@ -593,8 +593,7 @@ public class MarkDataServiceImpl implements MarkDataService {
// keyword
if (StringUtils.isNotEmpty(dto.getKeyword())) {
// Query
String[] fieldSearch = "仅标题".equals(dto.getSearchField()) ? new String[]{GenericAttribute.ES_IND_TITLE} :
new String[]{GenericAttribute.ES_IND_FULL_TEXT};
String[] fieldSearch = "仅标题".equals(dto.getSearchField()) ? new String[]{GenericAttribute.ES_IND_TITLE} : new String[]{GenericAttribute.ES_IND_FULL_TEXT};
postFilter.must(EsQueryTools.assembleNormalKeywordQuery(dto.getKeyword(), fieldSearch));
}
// sourceKeyword
......@@ -812,7 +811,7 @@ public class MarkDataServiceImpl implements MarkDataService {
// TODO script使用
// Script script = new Script("params._source.ind_title");
// TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("titles").script(script).order(BucketOrder.count(false)).size(60000);
TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("titles").field("agg_title.keyword").size(size * 10);
TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("titles").field("agg_title.keyword").size(size + 1);
// query
BoolQueryBuilder query;
if (include) {
......@@ -1001,8 +1000,7 @@ public class MarkDataServiceImpl implements MarkDataService {
// keyword
if (StringUtils.isNotEmpty(dto.getKeyword())) {
// Query
String[] fieldSearch = "仅标题".equals(dto.getSearchField()) ? new String[]{GenericAttribute.ES_IND_TITLE} :
new String[]{GenericAttribute.ES_IND_FULL_TEXT};
String[] fieldSearch = "仅标题".equals(dto.getSearchField()) ? new String[]{GenericAttribute.ES_IND_TITLE} : new String[]{GenericAttribute.ES_IND_FULL_TEXT};
postFilter.must(EsQueryTools.assembleNormalKeywordQuery(dto.getKeyword(), fieldSearch));
}
// sourceKeyword
......@@ -1785,7 +1783,7 @@ public class MarkDataServiceImpl implements MarkDataService {
EsClientDao.SearchHelper searchHelper = EsClientDao.createSearchHelper();
BoolQueryBuilder postFilter = projectLinkedGroupContendIdQuery(projectId, linkedGroupId, "0");
postFilter.must(QueryBuilders.rangeQuery("time").gte(startTime).lt(endTime));
postFilter.must(EsQueryTools.cacheMapsNestedQuery(QueryBuilders.existsQuery("brandkbs_cache_maps.channel_emotion")));
postFilter.mustNot(EsQueryTools.cacheMapsNestedQuery(QueryBuilders.termQuery("brandkbs_cache_maps.channel_emotion", 0)));
searchHelper.setPostFilter(postFilter);
searchHelper.setFetchSource(new String[]{GenericAttribute.ES_BRANDKBS_CACHE_MAPS});
List<SearchResponse> searchResponses = esClientDao.searchScrollResponse(searchHelper);
......@@ -1803,9 +1801,9 @@ public class MarkDataServiceImpl implements MarkDataService {
}
}
long total = emotionMap.values().stream().mapToInt(AtomicInteger::getAndIncrement).sum();
channelEmotionResult.put("positivePercent", (double) emotionMap.get(ChannelEmotion.POSITIVE.getState()).get() / total);
channelEmotionResult.put("negativePercent", (double) emotionMap.get(ChannelEmotion.NEGATIVE.getState()).get() / total);
channelEmotionResult.put("neutralPercent", (double) emotionMap.get(ChannelEmotion.NEUTRAL.getState()).get() / total);
channelEmotionResult.put("positivePercent", total == 0 ? 0 : (double) emotionMap.get(ChannelEmotion.POSITIVE.getState()).get() / total);
channelEmotionResult.put("negativePercent", total == 0 ? 0 : (double) emotionMap.get(ChannelEmotion.NEGATIVE.getState()).get() / total);
channelEmotionResult.put("neutralPercent", total == 0 ? 0 : (double) emotionMap.get(ChannelEmotion.NEUTRAL.getState()).get() / total);
return channelEmotionResult;
}
......@@ -1822,9 +1820,7 @@ public class MarkDataServiceImpl implements MarkDataService {
mediaTypeMap.put(channelEnum.name().toLowerCase() + "Percent", count);
}
long total = mediaTypeMap.values().stream().mapToLong(v -> v).sum();
mediaTypeMap.forEach((percentName, size) -> {
mediaTypePercent.put(percentName, (double) size / total == 0 ? 1 : total);
});
mediaTypePercent.putAll(mediaTypeMap);
return mediaTypePercent;
}
......
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