Skip to content

Commit

Permalink
Compute totals by module and by ownership. (#33)
Browse files Browse the repository at this point in the history
* WIP - Over Time

* Another OWNER entry for sample data.

* Working implementation.
  • Loading branch information
handstandsam authored Dec 1, 2024
1 parent 8200df2 commit 2fa5289
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
examples/jvm-with-anvil/ Anvil
examples/scopes/ @handstandsam
examples/scopes/ @handstandsam
examples/app/ AppTeam
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.squareup.invert.internal.models.CollectedOwnershipForProject
import com.squareup.invert.internal.models.CollectedPluginsForProject
import com.squareup.invert.internal.models.CollectedStatsForProject
import com.squareup.invert.internal.report.js.InvertJsReportUtils
import com.squareup.invert.internal.report.js.InvertJsReportUtils.computeGlobalStats
import com.squareup.invert.internal.report.js.InvertJsReportUtils.computeGlobalTotals
import com.squareup.invert.internal.report.js.InvertJsReportWriter
import com.squareup.invert.internal.report.json.InvertJsonReportWriter
import com.squareup.invert.logging.InvertLogger
Expand Down Expand Up @@ -40,7 +40,7 @@ class InvertReportWriter(
invertedModulesList = invertedDependenciesJsReportModel.getAllModulePaths()
)

val globalStats = computeGlobalStats(allProjectsStatsData)
val globalStats = computeGlobalTotals(allProjectsStatsData, collectedOwnershipInfo)

// JSON Report
InvertJsonReportWriter(invertLogger, rootBuildReportsDir).createInvertJsonReport(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.squareup.invert.models.ConfigurationName
import com.squareup.invert.models.DependencyId
import com.squareup.invert.models.GradlePluginId
import com.squareup.invert.models.ModulePath
import com.squareup.invert.models.OwnerName
import com.squareup.invert.models.Stat
import com.squareup.invert.models.StatDataType
import com.squareup.invert.models.StatKey
Expand Down Expand Up @@ -42,8 +43,28 @@ object InvertJsReportUtils {
)
}

fun computeGlobalStats(allProjectsStatsData: StatsJsReportModel): Map<StatKey, StatTotalAndMetadata> {
val globalStats: Map<StatMetadata, Int> = allProjectsStatsData.statInfos.values
fun countFromStat(stat: Stat?): Int {
return when (stat) {
is Stat.NumericStat -> stat.value
is Stat.CodeReferencesStat -> stat.value.size
is Stat.BooleanStat -> if (stat.value) {
1
} else {
0
}

else -> {
0 // Default Value
}
}
}

fun computeGlobalTotals(
allProjectsStatsData: StatsJsReportModel,
collectedOwnershipInfo: OwnershipJsReportModel
): Map<StatKey, StatTotalAndMetadata> {
val moduleToOwnerMap = collectedOwnershipInfo.modules
val allStatMetadatas: List<StatMetadata> = allProjectsStatsData.statInfos.values
.filter { statInfo ->
when (statInfo.dataType) {
StatDataType.BOOLEAN,
Expand All @@ -55,26 +76,51 @@ object InvertJsReportUtils {
}
}
}
.associateWith { statMetadata: StatMetadata ->
val statKey = statMetadata.key
allProjectsStatsData.statsByModule.values.sumOf { statsForModule: Map<StatKey, Stat> ->
val stat: Stat? = statsForModule[statKey]
when (stat) {
is Stat.NumericStat -> stat.value
is Stat.CodeReferencesStat -> stat.value.size
is Stat.BooleanStat -> if (stat.value) {
1
} else {
0
}

else -> {
0 // Default Value
}

val globalTotals: Map<StatKey, StatTotalAndMetadata> = allStatMetadatas.associate { statMetadata: StatMetadata ->
var totalCount = 0 // Total count of the stat across all modules
val totalByModule: Map<ModulePath, Int> = allProjectsStatsData.statsByModule.entries
.mapNotNull { (modulePath: ModulePath, statsForModule: Map<StatKey, Stat>) ->
val stat: Stat? = statsForModule[statMetadata.key]
if (stat != null) {
val countForStat = countFromStat(stat)
totalCount += countForStat
modulePath to countForStat
} else {
null
}
}
}.toMap()
return globalStats.entries.associate { it.key.key to StatTotalAndMetadata(it.key, it.value) }
.filter { it.second != 0 } // Drop all the `0` entries to avoid clutter in the JSON files
.toMap()

val ownerToTotalCount: Map<OwnerName, Int> = totalByModule.entries
.groupBy { (modulePath, totalForModule) ->
val ownerName = moduleToOwnerMap[modulePath]

ownerName
}
.map { (modulePath, entry) ->
modulePath to entry.sumOf { it.value }
}
.mapNotNull { (ownerName, totalCountForOwner) ->
if (ownerName == null) {
null
} else {
ownerName to totalCountForOwner
}
}
.toMap()

statMetadata.key to StatTotalAndMetadata(
metadata = statMetadata,
total = totalCount,
totalByModule = totalByModule,
totalByOwner = ownerToTotalCount
)
}.toMap()

return globalTotals
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.squareup.invert.models.js

import com.squareup.invert.models.ModulePath
import com.squareup.invert.models.OwnerName
import com.squareup.invert.models.StatKey
import com.squareup.invert.models.StatMetadata
import kotlinx.serialization.Serializable
Expand All @@ -13,4 +15,6 @@ data class CollectedStatTotalsJsReportModel(
data class StatTotalAndMetadata(
val metadata: StatMetadata,
val total: Int,
val totalByModule: Map<ModulePath, Int>,
val totalByOwner: Map<OwnerName, Int>,
)

0 comments on commit 2fa5289

Please sign in to comment.