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

org.sonar.server.measure.live.LiveMeasureComputerImpl Maven / Gradle / Ivy

There is a newer version: 7.2.1
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2018 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.measure.live;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.sonar.api.config.Configuration;
import org.sonar.api.measures.Metric;
import org.sonar.api.utils.log.Loggers;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.BranchDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.measure.LiveMeasureDto;
import org.sonar.db.metric.MetricDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.computation.task.projectanalysis.qualitymodel.DebtRatingGrid;
import org.sonar.server.computation.task.projectanalysis.qualitymodel.Rating;
import org.sonar.server.es.ProjectIndexer;
import org.sonar.server.es.ProjectIndexers;
import org.sonar.server.qualitygate.EvaluatedQualityGate;
import org.sonar.server.qualitygate.QualityGate;
import org.sonar.server.qualitygate.changeevent.QGChangeEvent;
import org.sonar.server.settings.ProjectConfigurationLoader;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Collections.emptyList;
import static java.util.Collections.singleton;
import static java.util.stream.Collectors.groupingBy;
import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
import static org.sonar.core.util.stream.MoreCollectors.toArrayList;
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;

public class LiveMeasureComputerImpl implements LiveMeasureComputer {

  private final DbClient dbClient;
  private final IssueMetricFormulaFactory formulaFactory;
  private final LiveQualityGateComputer qGateComputer;
  private final ProjectConfigurationLoader projectConfigurationLoader;
  private final ProjectIndexers projectIndexer;

  public LiveMeasureComputerImpl(DbClient dbClient, IssueMetricFormulaFactory formulaFactory,
    LiveQualityGateComputer qGateComputer, ProjectConfigurationLoader projectConfigurationLoader, ProjectIndexers projectIndexer) {
    this.dbClient = dbClient;
    this.formulaFactory = formulaFactory;
    this.qGateComputer = qGateComputer;
    this.projectConfigurationLoader = projectConfigurationLoader;
    this.projectIndexer = projectIndexer;
  }

  @Override
  public List refresh(DbSession dbSession, Collection components) {
    if (components.isEmpty()) {
      return emptyList();
    }

    List result = new ArrayList<>();
    Map> componentsByProjectUuid = components.stream().collect(groupingBy(ComponentDto::projectUuid));
    for (List groupedComponents : componentsByProjectUuid.values()) {
      Optional qgChangeEvent = refreshComponentsOnSameProject(dbSession, groupedComponents);
      qgChangeEvent.ifPresent(result::add);
    }
    return result;
  }

  private Optional refreshComponentsOnSameProject(DbSession dbSession, List touchedComponents) {
    // load all the components to be refreshed, including their ancestors
    List components = loadTreeOfComponents(dbSession, touchedComponents);
    ComponentDto project = findProject(components);
    OrganizationDto organization = loadOrganization(dbSession, project);
    BranchDto branch = loadBranch(dbSession, project);

    Optional lastAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, project.uuid());
    if (!lastAnalysis.isPresent()) {
      return Optional.empty();
    }
    Optional beginningOfLeakPeriod = lastAnalysis.map(SnapshotDto::getPeriodDate);

    QualityGate qualityGate = qGateComputer.loadQualityGate(dbSession, organization, project, branch);
    Collection metricKeys = getKeysOfAllInvolvedMetrics(qualityGate);

