Skip to content

Commit

Permalink
Add image flag to graph sub command
Browse files Browse the repository at this point in the history
  • Loading branch information
kondoumh committed Apr 24, 2020
1 parent 927fbd3 commit 81dd642
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ sbgraph fetch

To fetch from a private project, you needs to set the cookie to environment variables.

```bash
$ export SB_COOKIE_ID=connect.sid
$ export SB_COOKIE_VALUE=your-fancy-cookie
```
export SB_COOKIE_ID=connect.sid
export SB_COOKIE_VALUE=your-fancy-cookie
```

### Aggregate user activites in the project
Expand Down Expand Up @@ -129,9 +129,26 @@ sbgraph graph -t 100

Graphviz dot file will be created at `<WorkDir>/<project name>.dot`.


To generate graph data as JSON, specify -j(--json) flag.

```
sbgraph graph -j=true
```

To generate graph Image as SVG format, specify -m(--image) flag.

```
sbgraph graph -m=true
```

Graphviz needs to be installed. You can not specify layout engine.

### Visualize with Graphviz

You can generate graph image with dot command. e.g.
You can generate SVG with graph sub command, but to specify more options such as layout engine, you can use dot command directly.

e.g.

```
dot <project name>.dot -Tsvg -Kfdp -Goverlap=prism -<project name>.svg
Expand Down
18 changes: 15 additions & 3 deletions cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func init() {
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")
graphCmd.PersistentFlags().BoolP("image", "m", false, "Output SVG image")

rootCmd.AddCommand(graphCmd)
}
Expand All @@ -70,8 +71,10 @@ func buildGraph(cmd *cobra.Command) {
threshold, _ := cmd.PersistentFlags().GetInt("threshold")
includeUser, _ := cmd.PersistentFlags().GetBool("include")
anonymize, _ := cmd.PersistentFlags().GetBool("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)
oJSON, _ := cmd.PersistentFlags().GetBool("json")
oSVG, _ := cmd.PersistentFlags().GetBool("image")

fmt.Printf("Build graph project : %s, threshold : %d, include user: %t, anonymize : %t, json : %t, svg : %t\n", projectName, threshold, includeUser, anonymize, oJSON, oSVG)
var proj types.Project
err := proj.ReadFrom(projectName, config.WorkDir)
CheckErr(err)
Expand Down Expand Up @@ -151,11 +154,15 @@ func buildGraph(cmd *cobra.Command) {
}
err = writeDot(graph, projectName, config.WorkDir)
CheckErr(err)
if oJson {
if oJSON {
data, _ := json.Marshal(pGraph)
err = file.WriteBytes(data, projectName+"_graph.json", config.WorkDir)
CheckErr(err)
}
if oSVG {
err = writeSvg(graph, projectName, config.WorkDir)
CheckErr(err)
}
}

func createGraph() graphviz.Graph {
Expand Down Expand Up @@ -185,3 +192,8 @@ func writeDot(graph graphviz.Graph, projectName string, workDir string) error {
graph.GenerateDOT(file)
return nil
}

func writeSvg(graph graphviz.Graph, projectName string, workDir string) error {
path := fmt.Sprintf("%s/%s.svg", workDir, projectName)
return graph.GenerateImage("dot", path, "svg")
}

0 comments on commit 81dd642

Please sign in to comment.