ToolsController.java 4.65 KB
package com.yoho.search.consumer.restapi;

import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.consumer.common.IYohoIndexService;
import com.yoho.search.consumer.service.logicService.personal.PersonalVectorVersionManager;
import com.yoho.search.consumer.service.logicService.tbl.util.StringUtils;
import com.yoho.search.core.es.IElasticsearchClient;
import com.yoho.search.core.personalized.service.BidataServiceCaller;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class ToolsController {

    @Autowired
    private PersonalVectorVersionManager personalVectorVersionManager;
    @Autowired
    private BidataServiceCaller bidataServiceCaller;
    @Autowired
    private IYohoIndexService yohoIndexService;

    @RequestMapping(value = "/vectorVersion")
    public Map<String, Object> vectorVersion() {
        Map<String, Object> results = new HashMap<>();
        //大数据目前推荐的版本
        String bigDataRecomDateStr = personalVectorVersionManager.getBigDataRecomDateStr();
        results.put("bigDataRecomDateStr", bigDataRecomDateStr == null ? "" : bigDataRecomDateStr);
        //zk中目前的版本
        String currentVersionInZk = personalVectorVersionManager.getCurrentVersionInZk();
        results.put("currentVersionInZk", currentVersionInZk);
        //经过计算目前可以使用的版本
        String currentVersion = personalVectorVersionManager.getCurrentVersion();
        results.put("currentVersion", currentVersion);
        return results;
    }

    @RequestMapping(value = "/bigdataServiceTest")
    public Map<String, Object> bigdataServiceTets(Integer uid) {
        Map<String, Object> results = new HashMap<>();

        //大数据目前推荐的版本
        String bigDataRecomDateStr = personalVectorVersionManager.getBigDataRecomDateStr();
        results.put("bigDataRecomDateStr", bigDataRecomDateStr == null ? "" : bigDataRecomDateStr);
        results.put("bigDataRecomDateStrFeatures", bidataServiceCaller.getUserVectorFeature(uid.toString(), bigDataRecomDateStr));

        //zk中目前的版本
        String currentVersionInZk = personalVectorVersionManager.getCurrentVersionInZk();
        results.put("currentVersionInZk", currentVersionInZk == null ? "" : currentVersionInZk);
        results.put("currentVersionInZkFeatures", bidataServiceCaller.getUserVectorFeature(uid.toString(), currentVersionInZk));

        //用户性别偏好
        results.put("userGenderFeature", bidataServiceCaller.getUserGenderFeature(uid.toString()));
        //用户尺码偏好
        results.put("userFavoriteSizes", bidataServiceCaller.getUserFavoriteSizes(uid.toString()));
        //用户个性化因子
        results.put("userPersionalFactor", bidataServiceCaller.queryUserPersionalFactor(uid, null, null));
        return results;
    }

    @RequestMapping(value = "/ip2PrimaryShard")
    public Map<String, Object> nodeAndShardInfo() {
        IElasticsearchClient client = yohoIndexService.getElasticsearchClient(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
        ClusterState clusterState = client.getClusterStateResponse().getState();
        DiscoveryNodes discoveryNodes = clusterState.nodes();

        Map<String, List<String>> ip2PrimaryShards = new HashMap<>();
        Set<String> allIps = new HashSet<>();
        Set<String> hasPrimaryIps = new HashSet<>();
        for (ShardRouting shard : clusterState.routingTable().allShards()) {
            String indexName = shard.getIndexName();
            String hostAddress = discoveryNodes.get(shard.currentNodeId()).getHostAddress();
            boolean isPrimary = shard.primary();
            allIps.add(hostAddress);
            if (!isPrimary) {
                allIps.add(hostAddress);
                continue;
            }
            hasPrimaryIps.add(hostAddress);
            List<String> primaryShardList = ip2PrimaryShards.computeIfAbsent(hostAddress, a -> new ArrayList<>());
            primaryShardList.add(indexName);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("ip2PrimaryShards", ip2PrimaryShards);
        result.put("hasPrimaryIps", StringUtils.join(hasPrimaryIps, ","));
        allIps.removeAll(hasPrimaryIps);
        result.put("noPrimaryIps", StringUtils.join(allIps, ","));
        return result;
    }

}