1
|
package eu.dnetlib.administration.uis.modules.services;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.net.HttpURLConnection;
|
5
|
import java.net.URL;
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.Collection;
|
8
|
import java.util.List;
|
9
|
import java.util.Map;
|
10
|
import java.util.stream.Collectors;
|
11
|
|
12
|
import javax.servlet.http.HttpServletRequest;
|
13
|
|
14
|
import org.apache.commons.logging.Log;
|
15
|
import org.apache.commons.logging.LogFactory;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.ui.ModelMap;
|
18
|
import org.springframework.web.bind.annotation.RequestMapping;
|
19
|
import org.springframework.web.bind.annotation.RequestParam;
|
20
|
import org.springframework.web.bind.annotation.RestController;
|
21
|
import org.springframework.web.client.RestTemplate;
|
22
|
|
23
|
import com.google.common.collect.Maps;
|
24
|
|
25
|
import eu.dnetlib.administration.uis.annotations.Authorization;
|
26
|
import eu.dnetlib.administration.uis.annotations.MenuEntry;
|
27
|
import eu.dnetlib.administration.uis.annotations.MenuGroup;
|
28
|
import eu.dnetlib.administration.uis.annotations.PermissionLevel;
|
29
|
import eu.dnetlib.administration.uis.modules.UIModule;
|
30
|
import eu.dnetlib.clients.is.InformationServiceClient;
|
31
|
import eu.dnetlib.clients.locators.ServiceClientFactory;
|
32
|
import eu.dnetlib.clients.msro.MsroWorkerClient;
|
33
|
import eu.dnetlib.clients.msro.MsroWorkerRunningInstance;
|
34
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
35
|
|
36
|
@RestController
|
37
|
@RequestMapping("/ajax/services")
|
38
|
@MenuEntry(value = "Services Inspector", urlSection = "services", order = 3, group = MenuGroup.TOOLS)
|
39
|
@Authorization(PermissionLevel.SUPER_ADMIN)
|
40
|
public class ServicesInspectorModule extends UIModule {
|
41
|
|
42
|
private static final Log log = LogFactory.getLog(ServicesInspectorModule.class);
|
43
|
|
44
|
@Autowired
|
45
|
private InformationServiceClient isClient;
|
46
|
|
47
|
@Autowired
|
48
|
private ServiceClientFactory serviceClientFactory;
|
49
|
|
50
|
@Override
|
51
|
public void populateModelMap(final ModelMap map, final HttpServletRequest request) {}
|
52
|
|
53
|
@RequestMapping("")
|
54
|
public Collection<ServiceGrouperDesc> listServices() throws Exception {
|
55
|
|
56
|
final String xq = "for $x in collection('/db/DRIVER/dnetService') return concat ($x//RESOURCE_IDENTIFIER/@value, ' @ ', $x//RESOURCE_URI/@value)";
|
57
|
|
58
|
final List<ServiceDesc> list = isClient.find(xq).stream()
|
59
|
.map(e -> e.split("@"))
|
60
|
.filter(arr -> arr.length == 2)
|
61
|
.filter(arr -> arr[0].split("/").length == 3)
|
62
|
.map(arr -> new ServiceDesc(arr[0].trim(), arr[0].split("/")[1], arr[1].trim()))
|
63
|
.collect(Collectors.toList());
|
64
|
|
65
|
final Map<String, ServiceGrouperDesc> map = Maps.newHashMap();
|
66
|
for (final ServiceDesc s : list) {
|
67
|
final URL url = new URL(s.getUrl());
|
68
|
final String host = url.getHost();
|
69
|
final int port = url.getPort();
|
70
|
final String tmpKey = host + ":" + port;
|
71
|
if (!map.containsKey(tmpKey)) {
|
72
|
map.put(tmpKey, new ServiceGrouperDesc(host, port, new ArrayList<ServiceDesc>()));
|
73
|
}
|
74
|
map.get(tmpKey).getServices().add(s);
|
75
|
}
|
76
|
|
77
|
return map.values();
|
78
|
}
|
79
|
|
80
|
@RequestMapping("ping")
|
81
|
public long pingUrl(@RequestParam("url") final String url,
|
82
|
@RequestParam("timeout") final int timeout)
|
83
|
throws IOException {
|
84
|
|
85
|
final long start = DateUtils.now();
|
86
|
|
87
|
final HttpURLConnection urlConn = (HttpURLConnection) new URL(url).openConnection();
|
88
|
urlConn.setConnectTimeout(timeout);
|
89
|
urlConn.setReadTimeout(timeout);
|
90
|
|
91
|
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
92
|
return DateUtils.now() - start;
|
93
|
} else {
|
94
|
throw new IllegalArgumentException("Invalid Url");
|
95
|
}
|
96
|
}
|
97
|
|
98
|
@RequestMapping("workers")
|
99
|
public List<MsroWorkerRunningInstance> listWorkers() throws Exception {
|
100
|
|
101
|
final String xq =
|
102
|
"for $x in collection('/db/DRIVER/dnetService/msroWorker') return $x//RESOURCE_URI/@value/string()";
|
103
|
|
104
|
return isClient.find(xq)
|
105
|
.stream()
|
106
|
.map(url -> serviceClientFactory.getClient(MsroWorkerClient.class, url))
|
107
|
.map(MsroWorkerClient::getStatus)
|
108
|
.collect(Collectors.toList());
|
109
|
|
110
|
}
|
111
|
|
112
|
@RequestMapping("workers/pause")
|
113
|
public MsroWorkerRunningInstance pause(@RequestParam final String baseUrl) {
|
114
|
return (new RestTemplate()).getForObject(baseUrl + "/pause", MsroWorkerRunningInstance.class);
|
115
|
}
|
116
|
|
117
|
@RequestMapping("workers/resume")
|
118
|
public MsroWorkerRunningInstance resume(@RequestParam final String baseUrl) throws Exception {
|
119
|
return (new RestTemplate()).getForObject(baseUrl + "/resume", MsroWorkerRunningInstance.class);
|
120
|
}
|
121
|
|
122
|
}
|