Skip to content

Commit 0af4868

Browse files
committed
fixed publid status broadcast
1 parent 0d5fb01 commit 0af4868

5 files changed

Lines changed: 65 additions & 78 deletions

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public ApiResponse page(@ApiData JsonNode dataNode) {
8989
} else {
9090
sortDirection = Sort.Direction.DESC;
9191
}
92-
9392
Map<String, String[]> filters = new HashMap<String, String[]>();
9493
filters.put("title", arrayNodeToStringArray((ArrayNode) dataNode.get("filters").get("title")));
9594
FilteredPageRequest filteredPageRequest = new FilteredPageRequest(dataNode.get("page").get("number").asInt(), dataNode.get("page").get("size").asInt(), sortDirection, dataNode.get("direction").get("properties").asText(), filters);

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public ApiResponse getService(@ApiVariable Long id) {
5151
@ApiValidation(business = { @ApiValidation.Business(value = CREATE), @ApiValidation.Business(value = EXISTS) })
5252
public ApiResponse createService(@ApiValidatedModel Service service, @ApiCredentials Credentials credentials) {
5353
service = serviceRepo.create(service.getName(), service.getStatus(), service.getIsAuto(), service.getIsPublic(), service.getOnShortList(), service.getServiceUrl(), service.getDescription());
54-
serviceRepo.sendStatusUpdate(service, credentials);
5554
return new ApiResponse(SUCCESS, service);
5655
}
5756

@@ -60,7 +59,6 @@ public ApiResponse createService(@ApiValidatedModel Service service, @ApiCredent
6059
@ApiValidation(business = { @ApiValidation.Business(value = UPDATE), @ApiValidation.Business(value = NONEXISTS) })
6160
public ApiResponse updateService(@ApiValidatedModel Service service, @ApiCredentials Credentials credentials) {
6261
service = serviceRepo.update(service);
63-
serviceRepo.sendStatusUpdate(service, credentials);
6462
return new ApiResponse(SUCCESS, service);
6563
}
6664

src/main/java/edu/tamu/app/model/repo/custom/ServiceRepoCustom.java

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

33
import edu.tamu.app.enums.Status;
44
import edu.tamu.app.model.Service;
5-
import edu.tamu.framework.model.Credentials;
65

76
public interface ServiceRepoCustom {
87

@@ -12,6 +11,4 @@ public interface ServiceRepoCustom {
1211

1312
public void delete(Service service);
1413

15-
public void sendStatusUpdate(Service service, Credentials credentials);
16-
1714
}
Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
package edu.tamu.app.model.repo.impl;
22

3-
import static edu.tamu.app.enums.AppRole.ROLE_ANONYMOUS;
4-
import static edu.tamu.app.enums.AppRole.ROLE_USER;
53
import static edu.tamu.framework.enums.ApiResponseType.SUCCESS;
64

75
import org.springframework.beans.factory.annotation.Autowired;
86
import org.springframework.messaging.simp.SimpMessagingTemplate;
97

108
import edu.tamu.app.enums.Status;
11-
import edu.tamu.app.model.AppUser;
129
import edu.tamu.app.model.Service;
13-
import edu.tamu.app.model.repo.AppUserRepo;
1410
import edu.tamu.app.model.repo.NoteRepo;
1511
import edu.tamu.app.model.repo.ServiceRepo;
1612
import edu.tamu.app.model.repo.custom.ServiceRepoCustom;
1713
import edu.tamu.app.service.SystemMonitorService;
1814
import edu.tamu.framework.model.ApiResponse;
19-
import edu.tamu.framework.model.Credentials;
2015

2116
public class ServiceRepoImpl implements ServiceRepoCustom {
2217

@@ -26,9 +21,6 @@ public class ServiceRepoImpl implements ServiceRepoCustom {
2621
@Autowired
2722
private NoteRepo noteRepo;
2823

29-
@Autowired
30-
private AppUserRepo userRepo;
31-
3224
@Autowired
3325
private SystemMonitorService systemMonitorService;
3426

@@ -39,13 +31,15 @@ public class ServiceRepoImpl implements ServiceRepoCustom {
3931
public Service create(String name, Status status, Boolean isAuto, Boolean isPublic, Boolean onShortList, String serviceUrl, String description) {
4032
Service service = serviceRepo.save(new Service(name, status, isAuto, isPublic, onShortList, serviceUrl, description));
4133
simpMessagingTemplate.convertAndSend("/channel/service/create", new ApiResponse(SUCCESS, service));
34+
sendStatusUpdate();
4235
return service;
4336
}
4437

4538
@Override
4639
public Service update(Service service) {
4740
service = serviceRepo.save(service);
4841
simpMessagingTemplate.convertAndSend("/channel/service/" + service.getId(), new ApiResponse(SUCCESS, service));
42+
sendStatusUpdate();
4943
return service;
5044
}
5145

@@ -56,16 +50,12 @@ public void delete(Service service) {
5650
});
5751
serviceRepo.delete(service.getId());
5852
simpMessagingTemplate.convertAndSend("/channel/service/delete", new ApiResponse(SUCCESS, service.getId()));
53+
sendStatusUpdate();
5954
}
6055

61-
@Override
62-
public void sendStatusUpdate(Service service, Credentials credentials) {
63-
AppUser user = userRepo.findByUin(credentials.getUin());
64-
if (user.getRole() == ROLE_ANONYMOUS || user.getRole() == ROLE_USER) {
65-
simpMessagingTemplate.convertAndSend("/channel/status/overall-public", new ApiResponse(SUCCESS, systemMonitorService.getOverallStatusPublic()));
66-
} else {
67-
simpMessagingTemplate.convertAndSend("/channel/status/overall-full", new ApiResponse(SUCCESS, systemMonitorService.getOverallStatus()));
68-
}
56+
private void sendStatusUpdate() {
57+
simpMessagingTemplate.convertAndSend("/channel/status/overall-public", new ApiResponse(SUCCESS, systemMonitorService.getOverallStatusPublic()));
58+
simpMessagingTemplate.convertAndSend("/channel/status/overall-full", new ApiResponse(SUCCESS, systemMonitorService.getOverallStatus()));
6959
}
7060

7161
}

src/main/java/edu/tamu/app/service/SystemMonitorService.java

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,60 +23,63 @@
2323

2424
@Service
2525
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-
}
26+
27+
@Autowired
28+
private ServiceRepo serviceRepo;
29+
30+
@Autowired
31+
private HttpUtility httpUtility;
32+
33+
@Autowired
34+
private ObjectMapper objectMapper;
35+
36+
private static final String SUCCESS_MESSAGE = "All services are working.";
37+
38+
private static final String ERROR_MESSAGE = "Some services are experiencing problems.";
39+
40+
private Logger logger = LoggerFactory.getLogger(this.getClass());
41+
42+
@Override
43+
public void updateAll() {
44+
logger.debug("Monitor Service is checking the status of the managed services");
45+
// get all monitor-able services from ServiceRepo
46+
serviceRepo.findByIsAuto(true).forEach(service -> {
47+
try {
48+
Status serviceStatus = getServiceStatus(service.getServiceUrl());
49+
// update the service status if it's changed
50+
logger.debug("The status reported by [" + service.getServiceUrl() + "] of [" + service.getName() + "] is: " + serviceStatus);
51+
if (serviceStatus != service.getStatus()) {
52+
logger.debug("Updating the status of [" + service.getName() + "] to: " + serviceStatus);
53+
service.setStatus(serviceStatus);
54+
serviceRepo.save(service);
55+
}
56+
} catch (MalformedURLException e) {
57+
logger.error("Did not check the status of [" + service.getName() + "] due to a malformed URL: " + service.getServiceUrl());
58+
} catch (IOException e) {
59+
logger.error("Attempt to check the status of [" + service.getName() + "] failed due to an IOException");
60+
}
61+
});
62+
}
63+
64+
public OverallStatus getOverallStatus() {
65+
Long downCount = serviceRepo.countByStatus(Status.DOWN);
66+
if (downCount == 0) {
67+
return new OverallStatus(SUCCESS, SUCCESS_MESSAGE);
68+
}
69+
return new OverallStatus(ERROR, ERROR_MESSAGE);
70+
}
71+
72+
public OverallStatus getOverallStatusPublic() {
73+
Long downCount = serviceRepo.countByStatusAndIsPublic(Status.DOWN, true);
74+
if (downCount == 0) {
75+
return new OverallStatus(SUCCESS, SUCCESS_MESSAGE);
76+
}
77+
return new OverallStatus(ERROR, ERROR_MESSAGE);
78+
}
79+
80+
protected Status getServiceStatus(String serviceUrl) throws MalformedURLException, IOException {
81+
String rawStatusResponse = httpUtility.makeHttpRequest(serviceUrl, "GET");
82+
List<Map<String, String>> mappedStatusResponse = objectMapper.readValue(rawStatusResponse, new TypeReference<List<Map<String, String>>>() {});
83+
return Status.valueOf(mappedStatusResponse.get(0).get("service").toUpperCase());
84+
}
8285
}

0 commit comments

Comments
 (0)