Skip to content

Commit

Permalink
WIP - Over Time
Browse files Browse the repository at this point in the history
  • Loading branch information
handstandsam committed Nov 28, 2024
1 parent 8200df2 commit a814448
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,71 @@ object InvertJsReportUtils {
)
}

fun computeGlobalStats(allProjectsStatsData: StatsJsReportModel): Map<StatKey, StatTotalAndMetadata> {
val globalStats: Map<StatMetadata, Int> = allProjectsStatsData.statInfos.values
.filter { statInfo ->
when (statInfo.dataType) {
StatDataType.BOOLEAN,
StatDataType.NUMERIC,
StatDataType.CODE_REFERENCES -> true

else -> {
false
data class ModuleAndStat(
val modulePath: ModulePath,
val metadata: StatMetadata,
val stat: Stat,
)

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 computeGlobalStats(allProjectsStatsData: StatsJsReportModel): Map<StatKey, StatTotalAndMetadata> {
val statMetadataToCompute: List<StatMetadata> = allProjectsStatsData.statInfos.values
.filter { statInfo ->
when (statInfo.dataType) {
StatDataType.BOOLEAN,
StatDataType.NUMERIC,
StatDataType.CODE_REFERENCES -> true

else -> {
false
}
}
}

val toTotals: Map<StatKey, StatTotalAndMetadata> = statMetadataToCompute.associate { metadata ->

allProjectsStatsData.statsByModule.entries.map { (modulePath: ModulePath, statsMap: Map<StatKey, Stat>) ->
ModuleAndStat(
modulePath = modulePath,
metadata = metadata,
stat = statsMap[metadata.key] ?: return@map null
)
}

// allProjectsStatsData.statsByModule.mapVa

metadata.key to StatTotalAndMetadata(
metadata = metadata,
total = 0,
totalByModule = mapOf(),
totalByOwner = mapOf()
)
}
.associateWith { statMetadata: StatMetadata ->

val globalStats1 = statMetadataToCompute.associateWith { statMetadata: StatMetadata ->
val statKey = statMetadata.key
allProjectsStatsData.statsByModule.values.sumOf { statsForModule: Map<StatKey, Stat> ->
val stat: Stat? = statsForModule[statKey]
countFromStat(stat)
}

}.toMap()

val globalStats: Map<StatMetadata, Int> = statMetadataToCompute.associateWith { statMetadata: StatMetadata ->
val statKey = statMetadata.key
allProjectsStatsData.statsByModule.values.sumOf { statsForModule: Map<StatKey, Stat> ->
val stat: Stat? = statsForModule[statKey]
Expand All @@ -74,118 +125,125 @@ object InvertJsReportUtils {
}
}
}.toMap()
return globalStats.entries.associate { it.key.key to StatTotalAndMetadata(it.key, it.value) }
}
return globalStats.entries.associate { (statMetadata, total) ->
statMetadata.key to StatTotalAndMetadata(
statMetadata,
total,
mapOf(),
mapOf()
)
}
}

/**
* Takes all [CollectedStatsForProject] collected by Invert, and creates the JS Report Model.
*/
fun buildModuleToStatsMap(collectedStats: List<CollectedStatsForProject>): StatsJsReportModel {
val statInfos: Map<StatKey, StatMetadata> = mutableSetOf<StatMetadata>()
.also { statInfos ->
collectedStats.forEach { collectedStatsForProject ->
statInfos.addAll(collectedStatsForProject.statInfos.values)
/**
* Takes all [CollectedStatsForProject] collected by Invert, and creates the JS Report Model.
*/
fun buildModuleToStatsMap(collectedStats: List<CollectedStatsForProject>): StatsJsReportModel {
val statInfos: Map<StatKey, StatMetadata> = mutableSetOf<StatMetadata>()
.also { statInfos ->
collectedStats.forEach { collectedStatsForProject ->
statInfos.addAll(collectedStatsForProject.statInfos.values)
}
}
.associateBy { it.key }

val statData = mutableMapOf<ModulePath, Map<StatKey, Stat>>()
collectedStats.forEach { collectedStatForProject: CollectedStatsForProject ->
statData[collectedStatForProject.path] = collectedStatForProject.stats
}
.associateBy { it.key }

val statData = mutableMapOf<ModulePath, Map<StatKey, Stat>>()
collectedStats.forEach { collectedStatForProject: CollectedStatsForProject ->
statData[collectedStatForProject.path] = collectedStatForProject.stats
return StatsJsReportModel(
statInfos = statInfos,
statsByModule = statData,
)
}

return StatsJsReportModel(
statInfos = statInfos,
statsByModule = statData,
)
}

/**
* Takes all [CollectedPluginsForProject] collected by Invert, and creates the JS Report Model.
*/
fun toCollectedPlugins(allPlugins: List<CollectedPluginsForProject>): PluginsJsReportModel {
return PluginsJsReportModel(
plugins = mutableMapOf<GradlePluginId, MutableList<ModulePath>>()
.also { resultingDepIdToModuleUsageInfo ->
allPlugins.forEach { collectedDataFromModule ->
collectedDataFromModule.plugins.forEach { pluginId ->
val curr = resultingDepIdToModuleUsageInfo[pluginId] ?: mutableListOf()
curr.add(collectedDataFromModule.path)
resultingDepIdToModuleUsageInfo[pluginId] = curr
/**
* Takes all [CollectedPluginsForProject] collected by Invert, and creates the JS Report Model.
*/
fun toCollectedPlugins(allPlugins: List<CollectedPluginsForProject>): PluginsJsReportModel {
return PluginsJsReportModel(
plugins = mutableMapOf<GradlePluginId, MutableList<ModulePath>>()
.also { resultingDepIdToModuleUsageInfo ->
allPlugins.forEach { collectedDataFromModule ->
collectedDataFromModule.plugins.forEach { pluginId ->
val curr = resultingDepIdToModuleUsageInfo[pluginId] ?: mutableListOf()
curr.add(collectedDataFromModule.path)
resultingDepIdToModuleUsageInfo[pluginId] = curr
}
}
}
},
modules = mutableMapOf<ModulePath, List<GradlePluginId>>()
.also { map ->
allPlugins.forEach { collectedDataFromModule ->
collectedDataFromModule.plugins.onEach {
map[collectedDataFromModule.path] = collectedDataFromModule.plugins
},
modules = mutableMapOf<ModulePath, List<GradlePluginId>>()
.also { map ->
allPlugins.forEach { collectedDataFromModule ->
collectedDataFromModule.plugins.onEach {
map[collectedDataFromModule.path] = collectedDataFromModule.plugins
}
}
}
},
)
}
},
)
}

/**
* Takes all [CollectedDependenciesForProject] collected by Invert, and creates the JS Report Model.
*/
fun toInvertedDependenciesJsReportModel(
collectedDependenciesForProjects: List<CollectedDependenciesForProject>
): DependenciesJsReportModel {
return DependenciesJsReportModel(
mutableMapOf<DependencyId, MutableMap<ModulePath, MutableList<ConfigurationName>>>()
.also { resultingDepIdToModuleUsageInfo ->
collectedDependenciesForProjects.forEach { collectedDataFromModule ->
val collectedDataFromModuleGradlePath = collectedDataFromModule.path
collectedDataFromModule.dependencies.forEach { (dependencyId, usedInConfigurationNames) ->
val currDataForDepName: MutableMap<ModulePath, MutableList<ConfigurationName>> =
resultingDepIdToModuleUsageInfo[dependencyId] ?: mutableMapOf()
val currConfigsForPath: MutableList<ConfigurationName> =
currDataForDepName[collectedDataFromModuleGradlePath] ?: mutableListOf()
currConfigsForPath.addAll(usedInConfigurationNames)
currDataForDepName[collectedDataFromModuleGradlePath] = currConfigsForPath
resultingDepIdToModuleUsageInfo[dependencyId] = currDataForDepName
/**
* Takes all [CollectedDependenciesForProject] collected by Invert, and creates the JS Report Model.
*/
fun toInvertedDependenciesJsReportModel(
collectedDependenciesForProjects: List<CollectedDependenciesForProject>
): DependenciesJsReportModel {
return DependenciesJsReportModel(
mutableMapOf<DependencyId, MutableMap<ModulePath, MutableList<ConfigurationName>>>()
.also { resultingDepIdToModuleUsageInfo ->
collectedDependenciesForProjects.forEach { collectedDataFromModule ->
val collectedDataFromModuleGradlePath = collectedDataFromModule.path
collectedDataFromModule.dependencies.forEach { (dependencyId, usedInConfigurationNames) ->
val currDataForDepName: MutableMap<ModulePath, MutableList<ConfigurationName>> =
resultingDepIdToModuleUsageInfo[dependencyId] ?: mutableMapOf()
val currConfigsForPath: MutableList<ConfigurationName> =
currDataForDepName[collectedDataFromModuleGradlePath] ?: mutableListOf()
currConfigsForPath.addAll(usedInConfigurationNames)
currDataForDepName[collectedDataFromModuleGradlePath] = currConfigsForPath
resultingDepIdToModuleUsageInfo[dependencyId] = currDataForDepName
}
}
}
}
)
}
)
}

fun toCollectedConfigurations(
allProjectsConfigurationsData: List<CollectedConfigurationsForProject>
): ConfigurationsJsReportModel {
val allConfigurationNames = mutableSetOf<String>()
val moduleToAllConfigurationNames = mutableMapOf<ModulePath, Set<ConfigurationName>>()
val moduleToAnalyzedConfigurationNames = mutableMapOf<ModulePath, Set<ConfigurationName>>()
val analyzedConfigurationNameToModules = mutableMapOf<ConfigurationName, MutableSet<ModulePath>>()

fun toCollectedConfigurations(
allProjectsConfigurationsData: List<CollectedConfigurationsForProject>
): ConfigurationsJsReportModel {
val allConfigurationNames = mutableSetOf<String>()
val moduleToAllConfigurationNames = mutableMapOf<ModulePath, Set<ConfigurationName>>()
val moduleToAnalyzedConfigurationNames = mutableMapOf<ModulePath, Set<ConfigurationName>>()
val analyzedConfigurationNameToModules = mutableMapOf<ConfigurationName, MutableSet<ModulePath>>()

allProjectsConfigurationsData.forEach { projectConfigurationsData ->
allConfigurationNames.addAll(projectConfigurationsData.allConfigurationNames)
moduleToAllConfigurationNames[projectConfigurationsData.modulePath] =
projectConfigurationsData.allConfigurationNames
moduleToAnalyzedConfigurationNames[projectConfigurationsData.modulePath] =
projectConfigurationsData.analyzedConfigurationNames
projectConfigurationsData.analyzedConfigurationNames.forEach { analyzedConfigurationName ->
val modules =
analyzedConfigurationNameToModules[analyzedConfigurationName] ?: mutableSetOf()
modules.add(projectConfigurationsData.modulePath)
analyzedConfigurationNameToModules[analyzedConfigurationName] = modules
allProjectsConfigurationsData.forEach { projectConfigurationsData ->
allConfigurationNames.addAll(projectConfigurationsData.allConfigurationNames)
moduleToAllConfigurationNames[projectConfigurationsData.modulePath] =
projectConfigurationsData.allConfigurationNames
moduleToAnalyzedConfigurationNames[projectConfigurationsData.modulePath] =
projectConfigurationsData.analyzedConfigurationNames
projectConfigurationsData.analyzedConfigurationNames.forEach { analyzedConfigurationName ->
val modules =
analyzedConfigurationNameToModules[analyzedConfigurationName] ?: mutableSetOf()
modules.add(projectConfigurationsData.modulePath)
analyzedConfigurationNameToModules[analyzedConfigurationName] = modules
}
}
}

return ConfigurationsJsReportModel(
allConfigurationNames = allConfigurationNames,
moduleToAllConfigurationNames = moduleToAllConfigurationNames,
moduleToAnalyzedConfigurationNames = moduleToAnalyzedConfigurationNames,
configurationNameToModules = analyzedConfigurationNameToModules
)
}
return ConfigurationsJsReportModel(
allConfigurationNames = allConfigurationNames,
moduleToAllConfigurationNames = moduleToAllConfigurationNames,
moduleToAnalyzedConfigurationNames = moduleToAnalyzedConfigurationNames,
configurationNameToModules = analyzedConfigurationNameToModules
)
}

fun toDirectDependenciesJsReportModel(
allProjectsDependencyData: List<CollectedDependenciesForProject>
): DirectDependenciesJsReportModel {
return DirectDependenciesJsReportModel(
allProjectsDependencyData.associate { it.path to it.directDependencies }
)
fun toDirectDependenciesJsReportModel(
allProjectsDependencyData: List<CollectedDependenciesForProject>
): DirectDependenciesJsReportModel {
return DirectDependenciesJsReportModel(
allProjectsDependencyData.associate { it.path to it.directDependencies }
)
}
}
}
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 a814448

Please sign in to comment.