You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QualityProfileEventsStep.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.computation.step;
  21. import com.google.common.base.Optional;
  22. import com.google.common.collect.ImmutableSortedMap;
  23. import java.util.Collections;
  24. import java.util.Date;
  25. import java.util.Map;
  26. import javax.annotation.Nullable;
  27. import org.apache.commons.lang.time.DateUtils;
  28. import org.sonar.api.measures.CoreMetrics;
  29. import org.sonar.api.resources.Language;
  30. import org.sonar.api.utils.KeyValueFormat;
  31. import org.sonar.core.util.UtcDateUtils;
  32. import org.sonar.server.computation.component.Component;
  33. import org.sonar.server.computation.component.DepthTraversalTypeAwareVisitor;
  34. import org.sonar.server.computation.component.TreeRootHolder;
  35. import org.sonar.server.computation.event.Event;
  36. import org.sonar.server.computation.event.EventRepository;
  37. import org.sonar.server.computation.language.LanguageRepository;
  38. import org.sonar.server.computation.measure.Measure;
  39. import org.sonar.server.computation.measure.MeasureRepository;
  40. import org.sonar.server.computation.metric.MetricRepository;
  41. import org.sonar.server.computation.qualityprofile.QPMeasureData;
  42. import org.sonar.server.computation.qualityprofile.QualityProfile;
  43. import static org.sonar.server.computation.component.ComponentVisitor.Order.POST_ORDER;
  44. public class QualityProfileEventsStep implements ComputationStep {
  45. private final TreeRootHolder treeRootHolder;
  46. private final MetricRepository metricRepository;
  47. private final MeasureRepository measureRepository;
  48. private final EventRepository eventRepository;
  49. private final LanguageRepository languageRepository;
  50. public QualityProfileEventsStep(TreeRootHolder treeRootHolder,
  51. MetricRepository metricRepository, MeasureRepository measureRepository, LanguageRepository languageRepository,
  52. EventRepository eventRepository) {
  53. this.treeRootHolder = treeRootHolder;
  54. this.metricRepository = metricRepository;
  55. this.measureRepository = measureRepository;
  56. this.eventRepository = eventRepository;
  57. this.languageRepository = languageRepository;
  58. }
  59. @Override
  60. public void execute() {
  61. new DepthTraversalTypeAwareVisitor(Component.Type.PROJECT, POST_ORDER) {
  62. @Override
  63. public void visitProject(Component tree) {
  64. executeForProject(tree);
  65. }
  66. }.visit(treeRootHolder.getRoot());
  67. }
  68. private void executeForProject(Component projectComponent) {
  69. Optional<Measure> baseMeasure = measureRepository.getBaseMeasure(projectComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY));
  70. if (!baseMeasure.isPresent()) {
  71. // first analysis -> do not generate events
  72. return;
  73. }
  74. // Load base profiles
  75. Optional<Measure> rawMeasure = measureRepository.getRawMeasure(projectComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY));
  76. if (!rawMeasure.isPresent()) {
  77. throw new IllegalStateException("Missing measure " + CoreMetrics.QUALITY_PROFILES + " for component " + projectComponent.getRef());
  78. }
  79. Map<String, QualityProfile> rawProfiles = QPMeasureData.fromJson(rawMeasure.get().getStringValue()).getProfilesByKey();
  80. Map<String, QualityProfile> baseProfiles = parseJsonData(baseMeasure);
  81. detectNewOrUpdatedProfiles(projectComponent, baseProfiles, rawProfiles);
  82. detectNoMoreUsedProfiles(projectComponent, baseProfiles, rawProfiles);
  83. }
  84. private static Map<String, QualityProfile> parseJsonData(Optional<Measure> measure) {
  85. String data = measure.get().getStringValue();
  86. if (data == null) {
  87. return Collections.emptyMap();
  88. }
  89. return QPMeasureData.fromJson(data).getProfilesByKey();
  90. }
  91. private void detectNoMoreUsedProfiles(Component context, Map<String, QualityProfile> baseProfiles, Map<String, QualityProfile> rawProfiles) {
  92. for (QualityProfile baseProfile : baseProfiles.values()) {
  93. if (!rawProfiles.containsKey(baseProfile.getQpKey())) {
  94. markAsRemoved(context, baseProfile);
  95. }
  96. }
  97. }
  98. private void detectNewOrUpdatedProfiles(Component component, Map<String, QualityProfile> baseProfiles, Map<String, QualityProfile> rawProfiles) {
  99. for (QualityProfile profile : rawProfiles.values()) {
  100. QualityProfile baseProfile = baseProfiles.get(profile.getQpKey());
  101. if (baseProfile == null) {
  102. markAsAdded(component, profile);
  103. } else if (profile.getRulesUpdatedAt().after(baseProfile.getRulesUpdatedAt())) {
  104. markAsChanged(component, baseProfile, profile);
  105. }
  106. }
  107. }
  108. private void markAsChanged(Component component, QualityProfile baseProfile, QualityProfile profile) {
  109. Date from = baseProfile.getRulesUpdatedAt();
  110. String data = KeyValueFormat.format(ImmutableSortedMap.of(
  111. "key", profile.getQpKey(),
  112. "from", UtcDateUtils.formatDateTime(fixDate(from)),
  113. "to", UtcDateUtils.formatDateTime(fixDate(profile.getRulesUpdatedAt()))));
  114. eventRepository.add(component, createQProfileEvent(profile, "Changes in %s", data));
  115. }
  116. private void markAsRemoved(Component component, QualityProfile profile) {
  117. eventRepository.add(component, createQProfileEvent(profile, "Stop using %s"));
  118. }
  119. private void markAsAdded(Component component, QualityProfile profile) {
  120. eventRepository.add(component, createQProfileEvent(profile, "Use %s"));
  121. }
  122. private Event createQProfileEvent(QualityProfile profile, String namePattern) {
  123. return createQProfileEvent(profile, namePattern, null);
  124. }
  125. private Event createQProfileEvent(QualityProfile profile, String namePattern, @Nullable String data) {
  126. return Event.createProfile(String.format(namePattern, profileLabel(profile)), data, null);
  127. }
  128. private String profileLabel(QualityProfile profile) {
  129. Optional<Language> language = languageRepository.find(profile.getLanguageKey());
  130. String languageName = language.isPresent() ? language.get().getName() : profile.getLanguageKey();
  131. return String.format("'%s' (%s)", profile.getQpName(), languageName);
  132. }
  133. /**
  134. * This hack must be done because date precision is millisecond in db/es and date format is select only
  135. */
  136. private static Date fixDate(Date date) {
  137. return DateUtils.addSeconds(date, 1);
  138. }
  139. @Override
  140. public String getDescription() {
  141. return "Compute Quality Profile events";
  142. }
  143. }