All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.infusers.core.eng.selfheal.SelfHealService Maven / Gradle / Ivy

There is a newer version: 2024.12.0008
Show newest version
package com.infusers.core.eng.selfheal;

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import com.infusers.core.eng.selfheal.deployment.CloudProviderService;
import com.infusers.core.eng.selfheal.insights.spring.pom.dto.ArtifactDependencyInsightDto;
import com.infusers.core.eng.selfheal.insights.spring.pom.dto.POMInsightDTO;
import com.infusers.core.eng.selfheal.util.SelfHealUtil;
import com.infusers.core.eng.selfheal.version.java.JavaVersionDetails;
import com.infusers.core.eng.selfheal.version.spring.VersionDependencyDetails;

@Service
public class SelfHealService {
		
	final Logger log = LogManager.getLogger(SelfHealService.class);
	private static final String CLASS_NAME = "SelfHealService";
	
	//@Autowired(required = true)
	//private AuditService auditService;
	
	@Autowired(required = true)
	private CloudProviderService cloudProviderService;
	private ArtifactDependencyInsightDto infuserLibDependencyDto;
		
	@Value("${infusers.app.name}")
	private  String appName;

	@Value("${commit.timestamp}")
	private  String versionCommitTime;

	@Value("${version.number}")
	private  String version;
	
	public SelfHealService() {
		init();
	}
	
	private void init() {
		infuserLibDependencyDto = new ArtifactDependencyInsightDto("in.infusers.library", "infusers-library", "", POMInsightDTO.NO_PARENT);		
    	String presentVersion = VersionDependencyDetails.getInstance().getDependencyVersion(infuserLibDependencyDto.getGroupId(), infuserLibDependencyDto.getArtifactId());
    	presentVersion = presentVersion!=null ? presentVersion.trim() : presentVersion;
    	infuserLibDependencyDto.setVersion(presentVersion);    	
    	infuserLibDependencyDto.fillLatestVersionV2();
	}
	
	public ResponseEntity getVersion() {
		//auditService.logAudit("", "API Service", "", "getVersion", "Non protected API");				
		log.debug(CLASS_NAME+".getVersion() Fetching version details :: Commit Time Stamp : ", this.versionCommitTime+" :: version = "+this.version);
				
	    // Create success response
	    Map response = new HashMap<>();
	    response.put("status", "success");
	    response.put("data", getVersionInformation());
	    
	    // Return response with 200 status code	    
	    return new ResponseEntity<>(response, HttpStatus.OK);	    
	}
	
	private static final String LINE_SEPARATOR = System.lineSeparator();
	private static final String PREFIX = " -- ";

	private String getVersionInformation() {
	    StringBuilder versionInfoSummary = new StringBuilder();

	    versionInfoSummary.append(getVersionDetails());
	    versionInfoSummary.append(String.format("%sInfuser's Lib: %s", LINE_SEPARATOR, getInfusersLibraryUpgradeOption()));
	    versionInfoSummary.append(String.format("%sJava: %s", LINE_SEPARATOR, JavaVersionDetails.getInstance().getJavaVersion()));
	    versionInfoSummary.append(String.format("%sSpring Boot: %s", LINE_SEPARATOR, VersionDependencyDetails.getInstance().getSpringBootVersion()));
	    versionInfoSummary.append(String.format("%sHosted on:", LINE_SEPARATOR));
	    versionInfoSummary.append(String.format("%s%s%s", LINE_SEPARATOR, PREFIX, cloudProviderService.getCloudProviderName()));
	    versionInfoSummary.append(String.format("%s%s%s", LINE_SEPARATOR, PREFIX, getOSDetails()));

	    return versionInfoSummary.toString();
	}
	
	
	public String getVersionDetails() {
		return String.format("%s:  %s (%s)", appName, this.version, this.versionCommitTime);
	}

	
	private String getOSDetails() {
        String os = System.getProperty("os.name");
        String osVersion = System.getProperty("os.version");
        String osArch = System.getProperty("os.arch");
        
		StringBuffer osInfoSummary = new StringBuffer();
		osInfoSummary.append("OS details: ");
		osInfoSummary.append(os);
		osInfoSummary.append(", "+osVersion);
		osInfoSummary.append(", "+osArch);
		
        return osInfoSummary.toString();
	}
	
	private String getInfusersLibraryUpgradeOption() {
    	String presentVersion = infuserLibDependencyDto.getVersion();
		String latestVer = infuserLibDependencyDto.getLatestVersion();
		
		StringBuffer response = new StringBuffer();
		if(presentVersion!=null && presentVersion.equalsIgnoreCase(latestVer)) {
			response.append(presentVersion);
		}
		else {
			response.append(presentVersion+", Upgrade option: "+latestVer);			
		}
		
		return response.toString();
	}
	
	public ResponseEntity auditSpringBootDependencies(boolean recursive) {
		try {
			POMInsightDTO pomInsight = new POMInsightDTO(recursive);			
			pomInsight.processInsightForGivenPOMFile(new FileReader("pom.xml"));
			pomInsight.getInsightForGiven3PLib(infuserLibDependencyDto.getGroupId(), infuserLibDependencyDto.getArtifactId(), infuserLibDependencyDto.getVersion());
			
			return SelfHealUtil.getJSonDownloadResponse(pomInsight, "infuser_micro_service_dep_insights");
							
        } catch (IOException e) {
			log.error(String.format(CLASS_NAME+".auditSpringBootDependencies() IOException :: %s",e.getMessage()));        	
            // Handle IO exception
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to generate JSON file", e);
        } catch (Exception e) {
			log.error(String.format(CLASS_NAME+".auditSpringBootDependencies() Exception :: %s",e.getMessage()));        	
            // Handle other exceptions
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "An unexpected error occurred", e);
        }	
	}
 
}