Skip to content

Commit 5885514

Browse files
nav-navSpace Team
authored and
Space Team
committed
Add JSON output type for build reports
#KT-65792 Fixed
1 parent f493df4 commit 5885514

File tree

21 files changed

+515
-330
lines changed

21 files changed

+515
-330
lines changed

compiler/build-tools/kotlin-build-statistics/src/org/jetbrains/kotlin/build/report/metrics/BuildAttributes.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ package org.jetbrains.kotlin.build.report.metrics
88
import java.io.Serializable
99
import java.util.*
1010

11-
class BuildAttributes : Serializable {
12-
private val myAttributes =
13-
EnumMap<BuildAttribute, Int>(
14-
BuildAttribute::class.java
15-
)
11+
data class BuildAttributes(
12+
private val myAttributes: MutableMap<BuildAttribute, Int> = EnumMap(BuildAttribute::class.java)
13+
) : Serializable {
1614

1715
fun add(attr: BuildAttribute, count: Int = 1) {
1816
myAttributes[attr] = myAttributes.getOrDefault(attr, 0) + count
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.build.report.statistics
7+
8+
import org.jetbrains.kotlin.buildtools.api.KotlinLogger
9+
import java.io.Serializable
10+
11+
interface BuildReportService<T> : Serializable {
12+
fun process(data: T, log: KotlinLogger)
13+
}

compiler/build-tools/kotlin-build-statistics/src/org/jetbrains/kotlin/build/report/statistics/CompileStatisticsData.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import java.text.SimpleDateFormat
1010
import java.util.*
1111

1212
//Sensitive data. This object is used directly for statistic via http
13-
private val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").also { it.timeZone = TimeZone.getTimeZone("UTC") }
13+
internal val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").also { it.timeZone = TimeZone.getTimeZone("UTC") }
1414

1515
interface CompileStatisticsData<B : BuildTime, P : BuildPerformanceMetric> {
1616
fun getVersion(): Int = 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.build.report.statistics
7+
8+
import org.jetbrains.kotlin.buildtools.api.KotlinLogger
9+
import java.io.File
10+
import java.text.SimpleDateFormat
11+
import java.util.*
12+
13+
abstract class FileReportService<T>(
14+
buildReportDir: File,
15+
projectName: String,
16+
fileSuffix: String,
17+
) : BuildReportService<T> {
18+
companion object {
19+
internal val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").also { it.timeZone = TimeZone.getTimeZone("UTC") }
20+
}
21+
22+
private val ts = SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(Calendar.getInstance().time)
23+
private val outputFile = buildReportDir.resolve("$projectName-build-$ts.$fileSuffix")
24+
25+
abstract fun printBuildReport(data: T, outputFile: File)
26+
27+
override fun process(data: T, log: KotlinLogger) {
28+
val buildReportPath = outputFile.toPath().toUri().toString()
29+
try {
30+
outputFile.parentFile.mkdirs()
31+
if (!(outputFile.parentFile.exists() && outputFile.parentFile.isDirectory)) {
32+
log.error("Kotlin build report cannot be created: '${outputFile.parentFile}' is a file or do not have permissions to create")
33+
return
34+
}
35+
printBuildReport(data, outputFile)
36+
37+
log.lifecycle("Kotlin build report is written to $buildReportPath")
38+
} catch (e: Exception) {
39+
log.error("Could not write Kotlin build report to $buildReportPath", e)
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.build.report.statistics
7+
8+
import com.google.gson.Gson
9+
import java.io.File
10+
11+
class JsonReportService(
12+
buildReportDir: File,
13+
projectName: String,
14+
) : FileReportService<Any>(buildReportDir, projectName, "json") {
15+
16+
/**
17+
* Prints general build information and task/transform build metrics
18+
*/
19+
override fun printBuildReport(data: Any, outputFile: File) {
20+
outputFile.bufferedWriter().use {
21+
it.write(Gson().toJson(data))
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)