11package com.ebay.plugins.metrics.develocity
22
3+ import com.ebay.plugins.metrics.develocity.MetricsForDevelocityConstants.EXTENSION_NAME
4+ import com.ebay.plugins.metrics.develocity.MetricsForDevelocityConstants.QUERY_FILTER_PROPERTY
5+ import com.ebay.plugins.metrics.develocity.MetricsForDevelocityConstants.SUMMARIZER_ALL
6+ import com.ebay.plugins.metrics.develocity.MetricsForDevelocityConstants.SUMMARIZER_ATTRIBUTE
7+ import com.ebay.plugins.metrics.develocity.NameUtil.DATETIME_TASK_PATTERN
8+ import com.ebay.plugins.metrics.develocity.NameUtil.DURATION_TASK_PATTERN
39import com.ebay.plugins.metrics.develocity.projectcost.ProjectCostPlugin
410import com.ebay.plugins.metrics.develocity.service.DevelocityBuildService
511import com.ebay.plugins.metrics.develocity.userquery.UserQueryPlugin
@@ -20,7 +26,7 @@ import javax.inject.Inject
2026 * Plugin implementation which defines tasks and configurations artifacts which are used to
2127 * generate aggregate metric data based upon Develocity build scans.
2228 */
23- @Suppress(" unused" ) // False positive
29+ @Suppress(" unused" , " UnstableApiUsage " ) // False positive
2430internal class MetricsForDevelocityPlugin @Inject constructor(
2531 private val providerFactory : ProviderFactory
2632) : Plugin<Any> {
@@ -70,6 +76,8 @@ internal class MetricsForDevelocityPlugin @Inject constructor(
7076 }
7177
7278 private fun applyProject (project : Project ) {
79+ project.dependencies.attributesSchema.attribute(SUMMARIZER_ATTRIBUTE )
80+
7381 if (project.parent == null ) {
7482 applyRootProject(project)
7583 }
@@ -135,11 +143,68 @@ internal class MetricsForDevelocityPlugin @Inject constructor(
135143 ext,
136144 )
137145
146+ /*
147+ * Creates a rule for generating consumable configurations for date time requests.
148+ * These can be scoped to daily or hourly time specifications.
149+ */
150+ project.configurations.addRule(
151+ " Pattern: ${NameUtil .dateTime(" <YYYY>-<MM>-<DD>[T<HH>]" )} " +
152+ " Gathers Develocity metrics for the date (and optionally hour) specified."
153+ ) { configurationName ->
154+ val matcher = DATETIME_TASK_PATTERN .matcher(configurationName)
155+ if (! matcher.matches()) return @addRule
156+
157+ val date = matcher.group(2 )
158+ val timeSpec: String = matcher.group(1 )
159+ val hour: String? = matcher.group(3 )
160+
161+ project.configurations.register(configurationName) { config ->
162+ with (config) {
163+ isCanBeConsumed = true
164+ isCanBeResolved = false
165+ isTransitive = false
166+ attributes.attribute(SUMMARIZER_ATTRIBUTE , SUMMARIZER_ALL )
167+ }
168+ }
169+
170+ val artifactTaskProvider = if (hour == null ) {
171+ pluginContext.registerDaily(date)
172+ } else {
173+ pluginContext.registerHourly(timeSpec)
174+ }
175+ project.artifacts.add(configurationName, artifactTaskProvider)
176+ }
177+
178+ /*
179+ * Creates a rule for generating consumable configurations for duration-based requests.
180+ * These can be scoped to specific time duration specifications.
181+ */
182+ project.configurations.addRule(
183+ " Pattern: ${NameUtil .duration(" <Java Duration String>" )} " +
184+ " Aggregates Develocity metrics for the current date back in time for the " +
185+ " specified duration."
186+ ) { configurationName ->
187+ val matcher = DURATION_TASK_PATTERN .matcher(configurationName)
188+ if (! matcher.matches()) return @addRule
189+
190+ val durationStr: String = matcher.group(1 )
191+ val consumableConfig = project.configurations.register(configurationName)
192+ consumableConfig.configure { config ->
193+ with (config) {
194+ isCanBeConsumed = true
195+ isCanBeResolved = false
196+ isTransitive = false
197+ attributes.attribute(SUMMARIZER_ATTRIBUTE , SUMMARIZER_ALL )
198+ }
199+ }
200+ project.artifacts.add(configurationName, pluginContext.registerDurationAggregation(durationStr))
201+ }
202+
138203 /*
139204 * Rule to register a task for a specific date and optionally hour.
140205 */
141206 project.tasks.addRule(
142- " Pattern: $TASK_PREFIX - <YYYY>-<MM>-<DD>T[ <HH>] " +
207+ " Pattern: ${ NameUtil .dateTime( " <YYYY>-<MM>-<DD>[T <HH>]" )} " +
143208 " Gathers Develocity metrics for the date (and optionally hour) specified."
144209 ) { taskName ->
145210 val matcher = DATETIME_TASK_PATTERN .matcher(taskName)
@@ -162,7 +227,7 @@ internal class MetricsForDevelocityPlugin @Inject constructor(
162227 * Rule to register a task for a time window / duration, relative to the current day and hour.
163228 */
164229 project.tasks.addRule(
165- " Pattern: $TASK_PREFIX -last- <Java Duration String> " +
230+ " Pattern: ${ NameUtil .duration( " <Java Duration String>" )} " +
166231 " Aggregates Develocity metrics for the current date back in time for the " +
167232 " specified duration."
168233 ) { taskName ->
@@ -172,22 +237,14 @@ internal class MetricsForDevelocityPlugin @Inject constructor(
172237 val durationStr: String = matcher.group(1 )
173238 pluginContext.registerDurationAggregation(durationStr)
174239 }
175-
176- ext.extensions.create(
177- INTERNAL_EXTENSION_NAME ,
178- MetricsForDevelocityInternalExtension ::class .java,
179- { timeSpec: String -> pluginContext.registerHourly(timeSpec) },
180- { dateStr: String -> pluginContext.registerDaily(dateStr) },
181- { durationStr: String -> pluginContext.registerDurationAggregation(durationStr) },
182- )
183240 }
184241
185242 /*
186243 * Function to register an hourly gather task. This function must be re-entrant and allow for
187244 * the same task to be registered multiple times without error.
188245 */
189246 private fun PluginContext.registerHourly (timeSpec : String ): TaskProvider <GatherHourlyTask > {
190- val taskName = " $TASK_PREFIX - $ timeSpec"
247+ val taskName = NameUtil .dateTime( timeSpec)
191248 return if (project.tasks.names.contains(taskName)) {
192249 // task already exists. Return its TaskProvider.
193250 project.tasks.named(taskName, GatherHourlyTask ::class .java)
@@ -227,7 +284,7 @@ internal class MetricsForDevelocityPlugin @Inject constructor(
227284 * the same task to be registered multiple times without error.
228285 */
229286 private fun PluginContext.registerDaily (dateString : String ): TaskProvider <GatherAggregateTask > {
230- val taskName = " $TASK_PREFIX - $ dateString"
287+ val taskName = NameUtil .dateTime( dateString)
231288 return if (project.tasks.names.contains(taskName)) {
232289 // task already exists. Return its TaskProvider.
233290 project.tasks.named(taskName, GatherAggregateTask ::class .java)
@@ -281,7 +338,7 @@ internal class MetricsForDevelocityPlugin @Inject constructor(
281338 }
282339 }.toList()
283340
284- val taskName = " $TASK_PREFIX -last- $ durationStr"
341+ val taskName = NameUtil .duration( durationStr)
285342 return if (project.tasks.names.contains(taskName)) {
286343 // task already exists. Return its TaskProvider.
287344 project.tasks.named(taskName, GatherAggregateTask ::class .java)
@@ -357,25 +414,6 @@ internal class MetricsForDevelocityPlugin @Inject constructor(
357414 }
358415
359416 companion object {
360- const val EXTENSION_NAME = " metricsForDevelocity"
361- const val INTERNAL_EXTENSION_NAME = " metricsForDevelocityInternal"
362- const val TASK_PREFIX = " metricsForDevelocity"
363- const val TASK_GROUP = " develocity metrics"
364- const val QUERY_FILTER_PROPERTY = " metricsForDevelocityQueryFilter"
365-
366- private val DATETIME_TASK_PATTERN = Regex (
367- // Examples:
368- // metricsForDevelocity-2024-06-01
369- // metricsForDevelocity-2024-06-01T05
370- " ^\\ Q$TASK_PREFIX -\\ E((\\ d{4}-\\ d{2}-\\ d{2})(?:T(\\ d{2}))?)$"
371- ).toPattern()
372-
373- private val DURATION_TASK_PATTERN = Regex (
374- // Examples:
375- // metricsForDevelocity-last-P7D
376- // metricsForDevelocity-last-PT8H
377- // metricsForDevelocity-last-P2DT8H
378- " ^\\ Q$TASK_PREFIX -last-\\ E(\\ w+)$"
379- ).toPattern()
417+ internal const val TASK_GROUP = " develocity metrics"
380418 }
381419}
0 commit comments