-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-script.main.kts
73 lines (65 loc) · 2.39 KB
/
example-script.main.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env kotlinc -script
/*
* See what builds are most commonly invoked by developers, e.g. 'clean assemble',
* 'test' or 'check'. You can set up the URL and a token for your Gradle
* Enterprise instance and run this notebook as-is for your own project. This is a
* simple example of something you can do with the API. It could bring insights,
* for example:
*
* - "Our developers frequently clean together with assemble. We should ask them why,
* because they shouldn't have to. Just an old habit from Maven or are they working
* around a build issue we don't know about?"
*
* - "Some are doing check builds locally, which we set up to trigger our notably slow
* legacy tests. We should suggest they run test instead, leaving check for CI to run."
*
* Run this with at least 1GB of heap to accomodate the fetched data: JAVA_OPTS=-Xmx1g
*/
@file:DependsOn("com.gabrielfeo:develocity-api-kotlin:2024.2.0")
import com.gabrielfeo.develocity.api.*
import com.gabrielfeo.develocity.api.model.*
import com.gabrielfeo.develocity.api.extension.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import java.time.*
import java.util.LinkedList
// Parameters
val startDate = LocalDate.now().minusWeeks(1)
val buildFilter: (GradleAttributes) -> Boolean = { build ->
"LOCAL" in build.tags
}
// Fetch builds from the API
val api = DevelocityApi.newInstance()
val builds: List<GradleAttributes> = runBlocking {
api.buildsApi.getBuildsFlow(
fromInstant = 0,
query = """buildStartTime>-7d""",
models = listOf(BuildModelName.gradleAttributes),
).map {
it.models!!.gradleAttributes!!.model!!
}.toList(LinkedList())
}
// Process builds and count how many times each was invoked
check(builds.isNotEmpty()) { "No builds found. Adjust query and try again." }
val buildCounts = builds.groupBy { build ->
val tasks = build.requestedTasks.joinToString(" ").trim(':')
tasks.ifBlank { "IDE sync" }
}.mapValues { (_, builds) ->
builds.size
}.entries.sortedByDescending { (_, count) ->
count
}
// Print the top 5 as a pretty table
val table = buildCounts.take(5).joinToString("\n") { (tasks, count) ->
"${tasks.padEnd(100)} | $count"
}
println(
"""
|---------------------
|Most frequent builds:
|
|$table
""".trimMargin()
)
// Shutdown to end background threads and allow script to exit earlier (see README)
api.shutdown()