    List metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys);
    Map metricsPerId = metrics.stream()
      .collect(uniqueIndex(MetricDto::getId));
    List componentUuids = components.stream().map(ComponentDto::uuid).collect(toArrayList(components.size()));
    List dbMeasures = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricIds(dbSession, componentUuids, metricsPerId.keySet());
    // previous status must be load now as MeasureMatrix mutate the LiveMeasureDto which are passed to it
    Metric.Level previousStatus = loadPreviousStatus(metrics, dbMeasures);

    Configuration config = projectConfigurationLoader.loadProjectConfiguration(dbSession, project);
    DebtRatingGrid debtRatingGrid = new DebtRatingGrid(config);

    MeasureMatrix matrix = new MeasureMatrix(components, metricsPerId.values(), dbMeasures);
    FormulaContextImpl context = new FormulaContextImpl(matrix, debtRatingGrid);
    components.forEach(c -> {
      IssueCounter issueCounter = new IssueCounter(dbClient.issueDao().selectIssueGroupsByBaseComponent(dbSession, c, beginningOfLeakPeriod.orElse(Long.MAX_VALUE)));
      for (IssueMetricFormula formula : formulaFactory.getFormulas()) {
        // exclude leak formulas when leak period is not defined
        if (beginningOfLeakPeriod.isPresent() || !formula.isOnLeak()) {
          context.change(c, formula);
          try {
            formula.compute(context, issueCounter);
          } catch (RuntimeException e) {
            throw new IllegalStateException("Fail to compute " + formula.getMetric().getKey() + " on " + context.getComponent().getDbKey(), e);
          }
        }
      }
    });

    EvaluatedQualityGate evaluatedQualityGate = qGateComputer.refreshGateStatus(project, qualityGate, matrix);

    // persist the measures that have been created or updated
    matrix.getChanged().forEach(m -> dbClient.liveMeasureDao().insertOrUpdate(dbSession, m, null));
    projectIndexer.commitAndIndex(dbSession, singleton(project), ProjectIndexer.Cause.MEASURE_CHANGE);

    return Optional.of(
      new QGChangeEvent(project, branch, lastAnalysis.get(), config, previousStatus, () -> Optional.of(evaluatedQualityGate)));
  }

  @CheckForNull
  private static Metric.Level loadPreviousStatus(List metrics, List dbMeasures) {
    MetricDto alertStatusMetric = metrics.stream()
      .filter(m -> ALERT_STATUS_KEY.equals(m.getKey()))
      .findAny()
      .orElseThrow(() -> new IllegalStateException(String.format("Metric with key %s is not registered", ALERT_STATUS_KEY)));
    return dbMeasures.stream()
      .filter(m -> m.getMetricId() == alertStatusMetric.getId())
      .map(LiveMeasureDto::getTextValue)
      .filter(Objects::nonNull)
      .map(m -> {
        try {
          return Metric.Level.valueOf(m);
        } catch (IllegalArgumentException e) {
          Loggers.get(LiveMeasureComputerImpl.class)
            .trace("Failed to parse value of metric '{}'", m, e);
          return null;
        }
      })
      .filter(Objects::nonNull)
      .findAny()
      .orElse(null);
  }

  private List loadTreeOfComponents(DbSession dbSession, List touchedComponents) {
    Set componentUuids = new HashSet<>();
    for (ComponentDto component : touchedComponents) {
      componentUuids.add(component.uuid());
      // ancestors, excluding self
      componentUuids.addAll(component.getUuidPathAsList());
    }
    // Contrary to the formulas in Compute Engine,
    // measures do not aggregate values of descendant components.
    // As a consequence nodes do not need to be sorted. Formulas can be applied
    // on components in any order.
    return dbClient.componentDao().selectByUuids(dbSession, componentUuids);
  }

  private Set getKeysOfAllInvolvedMetrics(QualityGate gate) {
    Set metricKeys = new HashSet<>();
    for (Metric metric : formulaFactory.getFormulaMetrics()) {
      metricKeys.add(metric.getKey());
    }
    metricKeys.addAll(qGateComputer.getMetricsRelatedTo(gate));
    return metricKeys;
  }

  private static ComponentDto findProject(Collection components) {
    return components.stream().filter(ComponentDto::isRootProject).findFirst()
      .orElseThrow(() -> new IllegalStateException("No project found in " + components));
  }

  private BranchDto loadBranch(DbSession dbSession, ComponentDto project) {
    return dbClient.branchDao().selectByUuid(dbSession, project.uuid())
      .orElseThrow(() -> new IllegalStateException("Branch not found: " + project.uuid()));
  }

  private OrganizationDto loadOrganization(DbSession dbSession, ComponentDto project) {
    String organizationUuid = project.getOrganizationUuid();
    return dbClient.organizationDao().selectByUuid(dbSession, organizationUuid)
      .orElseThrow(() -> new IllegalStateException("No organization with UUID " + organizationUuid));
  }

  private static class FormulaContextImpl implements IssueMetricFormula.Context {
    private final MeasureMatrix matrix;
    private final DebtRatingGrid debtRatingGrid;
    private ComponentDto currentComponent;
    private IssueMetricFormula currentFormula;

    private FormulaContextImpl(MeasureMatrix matrix, DebtRatingGrid debtRatingGrid) {
      this.matrix = matrix;
      this.debtRatingGrid = debtRatingGrid;
    }

    private void change(ComponentDto component, IssueMetricFormula formula) {
      this.currentComponent = component;
      this.currentFormula = formula;
    }

    @Override
    public ComponentDto getComponent() {
      return currentComponent;
    }

    @Override
    public DebtRatingGrid getDebtRatingGrid() {
      return debtRatingGrid;
    }

    @Override
    public Optional getValue(Metric metric) {
      Optional measure = matrix.getMeasure(currentComponent, metric.getKey());
      return measure.map(LiveMeasureDto::getValue);
    }

    @Override
    public Optional getLeakValue(Metric metric) {
      Optional measure = matrix.getMeasure(currentComponent, metric.getKey());
      return measure.map(LiveMeasureDto::getVariation);
    }

    @Override
    public void setValue(double value) {
      String metricKey = currentFormula.getMetric().getKey();
      checkState(!currentFormula.isOnLeak(), "Formula of metric %s accepts only leak values", metricKey);
      matrix.setValue(currentComponent, metricKey, value);
    }

    @Override
    public void setLeakValue(double value) {
      String metricKey = currentFormula.getMetric().getKey();
      checkState(currentFormula.isOnLeak(), "Formula of metric %s does not accept leak values", metricKey);
      matrix.setLeakValue(currentComponent, metricKey, value);
    }

    @Override
    public void setValue(Rating value) {
      String metricKey = currentFormula.getMetric().getKey();
      checkState(!currentFormula.isOnLeak(), "Formula of metric %s accepts only leak values", metricKey);
      matrix.setValue(currentComponent, metricKey, value);
    }

    @Override
    public void setLeakValue(Rating value) {
      String metricKey = currentFormula.getMetric().getKey();
      checkState(currentFormula.isOnLeak(), "Formula of metric %s does not accept leak values", metricKey);
      matrix.setLeakValue(currentComponent, metricKey, value);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy