Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ProjectMeasuresDoc.java 5.5KB

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