|
| 1 | +package io.agodadev.localmetrics.gradle |
| 2 | + |
| 3 | +import java.lang.management.ManagementFactory |
| 4 | +import java.net.InetAddress |
| 5 | +import java.time.Instant |
| 6 | +import java.util.UUID |
| 7 | + |
| 8 | +internal data class BuildMetricsPayload( |
| 9 | + val id: String, |
| 10 | + val metricsVersion: String, |
| 11 | + val userName: String, |
| 12 | + val cpuCount: Int, |
| 13 | + val hostname: String, |
| 14 | + val platform: String, |
| 15 | + val os: String, |
| 16 | + val timeTaken: String, |
| 17 | + val branch: String?, |
| 18 | + val commitSha: String?, |
| 19 | + val type: String, |
| 20 | + val projectName: String, |
| 21 | + val repository: String?, |
| 22 | + val repositoryName: String?, |
| 23 | + val date: String, |
| 24 | + val isDebuggerAttached: Boolean, |
| 25 | + val ide: String, |
| 26 | + val buildKind: String, |
| 27 | + val requestedTasks: List<String>, |
| 28 | + val taskCount: Int, |
| 29 | + val executedTaskCount: Int, |
| 30 | + val upToDateTaskCount: Int, |
| 31 | + val fromCacheTaskCount: Int, |
| 32 | + val failedTaskCount: Int, |
| 33 | + val compileTaskCount: Int, |
| 34 | + val compileTimeMs: Long, |
| 35 | + val taskTimeMs: Long, |
| 36 | + val projects: List<ProjectCompilationMetrics>, |
| 37 | +) |
| 38 | + |
| 39 | +internal data class ProjectCompilationMetrics( |
| 40 | + val projectPath: String, |
| 41 | + val compileTaskCount: Int, |
| 42 | + val compileTimeMs: Long, |
| 43 | +) |
| 44 | + |
| 45 | +internal object BuildMetricsPayloadFactory { |
| 46 | + fun create( |
| 47 | + metricsVersion: String, |
| 48 | + projectName: String, |
| 49 | + requestedTasks: List<String>, |
| 50 | + taskMetrics: List<TaskMetric>, |
| 51 | + gitContext: GitContext, |
| 52 | + ): BuildMetricsPayload { |
| 53 | + val compilationTasks = taskMetrics.filter(TaskMetric::isCompilation) |
| 54 | + val completedCompilationTasks = compilationTasks.filter { it.didWork() } |
| 55 | + val taskExecutionStart = taskMetrics.minOf(TaskMetric::startedAtMillis) |
| 56 | + val taskExecutionEnd = taskMetrics.maxOf(TaskMetric::finishedAtMillis) |
| 57 | + val upToDateTaskCount = taskMetrics.count { it.outcome == TaskOutcome.UP_TO_DATE } |
| 58 | + val fromCacheTaskCount = taskMetrics.count { it.outcome == TaskOutcome.FROM_CACHE } |
| 59 | + |
| 60 | + return BuildMetricsPayload( |
| 61 | + id = UUID.randomUUID().toString(), |
| 62 | + metricsVersion = metricsVersion, |
| 63 | + userName = System.getProperty("user.name").orEmpty(), |
| 64 | + cpuCount = Runtime.getRuntime().availableProcessors(), |
| 65 | + hostname = hostName(), |
| 66 | + platform = System.getProperty("os.name").orEmpty(), |
| 67 | + os = listOfNotNull( |
| 68 | + System.getProperty("os.name"), |
| 69 | + System.getProperty("os.version"), |
| 70 | + System.getProperty("os.arch"), |
| 71 | + ).joinToString(" "), |
| 72 | + timeTaken = (taskExecutionEnd - taskExecutionStart).coerceAtLeast(0).toString(), |
| 73 | + branch = gitContext.branch, |
| 74 | + commitSha = gitContext.commitSha, |
| 75 | + type = "Gradle", |
| 76 | + projectName = gitContext.repositoryName ?: projectName, |
| 77 | + repository = gitContext.repository, |
| 78 | + repositoryName = gitContext.repositoryName, |
| 79 | + date = Instant.now().toString(), |
| 80 | + isDebuggerAttached = ManagementFactory.getRuntimeMXBean().inputArguments |
| 81 | + .any { it.contains("jdwp", ignoreCase = true) }, |
| 82 | + ide = if (System.getProperty("idea.active").toBoolean()) "IntelliJ IDEA" else "CLI", |
| 83 | + buildKind = if (upToDateTaskCount + fromCacheTaskCount > 0) "incremental" else "clean", |
| 84 | + requestedTasks = requestedTasks, |
| 85 | + taskCount = taskMetrics.size, |
| 86 | + executedTaskCount = taskMetrics.count { it.outcome == TaskOutcome.EXECUTED }, |
| 87 | + upToDateTaskCount = upToDateTaskCount, |
| 88 | + fromCacheTaskCount = fromCacheTaskCount, |
| 89 | + failedTaskCount = taskMetrics.count { it.outcome == TaskOutcome.FAILED }, |
| 90 | + compileTaskCount = compilationTasks.size, |
| 91 | + compileTimeMs = completedCompilationTasks.sumOf(TaskMetric::durationMillis), |
| 92 | + taskTimeMs = taskMetrics.sumOf(TaskMetric::durationMillis), |
| 93 | + projects = completedCompilationTasks |
| 94 | + .groupBy(TaskMetric::projectPath) |
| 95 | + .map { (path, tasks) -> |
| 96 | + ProjectCompilationMetrics( |
| 97 | + projectPath = path, |
| 98 | + compileTaskCount = tasks.size, |
| 99 | + compileTimeMs = tasks.sumOf(TaskMetric::durationMillis), |
| 100 | + ) |
| 101 | + } |
| 102 | + .sortedBy(ProjectCompilationMetrics::projectPath), |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + private fun TaskMetric.didWork(): Boolean = |
| 107 | + outcome == TaskOutcome.EXECUTED || outcome == TaskOutcome.FAILED |
| 108 | + |
| 109 | + private fun hostName(): String = |
| 110 | + System.getenv("HOSTNAME") |
| 111 | + ?: System.getenv("COMPUTERNAME") |
| 112 | + ?: runCatching { InetAddress.getLocalHost().hostName }.getOrDefault("") |
| 113 | +} |
0 commit comments