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.

ProjectMeasuresDoc.java 5.6KB

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