Skip to content

Commit

Permalink
Merge pull request #10 from maartenlterpstra/master
Browse files Browse the repository at this point in the history
Added service for querying service ingest status
  • Loading branch information
maartenplieger authored Apr 4, 2018
2 parents 40b26bf + 0ea60d6 commit d3835a0
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package nl.knmi.adaguc.services.servicehealth;

import nl.knmi.adaguc.tools.ElementNotFoundException;
import nl.knmi.adaguc.config.ConfigurationReader;
import nl.knmi.adaguc.tools.MyXMLParser.XMLElement;

/**
*
* @author maartenplieger
*
*/


public class ServiceHealthConfigurator implements nl.knmi.adaguc.config.ConfiguratorInterface {
private static boolean enabled=false;
// The directory that contains a file per service with the return code of updatedb
private static String serviceHealthDirectory = null;
static ConfigurationReader configurationReader = new ConfigurationReader ();
public void doConfig(XMLElement configReader){
if(configReader.getNodeValue("adaguc-services.servicehealth") == null){
return;
}
String enabledStr=configReader.getNodeValue("adaguc-services.servicehealth.enabled");
if(enabledStr != null && enabledStr.equals("true")){
enabled = true;
}

if(enabled){
serviceHealthDirectory=configReader.getNodeValue("adaguc-services.servicehealth.servicehealthpath");

}
}


public static String getServiceHealthDirectory() throws ElementNotFoundException {
configurationReader.readConfig();
return serviceHealthDirectory;
}

public static boolean getEnabled() throws ElementNotFoundException {
configurationReader.readConfig();
return enabled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package nl.knmi.adaguc.services.servicehealth;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import nl.knmi.adaguc.tools.ElementNotFoundException;
import nl.knmi.adaguc.config.MainServicesConfigurator;
import nl.knmi.adaguc.services.adagucserver.ADAGUCServer;
import nl.knmi.adaguc.tools.Debug;
import nl.knmi.adaguc.tools.HTTPTools;
import nl.knmi.adaguc.tools.JSONResponse;
import nl.knmi.adaguc.tools.Tools;

@RestController
public class ServiceHealthRequestMapper {
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true);
MappingJackson2HttpMessageConverter converter =
new MappingJackson2HttpMessageConverter(mapper);
return converter;
}

@ResponseBody
@RequestMapping("servicehealth")
public void serviceHealth(HttpServletResponse response, HttpServletRequest request) throws IOException{
JSONResponse jsonResponse = new JSONResponse(request);
try {
boolean enabled = ServiceHealthConfigurator.getEnabled();
if(!enabled){
jsonResponse.setMessage(new JSONObject().put("error","ADAGUC Service Health is not enabled"));
}else{
String basePath = new File(ServiceHealthConfigurator.getServiceHealthDirectory()).getCanonicalPath();
String[] fileList = Tools.ls(basePath);
JSONObject statusList = new JSONObject();
for (String service : fileList) {
String status = Tools.readFile(basePath + "/" + service).trim();
String[] contents = status.split("\\r?\\n");
if (contents[0].startsWith("0")) {
statusList.put(service, new JSONObject().put("statusCode", contents[0]).put("ok", true));
} else {
JSONObject serviceResponse = new JSONObject();
serviceResponse.put("statusCode", contents[0])
.put("ok", false);
if (contents.length > 1)
serviceResponse.put("message", contents[1]);
statusList.put(service, serviceResponse);

}
}
jsonResponse.setMessage(statusList);
}
} catch (Exception e) {
e.printStackTrace();
jsonResponse.setException("error: "+e.getMessage(), e);
}
jsonResponse.print(response);

}
}

0 comments on commit d3835a0

Please sign in to comment.