Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ProjectMeasuresDoc.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.measure.index;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.util.Collection;
  23. import java.util.Date;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import javax.annotation.CheckForNull;
  28. import javax.annotation.Nullable;
  29. import org.sonar.core.util.stream.MoreCollectors;
  30. import org.sonar.server.es.BaseDoc;
  31. import org.sonar.server.permission.index.AuthorizationDoc;
  32. import static org.sonar.api.measures.Metric.Level.ERROR;
  33. import static org.sonar.api.measures.Metric.Level.OK;
  34. import static org.sonar.api.measures.Metric.Level.WARN;
  35. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_ANALYSED_AT;
  36. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_KEY;
  37. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_LANGUAGES;
  38. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_MEASURES;
  39. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_NAME;
  40. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_NCLOC_DISTRIBUTION;
  41. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_QUALIFIER;
  42. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_QUALITY_GATE_STATUS;
  43. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_TAGS;
  44. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_UUID;
  45. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.SUB_FIELD_DISTRIB_LANGUAGE;
  46. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.SUB_FIELD_DISTRIB_NCLOC;
  47. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.SUB_FIELD_MEASURES_KEY;
  48. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.SUB_FIELD_MEASURES_VALUE;
  49. import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.TYPE_PROJECT_MEASURES;
  50. public class ProjectMeasuresDoc extends BaseDoc {
  51. public static final Map<String, Integer> QUALITY_GATE_STATUS = ImmutableMap.of(OK.name(), 1, WARN.name(), 2, ERROR.name(), 3);
  52. public ProjectMeasuresDoc() {
  53. super(TYPE_PROJECT_MEASURES, new HashMap<>(8));
  54. }
  55. @Override
  56. public String getId() {
  57. return getField(FIELD_UUID);
  58. }
  59. public ProjectMeasuresDoc setId(String s) {
  60. setField(FIELD_UUID, s);
  61. setParent(AuthorizationDoc.idOf(s));
  62. return this;
  63. }
  64. public String getKey() {
  65. return getField(FIELD_KEY);
  66. }
  67. public ProjectMeasuresDoc setKey(String s) {
  68. setField(FIELD_KEY, s);
  69. return this;
  70. }
  71. public String getName() {
  72. return getField(FIELD_NAME);
  73. }
  74. public ProjectMeasuresDoc setName(String s) {
  75. setField(FIELD_NAME, s);
  76. return this;
  77. }
  78. public String getQualifier() {
  79. return getField(FIELD_QUALIFIER);
  80. }
  81. public ProjectMeasuresDoc setQualifier(String s) {
  82. setField(FIELD_QUALIFIER, s);
  83. return this;
  84. }
  85. @CheckForNull
  86. public Date getAnalysedAt() {
  87. return getNullableField(FIELD_ANALYSED_AT);
  88. }
  89. public ProjectMeasuresDoc setAnalysedAt(@Nullable Date d) {
  90. setField(FIELD_ANALYSED_AT, d);
  91. return this;
  92. }
  93. public Collection<Map<String, Object>> getMeasures() {
  94. return getField(FIELD_MEASURES);
  95. }
  96. public ProjectMeasuresDoc setMeasures(Collection<Map<String, Object>> measures) {
  97. setField(FIELD_MEASURES, measures);
  98. return this;
  99. }
  100. public ProjectMeasuresDoc setMeasuresFromMap(Map<String, Double> measures) {
  101. setMeasures(
  102. measures.entrySet().stream()
  103. .map(entry -> ImmutableMap.<String, Object>of(
  104. SUB_FIELD_MEASURES_KEY, entry.getKey(),
  105. SUB_FIELD_MEASURES_VALUE, entry.getValue()))
  106. .collect(MoreCollectors.toList()));
  107. return this;
  108. }
  109. public ProjectMeasuresDoc setLanguages(List<String> languages) {
  110. setField(FIELD_LANGUAGES, languages);
  111. return this;
  112. }
  113. public Collection<Map<String, Object>> getNclocLanguageDistribution() {
  114. return getField(FIELD_NCLOC_DISTRIBUTION);
  115. }
  116. public ProjectMeasuresDoc setNclocLanguageDistribution(Collection<Map<String, Object>> distribution) {
  117. setField(FIELD_NCLOC_DISTRIBUTION, distribution);
  118. return this;
  119. }
  120. public ProjectMeasuresDoc setNclocLanguageDistributionFromMap(Map<String, Integer> distribution) {
  121. setNclocLanguageDistribution(
  122. distribution.entrySet().stream()
  123. .map(entry -> ImmutableMap.<String, Object>of(
  124. SUB_FIELD_DISTRIB_LANGUAGE, entry.getKey(),
  125. SUB_FIELD_DISTRIB_NCLOC, entry.getValue()))
  126. .collect(MoreCollectors.toList()));
  127. return this;
  128. }
  129. public ProjectMeasuresDoc setQualityGateStatus(@Nullable String s) {
  130. setField(FIELD_QUALITY_GATE_STATUS, s != null ? QUALITY_GATE_STATUS.get(s) : null);
  131. return this;
  132. }
  133. public ProjectMeasuresDoc setTags(List<String> tags) {
  134. setField(FIELD_TAGS, tags);
  135. return this;
  136. }
  137. }