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

com.ibm.ta.sdk.spi.assess.RecommendationJson Maven / Gradle / Ivy

/*
 * (C) Copyright IBM Corp. 2019,2020
 *
 * SPDX-License-Identifier: Apache-2.0
 */

package com.ibm.ta.sdk.spi.assess;

import com.google.gson.annotations.Expose;
import com.ibm.ta.sdk.spi.plugin.TAException;
import com.ibm.ta.sdk.spi.collect.AssessmentUnit;
import com.ibm.ta.sdk.spi.collect.Environment;
import com.ibm.ta.sdk.spi.recommendation.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;

public class RecommendationJson {
  private static final String REC_ATTR_QM_NAME = "name";
  private static final String ASS_ATTR_TARGET = "targets";
  private static final String ASS_ATTR_TARGET_ICP_MQ_VERSION = "version";
  private static final String ASS_ATTR_TARGET_PLATFORM = "platform";
  private static final String ASS_ATTR_TARGET_LOCATION = "location";
  private static final String ASS_ATTR_TARGET_RUNTIME  = "runtime";
  private static final String ASS_ATTR_TARGET_ProductName  = "productName";
  private static final String ASS_ATTR_TARGET_ProductVersion  = "productVersion";
  private static final String ASS_ATTR_ISSUES = "issues";
  private static final String ASS_ATTR_SUMMARY = "summary";
  private static final String ASS_ATTR_VALID = "valid";
  private static final String ASS_SUMMARY_COMPLEXITY = "complexity";
  private static final String ASS_SUMMARY_ISSUES = "issues";
  private static final String ASS_SUMMARY_EFFORT = "effort";
  private static final String ASS_SUMMARY_COMPLEXITY_SCORE = "score";

  @Expose
  protected String domain;

  @Expose
  protected String collectionUnitType;

  @Expose
  protected String collectionUnitName;

  @Expose
  protected String middleware;

  @Expose
  protected String version;

  @Expose
  protected List complexityRules;

  @Expose
  protected Map issueCategories;

  @Expose
  protected List> assessmentUnits = new ArrayList>();
  private Recommendation recommendation;

  private static Logger logger = LogManager.getLogger(RecommendationJson.class.getName());

  public RecommendationJson(Recommendation recommendation, Environment environment, List auList) throws TAException {
    this.recommendation = recommendation;
    domain = environment.getDomain();
    middleware = environment.getMiddlewareName();
    collectionUnitType = environment.getCollectionUnitType();
    collectionUnitName = environment.getCollectionUnitName();
    version = environment.getMiddlewareVersion();
    complexityRules = ComplexityContributionJson.getComplexityContributionJsonList(recommendation.getComplexityContributions());
    issueCategories = IssueCategoryJson.getIssueCategoryJsonMap(recommendation.getIssueCategories());

    for (Target target : recommendation.getTargets()) {
      for (AssessmentUnit au : auList) {
        List auIssues = recommendation.getIssues(target, au);

        // Build map of issues by category
        Map> issuesMap = new LinkedHashMap>();
        for (Issue issue : auIssues) {
          IssueCategory iCat = issue.getCategory();
          if (iCat == null) {
            throw new TAException("No matching issue category found for issue with ID:" + issue.getId());
          }
          String issueCat = iCat.getId();

          List issuesList = issuesMap.get(issueCat);
          if (issuesList == null) {
            issuesList = new ArrayList();
            issuesMap.put(issueCat, issuesList);
          }
          issuesList.add(issue);
        }

        Map auMap = getAssessmentUnit(target, au,issuesMap);
        assessmentUnits.add(auMap);
      }
    }
  }

  private Map getAssessmentUnit(Target target, AssessmentUnit au, Map>  issuesMap) {
    Map auMap = new LinkedHashMap();
    auMap.put(REC_ATTR_QM_NAME, au.getName());
    List> targetList = new LinkedList>();
    auMap.put(ASS_ATTR_TARGET, targetList);
    Map targetMap = new LinkedHashMap();
    targetList.add(targetMap);

    targetMap.put(ASS_ATTR_TARGET_ICP_MQ_VERSION, target.getProductVersion());
    targetMap.put(ASS_ATTR_TARGET_ProductName, target.getProductName());
    targetMap.put(ASS_ATTR_TARGET_ProductVersion, target.getProductVersion());
    String targetRuntime = target.getRuntime();
    if (targetRuntime != null && !targetRuntime.equals("")) {
      targetMap.put(ASS_ATTR_TARGET_RUNTIME, target.getRuntime());
    }
    targetMap.put(ASS_ATTR_TARGET_PLATFORM, target.getPlatform());
    targetMap.put(ASS_ATTR_TARGET_LOCATION, target.getLocation());

    targetMap.put(ASS_ATTR_VALID, "true");
    targetMap.put(ASS_ATTR_ISSUES, issuesMap);
    targetMap.put(ASS_ATTR_SUMMARY, getAssessmentSummary(issuesMap));
    return auMap;
  }

