com.capitalone.dashboard.service.FeatureMetricServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api-audit Show documentation
Show all versions of api-audit Show documentation
Hygieia Audit Rest API Layer
package com.capitalone.dashboard.service;
import com.capitalone.dashboard.ApiSettings;
import com.capitalone.dashboard.model.Build;
import com.capitalone.dashboard.model.BuildStage;
import com.capitalone.dashboard.model.Cmdb;
import com.capitalone.dashboard.model.CodeQuality;
import com.capitalone.dashboard.model.CodeQualityMetric;
import com.capitalone.dashboard.model.CollectorItem;
import com.capitalone.dashboard.model.CollectorType;
import com.capitalone.dashboard.model.ComponentFeatureMetrics;
import com.capitalone.dashboard.model.Dashboard;
import com.capitalone.dashboard.model.DashboardType;
import com.capitalone.dashboard.model.ExecutiveFeatureMetrics;
import com.capitalone.dashboard.model.Feature;
import com.capitalone.dashboard.model.LobFeatureMetrics;
import com.capitalone.dashboard.model.ProductFeatureMetrics;
import com.capitalone.dashboard.model.TestCapability;
import com.capitalone.dashboard.model.TestCase;
import com.capitalone.dashboard.model.TestResult;
import com.capitalone.dashboard.model.TestSuite;
import com.capitalone.dashboard.model.TestSuiteType;
import com.capitalone.dashboard.model.Widget;
import com.capitalone.dashboard.repository.BuildRepository;
import com.capitalone.dashboard.repository.CmdbRepository;
import com.capitalone.dashboard.repository.CodeQualityRepository;
import com.capitalone.dashboard.repository.ComponentRepository;
import com.capitalone.dashboard.repository.DashboardRepository;
import com.capitalone.dashboard.repository.FeatureRepository;
import com.capitalone.dashboard.repository.TestResultRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Component
public class FeatureMetricServiceImpl implements FeatureMetricsService {
private static final String STR_COVERAGE = "CODE_COVERAGE";
private static final String STR_ERRORRATE = "PERF_ERROR_RATE";
private static final String STR_FEATURE_TEST_PASS = "FEATURE_TEST_PASS";
private static final String STR_TRACEABILITY = "TRACEABILITY";
private static final String STR_DEPLOY_SCRIPTS = "DEPLOY_SCRIPTS";
private static final String STR_PERCENTAGE = "percentage";
private static final String STR_ACTUAL_ERRORRATE = "Actual Error Rate";
private static final String STR_KPI_ERRORRATE = "KPI : Error Rate Threshold";
private static final String STR_MTR_COVERAGE = "coverage";
private static final String STR_WIDGETNAME = "codeanalysis";
private static final String STR_WIDGET_BUILD = "build";
private static final String WIDGET_FEATURE = "feature";
private static final String STR_TEAM_ID = "teamId";
private static final String SUCCESS = "success";
private static final String FAILED = "failed";
private static final String STR_UNDERSCORE = "_";
private static final String STR_HYPHEN = "-";
private static final String STR_AT = "@";
private static final String STR_EMPTY = "";
private final DashboardRepository dashboardRepository;
private final ComponentRepository componentRepository;
private final CodeQualityRepository codeQualityRepository;
private final TestResultRepository testResultRepository;
private final CmdbRepository cmdbRepository;
private final FeatureRepository featureRepository;
private final BuildRepository buildRepository;
private final ApiSettings apiSettings;
private static DecimalFormat df2 = new DecimalFormat("#.##");
@Autowired
public FeatureMetricServiceImpl(DashboardRepository dashboardRepository, ComponentRepository componentRepository, CodeQualityRepository codeQualityRepository, TestResultRepository testResultRepository, CmdbRepository cmdbRepository, FeatureRepository featureRepository, BuildRepository buildRepository, ApiSettings apiSettings) {
this.dashboardRepository = dashboardRepository;
this.componentRepository = componentRepository;
this.codeQualityRepository = codeQualityRepository;
this.testResultRepository = testResultRepository;
this.cmdbRepository = cmdbRepository;
this.featureRepository = featureRepository;
this.buildRepository = buildRepository;
this.apiSettings = apiSettings;
}
@Override
public ComponentFeatureMetrics getComponentFeatureMetrics(String name) {
ComponentFeatureMetrics featureMetrics = new ComponentFeatureMetrics();
Dashboard dashboard = dashboardRepository.findByTitleAndType(name, DashboardType.Team);
if(dashboard != null) {
List metrics = getMetrics(dashboard, null);
featureMetrics.setId(dashboard.getTitle());
featureMetrics.setType("Component");
featureMetrics.setName(dashboard.getApplication().getName());
featureMetrics.setMetrics(metrics);
}else{
featureMetrics.setName(name);
featureMetrics.setMessage("Component is not configured");
}
return featureMetrics;
}
@Override
public ComponentFeatureMetrics getComponentFeatureMetricByType(String name, String type) {
ComponentFeatureMetrics featureMetrics = new ComponentFeatureMetrics();
Dashboard dashboard = dashboardRepository.findByTitleAndType(name, DashboardType.Team);
if(dashboard != null){
List metrics = getMetrics(dashboard, type);
featureMetrics.setId(dashboard.getTitle());
featureMetrics.setType("Component");
featureMetrics.setName(dashboard.getApplication().getName());
featureMetrics.setMetrics(metrics);
}else{
featureMetrics.setName(name);
featureMetrics.setMessage("Component is not configured");
}
return featureMetrics;
}
@Override
public ProductFeatureMetrics getProductFeatureMetrics(String name) {
ProductFeatureMetrics productFeatureMetrics = new ProductFeatureMetrics();
Cmdb cmdb = cmdbRepository.findByConfigurationItemAndItemTypeAndValidConfigItem(name, "app", true);
List components = cmdb.getComponents();
List productComponent = new ArrayList<>();
Optional.ofNullable(components).orElseGet(Collections::emptyList)
.stream().forEach(component -> {productComponent.add(getComponentFeatureMetrics(component));});
productFeatureMetrics.setId(name);
productFeatureMetrics.setLob(cmdb.getOwnerDept());
productFeatureMetrics.setName(cmdb.getCommonName());
productFeatureMetrics.setType("application");
productFeatureMetrics.setComponents(productComponent);
return productFeatureMetrics;
}
@Override
public ProductFeatureMetrics getProductFeatureMetricsByType(String name, String type) {
ProductFeatureMetrics productFeatureMetrics = new ProductFeatureMetrics();
Cmdb cmdb = cmdbRepository.findByConfigurationItemAndItemTypeAndValidConfigItem(name,"app" ,true );
List components = cmdb.getComponents();
List productComponent = new ArrayList<>();
Optional.ofNullable(components).orElseGet(Collections::emptyList)
.stream().forEach(component -> {productComponent.add(getComponentFeatureMetricByType(component,type));});
productFeatureMetrics.setId(name);
productFeatureMetrics.setLob(cmdb.getOwnerDept());
productFeatureMetrics.setName(cmdb.getCommonName());
productFeatureMetrics.setType("application");
productFeatureMetrics.setComponents(productComponent);
return productFeatureMetrics;
}
@Override
public LobFeatureMetrics getLobFeatureMetrics(String lob) {
LobFeatureMetrics lobFeatureMetrics = new LobFeatureMetrics();
List productFeatureMetrics = new ArrayList<>();
List cmdb = cmdbRepository.findByItemTypeAndOwnerDeptAndValidConfigItem("app", lob,true);
List componentNames = new ArrayList<>();
cmdb.forEach(cmdb1 -> componentNames.add(cmdb1.getConfigurationItem()));
componentNames.forEach(componentName -> productFeatureMetrics.add(getProductFeatureMetrics(componentName)));
lobFeatureMetrics.setName(lob);
lobFeatureMetrics.setType("lob");
lobFeatureMetrics.setApplications(productFeatureMetrics);
return lobFeatureMetrics;
}
@Override
public LobFeatureMetrics getLobFeatureMetricsByType(String lob, String type) {
LobFeatureMetrics lobFeatureMetrics = new LobFeatureMetrics();
List productFeatureMetrics = new ArrayList<>();
List cmdb = cmdbRepository.findByItemTypeAndOwnerDeptAndValidConfigItem("app", lob,true);
cmdb.stream().forEach(cmdb1 -> productFeatureMetrics.add(getProductMetrics(cmdb1,type)));
List percentages = productFeatureMetrics.stream().map(ProductFeatureMetrics::getPercentage).collect(Collectors.toList());
List actualPercentage = new ArrayList<>();
percentages.forEach(percentage -> {
if(!percentage.equals(Double.NaN)){
actualPercentage.add(percentage);
}
});
DoubleSummaryStatistics stats = actualPercentage.stream().mapToDouble((x) -> x).summaryStatistics();
lobFeatureMetrics.setName(lob);
lobFeatureMetrics.setType("lob");
lobFeatureMetrics.setPercentage(String.valueOf(df2.format(stats.getAverage())));
lobFeatureMetrics.setApplications(productFeatureMetrics);
return lobFeatureMetrics;
}
@Override
public ExecutiveFeatureMetrics getExecutiveFeatureMetrics(String name) {
ExecutiveFeatureMetrics executiveFeatureMetrics = new ExecutiveFeatureMetrics();
List cmdb = cmdbRepository.findByBusinessOwnerAndItemTypeAndValidConfigItem(name, "app",true);
List productFeatureMetrics = new ArrayList<>();
cmdb.stream().forEach(cmdb1 -> productFeatureMetrics.add(getProductMetrics(cmdb1,null)));
executiveFeatureMetrics.setName(name);
executiveFeatureMetrics.setType("executive");
executiveFeatureMetrics.setApplications(productFeatureMetrics);
return executiveFeatureMetrics;
}
@Override
public ExecutiveFeatureMetrics getExecutiveFeatureMetricsByType(String name, String metricType) {
ExecutiveFeatureMetrics executiveFeatureMetrics = new ExecutiveFeatureMetrics();
List cmdb = cmdbRepository.findByBusinessOwnerAndItemTypeAndValidConfigItem(name, "app",true);
List productFeatureMetrics = new ArrayList<>();
cmdb.stream().forEach(cmdb1 -> productFeatureMetrics.add(getProductMetrics(cmdb1,metricType)));
List percentages = productFeatureMetrics.stream().map(ProductFeatureMetrics::getPercentage).collect(Collectors.toList());
List actualPercentage = new ArrayList<>();
percentages.forEach(percentage -> {
if(!percentage.equals(Double.NaN)){
actualPercentage.add(percentage);
}
});
DoubleSummaryStatistics stats = actualPercentage.stream().mapToDouble((x) -> x).summaryStatistics();
executiveFeatureMetrics.setName(name);
executiveFeatureMetrics.setType("executive");
executiveFeatureMetrics.setPercentage(df2.format(stats.getAverage()));
executiveFeatureMetrics.setApplications(productFeatureMetrics);
return executiveFeatureMetrics;
}
/**
* Get product metrics
* @param cmdb
* @param type
* @return
*/
private ProductFeatureMetrics getProductMetrics(Cmdb cmdb,String type){
ProductFeatureMetrics productFeatureMetrics = new ProductFeatureMetrics();
List percentages = new ArrayList<>();
List components = cmdb.getComponents();
List productComponent = new ArrayList<>();
Optional.ofNullable(components).orElseGet(Collections::emptyList)
.stream().forEach(component -> {
ComponentFeatureMetrics featureMetrics = getComponentFeatureMetricByType(component,type);
productComponent.add(featureMetrics);
Optional.ofNullable(featureMetrics.getMetrics()).orElseGet(Collections ::emptyList).stream().forEach(metric-> {
if(metric.containsKey(type)){
Optional> percent = Optional.of((HashMap) metric.get(type));
if(percent.get().containsKey(STR_PERCENTAGE)){
percentages.add(Double.parseDouble(percent.get().get(STR_PERCENTAGE)));
}
}
});
});
List actualPercentage = new ArrayList<>();
percentages.forEach(percentage -> {
if(!percentage.equals(Double.NaN)){
actualPercentage.add(percentage);
}
});
DoubleSummaryStatistics stats = actualPercentage.stream().mapToDouble((x) -> x).summaryStatistics();
productFeatureMetrics.setId(cmdb.getConfigurationItem());
productFeatureMetrics.setLob(cmdb.getOwnerDept());
productFeatureMetrics.setName(cmdb.getCommonName());
productFeatureMetrics.setType("application");
if(actualPercentage.size() > 0){
productFeatureMetrics.setPercentage(Double.valueOf(df2.format(stats.getAverage())));
}else{
productFeatureMetrics.setPercentage(Double.NaN);
}
productFeatureMetrics.setComponents(productComponent);
return productFeatureMetrics;
}
/**
* Get metrics
* @param dashboard
* @param type
* @return
*/
private List getMetrics(Dashboard dashboard, String type){
List metrics = new ArrayList<>();
if(type == null) {
List codeQualityItems = getCollectorItems(dashboard, STR_WIDGETNAME, CollectorType.CodeQuality);
List perfItems = getCollectorItems(dashboard, STR_WIDGETNAME, CollectorType.Test);
List testItems = getCollectorItems(dashboard, STR_WIDGETNAME, CollectorType.Test);
List buildItems = getCollectorItems(dashboard, STR_WIDGET_BUILD, CollectorType.Build);
HashMap> codeQuality = getAggregatedCodeQuality(codeQualityItems);
HashMap> errorRate = getAggregatedErrorRate(perfItems);
HashMap> featureTest = getAggregatedFeatureTestPass(testItems);
HashMap> traceability = getAggregatedTraceability(testItems,dashboard);
HashMap> deployScripts = getAggregatedDeployScripts(buildItems,dashboard);
metrics.add(codeQuality);
metrics.add(errorRate);
metrics.add(featureTest);
metrics.add(traceability);
metrics.add(deployScripts);
}else if(STR_COVERAGE.equalsIgnoreCase(type)){
List codeQualityItems = getCollectorItems(dashboard, STR_WIDGETNAME, CollectorType.CodeQuality);
HashMap> codeQuality = getAggregatedCodeQuality(codeQualityItems);
metrics.add(codeQuality);
}else if(STR_ERRORRATE.equalsIgnoreCase(type)){
List perfItems = getCollectorItems(dashboard, STR_WIDGETNAME, CollectorType.Test);
HashMap> errorRate = getAggregatedErrorRate(perfItems);
metrics.add(errorRate);
}else if(STR_FEATURE_TEST_PASS.equalsIgnoreCase(type)){
List testItems = getCollectorItems(dashboard, STR_WIDGETNAME, CollectorType.Test);
HashMap> testItems1 = getAggregatedFeatureTestPass(testItems);
metrics.add(testItems1);
} else if (STR_TRACEABILITY.equalsIgnoreCase(type)) {
List testItems = getCollectorItems(dashboard, STR_WIDGETNAME, CollectorType.Test);
HashMap> traceability = getAggregatedTraceability(testItems,dashboard);
metrics.add(traceability);
}else if(STR_DEPLOY_SCRIPTS.equalsIgnoreCase(type)){
List buildItems = getCollectorItems(dashboard, STR_WIDGET_BUILD, CollectorType.Build);
HashMap> deployScripts = getAggregatedDeployScripts(buildItems,dashboard);
metrics.add(deployScripts);
}
return metrics;
}
/**
* Get deploy scripts map
* @param buildItems
* @param dashboard
* @return
*/
private HashMap> getAggregatedDeployScripts(List buildItems, Dashboard dashboard) {
List values = new ArrayList();
HashMap> deployScripts = new HashMap<>();
HashMap deployScriptsPercentage = new HashMap<>();
if(CollectionUtils.isNotEmpty(buildItems)){
buildItems.forEach(collectorItemId -> {
Build build = buildRepository.findTop1ByCollectorItemIdOrderByTimestampDesc(collectorItemId);
if(build != null){
List stages = build.getStages();
if (matchStage(stages, SUCCESS, apiSettings)) {
values.add(100.0);
} else if (matchStage(stages, FAILED, apiSettings)) {
values.add(0.0);
}else{
values.add(0.0);
}
}
});
deployScripts.put(STR_DEPLOY_SCRIPTS, componentLevelAverage(values));
}else {
deployScriptsPercentage.put("message", "BuildItems not configured");
deployScripts.put(STR_DEPLOY_SCRIPTS, deployScriptsPercentage);
}
return deployScripts;
}
public boolean matchStage(List stages, String status, ApiSettings settings) {
boolean isMatch = false;
if (CollectionUtils.isEmpty(settings.getBuildStageRegEx()) || CollectionUtils.isEmpty(stages)) return false;
for (String pattern : settings.getBuildStageRegEx()) {
if (stages.stream().filter(s -> Pattern.compile(pattern).matcher(s.getName()).find() && s.getStatus().equalsIgnoreCase(status)).findAny().isPresent()) {
isMatch = true;
}
}
return isMatch;
}
/**
* Get traceability
* @param testItems
* @param dashboard
* @return
*/
private HashMap> getAggregatedTraceability(List testItems,Dashboard dashboard) {
List values = new ArrayList<>();
HashMap> traceability = new HashMap<>();
HashMap featureTestPercent = new HashMap<>();
Widget featureWidget = getFeatureWidget(dashboard);
Optional
© 2015 - 2024 Weber Informatics LLC | Privacy Policy