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.

TelemetryData.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.util.Arrays;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Objects;
  26. import java.util.Optional;
  27. import java.util.Set;
  28. import javax.annotation.Nullable;
  29. import org.sonar.core.platform.EditionProvider.Edition;
  30. import org.sonar.db.project.CreationMethod;
  31. import org.sonar.db.user.UserTelemetryDto;
  32. import org.sonar.server.qualitygate.Condition;
  33. import static java.util.Objects.requireNonNullElse;
  34. import static org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION;
  35. public class TelemetryData {
  36. private final String serverId;
  37. private final String version;
  38. private final Long messageSequenceNumber;
  39. private final Map<String, String> plugins;
  40. private final Database database;
  41. private final Edition edition;
  42. private final String defaultQualityGate;
  43. private final String sonarWayQualityGate;
  44. private final Long installationDate;
  45. private final String installationVersion;
  46. private final boolean inContainer;
  47. private final ManagedInstanceInformation managedInstanceInformation;
  48. private final CloudUsage cloudUsage;
  49. private final List<UserTelemetryDto> users;
  50. private final List<Project> projects;
  51. private final List<ProjectStatistics> projectStatistics;
  52. private final List<Branch> branches;
  53. private final List<QualityGate> qualityGates;
  54. private final List<QualityProfile> qualityProfiles;
  55. private final Collection<NewCodeDefinition> newCodeDefinitions;
  56. private final Boolean hasUnanalyzedC;
  57. private final Boolean hasUnanalyzedCpp;
  58. private final int ncdId;
  59. private final Set<String> customSecurityConfigs;
  60. private TelemetryData(Builder builder) {
  61. serverId = builder.serverId;
  62. version = builder.version;
  63. messageSequenceNumber = builder.messageSequenceNumber;
  64. plugins = builder.plugins;
  65. database = builder.database;
  66. edition = builder.edition;
  67. defaultQualityGate = builder.defaultQualityGate;
  68. sonarWayQualityGate = builder.sonarWayQualityGate;
  69. installationDate = builder.installationDate;
  70. installationVersion = builder.installationVersion;
  71. inContainer = builder.inContainer;
  72. users = builder.users;
  73. projects = builder.projects;
  74. projectStatistics = builder.projectStatistics;
  75. qualityGates = builder.qualityGates;
  76. qualityProfiles = builder.qualityProfiles;
  77. hasUnanalyzedC = builder.hasUnanalyzedC;
  78. hasUnanalyzedCpp = builder.hasUnanalyzedCpp;
  79. customSecurityConfigs = requireNonNullElse(builder.customSecurityConfigs, Set.of());
  80. managedInstanceInformation = builder.managedInstanceInformation;
  81. cloudUsage = builder.cloudUsage;
  82. ncdId = builder.ncdId;
  83. branches = builder.branches;
  84. newCodeDefinitions = builder.newCodeDefinitions;
  85. }
  86. public String getServerId() {
  87. return serverId;
  88. }
  89. public String getVersion() {
  90. return version;
  91. }
  92. public Long getMessageSequenceNumber() {
  93. return messageSequenceNumber;
  94. }
  95. public Map<String, String> getPlugins() {
  96. return plugins;
  97. }
  98. public Database getDatabase() {
  99. return database;
  100. }
  101. public Optional<Edition> getEdition() {
  102. return Optional.ofNullable(edition);
  103. }
  104. public String getDefaultQualityGate() {
  105. return defaultQualityGate;
  106. }
  107. public String getSonarWayQualityGate() {
  108. return sonarWayQualityGate;
  109. }
  110. public Long getInstallationDate() {
  111. return installationDate;
  112. }
  113. public String getInstallationVersion() {
  114. return installationVersion;
  115. }
  116. public boolean isInContainer() {
  117. return inContainer;
  118. }
  119. public ManagedInstanceInformation getManagedInstanceInformation() {
  120. return managedInstanceInformation;
  121. }
  122. public CloudUsage getCloudUsage() {
  123. return cloudUsage;
  124. }
  125. public Optional<Boolean> hasUnanalyzedC() {
  126. return Optional.ofNullable(hasUnanalyzedC);
  127. }
  128. public Optional<Boolean> hasUnanalyzedCpp() {
  129. return Optional.ofNullable(hasUnanalyzedCpp);
  130. }
  131. public Set<String> getCustomSecurityConfigs() {
  132. return customSecurityConfigs;
  133. }
  134. public List<UserTelemetryDto> getUserTelemetries() {
  135. return users;
  136. }
  137. public List<Project> getProjects() {
  138. return projects;
  139. }
  140. public List<ProjectStatistics> getProjectStatistics() {
  141. return projectStatistics;
  142. }
  143. public List<QualityGate> getQualityGates() {
  144. return qualityGates;
  145. }
  146. public List<QualityProfile> getQualityProfiles() {
  147. return qualityProfiles;
  148. }
  149. static Builder builder() {
  150. return new Builder();
  151. }
  152. public int getNcdId() {
  153. return ncdId;
  154. }
  155. public List<Branch> getBranches() {
  156. return branches;
  157. }
  158. public Collection<NewCodeDefinition> getNewCodeDefinitions() {
  159. return newCodeDefinitions;
  160. }
  161. static class Builder {
  162. private String serverId;
  163. private String version;
  164. private Long messageSequenceNumber;
  165. private Map<String, String> plugins;
  166. private Database database;
  167. private Edition edition;
  168. private String defaultQualityGate;
  169. private String sonarWayQualityGate;
  170. private Long installationDate;
  171. private String installationVersion;
  172. private boolean inContainer = false;
  173. private ManagedInstanceInformation managedInstanceInformation;
  174. private CloudUsage cloudUsage;
  175. private Boolean hasUnanalyzedC;
  176. private Boolean hasUnanalyzedCpp;
  177. private Set<String> customSecurityConfigs;
  178. private List<UserTelemetryDto> users;
  179. private List<Project> projects;
  180. private List<ProjectStatistics> projectStatistics;
  181. private List<Branch> branches;
  182. private Collection<NewCodeDefinition> newCodeDefinitions;
  183. private List<QualityGate> qualityGates;
  184. private List<QualityProfile> qualityProfiles;
  185. private int ncdId;
  186. private Builder() {
  187. // enforce static factory method
  188. }
  189. Builder setServerId(String serverId) {
  190. this.serverId = serverId;
  191. return this;
  192. }
  193. Builder setVersion(String version) {
  194. this.version = version;
  195. return this;
  196. }
  197. Builder setMessageSequenceNumber(@Nullable Long messageSequenceNumber) {
  198. this.messageSequenceNumber = messageSequenceNumber;
  199. return this;
  200. }
  201. Builder setPlugins(Map<String, String> plugins) {
  202. this.plugins = plugins;
  203. return this;
  204. }
  205. Builder setDatabase(Database database) {
  206. this.database = database;
  207. return this;
  208. }
  209. Builder setEdition(@Nullable Edition edition) {
  210. this.edition = edition;
  211. return this;
  212. }
  213. Builder setDefaultQualityGate(String defaultQualityGate) {
  214. this.defaultQualityGate = defaultQualityGate;
  215. return this;
  216. }
  217. Builder setSonarWayQualityGate(String sonarWayQualityGate) {
  218. this.sonarWayQualityGate = sonarWayQualityGate;
  219. return this;
  220. }
  221. Builder setInstallationDate(@Nullable Long installationDate) {
  222. this.installationDate = installationDate;
  223. return this;
  224. }
  225. Builder setInstallationVersion(@Nullable String installationVersion) {
  226. this.installationVersion = installationVersion;
  227. return this;
  228. }
  229. Builder setInContainer(boolean inContainer) {
  230. this.inContainer = inContainer;
  231. return this;
  232. }
  233. Builder setHasUnanalyzedC(@Nullable Boolean hasUnanalyzedC) {
  234. this.hasUnanalyzedC = hasUnanalyzedC;
  235. return this;
  236. }
  237. Builder setHasUnanalyzedCpp(@Nullable Boolean hasUnanalyzedCpp) {
  238. this.hasUnanalyzedCpp = hasUnanalyzedCpp;
  239. return this;
  240. }
  241. Builder setCustomSecurityConfigs(Set<String> customSecurityConfigs) {
  242. this.customSecurityConfigs = customSecurityConfigs;
  243. return this;
  244. }
  245. Builder setUsers(List<UserTelemetryDto> users) {
  246. this.users = users;
  247. return this;
  248. }
  249. Builder setProjects(List<Project> projects) {
  250. this.projects = projects;
  251. return this;
  252. }
  253. Builder setManagedInstanceInformation(ManagedInstanceInformation managedInstanceInformation) {
  254. this.managedInstanceInformation = managedInstanceInformation;
  255. return this;
  256. }
  257. Builder setCloudUsage(CloudUsage cloudUsage) {
  258. this.cloudUsage = cloudUsage;
  259. return this;
  260. }
  261. TelemetryData build() {
  262. requireNonNullValues(serverId, version, plugins, database, messageSequenceNumber);
  263. return new TelemetryData(this);
  264. }
  265. Builder setProjectStatistics(List<ProjectStatistics> projectStatistics) {
  266. this.projectStatistics = projectStatistics;
  267. return this;
  268. }
  269. Builder setQualityGates(List<QualityGate> qualityGates) {
  270. this.qualityGates = qualityGates;
  271. return this;
  272. }
  273. Builder setQualityProfiles(List<QualityProfile> qualityProfiles) {
  274. this.qualityProfiles = qualityProfiles;
  275. return this;
  276. }
  277. Builder setNcdId(int ncdId) {
  278. this.ncdId = ncdId;
  279. return this;
  280. }
  281. private static void requireNonNullValues(Object... values) {
  282. Arrays.stream(values).forEach(Objects::requireNonNull);
  283. }
  284. Builder setBranches(List<Branch> branches) {
  285. this.branches = branches;
  286. return this;
  287. }
  288. Builder setNewCodeDefinitions(Collection<NewCodeDefinition> newCodeDefinitions) {
  289. this.newCodeDefinitions = newCodeDefinitions;
  290. return this;
  291. }
  292. }
  293. record Database(String name, String version) {
  294. }
  295. record NewCodeDefinition(String type, @Nullable String value, String scope) {
  296. private static final NewCodeDefinition instanceDefault = new NewCodeDefinition(PREVIOUS_VERSION.name(), "", "instance");
  297. public static NewCodeDefinition getInstanceDefault() {
  298. return instanceDefault;
  299. }
  300. @Override
  301. public String value() {
  302. return value == null ? "" : value;
  303. }
  304. }
  305. record Branch(String projectUuid, String branchUuid, int ncdId, int greenQualityGateCount, int analysisCount, boolean excludeFromPurge) {
  306. }
  307. record Project(String projectUuid, Long lastAnalysis, String language, String qualityProfile, Long loc) {
  308. }
  309. record QualityGate(String uuid, String caycStatus, List<Condition> conditions) {
  310. }
  311. public record QualityProfile(String uuid, @Nullable String parentUuid, String language, boolean isDefault,
  312. boolean isBuiltIn,
  313. @Nullable Boolean builtInParent, @Nullable Integer rulesOverriddenCount,
  314. @Nullable Integer rulesActivatedCount, @Nullable Integer rulesDeactivatedCount) {
  315. }
  316. record ManagedInstanceInformation(boolean isManaged, @Nullable String provider) {
  317. }
  318. record CloudUsage(boolean kubernetes, @Nullable String kubernetesVersion, @Nullable String kubernetesPlatform,
  319. @Nullable String kubernetesProvider,
  320. @Nullable String officialHelmChart, @Nullable String containerRuntime, boolean officialImage) {
  321. }
  322. public static class ProjectStatistics {
  323. private final String projectUuid;
  324. private final Long branchCount;
  325. private final Long pullRequestCount;
  326. private final String qualityGate;
  327. private final String scm;
  328. private final String ci;
  329. private final String devopsPlatform;
  330. private final Long bugs;
  331. private final Long vulnerabilities;
  332. private final Long securityHotspots;
  333. private final Long technicalDebt;
  334. private final Long developmentCost;
  335. private final int ncdId;
  336. private final Long externalSecurityReportExportedAt;
  337. private final CreationMethod creationMethod;
  338. private final Boolean monorepo;
  339. ProjectStatistics(Builder builder) {
  340. this.projectUuid = builder.projectUuid;
  341. this.branchCount = builder.branchCount;
  342. this.pullRequestCount = builder.pullRequestCount;
  343. this.qualityGate = builder.qualityGate;
  344. this.scm = builder.scm;
  345. this.ci = builder.ci;
  346. this.devopsPlatform = builder.devopsPlatform;
  347. this.bugs = builder.bugs;
  348. this.vulnerabilities = builder.vulnerabilities;
  349. this.securityHotspots = builder.securityHotspots;
  350. this.technicalDebt = builder.technicalDebt;
  351. this.developmentCost = builder.developmentCost;
  352. this.ncdId = builder.ncdId;
  353. this.externalSecurityReportExportedAt = builder.externalSecurityReportExportedAt;
  354. this.creationMethod = builder.creationMethod;
  355. this.monorepo = builder.monorepo;
  356. }
  357. public int getNcdId() {
  358. return ncdId;
  359. }
  360. public String getProjectUuid() {
  361. return projectUuid;
  362. }
  363. public Long getBranchCount() {
  364. return branchCount;
  365. }
  366. public Long getPullRequestCount() {
  367. return pullRequestCount;
  368. }
  369. public String getQualityGate() {
  370. return qualityGate;
  371. }
  372. public String getScm() {
  373. return scm;
  374. }
  375. public String getCi() {
  376. return ci;
  377. }
  378. public String getDevopsPlatform() {
  379. return devopsPlatform;
  380. }
  381. public Optional<Long> getBugs() {
  382. return Optional.ofNullable(bugs);
  383. }
  384. public Optional<Long> getVulnerabilities() {
  385. return Optional.ofNullable(vulnerabilities);
  386. }
  387. public Optional<Long> getSecurityHotspots() {
  388. return Optional.ofNullable(securityHotspots);
  389. }
  390. public Optional<Long> getTechnicalDebt() {
  391. return Optional.ofNullable(technicalDebt);
  392. }
  393. public Optional<Long> getDevelopmentCost() {
  394. return Optional.ofNullable(developmentCost);
  395. }
  396. public Optional<Long> getExternalSecurityReportExportedAt() {
  397. return Optional.ofNullable(externalSecurityReportExportedAt);
  398. }
  399. public CreationMethod getCreationMethod() {
  400. return creationMethod;
  401. }
  402. public Boolean isMonorepo() {
  403. return monorepo;
  404. }
  405. static class Builder {
  406. private String projectUuid;
  407. private Long branchCount;
  408. private Long pullRequestCount;
  409. private String qualityGate;
  410. private String scm;
  411. private String ci;
  412. private String devopsPlatform;
  413. private Long bugs;
  414. private Long vulnerabilities;
  415. private Long securityHotspots;
  416. private Long technicalDebt;
  417. private Long developmentCost;
  418. private int ncdId;
  419. private Long externalSecurityReportExportedAt;
  420. private CreationMethod creationMethod;
  421. private Boolean monorepo;
  422. public Builder setProjectUuid(String projectUuid) {
  423. this.projectUuid = projectUuid;
  424. return this;
  425. }
  426. public Builder setNcdId(int ncdId) {
  427. this.ncdId = ncdId;
  428. return this;
  429. }
  430. public Builder setBranchCount(Long branchCount) {
  431. this.branchCount = branchCount;
  432. return this;
  433. }
  434. public Builder setPRCount(Long pullRequestCount) {
  435. this.pullRequestCount = pullRequestCount;
  436. return this;
  437. }
  438. public Builder setQG(String qualityGate) {
  439. this.qualityGate = qualityGate;
  440. return this;
  441. }
  442. public Builder setScm(String scm) {
  443. this.scm = scm;
  444. return this;
  445. }
  446. public Builder setCi(String ci) {
  447. this.ci = ci;
  448. return this;
  449. }
  450. public Builder setDevops(String devopsPlatform) {
  451. this.devopsPlatform = devopsPlatform;
  452. return this;
  453. }
  454. public Builder setBugs(@Nullable Number bugs) {
  455. this.bugs = bugs != null ? bugs.longValue() : null;
  456. return this;
  457. }
  458. public Builder setVulnerabilities(@Nullable Number vulnerabilities) {
  459. this.vulnerabilities = vulnerabilities != null ? vulnerabilities.longValue() : null;
  460. return this;
  461. }
  462. public Builder setSecurityHotspots(@Nullable Number securityHotspots) {
  463. this.securityHotspots = securityHotspots != null ? securityHotspots.longValue() : null;
  464. return this;
  465. }
  466. public Builder setTechnicalDebt(@Nullable Number technicalDebt) {
  467. this.technicalDebt = technicalDebt != null ? technicalDebt.longValue() : null;
  468. return this;
  469. }
  470. public Builder setDevelopmentCost(@Nullable Number developmentCost) {
  471. this.developmentCost = developmentCost != null ? developmentCost.longValue() : null;
  472. return this;
  473. }
  474. public Builder setExternalSecurityReportExportedAt(@Nullable Number externalSecurityReportExportedAt) {
  475. this.externalSecurityReportExportedAt = externalSecurityReportExportedAt != null ? externalSecurityReportExportedAt.longValue() : null;
  476. return this;
  477. }
  478. public Builder setCreationMethod(CreationMethod creationMethod) {
  479. this.creationMethod = creationMethod;
  480. return this;
  481. }
  482. public Builder setMonorepo(Boolean monorepo) {
  483. this.monorepo = monorepo;
  484. return this;
  485. }
  486. public ProjectStatistics build() {
  487. return new ProjectStatistics(this);
  488. }
  489. }
  490. }
  491. }