Skip to content

Commit

Permalink
Merge pull request #46 from RinkiyaKeDad/fix_stats
Browse files Browse the repository at this point in the history
change metrics shown by stats command
  • Loading branch information
k8s-ci-robot authored Jul 13, 2021
2 parents 7d7beaa + 4d2834c commit 6171899
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 33 deletions.
6 changes: 5 additions & 1 deletion cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd
import (
"fmt"
"io/ioutil"
"sort"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -106,7 +107,10 @@ func getFileContentsForAllDeps(overview *DependencyOverview) string {

// color the main module as yellow
data := colorMainNode(overview.MainModuleName)
for _, dep := range overview.DepList {
allDeps := getAllDeps(overview.Graph[overview.MainModuleName], overview.TransDepList)
allDeps = append(allDeps, overview.MainModuleName)
sort.Strings(allDeps)
for _, dep := range allDeps {
_, ok := overview.Graph[dep]
if !ok {
continue
Expand Down
3 changes: 2 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var listCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
depGraph := getDepInfo()
fmt.Println("List of all dependencies:")
printDeps(depGraph.DepList)
allDeps := getAllDeps(depGraph.Graph[depGraph.MainModuleName], depGraph.TransDepList)
printDeps(allDeps)
return nil
},
}
Expand Down
23 changes: 13 additions & 10 deletions cmd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@ var statsCmd = &cobra.Command{
getLongestChain(depGraph.MainModuleName, depGraph.Graph, temp, &longestChain)

// get values
totalDeps := len(depGraph.DepList)
maxDepth := len(longestChain)
directDeps := len(depGraph.Graph[depGraph.MainModuleName])
transitiveDeps := totalDeps - directDeps
totalDeps := len(getAllDeps(depGraph.Graph[depGraph.MainModuleName], depGraph.TransDepList))
transitiveDeps := len(depGraph.TransDepList)

if !jsonOutput {
fmt.Printf("Direct Dependencies: %d \n", directDeps)
fmt.Printf("Transitive Dependencies: %d \n", transitiveDeps)
fmt.Printf("Total Dependencies: %d \n", totalDeps)
fmt.Printf("Max Depth Of Dependencies: %d \n", maxDepth)
fmt.Printf("Transitive Dependencies: %d \n", transitiveDeps)
}

if verbose {
fmt.Println("All dependencies:")
printDeps(depGraph.DepList)
printDeps(append(depGraph.Graph[depGraph.MainModuleName], depGraph.TransDepList...))
}

// print the longest chain
Expand All @@ -70,13 +71,15 @@ var statsCmd = &cobra.Command{
if jsonOutput {
// create json
outputObj := struct {
TotalDeps int `json:"totalDependencies"`
MaxDepth int `json:"maxDepthOfDependencies"`
TransDeps int `json:"transitiveDependencies"`
DirectDeps int `json:"directDependencies"`
TransDeps int `json:"transitiveDependencies"`
TotalDeps int `json:"totalDependencies"`
MaxDepth int `json:"maxDepthOfDependencies"`
}{
TotalDeps: totalDeps,
MaxDepth: maxDepth,
TransDeps: transitiveDeps,
DirectDeps: directDeps,
TransDeps: transitiveDeps,
TotalDeps: totalDeps,
MaxDepth: maxDepth,
}
outputRaw, err := json.MarshalIndent(outputObj, "", "\t")
if err != nil {
Expand Down
40 changes: 26 additions & 14 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func printChain(slice []string) {
type DependencyOverview struct {
// Dependency graph edges modelled as node plus adjacency nodes
Graph map[string][]string
// List of all (including transitive) dependencies
DepList []string
// List of all transitive dependencies
TransDepList []string
// Name of the module from which the dependencies are computed
MainModuleName string
}
Expand All @@ -53,8 +53,8 @@ func getDepInfo() *DependencyOverview {
depGraph := make(map[string][]string)
scanner := bufio.NewScanner(strings.NewReader(goModGraphOutputString))

// deps will store all the dependencies
var deps []string
// transDeps will store all the transitive dependencies
var transDeps []string
mainModule := "notset"

for scanner.Scan() {
Expand All @@ -69,24 +69,21 @@ func getDepInfo() *DependencyOverview {
depGraph[words[0]] = append(depGraph[words[0]], words[1])
}

isMainModule := false
if mainModule == "notset" {
mainModule = words[0]
isMainModule = true
}

if !contains(deps, words[0]) && !isMainModule {
// we don't want to add mainModule to deps list
deps = append(deps, words[0])
// anything where the LHS is not mainModule
// is a transitive dependency
if words[0] != mainModule {
if !contains(transDeps, words[1]) {
transDeps = append(transDeps, words[1])
}
}
if !contains(deps, words[1]) {
deps = append(deps, words[1])
}

}
return &DependencyOverview{
Graph: depGraph,
DepList: deps,
TransDepList: transDeps,
MainModuleName: mainModule,
}
}
Expand All @@ -100,6 +97,21 @@ func printDeps(deps []string) {
fmt.Println()
}

func getAllDeps(directDeps []string, transDeps []string) []string {
var allDeps []string
for _, dep := range directDeps {
if !contains(allDeps, dep) {
allDeps = append(allDeps, dep)
}
}
for _, dep := range transDeps {
if !contains(allDeps, dep) {
allDeps = append(allDeps, dep)
}
}
return allDeps
}

func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
Expand Down
15 changes: 8 additions & 7 deletions cmd/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"fmt"
"testing"
)

Expand All @@ -43,10 +44,10 @@ func Test_getChains_simple(t *testing.T) {
graph["E"] = []string{"F"}
graph["F"] = []string{"H"}

deps := []string{"A", "B", "C", "D", "E", "F", "H"}
transDeps := []string{"E", "G", "F", "H"}
overview := &DependencyOverview{
Graph: graph,
DepList: deps,
TransDepList: transDeps,
MainModuleName: "A",
}

Expand Down Expand Up @@ -76,7 +77,7 @@ func Test_getChains_simple(t *testing.T) {
"E" -> "F"
"F" -> "H"
`

fmt.Println(getFileContentsForAllDeps(overview))
if correctFileContentsForAllDeps != getFileContentsForAllDeps(overview) {
t.Errorf("File contents for graph of all dependencies are wrong")
}
Expand Down Expand Up @@ -135,10 +136,10 @@ func Test_getChains_cycle(t *testing.T) {
graph["G"] = []string{"H"}
graph["H"] = []string{"D"}

deps := []string{"A", "B", "C", "D", "E", "F", "G", "H"}
transDeps := []string{"D", "E", "F", "G", "H"}
overview := &DependencyOverview{
Graph: graph,
DepList: deps,
TransDepList: transDeps,
MainModuleName: "A",
}

Expand Down Expand Up @@ -226,11 +227,11 @@ func Test_getChains_cycle_2(t *testing.T) {
graph["F"] = []string{"D"}
graph["D"] = []string{"C"}

deps := []string{"A", "B", "C", "D", "E", "F"}
transDeps := []string{"C", "B", "E", "F", "D"}

overview := &DependencyOverview{
Graph: graph,
DepList: deps,
TransDepList: transDeps,
MainModuleName: "A",
}

Expand Down

0 comments on commit 6171899

Please sign in to comment.