您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TelemetryDataJsonWriter.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.telemetry;
  21. import java.time.Instant;
  22. import java.time.ZoneOffset;
  23. import java.time.format.DateTimeFormatter;
  24. import java.util.Locale;
  25. import org.apache.commons.codec.digest.DigestUtils;
  26. import org.jetbrains.annotations.NotNull;
  27. import org.sonar.api.utils.text.JsonWriter;
  28. import static org.sonar.api.utils.DateUtils.DATETIME_FORMAT;
  29. public class TelemetryDataJsonWriter {
  30. public static final String LANGUAGE_PROP = "language";
  31. public void writeTelemetryData(JsonWriter json, TelemetryData statistics) {
  32. json.beginObject();
  33. json.prop("id", statistics.getServerId());
  34. json.prop("version", statistics.getVersion());
  35. statistics.getEdition().ifPresent(e -> json.prop("edition", e.name().toLowerCase(Locale.ENGLISH)));
  36. statistics.getLicenseType().ifPresent(e -> json.prop("licenseType", e));
  37. json.name("database");
  38. json.beginObject();
  39. json.prop("name", statistics.getDatabase().getName());
  40. json.prop("version", statistics.getDatabase().getVersion());
  41. json.endObject();
  42. json.name("plugins");
  43. json.beginArray();
  44. statistics.getPlugins().forEach((plugin, version) -> {
  45. json.beginObject();
  46. json.prop("name", plugin);
  47. json.prop("version", version);
  48. json.endObject();
  49. });
  50. json.endArray();
  51. if (!statistics.getCustomSecurityConfigs().isEmpty()) {
  52. json.name("customSecurityConfig");
  53. json.beginArray();
  54. json.values(statistics.getCustomSecurityConfigs());
  55. json.endArray();
  56. }
  57. statistics.hasUnanalyzedC().ifPresent(hasUnanalyzedC -> json.prop("hasUnanalyzedC", hasUnanalyzedC));
  58. statistics.hasUnanalyzedCpp().ifPresent(hasUnanalyzedCpp -> json.prop("hasUnanalyzedCpp", hasUnanalyzedCpp));
  59. if (statistics.getInstallationDate() != null) {
  60. json.prop("installationDate", toUtc(statistics.getInstallationDate()));
  61. }
  62. if (statistics.getInstallationVersion() != null) {
  63. json.prop("installationVersion", statistics.getInstallationVersion());
  64. }
  65. json.prop("docker", statistics.isInDocker());
  66. writeUserData(json, statistics);
  67. writeProjectData(json, statistics);
  68. writeProjectStatsData(json, statistics);
  69. json.endObject();
  70. }
  71. private static void writeUserData(JsonWriter json, TelemetryData statistics) {
  72. if (statistics.getUserTelemetries() != null) {
  73. json.name("users");
  74. json.beginArray();
  75. statistics.getUserTelemetries().forEach(user -> {
  76. json.beginObject();
  77. json.prop("userUuid", DigestUtils.sha3_224Hex(user.getUuid()));
  78. json.prop("status", user.isActive() ? "active" : "inactive");
  79. json.prop("identityProvider", user.getExternalIdentityProvider());
  80. if (user.getLastConnectionDate() != null) {
  81. json.prop("lastActivity", toUtc(user.getLastConnectionDate()));
  82. }
  83. if (user.getLastSonarlintConnectionDate() != null) {
  84. json.prop("lastSonarlintActivity", toUtc(user.getLastSonarlintConnectionDate()));
  85. }
  86. json.endObject();
  87. });
  88. json.endArray();
  89. }
  90. }
  91. private static void writeProjectData(JsonWriter json, TelemetryData statistics) {
  92. if (statistics.getProjects() != null) {
  93. json.name("projects");
  94. json.beginArray();
  95. statistics.getProjects().forEach(project -> {
  96. json.beginObject();
  97. json.prop("projectUuid", project.getProjectUuid());
  98. if (project.getLastAnalysis() != null) {
  99. json.prop("lastAnalysis", toUtc(project.getLastAnalysis()));
  100. }
  101. json.prop(LANGUAGE_PROP, project.getLanguage());
  102. json.prop("loc", project.getLoc());
  103. json.endObject();
  104. });
  105. json.endArray();
  106. }
  107. }
  108. private static void writeProjectStatsData(JsonWriter json, TelemetryData statistics) {
  109. if (statistics.getProjectStatistics() != null) {
  110. json.name("projects-general-stats");
  111. json.beginArray();
  112. statistics.getProjectStatistics().forEach(project -> {
  113. json.beginObject();
  114. json.prop("projectUuid", project.getProjectUuid());
  115. json.prop("branchCount", project.getBranchCount());
  116. json.prop("pullRequestCount", project.getPullRequestCount());
  117. json.prop("scm", project.getScm());
  118. json.prop("ci", project.getCi());
  119. json.prop("devopsPlatform", project.getDevopsPlatform());
  120. json.endObject();
  121. });
  122. json.endArray();
  123. }
  124. }
  125. @NotNull
  126. private static String toUtc(long date) {
  127. return DateTimeFormatter.ofPattern(DATETIME_FORMAT)
  128. .withZone(ZoneOffset.UTC)
  129. .format(Instant.ofEpochMilli(date));
  130. }
  131. }