All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.tinkoff.plugins.buildmetrics.gradle.metrics.internal.BuildTasksMetricsFactory.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC2
Show newest version
package ru.tinkoff.plugins.buildmetrics.gradle.metrics.internal

import org.gradle.api.internal.tasks.RealizeTaskBuildOperationType
import org.gradle.api.internal.tasks.RegisterTaskBuildOperationType
import ru.tinkoff.plugins.buildmetrics.api.builds.internal.BuildOperationData
import ru.tinkoff.plugins.buildmetrics.api.builds.internal.BuildOperationDataListener
import ru.tinkoff.plugins.buildmetrics.api.factories.Factory
import ru.tinkoff.plugins.buildmetrics.api.metrics.Metric
import ru.tinkoff.plugins.buildmetrics.gradle.internal.utils.ConfigurationCacheData
import ru.tinkoff.plugins.buildmetrics.gradle.internal.utils.TasksData

/**
 * Metrics:
 * - gradle_total_tasks_count;
 * - gradle_run_tasks_count;
 * - gradle_executed_tasks_count;
 * - gradle_from_cache_tasks_count;
 * - gradle_up_to_date_tasks_count;
 * - gradle_skipped_tasks_count;
 * - gradle_no_source_tasks_count;
 * - gradle_lifecycle_tasks_count;
 * - gradle_requested_from_cache_tasks_count;
 * - gradle_created_immediately_tasks_count;
 */
class BuildTasksMetricsFactory : Factory.Metrics, BuildOperationDataListener {

    private class FactoryData(
        private val tasksDataBuilder: TasksData.Builder = TasksData.Builder(),
        private val configurationCacheDataBuilder: ConfigurationCacheData.Builder = ConfigurationCacheData.Builder(),
    ) {

        val registerTasks: MutableSet = mutableSetOf()

        val realizeTasks: MutableSet = mutableSetOf()

        fun onBuildOperationData(data: BuildOperationData) {
            when (val details = data.details) {
                is RegisterTaskBuildOperationType.Details -> {
                    registerTasks.add(details.taskId)
                }

                is RealizeTaskBuildOperationType.Details -> {
                    realizeTasks.add(details.taskId)
                }
            }
            tasksDataBuilder.onBuildOperationData(data = data)
            configurationCacheDataBuilder.onBuildOperationData(data = data)
        }

        fun totalTasksCount(): Int = registerTasks.plus(realizeTasks).size

        fun createTasksCountWithoutRegister(): Int = realizeTasks.minus(registerTasks).size

        fun tasksData(): TasksData = tasksDataBuilder.build()

        fun configurationCacheData(): ConfigurationCacheData = configurationCacheDataBuilder.build()
    }

    @Transient
    private var factoryData: FactoryData = FactoryData()

    override fun reinitialize() {
        factoryData = FactoryData()
    }

    override fun onBuildOperationData(data: BuildOperationData) {
        factoryData.onBuildOperationData(data = data)
    }

    @Synchronized
    override fun create(): List> {
        val tasksData = factoryData.tasksData()
        val configurationCacheData = factoryData.configurationCacheData()
        return listOf(
            Metric(name = "gradle_total_tasks_count", value = factoryData.totalTasksCount()),
            Metric(name = "gradle_run_tasks_count", value = tasksData.runTasksCount),
            Metric(name = "gradle_executed_tasks_count", value = tasksData.executedTasksCount),
            Metric(name = "gradle_from_cache_tasks_count", value = tasksData.fromCacheTasksCount),
            Metric(name = "gradle_up_to_date_tasks_count", value = tasksData.upToDateTasksCount),
            Metric(name = "gradle_skipped_tasks_count", value = tasksData.skippedTasksCount),
            Metric(name = "gradle_no_source_tasks_count", value = tasksData.noSourceTasksCount),
            Metric(name = "gradle_lifecycle_tasks_count", value = tasksData.lifecycleTasksCount),
            Metric(name = "gradle_requested_from_cache_tasks_count", value = tasksData.requestedFromCacheTasksCount),
            Metric(
                name = "gradle_created_immediately_tasks_count",
                value = if (configurationCacheData.reused) {
                    0
                } else {
                    factoryData.createTasksCountWithoutRegister()
                }
            ),
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy