Skip to content

Commit

Permalink
Add JSON output type for build reports
Browse files Browse the repository at this point in the history
#KT-65792 Fixed
  • Loading branch information
nav-nav authored and qodana-bot committed Mar 6, 2024
1 parent 666a2f2 commit 2f19d2e
Show file tree
Hide file tree
Showing 20 changed files with 772 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ package org.jetbrains.kotlin.build.report.metrics
import java.io.Serializable
import java.util.*

class BuildAttributes : Serializable {
private val myAttributes =
EnumMap<BuildAttribute, Int>(
BuildAttribute::class.java
)
data class BuildAttributes(
private val myAttributes: MutableMap<BuildAttribute, Int> = EnumMap(BuildAttribute::class.java)
) : Serializable {

fun add(attr: BuildAttribute, count: Int = 1) {
myAttributes[attr] = myAttributes.getOrDefault(attr, 0) + count
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

package org.jetbrains.kotlin.build.report.statistics

import org.jetbrains.kotlin.buildtools.api.KotlinLogger
import java.io.Serializable

interface BuildReportService<T> : Serializable {
fun process(data: T, log: KotlinLogger)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.text.SimpleDateFormat
import java.util.*

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

interface CompileStatisticsData<B : BuildTime, P : BuildPerformanceMetric> {
fun getVersion(): Int = 4
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

package org.jetbrains.kotlin.build.report.statistics

import org.jetbrains.kotlin.buildtools.api.KotlinLogger
import java.io.File
import java.text.SimpleDateFormat
import java.util.*

abstract class FileReportService<T>(
buildReportDir: File,
projectName: String,
fileSuffix: String,
) : BuildReportService<T> {
companion object {
internal val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").also { it.timeZone = TimeZone.getTimeZone("UTC") }
}

private val ts = SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(Calendar.getInstance().time)
private val outputFile = buildReportDir.resolve("$projectName-build-$ts.$fileSuffix")

abstract fun printBuildReport(data: T, outputFile: File)

override fun process(data: T, log: KotlinLogger) {
val buildReportPath = outputFile.toPath().toUri().toString()
try {
outputFile.parentFile.mkdirs()
if (!(outputFile.parentFile.exists() && outputFile.parentFile.isDirectory)) {
log.error("Kotlin build report cannot be created: '${outputFile.parentFile}' is a file or do not have permissions to create")
return
}
printBuildReport(data, outputFile)

log.lifecycle("Kotlin build report is written to $buildReportPath")
} catch (e: Exception) {
log.error("Could not write Kotlin build report to $buildReportPath", e)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

package org.jetbrains.kotlin.build.report.statistics

import com.google.gson.Gson
import java.io.File

class JsonReportService(
buildReportDir: File,
projectName: String,
) : FileReportService<Any>(buildReportDir, projectName, "json") {

/**
* Prints general build information and task/transform build metrics
*/
override fun printBuildReport(data: Any, outputFile: File) {
outputFile.bufferedWriter().use {
it.write(Gson().toJson(data))
}
}
}
Loading

0 comments on commit 2f19d2e

Please sign in to comment.