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.

RegisterMetrics.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.startup;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.picocontainer.Startable;
  26. import org.sonar.api.measures.CoreMetrics;
  27. import org.sonar.api.measures.Metric;
  28. import org.sonar.api.measures.Metrics;
  29. import org.sonar.api.utils.log.Logger;
  30. import org.sonar.api.utils.log.Loggers;
  31. import org.sonar.api.utils.log.Profiler;
  32. import org.sonar.core.util.UuidFactory;
  33. import org.sonar.db.DbClient;
  34. import org.sonar.db.DbSession;
  35. import org.sonar.db.metric.MetricDto;
  36. import org.sonar.server.metric.MetricToDto;
  37. import static com.google.common.collect.FluentIterable.concat;
  38. import static com.google.common.collect.Lists.newArrayList;
  39. public class RegisterMetrics implements Startable {
  40. private static final Logger LOG = Loggers.get(RegisterMetrics.class);
  41. private final DbClient dbClient;
  42. private final UuidFactory uuidFactory;
  43. private final Metrics[] metricsRepositories;
  44. public RegisterMetrics(DbClient dbClient, UuidFactory uuidFactory, Metrics[] metricsRepositories) {
  45. this.dbClient = dbClient;
  46. this.uuidFactory = uuidFactory;
  47. this.metricsRepositories = metricsRepositories;
  48. }
  49. /**
  50. * Used when no plugin is defining Metrics
  51. */
  52. public RegisterMetrics(DbClient dbClient, UuidFactory uuidFactory) {
  53. this(dbClient, uuidFactory, new Metrics[] {});
  54. }
  55. @Override
  56. public void start() {
  57. register(concat(CoreMetrics.getMetrics(), getPluginMetrics()));
  58. }
  59. @Override
  60. public void stop() {
  61. // nothing to do
  62. }
  63. void register(Iterable<Metric> metrics) {
  64. Profiler profiler = Profiler.create(LOG).startInfo("Register metrics");
  65. try (DbSession session = dbClient.openSession(true)) {
  66. save(session, metrics);
  67. sanitizeQualityGates(session);
  68. session.commit();
  69. }
  70. profiler.stopDebug();
  71. }
  72. private void sanitizeQualityGates(DbSession session) {
  73. dbClient.gateConditionDao().deleteConditionsWithInvalidMetrics(session);
  74. }
  75. private void save(DbSession session, Iterable<Metric> metrics) {
  76. Map<String, MetricDto> basesByKey = new HashMap<>();
  77. var allMetrics = dbClient.metricDao().selectAll(session);
  78. for (MetricDto base : allMetrics) {
  79. basesByKey.put(base.getKey(), base);
  80. }
  81. for (Metric metric : metrics) {
  82. MetricDto dto = MetricToDto.INSTANCE.apply(metric);
  83. MetricDto base = basesByKey.get(metric.getKey());
  84. if (base == null) {
  85. // new metric, never installed
  86. dto.setUuid(uuidFactory.create());
  87. dbClient.metricDao().insert(session, dto);
  88. } else {
  89. dto.setUuid(base.getUuid());
  90. dbClient.metricDao().update(session, dto);
  91. }
  92. basesByKey.remove(metric.getKey());
  93. }
  94. for (MetricDto nonUpdatedBase : basesByKey.values()) {
  95. if (dbClient.metricDao().disableByKey(session, nonUpdatedBase.getKey())) {
  96. LOG.info("Disable metric {} [{}]", nonUpdatedBase.getShortName(), nonUpdatedBase.getKey());
  97. }
  98. }
  99. }
  100. @VisibleForTesting
  101. List<Metric> getPluginMetrics() {
  102. List<Metric> metricsToRegister = newArrayList();
  103. Map<String, Metrics> metricsByRepository = new HashMap<>();
  104. for (Metrics metrics : metricsRepositories) {
  105. checkMetrics(metricsByRepository, metrics);
  106. metricsToRegister.addAll(metrics.getMetrics());
  107. }
  108. return metricsToRegister;
  109. }
  110. private static void checkMetrics(Map<String, Metrics> metricsByRepository, Metrics metrics) {
  111. for (Metric metric : metrics.getMetrics()) {
  112. String metricKey = metric.getKey();
  113. if (CoreMetrics.getMetrics().contains(metric)) {
  114. throw new IllegalStateException(String.format("Metric [%s] is already defined by SonarQube", metricKey));
  115. }
  116. Metrics anotherRepository = metricsByRepository.get(metricKey);
  117. if (anotherRepository != null) {
  118. throw new IllegalStateException(String.format("Metric [%s] is already defined by the repository [%s]", metricKey, anotherRepository));
  119. }
  120. metricsByRepository.put(metricKey, metrics);
  121. }
  122. }
  123. }