Skip to content

Commit

Permalink
Graph json flag (#4)
Browse files Browse the repository at this point in the history
* Add json format

* nest struct
  • Loading branch information
kondoumh authored Apr 20, 2020
1 parent 0c2f6b1 commit 927fbd3
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion cmd/graph.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
package cmd

import (
"encoding/json"
"fmt"
"os"

"github.com/mamezou-tech/sbgraph/pkg/file"

"github.com/mamezou-tech/sbgraph/pkg/types"
"github.com/mzohreva/GoGraphviz/graphviz"
"github.com/spf13/cobra"
)

type page struct {
ID string `json:"id"`
Title string `json:"title"`
}

type user struct {
ID string `json:"id"`
Name string `json:"name"`
}

type pageLink struct {
From string `json:"from"`
To string `json:"to"`
}

type userPage struct {
UserID string `json:"user"`
PageID string `json:"page"`
}

type projectGraph struct {
Pages []page `json:"pages"`
Users []user `json:"users"`
Links []pageLink `json:"links"`
UserPages []userPage `json:"userPages"`
}

// graphCmd represents the graph command
var graphCmd = &cobra.Command{
Use: "graph",
Expand All @@ -29,6 +59,7 @@ func init() {
graphCmd.PersistentFlags().IntP("threshold", "t", 0, "Threshold value of views to filter page")
graphCmd.PersistentFlags().BoolP("include", "i", false, "Include user node")
graphCmd.PersistentFlags().BoolP("anonymize", "a", false, "Anonymize user")
graphCmd.PersistentFlags().BoolP("json", "j", false, "Output as JSON format")

rootCmd.AddCommand(graphCmd)
}
Expand All @@ -39,7 +70,8 @@ func buildGraph(cmd *cobra.Command) {
threshold, _ := cmd.PersistentFlags().GetInt("threshold")
includeUser, _ := cmd.PersistentFlags().GetBool("include")
anonymize, _ := cmd.PersistentFlags().GetBool("anonymize")
fmt.Printf("Build graph project : %s, threshold : %d, include user: %t, anonymize : %t\n", projectName, threshold, includeUser, anonymize)
oJson, _ := cmd.PersistentFlags().GetBool("json")
fmt.Printf("Build graph project : %s, threshold : %d, include user: %t, anonymize : %t, json : %t\n", projectName, threshold, includeUser, anonymize, oJson)
var proj types.Project
err := proj.ReadFrom(projectName, config.WorkDir)
CheckErr(err)
Expand Down Expand Up @@ -70,10 +102,12 @@ func buildGraph(cmd *cobra.Command) {
}

graph := createGraph()
var pGraph projectGraph
pNodes := map[string]int{}
for _, p := range pages {
gid := graph.AddNode(p.Title)
pNodes[p.ID] = gid
pGraph.Pages = append(pGraph.Pages, page{p.ID, p.Title})
}
uNodes := map[string]int{}
if includeUser {
Expand All @@ -90,6 +124,7 @@ func buildGraph(cmd *cobra.Command) {
gid := graph.AddNode(username)
graph.NodeAttribute(gid, graphviz.FillColor, "cyan")
uNodes[u.ID] = gid
pGraph.Users = append(pGraph.Users, user{u.ID, username})
}
}
for _, p := range pages {
Expand All @@ -98,6 +133,7 @@ func buildGraph(cmd *cobra.Command) {
lid, contains := pNodes[link.ID]
if contains {
graph.AddEdge(pid, lid, "")
pGraph.Links = append(pGraph.Links, pageLink{p.ID, link.ID})
}
}
}
Expand All @@ -108,12 +144,18 @@ func buildGraph(cmd *cobra.Command) {
pid, contains := pNodes[c]
if contains {
graph.AddEdge(uid, pid, "")
pGraph.UserPages = append(pGraph.UserPages, userPage{u.ID, c})
}
}
}
}
err = writeDot(graph, projectName, config.WorkDir)
CheckErr(err)
if oJson {
data, _ := json.Marshal(pGraph)
err = file.WriteBytes(data, projectName+"_graph.json", config.WorkDir)
CheckErr(err)
}
}

func createGraph() graphviz.Graph {
Expand Down

0 comments on commit 927fbd3

Please sign in to comment.