123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- /*
- * SonarQube
- * Copyright (C) 2009-2022 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.server.telemetry;
-
- import java.time.Instant;
- import java.time.ZoneOffset;
- import java.time.format.DateTimeFormatter;
- import java.util.Locale;
- import org.apache.commons.codec.digest.DigestUtils;
- import org.jetbrains.annotations.NotNull;
- import org.sonar.api.utils.text.JsonWriter;
-
- import static org.sonar.api.utils.DateUtils.DATETIME_FORMAT;
-
- public class TelemetryDataJsonWriter {
-
- public static final String LANGUAGE_PROP = "language";
-
- public void writeTelemetryData(JsonWriter json, TelemetryData statistics) {
- json.beginObject();
- json.prop("id", statistics.getServerId());
- json.prop("version", statistics.getVersion());
- statistics.getEdition().ifPresent(e -> json.prop("edition", e.name().toLowerCase(Locale.ENGLISH)));
- statistics.getLicenseType().ifPresent(e -> json.prop("licenseType", e));
- json.name("database");
- json.beginObject();
- json.prop("name", statistics.getDatabase().getName());
- json.prop("version", statistics.getDatabase().getVersion());
- json.endObject();
- json.name("plugins");
- json.beginArray();
- statistics.getPlugins().forEach((plugin, version) -> {
- json.beginObject();
- json.prop("name", plugin);
- json.prop("version", version);
- json.endObject();
- });
- json.endArray();
-
- if (!statistics.getCustomSecurityConfigs().isEmpty()) {
- json.name("customSecurityConfig");
- json.beginArray();
- json.values(statistics.getCustomSecurityConfigs());
- json.endArray();
- }
-
- statistics.hasUnanalyzedC().ifPresent(hasUnanalyzedC -> json.prop("hasUnanalyzedC", hasUnanalyzedC));
- statistics.hasUnanalyzedCpp().ifPresent(hasUnanalyzedCpp -> json.prop("hasUnanalyzedCpp", hasUnanalyzedCpp));
-
- if (statistics.getInstallationDate() != null) {
- json.prop("installationDate", toUtc(statistics.getInstallationDate()));
- }
- if (statistics.getInstallationVersion() != null) {
- json.prop("installationVersion", statistics.getInstallationVersion());
- }
- json.prop("docker", statistics.isInDocker());
-
- writeUserData(json, statistics);
- writeProjectData(json, statistics);
- writeProjectStatsData(json, statistics);
-
- json.endObject();
- }
-
- private static void writeUserData(JsonWriter json, TelemetryData statistics) {
- if (statistics.getUserTelemetries() != null) {
- json.name("users");
- json.beginArray();
- statistics.getUserTelemetries().forEach(user -> {
- json.beginObject();
- json.prop("userUuid", DigestUtils.sha3_224Hex(user.getUuid()));
- json.prop("status", user.isActive() ? "active" : "inactive");
- json.prop("identityProvider", user.getExternalIdentityProvider());
-
- if (user.getLastConnectionDate() != null) {
- json.prop("lastActivity", toUtc(user.getLastConnectionDate()));
- }
- if (user.getLastSonarlintConnectionDate() != null) {
- json.prop("lastSonarlintActivity", toUtc(user.getLastSonarlintConnectionDate()));
- }
-
- json.endObject();
- });
- json.endArray();
- }
- }
-
- private static void writeProjectData(JsonWriter json, TelemetryData statistics) {
- if (statistics.getProjects() != null) {
- json.name("projects");
- json.beginArray();
- statistics.getProjects().forEach(project -> {
- json.beginObject();
- json.prop("projectUuid", project.getProjectUuid());
- if (project.getLastAnalysis() != null) {
- json.prop("lastAnalysis", toUtc(project.getLastAnalysis()));
- }
- json.prop(LANGUAGE_PROP, project.getLanguage());
- json.prop("loc", project.getLoc());
- json.endObject();
- });
- json.endArray();
- }
- }
-
- private static void writeProjectStatsData(JsonWriter json, TelemetryData statistics) {
- if (statistics.getProjectStatistics() != null) {
- json.name("projects-general-stats");
- json.beginArray();
- statistics.getProjectStatistics().forEach(project -> {
- json.beginObject();
- json.prop("projectUuid", project.getProjectUuid());
- json.prop("branchCount", project.getBranchCount());
- json.prop("pullRequestCount", project.getPullRequestCount());
- json.prop("scm", project.getScm());
- json.prop("ci", project.getCi());
- json.prop("devopsPlatform", project.getDevopsPlatform());
- json.endObject();
- });
- json.endArray();
- }
- }
-
- @NotNull
- private static String toUtc(long date) {
- return DateTimeFormatter.ofPattern(DATETIME_FORMAT)
- .withZone(ZoneOffset.UTC)
- .format(Instant.ofEpochMilli(date));
- }
-
- }
|