123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.computation.step;
-
- import com.google.common.base.Optional;
- import com.google.common.collect.ImmutableSortedMap;
- import java.util.Collections;
- import java.util.Date;
- import java.util.Map;
- import javax.annotation.Nullable;
- import org.apache.commons.lang.time.DateUtils;
- import org.sonar.api.measures.CoreMetrics;
- import org.sonar.api.resources.Language;
- import org.sonar.api.utils.KeyValueFormat;
- import org.sonar.core.util.UtcDateUtils;
- import org.sonar.server.computation.component.Component;
- import org.sonar.server.computation.component.DepthTraversalTypeAwareVisitor;
- import org.sonar.server.computation.component.TreeRootHolder;
- import org.sonar.server.computation.event.Event;
- import org.sonar.server.computation.event.EventRepository;
- import org.sonar.server.computation.language.LanguageRepository;
- import org.sonar.server.computation.measure.Measure;
- import org.sonar.server.computation.measure.MeasureRepository;
- import org.sonar.server.computation.metric.MetricRepository;
- import org.sonar.server.computation.qualityprofile.QPMeasureData;
- import org.sonar.server.computation.qualityprofile.QualityProfile;
-
- import static org.sonar.server.computation.component.ComponentVisitor.Order.POST_ORDER;
-
- public class QualityProfileEventsStep implements ComputationStep {
- private final TreeRootHolder treeRootHolder;
- private final MetricRepository metricRepository;
- private final MeasureRepository measureRepository;
- private final EventRepository eventRepository;
- private final LanguageRepository languageRepository;
-
- public QualityProfileEventsStep(TreeRootHolder treeRootHolder,
- MetricRepository metricRepository, MeasureRepository measureRepository, LanguageRepository languageRepository,
- EventRepository eventRepository) {
- this.treeRootHolder = treeRootHolder;
- this.metricRepository = metricRepository;
- this.measureRepository = measureRepository;
- this.eventRepository = eventRepository;
- this.languageRepository = languageRepository;
- }
-
-
- @Override
- public void execute() {
- new DepthTraversalTypeAwareVisitor(Component.Type.PROJECT, POST_ORDER) {
- @Override
- public void visitProject(Component tree) {
- executeForProject(tree);
- }
- }.visit(treeRootHolder.getRoot());
- }
-
- private void executeForProject(Component projectComponent) {
- Optional<Measure> baseMeasure = measureRepository.getBaseMeasure(projectComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY));
- if (!baseMeasure.isPresent()) {
- // first analysis -> do not generate events
- return;
- }
-
- // Load base profiles
- Optional<Measure> rawMeasure = measureRepository.getRawMeasure(projectComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY));
- if (!rawMeasure.isPresent()) {
- throw new IllegalStateException("Missing measure " + CoreMetrics.QUALITY_PROFILES + " for component " + projectComponent.getRef());
- }
- Map<String, QualityProfile> rawProfiles = QPMeasureData.fromJson(rawMeasure.get().getStringValue()).getProfilesByKey();
-
- Map<String, QualityProfile> baseProfiles = parseJsonData(baseMeasure);
- detectNewOrUpdatedProfiles(projectComponent, baseProfiles, rawProfiles);
- detectNoMoreUsedProfiles(projectComponent, baseProfiles, rawProfiles);
- }
-
- private static Map<String, QualityProfile> parseJsonData(Optional<Measure> measure) {
- String data = measure.get().getStringValue();
- if (data == null) {
- return Collections.emptyMap();
- }
- return QPMeasureData.fromJson(data).getProfilesByKey();
- }
-
- private void detectNoMoreUsedProfiles(Component context, Map<String, QualityProfile> baseProfiles, Map<String, QualityProfile> rawProfiles) {
- for (QualityProfile baseProfile : baseProfiles.values()) {
- if (!rawProfiles.containsKey(baseProfile.getQpKey())) {
- markAsRemoved(context, baseProfile);
- }
- }
- }
-
- private void detectNewOrUpdatedProfiles(Component component, Map<String, QualityProfile> baseProfiles, Map<String, QualityProfile> rawProfiles) {
- for (QualityProfile profile : rawProfiles.values()) {
- QualityProfile baseProfile = baseProfiles.get(profile.getQpKey());
- if (baseProfile == null) {
- markAsAdded(component, profile);
- } else if (profile.getRulesUpdatedAt().after(baseProfile.getRulesUpdatedAt())) {
- markAsChanged(component, baseProfile, profile);
- }
- }
- }
-
- private void markAsChanged(Component component, QualityProfile baseProfile, QualityProfile profile) {
- Date from = baseProfile.getRulesUpdatedAt();
-
- String data = KeyValueFormat.format(ImmutableSortedMap.of(
- "key", profile.getQpKey(),
- "from", UtcDateUtils.formatDateTime(fixDate(from)),
- "to", UtcDateUtils.formatDateTime(fixDate(profile.getRulesUpdatedAt()))));
- eventRepository.add(component, createQProfileEvent(profile, "Changes in %s", data));
- }
-
- private void markAsRemoved(Component component, QualityProfile profile) {
- eventRepository.add(component, createQProfileEvent(profile, "Stop using %s"));
- }
-
- private void markAsAdded(Component component, QualityProfile profile) {
- eventRepository.add(component, createQProfileEvent(profile, "Use %s"));
- }
-
- private Event createQProfileEvent(QualityProfile profile, String namePattern) {
- return createQProfileEvent(profile, namePattern, null);
- }
-
- private Event createQProfileEvent(QualityProfile profile, String namePattern, @Nullable String data) {
- return Event.createProfile(String.format(namePattern, profileLabel(profile)), data, null);
- }
-
- private String profileLabel(QualityProfile profile) {
- Optional<Language> language = languageRepository.find(profile.getLanguageKey());
- String languageName = language.isPresent() ? language.get().getName() : profile.getLanguageKey();
- return String.format("'%s' (%s)", profile.getQpName(), languageName);
- }
-
- /**
- * This hack must be done because date precision is millisecond in db/es and date format is select only
- */
- private static Date fixDate(Date date) {
- return DateUtils.addSeconds(date, 1);
- }
-
- @Override
- public String getDescription() {
- return "Compute Quality Profile events";
- }
- }
|