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 {
		
	private final Logger log = LogManager.getLogger(SelfHealService.class);
	private static final String CLASS_NAME = "SelfHealService";
	
	@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;
	
	private static final String LINE_SEPARATOR = System.lineSeparator();
	private static final String PREFIX = " -- ";	
	
	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() {
	    // 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 String getVersionInformation() {
	    StringBuilder versionInfoSummary = new StringBuilder();

	    versionInfoSummary.append(getVersionDetails());
	    versionInfoSummary.append(String.format("
Infuser's Lib: %s", getInfusersLibraryUpgradeOption())); versionInfoSummary.append(String.format("
Java: %s", JavaVersionDetails.getInstance().getJavaVersion())); versionInfoSummary.append(String.format("
Spring Boot: %s", VersionDependencyDetails.getInstance().getSpringBootVersion())); versionInfoSummary.append(String.format("
Hosted on: %s, %s", cloudProviderService.getCloudProviderName(), 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"); StringBuilder osInfoSummary = new StringBuilder(); osInfoSummary.append("OS details: "); osInfoSummary.append(os); osInfoSummary.append(", Version: "); osInfoSummary.append(osVersion); osInfoSummary.append(", Architecture: "); osInfoSummary.append(osArch); return osInfoSummary.toString(); } private String getInfusersLibraryUpgradeOption() { String presentVersion = infuserLibDependencyDto.getVersion(); String latestVer = infuserLibDependencyDto.getLatestVersion(); StringBuilder response = new StringBuilder(); 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); } } }