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

import com.yoho.search.base.constants.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.consumer.service.utils.LogUtils;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
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;
    }

    /**
     * 用于动态地变更日记级别。
     *
     * @param loggerName 日志名称,有三类: 第一种是ROOT,对所有日志都起作用,
     *                   第二种是具体的日志名称,如CACHE_MATCH_REQUEST,
     *                   第三种就是全限定类名,如com.yoho.search.service
     *                   .servicenew.impl.ProductListServiceImpl。
     * @param level      调整目标级别,支持ALL/TRACE/DEBUG/INFO/WARN/ERROR/OFF这些取值
     * @param seconds    调整日志级别的有效时间,默认为60秒,最多只能为600秒,到了时间后变更为原来的日志级别。
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/changeLogLevel")
    public Map<String, Object> changeLogLevel(@RequestParam(defaultValue = "ROOT") String loggerName, @RequestParam(defaultValue = "INFO") String level,
                                              @RequestParam(defaultValue = "60") int seconds) {
        try {
            return getResultMap(200, LogUtils.changeLogLevel(loggerName, level, seconds));
        } catch (Exception e) {
            return getResultMap(500, e.getMessage());
        }
    }

    private Map<String, Object> getResultMap(final int code, final String message) {
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        rtnMap.put("code", code);
        rtnMap.put("msg", message);
        return rtnMap;
    }

}