Skip to content

Commit cd4c902

Browse files
committed
Resolved merge conflicts
2 parents 3b83d08 + a5e20ad commit cd4c902

8 files changed

Lines changed: 133 additions & 15 deletions

File tree

src/main/java/edu/tamu/app/controller/ServiceController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public ApiResponse createService(@ApiValidatedModel Service service) {
5656
System.out.println(service.getServiceUrl());
5757
service = serviceRepo.create(service.getName(), service.getStatus(), service.getIsAuto(), service.getIsPublic(), service.getOnShortList(), service.getServiceUrl());
5858
simpMessagingTemplate.convertAndSend("/channel/service", new ApiResponse(SUCCESS, serviceRepo.findAll()));
59-
6059
return new ApiResponse(SUCCESS, service);
6160
}
6261

src/main/java/edu/tamu/app/controller/StatusController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import edu.tamu.app.model.OverallStatus;
44
import edu.tamu.app.service.AppRoleService;
5-
import edu.tamu.app.service.OverallStatusService;
5+
import edu.tamu.app.service.MonitorService;
66
import edu.tamu.framework.aspect.annotation.ApiCredentials;
77
import edu.tamu.framework.aspect.annotation.ApiMapping;
88
import edu.tamu.framework.aspect.annotation.Auth;
@@ -19,22 +19,22 @@
1919
public class StatusController {
2020

2121
@Autowired
22-
OverallStatusService overallStatusService;
22+
MonitorService monitorService;
2323

2424
@Autowired
2525
AppRoleService appRoleService;
2626

2727
@ApiMapping("/overall-full")
2828
@Auth(role = "ROLE_STAFF")
2929
public ApiResponse overallFull(@ApiCredentials Credentials credentials) {
30-
OverallStatus overallStatus = overallStatusService.getOverallStatusFull();
30+
OverallStatus overallStatus = monitorService.getOverallStatus();
3131
return new ApiResponse(SUCCESS, overallStatus);
3232
}
3333

3434
@ApiMapping("/overall-public")
3535
@Auth(role = "ROLE_ANONYMOUS")
3636
public ApiResponse overallPublic(@ApiCredentials Credentials credentials) {
37-
OverallStatus overallStatus = overallStatusService.getOverallStatusPublic();
37+
OverallStatus overallStatus = monitorService.getOverallStatusPublic();
3838
return new ApiResponse(SUCCESS, overallStatus);
3939
}
4040

src/main/java/edu/tamu/app/job/UpdateOveralStatuses.java renamed to src/main/java/edu/tamu/app/job/UpdateServiceStatuses.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
package edu.tamu.app.job;
22

3+
import static edu.tamu.framework.enums.ApiResponseType.SUCCESS;
4+
35
import org.springframework.beans.factory.annotation.Autowired;
46
import org.springframework.messaging.simp.SimpMessagingTemplate;
57
import org.springframework.scheduling.annotation.Scheduled;
68
import org.springframework.stereotype.Service;
79

8-
import edu.tamu.app.service.OverallStatusService;
10+
import edu.tamu.app.service.MonitorService;
911
import edu.tamu.framework.model.ApiResponse;
1012

11-
import static edu.tamu.framework.enums.ApiResponseType.SUCCESS;
12-
1313
@Service
14-
public class UpdateOveralStatuses {
14+
public class UpdateServiceStatuses {
1515

1616
@Autowired
17-
OverallStatusService overallStatusService;
17+
MonitorService monitorService;
1818

1919
@Autowired
2020
SimpMessagingTemplate simpMessagingTemplate;
2121

22-
@Scheduled(fixedRate=(1 * 30 * 1000))
22+
@Scheduled(fixedRate=(10*60*1000))
2323
public void updateOveralStatuses() {
24-
overallStatusService.updateStatuses();
24+
monitorService.updateAll();
2525

26-
simpMessagingTemplate.convertAndSend("/channel/status/overall-full", new ApiResponse(SUCCESS, overallStatusService.getOverallStatusFull()));
27-
simpMessagingTemplate.convertAndSend("/channel/status/overall-public", new ApiResponse(SUCCESS, overallStatusService.getOverallStatusPublic()));
26+
simpMessagingTemplate.convertAndSend("/channel/status/overall-full", new ApiResponse(SUCCESS, monitorService.getOverallStatus()));
27+
simpMessagingTemplate.convertAndSend("/channel/status/overall-public", new ApiResponse(SUCCESS, monitorService.getOverallStatusPublic()));
2828

2929
}
3030

src/main/java/edu/tamu/app/model/Service.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void setStatus(Status status) {
8989
public Boolean getIsAuto() {
9090
return isAuto;
9191
}
92-
92+
9393
public void setIsAuto(Boolean isAuto) {
9494
this.isAuto = isAuto;
9595
}

src/main/java/edu/tamu/app/model/repo/ServiceRepo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
import org.springframework.data.jpa.repository.JpaRepository;
66

7+
import edu.tamu.app.enums.Status;
78
import edu.tamu.app.model.Service;
89
import edu.tamu.app.model.repo.custom.ServiceRepoCustom;
910

1011
public interface ServiceRepo extends JpaRepository<Service, Long>, ServiceRepoCustom {
1112

1213
public List<Service> findByIsPublic(Boolean isPublic);
14+
public List<Service> findByIsAuto(Boolean isAuto);
15+
public Long countByStatus(Status status);
16+
public Long countByStatusAndIsPublic(Status status, Boolean isPublic);
1317
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package edu.tamu.app.service;
2+
3+
import edu.tamu.app.model.OverallStatus;
4+
5+
public interface MonitorService {
6+
public void updateAll();
7+
public OverallStatus getOverallStatus();
8+
public OverallStatus getOverallStatusPublic();
9+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package edu.tamu.app.service;
2+
3+
import static edu.tamu.app.enums.OverallMessageType.ERROR;
4+
import static edu.tamu.app.enums.OverallMessageType.SUCCESS;
5+
6+
import java.io.IOException;
7+
import java.net.MalformedURLException;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.stereotype.Service;
15+
16+
import com.fasterxml.jackson.core.type.TypeReference;
17+
import com.fasterxml.jackson.databind.ObjectMapper;
18+
19+
import edu.tamu.app.enums.Status;
20+
import edu.tamu.app.model.OverallStatus;
21+
import edu.tamu.app.model.repo.ServiceRepo;
22+
import edu.tamu.framework.util.HttpUtility;
23+
24+
@Service
25+
public class SystemMonitorService implements MonitorService {
26+
@Autowired
27+
ServiceRepo serviceRepo;
28+
29+
@Autowired
30+
HttpUtility httpUtility;
31+
32+
@Autowired
33+
ObjectMapper objectMapper;
34+
35+
private static final String SUCCESS_MESSAGE = "All services are working.";
36+
private static final String ERROR_MESSAGE = "Some services are experiencing problems.";
37+
38+
private Logger logger = LoggerFactory.getLogger(this.getClass());
39+
40+
@Override
41+
public void updateAll() {
42+
logger.debug("Monitor Service is checking the status of the managed services");
43+
//get all monitor-able services from ServiceRepo
44+
serviceRepo.findByIsAuto(true).forEach(service -> {
45+
try {
46+
Status serviceStatus = getServiceStatus(service.getServiceUrl());
47+
//update the service status if it's changed
48+
logger.debug("The status reported by ["+service.getServiceUrl()+"] of ["+service.getName()+"] is: "+serviceStatus);
49+
if (serviceStatus != service.getStatus()) {
50+
logger.debug("Updating the status of ["+service.getName()+"] to: "+serviceStatus);
51+
service.setStatus(serviceStatus);
52+
serviceRepo.save(service);
53+
}
54+
} catch(MalformedURLException e) {
55+
logger.error("Did not check the status of ["+service.getName()+"] due to a malformed URL: "+service.getServiceUrl());
56+
} catch(IOException e) {
57+
logger.error("Attempt to check the status of ["+service.getName()+"] failed due to an IOException");
58+
}
59+
});
60+
}
61+
62+
public OverallStatus getOverallStatus() {
63+
Long downCount = serviceRepo.countByStatus(Status.DOWN);
64+
if (downCount == 0) {
65+
return new OverallStatus(SUCCESS, SUCCESS_MESSAGE);
66+
}
67+
return new OverallStatus(ERROR, ERROR_MESSAGE);
68+
}
69+
70+
public OverallStatus getOverallStatusPublic() {
71+
Long downCount = serviceRepo.countByStatusAndIsPublic(Status.DOWN,true);
72+
if (downCount == 0) {
73+
return new OverallStatus(SUCCESS, SUCCESS_MESSAGE);
74+
}
75+
return new OverallStatus(ERROR, ERROR_MESSAGE);
76+
}
77+
protected Status getServiceStatus(String serviceUrl) throws MalformedURLException,IOException {
78+
String rawStatusResponse = httpUtility.makeHttpRequest(serviceUrl, "GET");
79+
List<Map<String,String>> mappedStatusResponse = objectMapper.readValue(rawStatusResponse, new TypeReference<List<Map<String, String>>>(){});
80+
return Status.valueOf(mappedStatusResponse.get(0).get("service").toUpperCase());
81+
}
82+
83+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package edu.tamu.app.util;
2+
3+
import java.io.IOException;
4+
import java.net.MalformedURLException;
5+
import java.net.URL;
6+
import java.util.Optional;
7+
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.stereotype.Service;
10+
11+
import edu.tamu.framework.util.HttpUtility;
12+
13+
@Service
14+
public class AppHttpUtility extends HttpUtility {
15+
@Value("${app.http.timeout}")
16+
private int DEFAULT_TIMEOUT;
17+
18+
@Override
19+
public String makeHttpRequest(String urlString, String method) throws IOException, MalformedURLException {
20+
URL url = new URL(urlString);
21+
return makeHttpRequest(urlString, method, Optional.empty(), Optional.empty(), DEFAULT_TIMEOUT);
22+
}
23+
}

0 commit comments

Comments
 (0)