ToolsController.java
4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;
}
}