Skip to content

Instantly share code, notes, and snippets.

@jonnycaley
Last active June 18, 2022 16:33
Show Gist options
  • Save jonnycaley/cc6c06b934bb7cd00a6246f4f0a8942e to your computer and use it in GitHub Desktop.
Save jonnycaley/cc6c06b934bb7cd00a6246f4f0a8942e to your computer and use it in GitHub Desktop.
BuildReporterService - report data from other services
abstract class BuildReporterService : BuildService<BuildReporterService.Params>, BuildOperationListener, AutoCloseable {
interface Params : BuildServiceParameters {
fun getBuildDurationServiceProvider(): Property<Provider<BuildDurationService>>
fun getBuildTaskServiceProvider(): Property<Provider<BuildTaskService>>
}
override fun started(p0: BuildOperationDescriptor, p1: OperationStartEvent) {}
override fun progress(p0: OperationIdentifier, p1: OperationProgressEvent) {}
override fun finished(buildOperationDescriptor: BuildOperationDescriptor, operationFinishEvent: OperationFinishEvent) {}
override fun close() {
logBuildStats()
}
private fun logBuildStats() {
val buildDurationService = parameters.getBuildDurationServiceProvider().get().get()
val buildTaskService = parameters.getBuildTaskServiceProvider().get().get()
val buildReport = getBuildReport(buildDurationService, buildTaskService)
val logger = Logging.getLogger("console-logger")
logger.lifecycle(buildReport.toString())
}
private fun getBuildReport(
buildDurationService: BuildDurationService,
buildTaskService: BuildTaskService,
): BuildReport {
val taskExecutionStatistics = getTasksExecutionStatistics(buildTaskService)
val buildFailureMessage = if (buildTaskService.buildPhaseFailed) {
buildTaskService.buildPhaseFailureMessage ?: "Build failed"
} else if (buildDurationService.configurationPhaseFailed) {
"Configuration Phase Failed"
} else {
null
}
val buildFailed = (buildFailureMessage != null)
return BuildReport(
totalElapsedBuildTimeMs = buildDurationService.buildDuration,
configurationTimeMs = buildDurationService.configurationDuration,
taskExecutionStatistics = taskExecutionStatistics,
buildFailed = buildFailed,
buildFailureMessage = buildFailureMessage
)
}
private fun getTasksExecutionStatistics(buildTaskService: BuildTaskService): TaskExecutionStatistics {
return TaskExecutionStatistics(
buildTaskService.executedTasksCount,
buildTaskService.fromCacheTasksCount,
buildTaskService.upToDateTasksCount
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment