|
| 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 | +} |
0 commit comments