  private Map getAssessmentSummary(Map> issues) {
    Map summaryMap = new LinkedHashMap();
    summaryMap.put(ASS_SUMMARY_COMPLEXITY, getSummaryComplexity(issues));
    summaryMap.put(ASS_SUMMARY_COMPLEXITY, getSummaryComplexity(issues));
    summaryMap.put(ASS_SUMMARY_ISSUES, getSummaryIssues(issues));
    summaryMap.put(ASS_SUMMARY_EFFORT, getSummaryDevEffort(issues));

    return summaryMap;
  }

  private Map  getSummaryComplexity(Map> issues) {
    Map complexityMap = new LinkedHashMap();

    Iterator itIssueCategory = issues.keySet().iterator();
    while (itIssueCategory.hasNext()) {
      String issueCategory = itIssueCategory.next();
      List categoryIssues = issues.get(issueCategory);
      logger.trace("Get complexity for category:" + issueCategory + " issues:" + categoryIssues);
      for (Issue catIs : categoryIssues) {
        ComplexityContribution matchedCC = getMatchedComplexityContribution(catIs);
        if (matchedCC != null) {
          catIs.setComplexityContribution(matchedCC);
          String issueComplexity = matchedCC.getComplexity().name();
          Map complexityScoreMap = (Map) complexityMap.get(issueComplexity);
          if (complexityScoreMap == null) {
            complexityScoreMap = new LinkedHashMap();
            complexityMap.put(issueComplexity, complexityScoreMap);
          }
          Integer ccount = complexityScoreMap.get(catIs.getCategory());
          if (ccount == null) {
            ccount = 0;
          }

          complexityScoreMap.put(catIs.getCategory().getId(), ++ccount);
        } else {
          logger.warn("No complexity rule found for issue:" + catIs.getId());
        }
      }
    }

    addComplexityScore(complexityMap);
    return complexityMap;
  }

  private ComplexityContribution getMatchedComplexityContribution(Issue issue) {
    logger.trace("Find matching complexity contribution for issue:" + issue);

    ComplexityContribution matchedCC = null;
    for (ComplexityContribution cc : recommendation.getComplexityContributions()) {
      List ruleIssues = cc.getIssues();
      if (ruleIssues != null) {
        for (String ruleIssue : ruleIssues) {
          if (ruleIssue.equals(issue.getId())) {
            logger.debug("Matching complexity contribution found (matched by ID):" + cc);
            return cc;
          }
        }
      }

      List ruleCategories = cc.getIssuesCategory();
      if (ruleCategories != null) {
        for (String ruleCategory : ruleCategories) {
          if (ruleCategory.equals(issue.getCategory().getId())) {
            logger.debug("Matching complexity contribution found (matched by category):" + cc);
            if (matchedCC == null) {
              matchedCC = cc;
            } else {
              if (cc.getComplexity().compareTo(matchedCC.getComplexity()) > 0) {
                matchedCC = cc;
              }
            }
            break; // Continue to the next rule
          }
        }
      }
    }
    return matchedCC;
  }

  private void addComplexityScore(Map complexityMap) {
    ComplexityRating maxComplexity = ComplexityRating.simple;

    Iterator itComplexity = complexityMap.keySet().iterator();
    while (itComplexity.hasNext()) {
      ComplexityRating complexity = ComplexityRating.valueOf(itComplexity.next());
      if (maxComplexity == null || complexity.compareTo(maxComplexity) > 0) {
        maxComplexity = complexity;
      }
    }

    complexityMap.put(ASS_SUMMARY_COMPLEXITY_SCORE, maxComplexity.toString());
  }

  private Map  getSummaryIssues(Map>  issues) {
    Map summaryIssuesMap = new LinkedHashMap();

    Iterator itIssueCategory = issues.keySet().iterator();
    while (itIssueCategory.hasNext()) {
      String issueCategory = itIssueCategory.next();
      List categoryIssues = issues.get(issueCategory);
      for (Issue catIs : categoryIssues) {
        String severity = catIs.getSeverity().name();
        Integer sevCount = summaryIssuesMap.get(severity);
        if (sevCount == null) {
          sevCount = 0;
        }
        summaryIssuesMap.put(severity, ++sevCount);
      }
    }

    return summaryIssuesMap;
  }

  private Map  getSummaryDevEffort(Map>  issues) {
    Map effortMap = new LinkedHashMap();
    float totalCost = 0f;

    Iterator itIssueCategory = issues.keySet().iterator();
    while (itIssueCategory.hasNext()) {
      float categoryTotalCost = 0f;

      String issueCategory = itIssueCategory.next();
      List categoryIssues = issues.get(issueCategory);
      for (Issue catIs : categoryIssues) {
        categoryTotalCost += catIs.getCost();
      }

      totalCost += categoryTotalCost;
      effortMap.put(issueCategory, categoryTotalCost);
    }
    effortMap.put("total", totalCost);

    return effortMap;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy