Skip to content

Commit 6171899

Browse files
authored
Merge pull request #46 from RinkiyaKeDad/fix_stats
change metrics shown by stats command
2 parents 7d7beaa + 4d2834c commit 6171899

File tree

5 files changed

+54
-33
lines changed

5 files changed

+54
-33
lines changed

cmd/graph.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cmd
1919
import (
2020
"fmt"
2121
"io/ioutil"
22+
"sort"
2223
"strings"
2324

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

107108
// color the main module as yellow
108109
data := colorMainNode(overview.MainModuleName)
109-
for _, dep := range overview.DepList {
110+
allDeps := getAllDeps(overview.Graph[overview.MainModuleName], overview.TransDepList)
111+
allDeps = append(allDeps, overview.MainModuleName)
112+
sort.Strings(allDeps)
113+
for _, dep := range allDeps {
110114
_, ok := overview.Graph[dep]
111115
if !ok {
112116
continue

cmd/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ var listCmd = &cobra.Command{
3131
RunE: func(cmd *cobra.Command, args []string) error {
3232
depGraph := getDepInfo()
3333
fmt.Println("List of all dependencies:")
34-
printDeps(depGraph.DepList)
34+
allDeps := getAllDeps(depGraph.Graph[depGraph.MainModuleName], depGraph.TransDepList)
35+
printDeps(allDeps)
3536
return nil
3637
},
3738
}

cmd/stats.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,21 @@ var statsCmd = &cobra.Command{
4545
getLongestChain(depGraph.MainModuleName, depGraph.Graph, temp, &longestChain)
4646

4747
// get values
48-
totalDeps := len(depGraph.DepList)
4948
maxDepth := len(longestChain)
5049
directDeps := len(depGraph.Graph[depGraph.MainModuleName])
51-
transitiveDeps := totalDeps - directDeps
50+
totalDeps := len(getAllDeps(depGraph.Graph[depGraph.MainModuleName], depGraph.TransDepList))
51+
transitiveDeps := len(depGraph.TransDepList)
5252

5353
if !jsonOutput {
54+
fmt.Printf("Direct Dependencies: %d \n", directDeps)
55+
fmt.Printf("Transitive Dependencies: %d \n", transitiveDeps)
5456
fmt.Printf("Total Dependencies: %d \n", totalDeps)
5557
fmt.Printf("Max Depth Of Dependencies: %d \n", maxDepth)
56-
fmt.Printf("Transitive Dependencies: %d \n", transitiveDeps)
5758
}
5859

5960
if verbose {
6061
fmt.Println("All dependencies:")
61-
printDeps(depGraph.DepList)
62+
printDeps(append(depGraph.Graph[depGraph.MainModuleName], depGraph.TransDepList...))
6263
}
6364

6465
// print the longest chain
@@ -70,13 +71,15 @@ var statsCmd = &cobra.Command{
7071
if jsonOutput {
7172
// create json
7273
outputObj := struct {
73-
TotalDeps int `json:"totalDependencies"`
74-
MaxDepth int `json:"maxDepthOfDependencies"`
75-
TransDeps int `json:"transitiveDependencies"`
74+
DirectDeps int `json:"directDependencies"`
75+
TransDeps int `json:"transitiveDependencies"`
76+
TotalDeps int `json:"totalDependencies"`
77+
MaxDepth int `json:"maxDepthOfDependencies"`
7678
}{
77-
TotalDeps: totalDeps,
78-
MaxDepth: maxDepth,
79-
TransDeps: transitiveDeps,
79+
DirectDeps: directDeps,
80+
TransDeps: transitiveDeps,
81+
TotalDeps: totalDeps,
82+
MaxDepth: maxDepth,
8083
}
8184
outputRaw, err := json.MarshalIndent(outputObj, "", "\t")
8285
if err != nil {

cmd/utils.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func printChain(slice []string) {
3434
type DependencyOverview struct {
3535
// Dependency graph edges modelled as node plus adjacency nodes
3636
Graph map[string][]string
37-
// List of all (including transitive) dependencies
38-
DepList []string
37+
// List of all transitive dependencies
38+
TransDepList []string
3939
// Name of the module from which the dependencies are computed
4040
MainModuleName string
4141
}
@@ -53,8 +53,8 @@ func getDepInfo() *DependencyOverview {
5353
depGraph := make(map[string][]string)
5454
scanner := bufio.NewScanner(strings.NewReader(goModGraphOutputString))
5555

56-
// deps will store all the dependencies
57-
var deps []string
56+
// transDeps will store all the transitive dependencies
57+
var transDeps []string
5858
mainModule := "notset"
5959

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

72-
isMainModule := false
7372
if mainModule == "notset" {
7473
mainModule = words[0]
75-
isMainModule = true
7674
}
7775

78-
if !contains(deps, words[0]) && !isMainModule {
79-
// we don't want to add mainModule to deps list
80-
deps = append(deps, words[0])
76+
// anything where the LHS is not mainModule
77+
// is a transitive dependency
78+
if words[0] != mainModule {
79+
if !contains(transDeps, words[1]) {
80+
transDeps = append(transDeps, words[1])
81+
}
8182
}
82-
if !contains(deps, words[1]) {
83-
deps = append(deps, words[1])
84-
}
85-
8683
}
8784
return &DependencyOverview{
8885
Graph: depGraph,
89-
DepList: deps,
86+
TransDepList: transDeps,
9087
MainModuleName: mainModule,
9188
}
9289
}
@@ -100,6 +97,21 @@ func printDeps(deps []string) {
10097
fmt.Println()
10198
}
10299

100+
func getAllDeps(directDeps []string, transDeps []string) []string {
101+
var allDeps []string
102+
for _, dep := range directDeps {
103+
if !contains(allDeps, dep) {
104+
allDeps = append(allDeps, dep)
105+
}
106+
}
107+
for _, dep := range transDeps {
108+
if !contains(allDeps, dep) {
109+
allDeps = append(allDeps, dep)
110+
}
111+
}
112+
return allDeps
113+
}
114+
103115
func contains(s []string, str string) bool {
104116
for _, v := range s {
105117
if v == str {

cmd/utils_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"fmt"
2021
"testing"
2122
)
2223

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

46-
deps := []string{"A", "B", "C", "D", "E", "F", "H"}
47+
transDeps := []string{"E", "G", "F", "H"}
4748
overview := &DependencyOverview{
4849
Graph: graph,
49-
DepList: deps,
50+
TransDepList: transDeps,
5051
MainModuleName: "A",
5152
}
5253

@@ -76,7 +77,7 @@ func Test_getChains_simple(t *testing.T) {
7677
"E" -> "F"
7778
"F" -> "H"
7879
`
79-
80+
fmt.Println(getFileContentsForAllDeps(overview))
8081
if correctFileContentsForAllDeps != getFileContentsForAllDeps(overview) {
8182
t.Errorf("File contents for graph of all dependencies are wrong")
8283
}
@@ -135,10 +136,10 @@ func Test_getChains_cycle(t *testing.T) {
135136
graph["G"] = []string{"H"}
136137
graph["H"] = []string{"D"}
137138

138-
deps := []string{"A", "B", "C", "D", "E", "F", "G", "H"}
139+
transDeps := []string{"D", "E", "F", "G", "H"}
139140
overview := &DependencyOverview{
140141
Graph: graph,
141-
DepList: deps,
142+
TransDepList: transDeps,
142143
MainModuleName: "A",
143144
}
144145

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

229-
deps := []string{"A", "B", "C", "D", "E", "F"}
230+
transDeps := []string{"C", "B", "E", "F", "D"}
230231

231232
overview := &DependencyOverview{
232233
Graph: graph,
233-
DepList: deps,
234+
TransDepList: transDeps,
234235
MainModuleName: "A",
235236
}
236237

0 commit comments

Comments
 (0)