Skip to content

Commit

Permalink
Merge pull request #30 from padok-team/feat/profile-alpha
Browse files Browse the repository at this point in the history
feat(profile): put some safeguards to avoid ugly failing
  • Loading branch information
cterence authored Oct 18, 2023
2 parents 0442f6d + 7b21452 commit 358469f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 20 deletions.
19 changes: 17 additions & 2 deletions checks/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ func Profile(codebase data.Codebase, verbose bool) {
log.Println("No layers found")
return
}
padding := len(strconv.Itoa(codebase.Layers[0].RootModule.Stats.CumulatedSize.Resources + codebase.Layers[0].RootModule.Stats.CumulatedSize.Datasources))

padding := 4
for _, l := range codebase.Layers {
if l.RootModule != nil {
cumulatedSizeStringLength := len(strconv.Itoa(l.RootModule.Stats.CumulatedSize.Resources + l.RootModule.Stats.CumulatedSize.Datasources))
if cumulatedSizeStringLength > padding {
padding = cumulatedSizeStringLength
}
break
}
}
if padding%2 == 0 {
padding += 4
} else {
Expand All @@ -39,6 +49,9 @@ func Profile(codebase data.Codebase, verbose bool) {
c = color.New(color.FgYellow).Add(color.Bold)
c.Println("Profiling by layer:")
for _, layer := range codebase.Layers {
if layer.RootModule == nil {
continue
}
fmt.Println(strings.Repeat("-", 50))
c = color.New(color.FgYellow).Add(color.Bold)
c.Printf("%s\n", layer.Name)
Expand Down Expand Up @@ -107,7 +120,9 @@ func Profile(codebase data.Codebase, verbose bool) {
// Show biggest layer
c.Printf(" %s [%d] %s\n", "Biggest layer:", codebase.Stats.BiggestLayer.RootModule.Stats.CumulatedSize.Resources+codebase.Stats.BiggestLayer.RootModule.Stats.CumulatedSize.Datasources, codebase.Stats.BiggestLayer.Name)
// Show biggest children
c.Printf(" %s [%d] %s\n", "Biggest child module:", codebase.Stats.BiggestChildModule.Stats.CumulatedSize.Resources+codebase.Stats.BiggestChildModule.Stats.CumulatedSize.Datasources, codebase.Stats.BiggestChildModule.Address)
if codebase.Stats.BiggestChildModule != nil {
c.Printf(" %s [%d] %s\n", "Biggest child module:", codebase.Stats.BiggestChildModule.Stats.CumulatedSize.Resources+codebase.Stats.BiggestChildModule.Stats.CumulatedSize.Datasources, codebase.Stats.BiggestChildModule.Address)
}
// Show warnings
c = color.New(color.FgYellow).Add(color.Bold)
c.Printf(" %s\n", "Warnings:")
Expand Down
32 changes: 22 additions & 10 deletions cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"fmt"
"guacamole/checks"
"guacamole/data"
"guacamole/helpers"
Expand All @@ -17,20 +18,31 @@ import (
// plan represents the run command
var profile = &cobra.Command{
Use: "profile",
Short: "Display informations about resources and datasources contained in the codebase",
Short: "[EXPERIMENTAL] Display informations about resources and datasources contained in the codebase",
Long: `[EXPERIMENTAL] Display informations about resources and datasources contained in the codebase
⚠️ WARNING: This command may fail in unexpected way if all the layers you want to check are not initialized properly.
`,
Run: func(cmd *cobra.Command, args []string) {
l := log.New(os.Stderr, "", 0)
l.Println("Profiling layers...")
layers, err := helpers.ComputeLayers(false)
codebase := data.Codebase{
Layers: layers,
var prompt string
fmt.Println("⚠️ WARNING: This command may fail in unexpected way if all the layers you want to check are not initialized properly.")
for prompt != "y" && prompt != "n" {
fmt.Print("Please confirm that you want to run this command (y/n) : ")
fmt.Scanln(&prompt)
}
if err != nil {
panic(err)
if prompt == "y" {
l.Println("Profiling layers...")
layers, err := helpers.ComputeLayers(false)
codebase := data.Codebase{
Layers: layers,
}
if err != nil {
panic(err)
}
verbose := viper.GetBool("verbose")
checks.Profile(codebase, verbose)
// helpers.RenderTable(checkResults)
}
verbose := viper.GetBool("verbose")
checks.Profile(codebase, verbose)
// helpers.RenderTable(checkResults)
},
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/state_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ import (
// state represents the run command
var state = &cobra.Command{
Use: "state",
Short: "Run state-related checks",
Short: "[EXPERIMENTAL] Run state-related checks",
Long: `[EXPERIMENTAL] Run state-related checks
⚠️ WARNING: This command may fail in unexpected way if all the layers you want to check are not initialized properly.
`,
Run: func(cmd *cobra.Command, args []string) {
verbose := viper.GetBool("verbose")
l := log.New(os.Stderr, "", 0)
var prompt string
fmt.Println("⚠ WARNING: This command may fail in unexpected way if all the layers you want to check are not initialized properly.")
fmt.Println("⚠ WARNING: This command may fail in unexpected way if all the layers you want to check are not initialized properly.")
for prompt != "y" && prompt != "n" {
fmt.Print("Please confirm that you want to run this command (y/n) : ")
fmt.Scanln(&prompt)
Expand Down
9 changes: 7 additions & 2 deletions data/codebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ type CodebaseStats struct {
func (c *Codebase) BuildLayers() {
wg := new(sync.WaitGroup)

wg.Add(len(c.Layers))

for i := range c.Layers {
if c.Layers[i].RootModule == nil {
continue
}
wg.Add(1)
go func(layer *Layer) {
defer wg.Done()
layer.BuildRootModule()
Expand All @@ -41,6 +43,9 @@ func (c *Codebase) ComputeStats() {
distinctDatasourceTypes, distinctResourceTypes := map[string]int{}, map[string]int{}

for _, l := range c.Layers {
if l.RootModule == nil {
continue
}
// Compute all module stats
l.RootModule.ComputeStats()

Expand Down
7 changes: 4 additions & 3 deletions data/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,13 @@ func (layer *Layer) BuildRootModule() {
}

func (layer *Layer) ComputeWarnings() {
if layer.RootModule.Name == "" {
if layer.RootModule == nil {
layer.BuildRootModule()
}

// Check for datasources in modules
layer.Warnings.DatasourceInModuleWarning = computeDatasourceInModuleWarning(layer.RootModule)
if layer.RootModule != nil {
layer.Warnings.DatasourceInModuleWarning = computeDatasourceInModuleWarning(layer.RootModule)
}
}

func computeDatasourceInModuleWarning(module *Module) []datasourceInModuleWarning {
Expand Down
5 changes: 4 additions & 1 deletion helpers/compute_layer_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ func ComputeLayers(withPlan bool) ([]*data.Layer, error) {
guard := make(chan struct{}, maxProcs)

wg := new(sync.WaitGroup)
wg.Add(len(layers))

for i := range layers {
if layers[i].RootModule != nil {
continue
}
wg.Add(1)
// Add a struct to the channel to start a goroutine
// If the channel is full, the goroutine will wait until another one finishes and removes the struct from the channel
guard <- struct{}{}
Expand Down

0 comments on commit 358469f

Please sign in to comment.