|
|
package com.yoho.search.consumer.ufosuggests.counter;
|
|
|
|
|
|
import com.yoho.search.consumer.index.rebuild.RebuildFlagService;
|
|
|
import com.yoho.search.consumer.suggests.common.RetryBusinessFlow;
|
|
|
import com.yoho.search.consumer.suggests.counter.CountUsage;
|
|
|
import com.yoho.search.consumer.suggests.counter.KeywordCounterService;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
|
import org.springframework.context.ApplicationEventPublisherAware;
|
|
|
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 建议词计数器 抽象类
|
|
|
* @author wangnan
|
|
|
* @version 2019/6/4
|
|
|
*/
|
|
|
public abstract class UfoAbstractSuggestionCounter implements ApplicationEventPublisherAware, RetryBusinessFlow {
|
|
|
|
|
|
protected static final Logger logger = LoggerFactory.getLogger("FLOW_EXECUTOR");
|
|
|
|
|
|
@Autowired
|
|
|
private KeywordCounterService keywordCounterService;
|
|
|
|
|
|
@Autowired
|
|
|
private RebuildFlagService rebuildFlagService;
|
|
|
|
|
|
protected ApplicationEventPublisher publisher;
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String flowName() {
|
|
|
return this.getClass().getSimpleName();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void init() {
|
|
|
rebuildFlagService.waitingRebuildingIndex();
|
|
|
rebuildFlagService.updateIsBuildingTrue();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean doBusiness(int pageNo, int batchSize) {
|
|
|
Map<String, Object> keywordMap = getKeywordMap(pageNo, batchSize);
|
|
|
int fetchSize = keywordMap != null ? keywordMap.size() : 0;
|
|
|
if (fetchSize == 0) {
|
|
|
logger.info("[{} business][pageNo={}][fetchWordSize={}]", flowName(), pageNo, fetchSize);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
Map<String, Integer> countMapForApp = keywordCounterService.batchCount(keywordMap.keySet().stream().collect(Collectors.toList()), CountUsage.APP);
|
|
|
int countMapSizeForApp = countMapForApp != null ? countMapForApp.size() : 0;
|
|
|
if (countMapSizeForApp == 0) {
|
|
|
logger.info("[{} business][pageNo={}][countMapSizeForApp={}]", flowName(), pageNo, countMapSizeForApp);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
Map<String, Integer> countMapForPC = keywordCounterService.batchCount(keywordMap.keySet().stream().collect(Collectors.toList()), CountUsage.PC);
|
|
|
int countMapSizeForPC = countMapForPC != null ? countMapForPC.size() : 0;
|
|
|
if (countMapSizeForPC == 0) {
|
|
|
logger.info("[{} business][pageNo={}][countMapForPC={}]", flowName(), pageNo, countMapSizeForPC);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
Map<String, Integer> countMapForBlk = keywordCounterService.batchCount(keywordMap.keySet().stream().collect(Collectors.toList()), CountUsage.BLK);
|
|
|
int countMapSizeForBlk = countMapForBlk != null ? countMapForBlk.size() : 0;
|
|
|
if (countMapSizeForBlk == 0) {
|
|
|
logger.info("[{} business][pageNo={}][countMapSizeForBlk={}]", flowName(), pageNo, countMapSizeForBlk);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
logger.info("[{} business][pageNo={}][fetchWordSize={}][countMapSizeForApp={}][countMapSizeForPC={}][countMapSizeForBlk={}]",
|
|
|
flowName(), pageNo, fetchSize, countMapSizeForApp, countMapSizeForPC, countMapSizeForBlk);
|
|
|
return persistence(keywordMap, countMapForApp, countMapForPC, countMapForBlk);
|
|
|
}
|
|
|
|
|
|
protected Integer calCount(Map<String, Integer> countResultMap, String suggestWord) {
|
|
|
Integer count = countResultMap.get(suggestWord);
|
|
|
return count != null ? count : Integer.valueOf(0);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void finish(boolean doBusinessResult, Exception exception) {
|
|
|
rebuildFlagService.updateIsBuildingFalse();
|
|
|
}
|
|
|
|
|
|
abstract Map<String, Object> getKeywordMap(int pageNo, int batchSize);
|
|
|
|
|
|
abstract boolean persistence(Map<String, Object> keywordMap, Map<String, Integer> countMapForApp, Map<String, Integer> countMapForPC, Map<String, Integer> countMapForBlk);
|
|
|
} |